Search in sources :

Example 16 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project OpenAM by OpenRock.

the class ResponseImpl method parseElement.

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("ResponseImpl.parseElement: " + "element input is null.");
        }
        throw new SAML2Exception(SAML2SDKUtils.bundle.getString("nullInput"));
    }
    // Make sure this is an Response.
    String tag = null;
    tag = element.getLocalName();
    if ((tag == null) || (!tag.equals("Response"))) {
        if (SAML2SDKUtils.debug.messageEnabled()) {
            SAML2SDKUtils.debug.message("ResponseImpl.parseElement: " + "not Response.");
        }
        throw new SAML2Exception(SAML2SDKUtils.bundle.getString("wrongInput"));
    }
    // handle the attributes of <Response> 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("ID")) {
                responseId = attrValue;
            } else if (attrName.equals("InResponseTo")) {
                inResponseTo = attrValue;
            } else if (attrName.equals("Version")) {
                version = attrValue;
            } else if (attrName.equals("IssueInstant")) {
                try {
                    issueInstant = DateUtils.stringToDate(attrValue);
                } catch (ParseException pe) {
                    throw new SAML2Exception(pe.getMessage());
                }
            } else if (attrName.equals("Destination")) {
                destination = attrValue;
            } else if (attrName.equals("Consent")) {
                consent = attrValue;
            }
        }
    }
    // handle child elements
    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("Issuer")) {
                if (issuer != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element: included more than one Issuer.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
                }
                if (signatureString != null || extensions != null || status != null || assertions != null || encAssertions != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
                }
                issuer = AssertionFactory.getInstance().createIssuer((Element) child);
            } else if (childName.equals("Signature")) {
                if (signatureString != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:included more than one Signature.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
                }
                if (extensions != null || status != null || assertions != null || encAssertions != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
                }
                signatureString = XMLUtils.print((Element) child, "UTF-8");
                isSigned = true;
            } else if (childName.equals("Extensions")) {
                if (extensions != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:included more than one Extensions.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
                }
                if (status != null || assertions != null || encAssertions != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
                }
                extensions = ProtocolFactory.getInstance().createExtensions((Element) child);
            } else if (childName.equals("Status")) {
                if (status != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element: included more than one Status.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
                }
                if (assertions != null || encAssertions != null) {
                    if (SAML2SDKUtils.debug.messageEnabled()) {
                        SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
                    }
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
                }
                status = ProtocolFactory.getInstance().createStatus((Element) child);
            } else if (childName.equals("Assertion")) {
                if (assertions == null) {
                    assertions = new ArrayList();
                }
                Element canoEle = SAMLUtils.getCanonicalElement(child);
                if (canoEle == null) {
                    throw new SAML2Exception(SAML2SDKUtils.bundle.getString("errorCanonical"));
                }
                assertions.add(AssertionFactory.getInstance().createAssertion(canoEle));
            } else if (childName.equals("EncryptedAssertion")) {
                if (encAssertions == null) {
                    encAssertions = new ArrayList();
                }
                encAssertions.add(AssertionFactory.getInstance().createEncryptedAssertion((Element) child));
            } else {
                if (SAML2SDKUtils.debug.messageEnabled()) {
                    SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element: Invalid element:" + childName);
                }
                throw new SAML2Exception(SAML2SDKUtils.bundle.getString("invalidElement"));
            }
        }
    }
    super.validateData();
    if (assertions != null) {
        Iterator iter = assertions.iterator();
        while (iter.hasNext()) {
            ((Assertion) iter.next()).makeImmutable();
        }
        assertions = Collections.unmodifiableList(assertions);
    }
    if (encAssertions != null) {
        encAssertions = Collections.unmodifiableList(encAssertions);
    }
    isMutable = false;
}
Also used : SAML2Exception(com.sun.identity.saml2.common.SAML2Exception) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) EncryptedAssertion(com.sun.identity.saml2.assertion.EncryptedAssertion) Assertion(com.sun.identity.saml2.assertion.Assertion) ParseException(java.text.ParseException) Attr(org.w3c.dom.Attr)

Example 17 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project OpenAM by OpenRock.

the class FSRequest method parseQuery.

/**
     * Parses the Query or <code>SubjectQuery</code> represented by
     * a DOM tree Node. It then checks and sets data members if it is a
     * supported query, such as <code>AuthenticationQuery</code>,
     * <code>AttributeQeury</code>, or <code>AuthorizationDecisionQuery</code>.
     *
     * @param child a <code>DOM</code> Node.
     * @throws <code>SAMLException</code> if the <code>Query</code> is invalid.
     */
private void parseQuery(Node child) throws SAMLException {
    NamedNodeMap nm = child.getAttributes();
    int len = nm.getLength();
    String attrName;
    String attrValue;
    Attr attr;
    boolean found = false;
    for (int j = 0; j < len; j++) {
        attr = (Attr) nm.item(j);
        attrName = attr.getLocalName();
        if ((attrName != null) && (attrName.equals("type"))) {
            attrValue = attr.getNodeValue();
            if (attrValue.equals("AuthenticationQueryType")) {
                if (contentType != NOT_SUPPORTED) {
                    if (FSUtils.debug.messageEnabled()) {
                        FSUtils.debug.message("Request(Element): should" + " contain only one AuthenticationQuery.");
                    }
                    throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
                }
                contentType = AUTHENTICATION_QUERY;
                query = new AuthenticationQuery((Element) child);
            } else if (attrValue.equals("AuthorizationDecisionQueryType")) {
                if (contentType != NOT_SUPPORTED) {
                    if (FSUtils.debug.messageEnabled()) {
                        FSUtils.debug.message("Request(Element): should " + "contain one " + "AuthorizationDecisionQuery.");
                    }
                    throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
                }
                contentType = AUTHORIZATION_DECISION_QUERY;
                query = new AuthorizationDecisionQuery((Element) child);
            } else if (attrValue.equals("AttributeQueryType")) {
                if (contentType != NOT_SUPPORTED) {
                    if (FSUtils.debug.messageEnabled()) {
                        FSUtils.debug.message("Request(Element): should " + "contain one AttributeQuery.");
                    }
                    throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
                }
                contentType = ATTRIBUTE_QUERY;
                query = new AttributeQuery((Element) child);
            } else {
                if (FSUtils.debug.messageEnabled()) {
                    FSUtils.debug.message("Request(Element): This type of" + " " + attrName + " is not supported.");
                }
                throw new SAMLResponderException(FSUtils.BUNDLE_NAME, "queryNotSupported", null);
            }
            // check typevalue
            found = true;
            break;
        }
    // if found type attribute
    }
    // if not found type
    if (!found) {
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("Request(Element): missing" + " xsi:type definition in " + child.getLocalName());
        }
        throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) AttributeQuery(com.sun.identity.saml.protocol.AttributeQuery) Element(org.w3c.dom.Element) SAMLRequesterException(com.sun.identity.saml.common.SAMLRequesterException) AuthenticationQuery(com.sun.identity.saml.protocol.AuthenticationQuery) AuthorizationDecisionQuery(com.sun.identity.saml.protocol.AuthorizationDecisionQuery) Attr(org.w3c.dom.Attr) SAMLResponderException(com.sun.identity.saml.common.SAMLResponderException)

Example 18 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project OpenAM by OpenRock.

the class SubjectConfirmationDataImpl method parseElement.

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("parseElement: " + "Input is null.");
        }
        throw new SAML2Exception(SAML2SDKUtils.bundle.getString("nullInput"));
    }
    // Make sure this is an SubjectConfirmationData.
    String tag = element.getLocalName();
    if ((tag == null) || (!tag.equals(elementName))) {
        if (SAML2SDKUtils.debug.messageEnabled()) {
            SAML2SDKUtils.debug.message("parseElement: " + "not SubjectConfirmationData.");
        }
        throw new SAML2Exception(SAML2SDKUtils.bundle.getString("wrongInput"));
    }
    // handle the attributes of <SubjectConfirmationData> element
    NamedNodeMap attrs = ((Node) element).getAttributes();
    parseAttributes(attrs);
    parseContent(element);
}
Also used : SAML2Exception(com.sun.identity.saml2.common.SAML2Exception) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node)

Example 19 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project OpenAM by OpenRock.

the class WebtopParser method walkTree.

/**
     * Parses and processes a Node.
     *
     * @param nd The Node to parse.
     * @return the parsed object for the node.
     * @throws Exception if node cannot be parsed or <code>ParseOutput</code>
     *         object cannot be instantiated.
     */
ParseOutput walkTree(Node nd) throws Exception {
    Vector elements = new Vector();
    Vector retelements;
    String pcdata = null;
    Hashtable atts = new Hashtable();
    NamedNodeMap nd_map = nd.getAttributes();
    if (nd_map != null) {
        for (int i = 0; i < nd_map.getLength(); i++) {
            Node att = nd_map.item(i);
            atts.put(att.getNodeName(), att.getNodeValue());
        }
    }
    for (Node ch = nd.getFirstChild(); ch != null; ch = ch.getNextSibling()) {
        switch(ch.getNodeType()) {
            case Node.ELEMENT_NODE:
                elements.addElement(walkTree(ch));
                break;
            case Node.TEXT_NODE:
                String tmp = stripWhitespaces(ch.getNodeValue());
                if (tmp != null && tmp.length() != 0) {
                    pcdata = tmp;
                }
                break;
            default:
        }
    }
    // lookup hash
    String po_name = (String) elemmap.get(nd.getNodeName());
    ParseOutput po;
    if (po_name == null) {
        if (useGenericClass) {
            po = (ParseOutput) new GenericNode();
        } else {
            throw new Exception("No class registered for" + nd.getNodeName());
        }
    } else {
        try {
            po = (ParseOutput) Class.forName(po_name).newInstance();
        } catch (Exception ex) {
            StringBuilder buf = new StringBuilder();
            buf.append("Got Exception while creating class instance of ");
            buf.append(nd.getNodeName());
            buf.append(" :");
            buf.append(ex.toString());
            throw new Exception(buf.toString());
        }
    }
    po.process(nd.getNodeName(), elements, atts, pcdata);
    return po;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Hashtable(java.util.Hashtable) Node(org.w3c.dom.Node) Vector(java.util.Vector) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 20 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project tdi-studio-se by Talend.

the class AutoConvertTypesUtils method load.

public static List<AutoConversionType> load(File file) {
    beanList = new ArrayList<>();
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder analyseur = documentBuilderFactory.newDocumentBuilder();
        analyseur.setErrorHandler(new ErrorHandler() {

            @Override
            public void error(final SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void fatalError(final SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void warning(final SAXParseException exception) throws SAXException {
                throw exception;
            }
        });
        Document document = analyseur.parse(file);
        //$NON-NLS-1$
        NodeList typeNodes = document.getElementsByTagName("conversionType");
        for (int i = 0; i < typeNodes.getLength(); i++) {
            Node typeNode = typeNodes.item(i);
            NamedNodeMap typeAttributes = typeNode.getAttributes();
            AutoConversionType typeObj = new AutoConversionType();
            //$NON-NLS-1$
            typeObj.setSourceDataType(typeAttributes.getNamedItem("source").getNodeValue());
            //$NON-NLS-1$
            typeObj.setTargetDataType(typeAttributes.getNamedItem("target").getNodeValue());
            //$NON-NLS-1$
            typeObj.setConversionFunction(typeAttributes.getNamedItem("function").getNodeValue());
            beanList.add(typeObj);
        }
    } catch (Exception e) {
        return beanList;
    }
    return beanList;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) PersistenceException(org.talend.commons.exception.PersistenceException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) AutoConversionType(org.talend.core.model.metadata.types.AutoConversionType) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException)

Aggregations

NamedNodeMap (org.w3c.dom.NamedNodeMap)991 Node (org.w3c.dom.Node)688 NodeList (org.w3c.dom.NodeList)338 Attr (org.w3c.dom.Attr)295 Element (org.w3c.dom.Element)237 Document (org.w3c.dom.Document)153 ArrayList (java.util.ArrayList)92 HashMap (java.util.HashMap)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)58 IOException (java.io.IOException)53 SAXException (org.xml.sax.SAXException)41 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)40 List (java.util.List)36 Map (java.util.Map)33 File (java.io.File)27 InputStream (java.io.InputStream)26 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)25 DOMException (org.w3c.dom.DOMException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)19