Search in sources :

Example 56 with XPathExpression

use of javax.xml.xpath.XPathExpression in project camel by apache.

the class XmlSignerProcessor method getParentForEnvelopedCase.

protected Element getParentForEnvelopedCase(Document doc, Message inMessage) throws Exception {
    //NOPMD
    if (getConfiguration().getParentXpath() != null) {
        XPathFilterParameterSpec xp = getConfiguration().getParentXpath();
        XPathExpression exp;
        try {
            exp = XmlSignatureHelper.getXPathExpression(xp);
        } catch (XPathExpressionException e) {
            throw new XmlSignatureException("The parent XPath " + getConfiguration().getParentXpath().getXPath() + " is wrongly configured: The XPath " + xp.getXPath() + " is invalid.", e);
        }
        NodeList list = (NodeList) exp.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
        if (list == null || list.getLength() == 0) {
            throw new XmlSignatureException("The parent XPath " + xp.getXPath() + " returned no result. Check the configuration of the XML signer component.");
        }
        int length = list.getLength();
        for (int i = 0; i < length; i++) {
            Node node = list.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                // return the first element
                return (Element) node;
            }
        }
        throw new XmlSignatureException("The parent XPath " + xp.getXPath() + " returned no element. Check the configuration of the XML signer component.");
    } else {
        // parent local name is not null!
        NodeList parents = doc.getElementsByTagNameNS(getConfiguration().getParentNamespace(), getConfiguration().getParentLocalName());
        if (parents == null || parents.getLength() == 0) {
            throw new XmlSignatureFormatException(String.format("Incoming message has wrong format: The parent element with the local name %s and the namespace %s was not found in the message to build an enveloped XML signature.", getConfiguration().getParentLocalName(), getConfiguration().getParentNamespace()));
        }
        // return the first element
        return (Element) parents.item(0);
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XmlSignatureException(org.apache.camel.component.xmlsecurity.api.XmlSignatureException) XmlSignatureFormatException(org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XPathFilterParameterSpec(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec)

Example 57 with XPathExpression

use of javax.xml.xpath.XPathExpression in project camel by apache.

the class XAdESSignaturePropertiesTest method checkNode.

private void checkNode(Document doc, String xpathString, final Map<String, String> prefix2Namespace, boolean exists) throws XPathExpressionException {
    XPathExpression expr = getXpath(xpathString, prefix2Namespace);
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    if (exists) {
        assertNotNull("The xpath " + xpathString + " returned null, expected was a node", result);
    } else {
        assertNull("The xpath " + xpathString + " returned a node, expected was none: ", result);
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression)

Example 58 with XPathExpression

use of javax.xml.xpath.XPathExpression in project camel by apache.

the class XAdESSignaturePropertiesTest method getXpath.

static XPathExpression getXpath(String xpathString, final Map<String, String> prefix2Namespace) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NamespaceContext nc = new NamespaceContext() {

        @SuppressWarnings("rawtypes")
        @Override
        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

        @Override
        public String getPrefix(String namespaceURI) {
            return null;
        }

        @Override
        public String getNamespaceURI(String prefix) {
            return prefix2Namespace.get(prefix);
        }
    };
    xpath.setNamespaceContext(nc);
    XPathExpression expr = xpath.compile(xpathString);
    return expr;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) NamespaceContext(javax.xml.namespace.NamespaceContext)

Example 59 with XPathExpression

use of javax.xml.xpath.XPathExpression in project camel by apache.

the class XAdESSignaturePropertiesTest method checkXpath.

static void checkXpath(Document doc, String xpathString, final Map<String, String> prefix2Namespace, String expectedResult) throws XPathExpressionException {
    XPathExpression expr = getXpath(xpathString, prefix2Namespace);
    String result = (String) expr.evaluate(doc, XPathConstants.STRING);
    assertNotNull("The xpath " + xpathString + " returned a null value", result);
    if (NOT_EMPTY.equals(expectedResult)) {
        assertTrue("Not empty result for xpath " + xpathString + " expected", !result.isEmpty());
    } else {
        assertEquals(expectedResult, result);
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression)

Example 60 with XPathExpression

use of javax.xml.xpath.XPathExpression in project camel by apache.

the class XmlSignatureTest method checkXpath.

private Object checkXpath(MockEndpoint mock, String xpathString, final Map<String, String> prefix2Namespace) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
    Message mess = getMessage(mock);
    InputStream body = mess.getBody(InputStream.class);
    assertNotNull(body);
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NamespaceContext nc = new NamespaceContext() {

        @SuppressWarnings("rawtypes")
        @Override
        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

        @Override
        public String getPrefix(String namespaceURI) {
            return null;
        }

        @Override
        public String getNamespaceURI(String prefix) {
            return prefix2Namespace.get(prefix);
        }
    };
    xpath.setNamespaceContext(nc);
    XPathExpression expr = xpath.compile(xpathString);
    Object result = expr.evaluate(XmlSignatureHelper.newDocumentBuilder(true).parse(body), XPathConstants.NODE);
    assertNotNull("The xpath " + xpathString + " returned a null value", result);
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) Message(org.apache.camel.Message) XmlSignature2Message(org.apache.camel.component.xmlsecurity.api.XmlSignature2Message) InputStream(java.io.InputStream) NamespaceContext(javax.xml.namespace.NamespaceContext)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)98 XPath (javax.xml.xpath.XPath)69 NodeList (org.w3c.dom.NodeList)56 Document (org.w3c.dom.Document)48 XPathExpressionException (javax.xml.xpath.XPathExpressionException)40 XPathFactory (javax.xml.xpath.XPathFactory)40 Node (org.w3c.dom.Node)38 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Element (org.w3c.dom.Element)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 InputSource (org.xml.sax.InputSource)9