📄 samlattribute.java
字号:
* Checks the attribute's syntactic validity. An exception * is thrown if any problems are detected. The exception will contain a * message describing the problem, and may wrap another exception.<P> * * Because attributes are generalized, this base method only handles * attributes whose values are of uniform schema type. The * attribute's schema type is set by the first xsi:type attribute found in * the value list, if any.<P> * * The addValue method is used to actually process the values, and can be * overridden to handle more complex values * * @param e Root element of a DOM tree * @exception SAMLException Raised if an exception occurs while constructing the object. */ public void fromDOM(Element e) throws SAMLException { super.fromDOM(e); if (config.getBooleanProperty("org.opensaml.strict-dom-checking") && !XML.isElementNamed(e,XML.SAML_NS,"Attribute")) throw new MalformedException("SAMLAttribute.fromDOM() requires saml:Attribute at root"); name = e.getAttributeNS(null, "AttributeName"); namespace = e.getAttributeNS(null, "AttributeNamespace"); // Iterate over AttributeValues. Element n = XML.getFirstChildElement(e, XML.SAML_NS, "AttributeValue"); while (n != null) { if (type == null) type = QName.getQNameAttribute(n, XML.XSI_NS, "type"); if (accept(n)) addValue(n); n = XML.getNextSiblingElement(n); } checkValidity(); } /** * Gets the AttributeName attribute of the SAML Attribute * * @return The name value */ public String getName() { return name; } /** * Sets the AttributeName attribute of the SAML Attribute * * @param name The name value */ public void setName(String name) { if (XML.isEmpty(name)) throw new IllegalArgumentException("name cannot be null"); this.name = name; if (root != null) { ((Element)root).getAttributeNodeNS(null, "AttributeName").setNodeValue(name); } } /** * Gets the AttributeNamespace attribute of the SAML Attribute * * @return The namespace value */ public String getNamespace() { return namespace; } /** * Sets the AttributeNamespace attribute of the SAML Attribute * * @param namespace The name value */ public void setNamespace(String namespace) { if (XML.isEmpty(namespace)) throw new IllegalArgumentException("namespace cannot be null"); this.namespace = namespace; if (root != null) { ((Element)root).getAttributeNodeNS(null, "AttributeNamespace").setNodeValue(namespace); } } /** * Gets the value of the xsi:type attribute, if any, of the SAML Attribute * * @return The schema type value */ public QName getType() { return type; } /** * Sets the value of the xsi:type attribute, if any, of the SAML Attribute * * @param type The schema type value */ public void setType(QName type) { this.type = type; if (root != null) { String xsitype = computeTypeDecl((Element)root); Element child = XML.getFirstChildElement(root); while (child != null) { child.removeAttributeNS(XML.XSI_NS, "xsi:type"); if (xsitype != null) child.setAttributeNS(XML.XSI_NS, "xsi:type", xsitype); child = XML.getNextSiblingElement(child); } } } /** * Gets the value's lifetime, in seconds * * @return The effective lifetime of the attribute value, in seconds (0 * means infinite) */ public long getLifetime() { return lifetime; } /** * Sets the value's lifetime, in seconds * * @param lifetime The effective lifetime of the attribute value, in seconds (0 * means infinite) */ public void setLifetime(long lifetime) { this.lifetime = lifetime; } /** * Gets the values of the SAML Attribute * * @return An iterator over the values */ public Iterator getValues() { return values.iterator(); } /** * Attribute acceptance hook used while consuming attributes from an * assertion. Base class simply accepts anything. Override for desired * behavior. * * @param e An AttributeValue element to check * @return true iff the value is deemed acceptable */ public boolean accept(Element e) { if (e == null) throw new IllegalArgumentException("e cannot be null"); return true; } /** * Imports a value to the state of the SAML Attribute, subject to acceptance.<P> * The base version only supports a simple text node content model and uses Strings.<P> * Override this method to perform advanced processing during XML import. * * @param e The AttributeValue element containing the value to add * @return true iff the value was understandable */ public boolean addValue(Element e) { Node n = e.getFirstChild(); if (accept(e)) { if (n != null && n.getNodeType() == Node.TEXT_NODE) { values.add(n.getNodeValue()); return true; } log.warn("rejecting an AttributeValue without a simple, non-empty text node"); } return false; } /** * Sets the values of the attribute * * @param values The values to use * @throws SAMLException Raised if the value cannot be added to the attribute */ public void setValues(Collection values) throws SAMLException { while (this.values.size() > 0) removeValue(0); if (values != null) { for (Iterator i = values.iterator(); i.hasNext(); ) addValue(i.next()); } } /** * Adds a value to the attribute * * @param value The value to add * @exception SAMLException Raised if the value cannot be properly added */ public void addValue(Object value) throws SAMLException { if (value != null) { if (root != null) { String xsitype = computeTypeDecl((Element)root); Element v = root.getOwnerDocument().createElementNS(XML.SAML_NS, "AttributeValue"); if (xsitype != null) v.setAttributeNS(XML.XSI_NS, "xsi:type", xsitype); valueToDOM(value, v); root.appendChild(v); } values.add(value); } else throw new IllegalArgumentException("value cannot be null"); } /** * Removes a value by position (zero-based) * * @param index The position of the value to remove */ public void removeValue(int index) throws IndexOutOfBoundsException { values.remove(index); if (root != null) { Element e = XML.getFirstChildElement(root); 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:xsi", XML.XSI_NS); ((Element)root).setAttributeNS(XML.XMLNS_NS, "xmlns:xsd", XML.XSD_NS); } return root; } Element a = doc.createElementNS(XML.SAML_NS, "Attribute"); if (xmlns) a.setAttributeNS(XML.XMLNS_NS, "xmlns", XML.SAML_NS); a.setAttributeNS(XML.XMLNS_NS, "xmlns:xsi", XML.XSI_NS); a.setAttributeNS(XML.XMLNS_NS, "xmlns:xsd", XML.XSD_NS); a.setAttributeNS(null, "AttributeName", name); a.setAttributeNS(null, "AttributeNamespace", namespace); String xsitype = computeTypeDecl(a); for (Iterator i = values.iterator(); i.hasNext(); ) { Element v = doc.createElementNS(XML.SAML_NS, "AttributeValue"); if (xsitype != null) v.setAttributeNS(XML.XSI_NS, "xsi:type", xsitype); valueToDOM(i.next(), v); a.appendChild(v); } return root = a; } /** * @see org.opensaml.SAMLObject#checkValidity() */ public void checkValidity() throws SAMLException { if (XML.isEmpty(name) || XML.isEmpty(namespace) || values.size() == 0) throw new MalformedException(SAMLException.RESPONDER, "Attribute invalid, requires name and namespace, and at least one value"); } /** * Copies a SAML object such that no dependencies exist between the original * and the copy. Does not clone values. * * @return The new object * @see java.lang.Object#clone() */ public Object clone() throws CloneNotSupportedException { SAMLAttribute dup=(SAMLAttribute)super.clone(); dup.values = (ArrayList)values.clone(); return dup; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -