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);
}
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
}
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();
}
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();
}
}
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
}
Aggregations