📄 samlrequest.java
字号:
} respondWiths.add(respondWith); } else throw new IllegalArgumentException("respondWith cannot be null"); } /** * Removes a statement type by position (zero-based) * * @param index The position of the statement type to remove */ public void removeRespondWith(int index) throws IndexOutOfBoundsException { respondWiths.remove(index); if (root != null) { unsign(); Element e = XML.getFirstChildElement(root, XML.SAMLP_NS, "RespondWith"); while (e != null && index > 0) { e = XML.getNextSiblingElement(e, XML.SAMLP_NS, "RespondWith"); index--; } if (e != null) root.removeChild(e); else throw new IndexOutOfBoundsException(); } } /** * Gets the query contained within the request * * @return The query in the request */ public SAMLQuery getQuery() { return query; } /** * Sets the query contained within the request * * @param query The query for the request * @exception SAMLException Raised if the query is invalid */ public void setQuery(SAMLQuery query) throws SAMLException { if (query != null) { setAssertionIdRefs(null); setArtifacts(null); } if (root != null) { unsign(); Element last = XML.getLastChildElement(root, XML.SAMLP_NS, "RespondWith"); if (query != null) { if (this.query != null) root.replaceChild(query.toDOM(root.getOwnerDocument()), this.query.root); else { if (last == null) root.insertBefore(query.toDOM(root.getOwnerDocument()), root.getFirstChild()); else root.insertBefore(query.toDOM(root.getOwnerDocument()), last.getNextSibling()); } } else { root.removeChild(this.query.root); } } this.query = query; } /** * Gets the assertion ID references contained within the request * * @return An iterator over the references */ public Iterator getAssertionIdRefs() { return assertionIdRefs.iterator(); } /** * Adds an assertion ID reference to the request * * @param ref The reference to add */ public void addAssertionIdRef(String ref) { if (XML.isEmpty(ref)) throw new IllegalArgumentException("ref cannot be null or empty"); try { setQuery(null); setArtifacts(null); } catch (SAMLException ex) { } if (root != null) { unsign(); Document doc = root.getOwnerDocument(); Element e = doc.createElementNS(XML.SAML_NS, "saml:AssertionIDReference"); e.appendChild(doc.createTextNode(ref)); Element last = XML.getLastChildElement(root, XML.SAMLP_NS, "RespondWith"); if (last == null) root.insertBefore(e, root.getFirstChild()); else root.insertBefore(e, last.getNextSibling()); } assertionIdRefs.add(ref); } /** * Sets the assertion ID references contained within the request * * @param refs The references to include */ public void setAssertionIdRefs(Collection refs) { while (this.assertionIdRefs.size() > 0) removeAssertionIdRef(0); if (refs != null) { for (Iterator i = refs.iterator(); i.hasNext(); ) addAssertionIdRef((String)i.next()); } } /** * Removes an assertion reference by position (zero-based) * * @param index The position of the reference to remove */ public void removeAssertionIdRef(int index) throws IndexOutOfBoundsException { assertionIdRefs.remove(index); if (root != null) { unsign(); Element e = XML.getFirstChildElement(root, XML.SAML_NS, "AssertionIDReference"); while (e != null && index > 0) { e = XML.getNextSiblingElement(e, XML.SAML_NS, "AssertionIDReference"); index--; } if (e != null) root.removeChild(e); else throw new IndexOutOfBoundsException(); } } /** * Gets the artifacts contained within the request * * @return An iterator over the artifacts */ public Iterator getArtifacts() { return artifacts.iterator(); } /** * Sets the artifacts contained within the request * * @param refs The artifacts to include */ public void setArtifacts(Collection artifacts) { while (this.artifacts.size() > 0) removeArtifact(0); if (artifacts != null) { for (Iterator i = artifacts.iterator(); i.hasNext(); ) addArtifact((String)i.next()); } } /** * Adds an artifact to the request * * @param artifact The artifact to add */ public void addArtifact(String artifact) { if (XML.isEmpty(artifact)) throw new IllegalArgumentException("artifact cannot be null or empty"); try { setQuery(null); setAssertionIdRefs(null); } catch (SAMLException ex) { } if (root != null) { unsign(); Document doc = root.getOwnerDocument(); Element e = doc.createElementNS(XML.SAMLP_NS, "AssertionArtifact"); e.appendChild(doc.createTextNode(artifact)); Element last = XML.getLastChildElement(root, XML.SAMLP_NS, "RespondWith"); if (last == null) root.insertBefore(e, root.getFirstChild()); else root.insertBefore(e, last.getNextSibling()); } artifacts.add(artifact); } /** * Removes an artifact by position (zero-based) * * @param index The position of the artifact to remove */ public void removeArtifact(int index) throws IndexOutOfBoundsException { artifacts.remove(index); if (root != null) { unsign(); Element e = XML.getFirstChildElement(root, XML.SAMLP_NS, "AssertionArtifact"); while (e != null && index > 0) { e = XML.getNextSiblingElement(e, XML.SAMLP_NS, "AssertionArtifact"); index--; } if (e != null) root.removeChild(e); else throw new IndexOutOfBoundsException(); } } /** * @see org.opensaml.SAMLObject#toDOM(org.w3c.dom.Document,boolean) */ public Node toDOM(Document doc, boolean xmlns) throws SAMLException { if ((root = super.toDOM(doc, xmlns)) != null) { if (xmlns) { ((Element)root).setAttributeNS(XML.XMLNS_NS, "xmlns", XML.SAMLP_NS); ((Element)root).setAttributeNS(XML.XMLNS_NS, "xmlns:saml", XML.SAML_NS); ((Element)root).setAttributeNS(XML.XMLNS_NS, "xmlns:samlp", XML.SAMLP_NS); ((Element)root).setAttributeNS(XML.XMLNS_NS, "xmlns:xsi", XML.XSI_NS); ((Element)root).setAttributeNS(XML.XMLNS_NS, "xmlns:xsd", XML.XSD_NS); } return root; } // Generate a SAML Request. Element r = doc.createElementNS(XML.SAMLP_NS, "Request"); if (xmlns) r.setAttributeNS(XML.XMLNS_NS, "xmlns", XML.SAMLP_NS); r.setAttributeNS(XML.XMLNS_NS, "xmlns:samlp", XML.SAMLP_NS); r.setAttributeNS(XML.XMLNS_NS, "xmlns:saml", XML.SAML_NS); r.setAttributeNS(null, "MajorVersion", "1"); r.setAttributeNS(null, "MinorVersion", config.getBooleanProperty("org.opensaml.compatibility-mode") ? "0" : "1"); r.setAttributeNS(null, "RequestID", requestId); r.setIdAttributeNS(null, "RequestID", true); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); formatter.setTimeZone(TimeZone.getTimeZone("GMT")); r.setAttributeNS(null, "IssueInstant", formatter.format(issueInstant)); Iterator i=respondWiths.iterator(); while (i.hasNext()) { QName qn=(QName)i.next(); Element rw = doc.createElementNS(XML.SAMLP_NS, "RespondWith"); String rwns = qn.getNamespaceURI(); if (rwns==null) rwns=""; if (!XML.SAML_NS.equals(rwns)) { rw.setAttributeNS(XML.XMLNS_NS, "xmlns:rw", rwns); rwns="rw:"; } else rwns="saml:"; rw.appendChild(doc.createTextNode(rwns + qn.getLocalName())); r.appendChild(rw); } if (query != null) r.appendChild(query.toDOM(doc, false)); else if (assertionIdRefs.size() > 0) { i=assertionIdRefs.iterator(); while (i.hasNext()) r.appendChild(doc.createElementNS(XML.SAML_NS,"saml:AssertionIDReference")).appendChild(doc.createTextNode((String)i.next())); } else { i=artifacts.iterator(); while (i.hasNext()) r.appendChild(doc.createElementNS(XML.SAMLP_NS,"AssertionArtifact")).appendChild(doc.createTextNode((String)i.next())); } return root = r; } /** * @see org.opensaml.SAMLObject#checkValidity() */ public void checkValidity() throws SAMLException { if (query == null && assertionIdRefs.size() == 0 && artifacts.size() == 0) throw new MalformedException("Request is invalid, must have Query, assertion references, or artifacts"); } /** * Copies a SAML object such that no dependencies exist between the original * and the copy * * @return The new object * @see java.lang.Object#clone() */ public Object clone() throws CloneNotSupportedException { SAMLRequest dup=(SAMLRequest)super.clone(); dup.respondWiths = (ArrayList)respondWiths.clone(); dup.query = (SAMLQuery)query.clone(); dup.assertionIdRefs = (ArrayList)assertionIdRefs.clone(); dup.artifacts = (ArrayList)artifacts.clone(); return dup; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -