📄 samlsubject.java
字号:
if (last == null) sc.insertBefore(meth, sc.getFirstChild()); else sc.insertBefore(meth, last.getNextSibling()); } } confirmationMethods.add(confirmationMethod); } else throw new IllegalArgumentException("confirmationMethod cannot be null or empty"); } /** * Removes a confirmation method by position (zero-based) * * @param index The position of the method to remove */ public void removeConfirmationMethod(int index) throws IndexOutOfBoundsException { confirmationMethods.remove(index); if (root != null) { Element sc = XML.getFirstChildElement(root, XML.SAML_NS, "SubjectConfirmation"); if (confirmationMethods.size() == 0 && confirmationData == null && keyInfo == null) { root.removeChild(sc); return; } Element e = XML.getFirstChildElement(sc); while (e != null && index > 0) { e = XML.getNextSiblingElement(e); index--; } if (e != null) sc.removeChild(e); else throw new IndexOutOfBoundsException(); } } /** * Gets the optional confirmation data of the Subject * * @return The saml:SubjectConfirmationData element */ public Element getConfirmationData() { return confirmationData; } /** * Sets the optional confirmation data of the Subject * * @param confirmationData The saml:SubjectConfirmationData element */ public void setConfirmationData(Element confirmationData) { if (confirmationData != null && !XML.isElementNamed(confirmationData, XML.SAML_NS, "SubjectConfirmationData")) throw new IllegalArgumentException("confirmationData must be a saml:SubjectConfirmationData element"); if (root != null) { //Clear out the existing value. Element sc = XML.getFirstChildElement(root, XML.SAML_NS, "SubjectConfirmation"); if (this.confirmationData != null) { sc.removeChild(this.confirmationData); //Still need the element at all? if (confirmationData == null && keyInfo == null && confirmationMethods.size() == 0) { root.removeChild(sc); } } if (confirmationData != null) { //Recreate element if needed. if (sc == null) { Element ident = XML.getFirstChildElement(root, XML.SAML_NS, "NameIdentifier"); if (ident == null) sc = (Element)root.insertBefore( root.getOwnerDocument().createElementNS(XML.SAML_NS, "SubjectConfirmation"), root.getFirstChild() ); else sc = (Element)root.insertBefore( root.getOwnerDocument().createElementNS(XML.SAML_NS, "SubjectConfirmation"), ident.getNextSibling() ); sc.appendChild(root.getOwnerDocument().adoptNode(confirmationData)); } else { Element last = XML.getLastChildElement(sc, XML.SAML_NS, "ConfirmationMethod"); if (last == null) sc.insertBefore(root.getOwnerDocument().adoptNode(confirmationData), sc.getFirstChild()); else sc.insertBefore(root.getOwnerDocument().adoptNode(confirmationData), last.getNextSibling()); } } } this.confirmationData = confirmationData; } /** * Gets the ds:KeyInfo DOM that is included in the subject, if any * * @return Root of the ds:KeyInfo DOM */ public Element getKeyInfo() { return (keyInfo != null) ? keyInfo.getElement() : null; } /** * Gets the native library object for the ds:KeyInfo that is included in the subject, if any * * @return The native library object containing the ds:KeyInfo */ public Object getNativeKeyInfo() { return keyInfo; } /** * Sets the ds:KeyInfo of the Subject * * @param keyInfo The ds:KeyInfo DOM or native library object * @exception SAMLException Raised if the object is invalid */ public void setKeyInfo(Object keyInfo) throws SAMLException { if (keyInfo != null && !(keyInfo instanceof KeyInfo || keyInfo instanceof Element)) throw new IllegalArgumentException("keyInfo must be a ds:KeyInfo element or a native library object"); //Try and build a native object. KeyInfo nativeki = null; try { if (keyInfo instanceof Element) { if (root != null) nativeki = new KeyInfo((Element)root.getOwnerDocument().adoptNode((Node)keyInfo), null); else nativeki = new KeyInfo((Element)keyInfo, null); } else nativeki = (KeyInfo)keyInfo; } catch (XMLSecurityException ex) { throw new SAMLException("setKeyInfo() caught an XML security exception", ex); } if (root != null) { //Clear out the existing value. Element sc = XML.getFirstChildElement(root, XML.SAML_NS, "SubjectConfirmation"); if (this.keyInfo != null) { sc.removeChild(this.keyInfo.getElement()); //Still need the element at all? if (confirmationData == null && keyInfo == null && confirmationMethods.size() == 0) { root.removeChild(sc); } } if (keyInfo != null) { //Recreate element if needed. if (sc == null) { Element ident = XML.getFirstChildElement(root, XML.SAML_NS, "NameIdentifier"); if (ident == null) sc = (Element)root.insertBefore( root.getOwnerDocument().createElementNS(XML.SAML_NS, "SubjectConfirmation"), root.getFirstChild() ); else sc = (Element)root.insertBefore( root.getOwnerDocument().createElementNS(XML.SAML_NS, "SubjectConfirmation"), ident.getNextSibling() ); sc.appendChild(nativeki.getElement()); } else { if (confirmationData == null) { Element last = XML.getLastChildElement(sc, XML.SAML_NS, "ConfirmationMethod"); if (last == null) sc.insertBefore(nativeki.getElement(), sc.getFirstChild()); else sc.insertBefore(nativeki.getElement(), last.getNextSibling()); } else sc.insertBefore(nativeki.getElement(), confirmationData.getNextSibling()); } } } this.keyInfo = nativeki; } /** * @see org.opensaml.SAMLObject#toDOM() */ public Node toDOM() throws SAMLException { if (confirmationData != null) return toDOM(confirmationData.getOwnerDocument()); else if (keyInfo != null) return toDOM(keyInfo.getDocument()); else return super.toDOM(); } /** * @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); return root; } // This is the meat. Build a new XML instance using our state and the specified Document. Element subject = doc.createElementNS(XML.SAML_NS, "Subject"); if (xmlns) subject.setAttributeNS(XML.XMLNS_NS, "xmlns", XML.SAML_NS); if (nameId != null) subject.appendChild(nameId.toDOM(doc, false)); if (confirmationMethods.size() > 0) { Element conf = doc.createElementNS(XML.SAML_NS, "SubjectConfirmation"); Iterator i=confirmationMethods.iterator(); while (i.hasNext()) conf.appendChild(doc.createElementNS(XML.SAML_NS, "ConfirmationMethod")).appendChild(doc.createTextNode((String)i.next())); if (confirmationData != null) conf.appendChild(doc.adoptNode(confirmationData)); if (keyInfo != null) conf.appendChild(doc.adoptNode(keyInfo.getElement())); subject.appendChild(conf); } return root = subject; } /** * @see org.opensaml.SAMLObject#checkValidity() */ public void checkValidity() throws SAMLException { if (nameId == null && (confirmationMethods == null || confirmationMethods.size() == 0)) throw new MalformedException("Subject is invalid, requires either NameIdentifier or at least one ConfirmationMethod"); else if (confirmationData != null && !XML.isElementNamed(confirmationData, XML.SAML_NS, "SubjectConfirmationData")) throw new MalformedException("Subject is invalid, requires that confirmation data be a saml:SubjectConfirmationData element"); } /** * 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 { SAMLSubject dup=(SAMLSubject)super.clone(); if (nameId != null) dup.nameId = (SAMLNameIdentifier)nameId.clone(); dup.confirmationMethods = (ArrayList)confirmationMethods.clone(); if (confirmationData != null) dup.confirmationData = (Element)confirmationData.cloneNode(true); if (keyInfo != null) { try { dup.keyInfo = new KeyInfo((Element)keyInfo.getElement().cloneNode(true), null); } catch (XMLSecurityException e) { throw new RuntimeException("SAMLSubject.clone() unable to copy keyInfo"); } } return dup; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -