📄 samlassertion.java
字号:
} if (notOnOrAfter != null) { //Recreate element if needed. if (cond == null) cond = (Element)root.insertBefore( root.getOwnerDocument().createElementNS(XML.SAML_NS, "Conditions"), root.getFirstChild()); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); formatter.setTimeZone(TimeZone.getTimeZone("GMT")); cond.setAttributeNS(null, "NotOnOrAfter", formatter.format(notOnOrAfter)); } } this.notOnOrAfter = notOnOrAfter; } /** * Gets the conditions included in the assertion * * @return An iterator of SAML conditions */ public Iterator getConditions() { return conditions.iterator(); } /** * Sets the conditions included in the assertion * * @param conditions The conditions to include in the assertion * @throws SAMLException Raised if any of the conditions are invalid */ public void setConditions(Collection conditions) throws SAMLException { while (this.conditions.size() > 0) { removeCondition(0); } if (conditions != null) { for (Iterator i = conditions.iterator(); i.hasNext(); ) addCondition((SAMLCondition)i.next()); } } /** * Adds a condition to the assertion * * @param c The condition to add * @exception SAMLException Raised if an error occurs while adding the condition */ public void addCondition(SAMLCondition c) throws SAMLException { if (c != null) { if (root != null) { unsign(); Element conds = XML.getFirstChildElement(root, XML.SAML_NS, "Conditions"); if (conds == null) root.insertBefore( root.getOwnerDocument().createElementNS(XML.SAML_NS, "Conditions"), root.getFirstChild() ); conds.appendChild(c.toDOM(root.getOwnerDocument())); } conditions.add(c); } else throw new IllegalArgumentException("c cannot be null"); } /** * Removes a condition by position (zero-based) * * @param index The position of the condition to remove */ public void removeCondition(int index) throws IndexOutOfBoundsException { conditions.remove(index); if (root != null) { unsign(); Element cond = XML.getFirstChildElement(root, XML.SAML_NS, "Conditions"); if (conditions.size() == 0 && notBefore == null && notOnOrAfter == null) { root.removeChild(cond); return; } Element e = XML.getFirstChildElement(cond); while (e != null && index > 0) { e = XML.getNextSiblingElement(e); index--; } if (e != null) cond.removeChild(e); else throw new IndexOutOfBoundsException(); } } /** * Gets the optional Advice data included in the assertion * * Advice can be Strings (assertion references), Assertions, or DOM Elements. * * @return An iterator over the advice */ public Iterator getAdvice() { return advice.iterator(); } /** * Sets the optional Advice data to include in the assertion * * @param advice The Advice to include in the assertion * @exception SAMLException Raised if unable to construct new Advice objects */ public void setAdvice(Collection advice) throws SAMLException { while (this.advice.size() > 0) { removeAdvice(0); } if (advice != null) { for (Iterator i = advice.iterator(); i.hasNext(); ) addAdvice(i.next()); } } /** * Adds an advice element * * @param data a String, SAMLAssertion, or DOM Element * @exception SAMLException Raised if object is invalid */ public void addAdvice(Object advice) throws SAMLException { if (advice != null && (advice instanceof String || advice instanceof SAMLAssertion || (advice instanceof Element && !((Element)advice).getNamespaceURI().equals(XML.SAML_NS)))) { if (root != null) { Document doc = root.getOwnerDocument(); Element a = XML.getFirstChildElement(root, XML.SAML_NS, "Advice"); if (advice instanceof String && !XML.isEmpty((String)advice)) { Element ref = doc.createElementNS(XML.SAML_NS, "AssertionIDReference"); ref.appendChild(doc.createTextNode((String)advice)); a.appendChild(ref); } else if (advice instanceof SAMLAssertion) { a.appendChild(((SAMLAssertion)advice).toDOM(doc)); } else if (advice instanceof Element) { a.appendChild(doc.adoptNode((Element)advice)); } } this.advice.add(advice); } else throw new IllegalArgumentException("SAMLAssertion.addAdvice() can only process Strings, SAMLAssertions, or DOM elements from a non-saml namespace"); } /** * Removes an advice element by position (zero-based) * * @param index The position of the element to remove */ public void removeAdvice(int index) throws IndexOutOfBoundsException { advice.remove(index); if (root != null) { Element a = XML.getFirstChildElement(root, XML.SAML_NS, "Advice"); Element e = XML.getFirstChildElement(a); while (e != null && index > 0) { e = XML.getNextSiblingElement(e); index--; } if (e != null) a.removeChild(e); else throw new IndexOutOfBoundsException(); } } /** * Gets the statements included in the assertion * * @return An iterator of SAML statements */ public Iterator getStatements() { return statements.iterator(); } /** * Sets the statements to include in the assertion * * @param statements The statements to include in the assertion * @exception SAMLException Raised if unable to construct new statement objects */ public void setStatements(Collection statements) throws SAMLException { while (this.statements.size() > 0) removeStatement(0); if (statements != null) { for (Iterator i = statements.iterator(); i.hasNext(); ) addStatement((SAMLStatement)i.next()); } } /** * Adds a statement to the assertion * * @param s The statement to add * @exception SAMLException Raised if an error occurs while adding the statement */ public void addStatement(SAMLStatement s) throws SAMLException { if (s != null) { if (root != null) { unsign(); if (statements.size() > 0) { Node n = ((SAMLStatement)statements.get(statements.size()-1)).root; root.insertBefore(s.toDOM(root.getOwnerDocument()),n.getNextSibling()); } else root.appendChild(s.toDOM(root.getOwnerDocument())); } statements.add(s); } else throw new IllegalArgumentException("s cannot be null"); } /** * Removes a statement by position (zero-based) * * @param index The position of the statement to remove */ public void removeStatement(int index) throws IndexOutOfBoundsException { statements.remove(index); if (root != null) { unsign(); Element e = XML.getFirstChildElement(root); if (XML.isElementNamed(e, XML.SAML_NS, "Conditions")) e = XML.getNextSiblingElement(e); if (XML.isElementNamed(e, XML.SAML_NS, "Advice")) e = XML.getNextSiblingElement(e); while (e != null && index > 0) { e = XML.getNextSiblingElement(e); 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.SAML_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:xsd", XML.XSD_NS); ((Element)root).setAttributeNS(XML.XMLNS_NS, "xmlns:xsi", XML.XSI_NS); } return root; } if (issuer == null || issuer.length() == 0 || statements == null || statements.size() == 0) throw new MalformedException(SAMLException.RESPONDER, "SAMLAssertion.toDOM() requires issuer and at least one statement"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); formatter.setTimeZone(TimeZone.getTimeZone("GMT")); Element assertion = doc.createElementNS(XML.SAML_NS, "Assertion"); assertion.setAttributeNS(XML.XMLNS_NS, "xmlns", XML.SAML_NS); assertion.setAttributeNS(XML.XMLNS_NS, "xmlns:saml", XML.SAML_NS); assertion.setAttributeNS(XML.XMLNS_NS, "xmlns:samlp", XML.SAMLP_NS); assertion.setAttributeNS(null, "MajorVersion", "1"); assertion.setAttributeNS(null, "MinorVersion", config.getBooleanProperty("org.opensaml.compatibility-mode") ? "0" : "1"); assertion.setAttributeNS(null, "AssertionID", assertionId); assertion.setIdAttributeNS(null, "AssertionID", true); assertion.setAttributeNS(null, "Issuer", issuer); assertion.setAttributeNS(null, "IssueInstant", formatter.format(issueInstant)); if (conditions.size() > 0 || notBefore != null || notOnOrAfter != null) { Element conds = doc.createElementNS(XML.SAML_NS, "Conditions"); if (notBefore != null) conds.setAttributeNS(null, "NotBefore", formatter.format(notBefore)); if (notOnOrAfter != null) conds.setAttributeNS(null, "NotOnOrAfter", formatter.format(notOnOrAfter)); assertion.appendChild(conds); for (Iterator i = conditions.iterator(); i.hasNext(); ) conds.appendChild(((SAMLCondition)i.next()).toDOM(doc, false)); } if (advice.size() > 0) { Element a = doc.createElementNS(XML.SAML_NS, "Advice"); Iterator i = advice.iterator(); while (i.hasNext()) { Object obj = i.next(); if (obj instanceof String && !XML.isEmpty((String)obj)) { Element ref = doc.createElementNS(XML.SAML_NS, "AssertionIDReference"); ref.appendChild(doc.createTextNode((String)obj)); a.appendChild(ref); } else if (obj instanceof SAMLAssertion) { a.appendChild(((SAMLAssertion)obj).toDOM(doc, false)); } else if (obj instanceof Element) { a.appendChild(doc.adoptNode((Element)obj)); } } assertion.appendChild(a); } for (Iterator i = statements.iterator(); i.hasNext(); ) assertion.appendChild(((SAMLStatement)i.next()).toDOM(doc, false)); return root = assertion; } /** * @see org.opensaml.SAMLObject#checkValidity() */ public void checkValidity() throws SAMLException { if (XML.isEmpty(issuer) || statements.size() == 0) throw new MalformedException("Assertion is invalid, must have Issuer and at least one Statement"); } /** * 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 { SAMLAssertion dup=(SAMLAssertion)super.clone(); // Clone the embedded objects. for (Iterator i = conditions.iterator(); i.hasNext(); ) dup.conditions.add(((SAMLCondition)i.next()).clone()); for (Iterator i = advice.iterator(); i.hasNext(); ) { Object obj=i.next(); if (obj instanceof String) dup.advice.add(obj); else if (obj instanceof SAMLAssertion) dup.advice.add(((SAMLAssertion)obj).clone()); else dup.advice.add(((Element)obj).cloneNode(true)); } for (Iterator i = statements.iterator(); i.hasNext(); ) dup.statements.add(((SAMLStatement)i.next()).clone()); return dup; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -