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