Search in sources :

Example 6 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class XPathTest method testXPathUsingSaxon.

@Test
public void testXPathUsingSaxon() throws Exception {
    XPathFactory fac = new XPathFactoryImpl();
    XPathBuilder builder = XPathBuilder.xpath("foo/bar").factory(fac);
    // will evaluate as XPathConstants.NODESET and have Camel convert that to String
    // this should return the String incl. xml tags
    String name = builder.evaluate(context, "<foo><bar id=\"1\">cheese</bar></foo>", String.class);
    assertEquals("<bar id=\"1\">cheese</bar>", name);
    // will evaluate using XPathConstants.STRING which just return the text content (eg like text())
    name = builder.evaluate(context, "<foo><bar id=\"1\">cheese</bar></foo>");
    assertEquals("cheese", name);
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathFactory(javax.xml.xpath.XPathFactory) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Example 7 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class XPathTest method testXPathFunctionTokenizeUsingSystemProperty.

@Test
public void testXPathFunctionTokenizeUsingSystemProperty() throws Exception {
    // START SNIPPET: e4
    // set system property with the XPath factory to use which is Saxon 
    System.setProperty(XPathFactory.DEFAULT_PROPERTY_NAME + ":" + "http://saxon.sf.net/jaxp/xpath/om", "net.sf.saxon.xpath.XPathFactoryImpl");
    // create a builder to evaluate the xpath using saxon
    XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]");
    // evaluate as a String result
    String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
    assertEquals("def", result);
// END SNIPPET: e4
}
Also used : XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Example 8 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class XMLSecurityDataFormat method decode.

private Object decode(Exchange exchange, Document encodedDocument, Key keyEncryptionKey) throws Exception {
    XMLCipher xmlCipher = XMLCipher.getInstance();
    xmlCipher.setSecureValidation(true);
    xmlCipher.init(XMLCipher.DECRYPT_MODE, null);
    xmlCipher.setKEK(keyEncryptionKey);
    if (secureTag.equalsIgnoreCase("")) {
        checkEncryptionAlgorithm(keyEncryptionKey, encodedDocument.getDocumentElement());
        encodedDocument = xmlCipher.doFinal(encodedDocument, encodedDocument.getDocumentElement());
    } else {
        XPathBuilder xpathBuilder = new XPathBuilder(secureTag);
        xpathBuilder.setNamespaceContext(getNamespaceContext());
        NodeList nodeList = xpathBuilder.evaluate(exchange, NodeList.class);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            encodedDocument = node.getOwnerDocument();
            if (getSecureTagContents()) {
                checkEncryptionAlgorithm(keyEncryptionKey, (Element) node);
                Document temp = xmlCipher.doFinal(encodedDocument, (Element) node, true);
                encodedDocument.importNode(temp.getDocumentElement().cloneNode(true), true);
            } else {
                NodeList childNodes = node.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    Node childNode = childNodes.item(j);
                    if (childNode.getLocalName().equals("EncryptedData")) {
                        checkEncryptionAlgorithm(keyEncryptionKey, (Element) childNode);
                        Document temp = xmlCipher.doFinal(encodedDocument, (Element) childNode, false);
                        encodedDocument.importNode(temp.getDocumentElement().cloneNode(true), true);
                    }
                }
            }
        }
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        DOMSource source = new DOMSource(encodedDocument);
        InputStream sis = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, source);
        IOHelper.copy(sis, bos);
    } finally {
        bos.close();
    }
    // Return the decrypted data
    return bos.toByteArray();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) XMLCipher(org.apache.xml.security.encryption.XMLCipher) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document)

Example 9 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class XMLSecurityDataFormat method encrypt.

private void encrypt(Exchange exchange, Document document, OutputStream stream, Key dataEncryptionKey, XMLCipher keyCipher, Key keyEncryptionKey) throws Exception {
    XMLCipher xmlCipher = XMLCipher.getInstance(xmlCipherAlgorithm);
    xmlCipher.init(XMLCipher.ENCRYPT_MODE, dataEncryptionKey);
    if (secureTag.equalsIgnoreCase("")) {
        embedKeyInfoInEncryptedData(document, keyCipher, xmlCipher, dataEncryptionKey, keyEncryptionKey);
        document = xmlCipher.doFinal(document, document.getDocumentElement());
    } else {
        XPathBuilder xpathBuilder = new XPathBuilder(secureTag);
        xpathBuilder.setNamespaceContext(getNamespaceContext());
        NodeList nodeList = xpathBuilder.evaluate(exchange, NodeList.class);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            document = node.getOwnerDocument();
            embedKeyInfoInEncryptedData(node.getOwnerDocument(), keyCipher, xmlCipher, dataEncryptionKey, keyEncryptionKey);
            Document temp = xmlCipher.doFinal(node.getOwnerDocument(), (Element) node, getSecureTagContents());
            document.importNode(temp.getDocumentElement().cloneNode(true), true);
        }
    }
    try {
        DOMSource source = new DOMSource(document);
        InputStream sis = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, source);
        IOHelper.copy(sis, stream);
    } finally {
        stream.close();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) XMLCipher(org.apache.xml.security.encryption.XMLCipher) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Document(org.w3c.dom.Document)

Example 10 with XPathBuilder

use of org.apache.camel.builder.xml.XPathBuilder in project camel by apache.

the class XPathTest method testXPathFunctionTokenizeUsingSaxon.

@Test
public void testXPathFunctionTokenizeUsingSaxon() throws Exception {
    // START SNIPPET: e3
    // create a builder to evaluate the xpath using saxon
    XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").saxon();
    // evaluate as a String result
    String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
    assertEquals("def", result);
// END SNIPPET: e3
}
Also used : XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Aggregations

XPathBuilder (org.apache.camel.builder.xml.XPathBuilder)14 Test (org.junit.Test)6 XPathFactory (javax.xml.xpath.XPathFactory)3 XPathFactoryImpl (net.sf.saxon.xpath.XPathFactoryImpl)3 InputStream (java.io.InputStream)2 DOMSource (javax.xml.transform.dom.DOMSource)2 SimpleBuilder (org.apache.camel.builder.SimpleBuilder)2 ValueBuilder (org.apache.camel.builder.ValueBuilder)2 ExpressionDefinition (org.apache.camel.model.language.ExpressionDefinition)2 SimpleExpression (org.apache.camel.model.language.SimpleExpression)2 XPathExpression (org.apache.camel.model.language.XPathExpression)2 XMLCipher (org.apache.xml.security.encryption.XMLCipher)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Expression (org.apache.camel.Expression)1 Predicate (org.apache.camel.Predicate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 NamespacePrefix (org.apache.camel.language.NamespacePrefix)1