/** * Verifies if the input xmlstring can be converted to an AttributeValue Element. If so, return * the element value. */ private String validateAndGetAttributeValue(String value) throws SAML2Exception { Document doc = XMLUtils.toDOMDocument(value, SAML2SDKUtils.debug); if (doc == null) { if (SAML2SDKUtils.debug.messageEnabled()) { SAML2SDKUtils.debug.message( "AttributeImpl." + "validateAttributeValue:" + " could not obtain AttributeValue element."); } throw new SAML2Exception(SAML2SDKUtils.bundle.getString("errorObtainingElement")); } Element element = doc.getDocumentElement(); if (element == null) { if (SAML2SDKUtils.debug.messageEnabled()) { SAML2SDKUtils.debug.message("AttributeImpl." + "validateAttributeValue: Input is null."); } throw new SAML2Exception(SAML2SDKUtils.bundle.getString("nullInput")); } // Make sure this is an AttributeValue. String tag = element.getLocalName(); if ((tag == null) || (!tag.equals("AttributeValue"))) { if (SAML2SDKUtils.debug.messageEnabled()) { SAML2SDKUtils.debug.message( "AttributeImpl." + "validateAttributeValue: not AttributeValue."); } throw new SAML2Exception(SAML2SDKUtils.bundle.getString("wrongInput")); } return XMLUtils.getChildrenValue(element); }
// used by the constructors. private void parseElement(Element element) throws SAML2Exception { // make sure that the input xml block is not null if (element == null) { if (SAML2SDKUtils.debug.messageEnabled()) { SAML2SDKUtils.debug.message("AttributeImpl.parseElement: " + "Input is null."); } throw new SAML2Exception(SAML2SDKUtils.bundle.getString("nullInput")); } // Make sure this is an Attribute. String tag = element.getLocalName(); if ((tag == null) || (!tag.equals("Attribute"))) { if (SAML2SDKUtils.debug.messageEnabled()) { SAML2SDKUtils.debug.message("AttributeImpl.parseElement: " + "not Attribute."); } throw new SAML2Exception(SAML2SDKUtils.bundle.getString("wrongInput")); } // handle the attributes of <Attribute> element NamedNodeMap atts = ((Node) element).getAttributes(); if (atts != null) { int length = atts.getLength(); for (int i = 0; i < length; i++) { Attr attr = (Attr) atts.item(i); String attrName = attr.getName(); String attrValue = attr.getValue().trim(); if (attrName.equals("Name")) { name = attrValue; } else if (attrName.equals("NameFormat")) { nameFormat = attrValue; } else if (attrName.equals("FriendlyName")) { friendlyName = attrValue; } else { if (!attrValue.equals(SAML2Constants.ASSERTION_NAMESPACE_URI)) { if (anyMap == null) { anyMap = new HashMap(); } anyMap.put(attrName, attrValue); } } } } // handle AttributeValue NodeList nl = element.getChildNodes(); Node child; String childName; int length = nl.getLength(); for (int i = 0; i < length; i++) { child = nl.item(i); if ((childName = child.getLocalName()) != null) { if (childName.equals("AttributeValue")) { if (attrValues == null) { attrValues = new ArrayList(); } attrValues.add(XMLUtils.print(child)); if (valueStrings == null) { valueStrings = new ArrayList(); } valueStrings.add(XMLUtils.getChildrenValue((Element) child)); } else { if (SAML2SDKUtils.debug.messageEnabled()) { SAML2SDKUtils.debug.message( "Attributempl.parseElement" + ": Invalid element:" + childName); } throw new SAML2Exception(SAML2SDKUtils.bundle.getString("invalidElement")); } } } if (name == null || name.trim().length() == 0) { if (SAML2SDKUtils.debug.messageEnabled()) { SAML2SDKUtils.debug.message("AttributeImpl.parseElement:" + " missing Name attribute."); } throw new SAML2Exception(SAML2SDKUtils.bundle.getString("missingAttribute")); } if (attrValues != null) { attrValues = Collections.unmodifiableList(attrValues); } if (valueStrings != null) { valueStrings = Collections.unmodifiableList(valueStrings); } if (anyMap != null) { anyMap = Collections.unmodifiableMap(anyMap); } mutable = false; }