use of javax.xml.xpath.XPathExpression in project camel by apache.
the class XAdESSignaturePropertiesTest method headers.
@Test
public void headers() throws Exception {
Map<String, Object> header = new TreeMap<String, Object>();
header.put(XmlSignatureConstants.HEADER_XADES_PREFIX, "ns1");
header.put(XmlSignatureConstants.HEADER_XADES_NAMESPACE, XAdESSignatureProperties.HTTP_URI_ETSI_ORG_01903_V1_2_2);
header.put(XmlSignatureConstants.HEADER_XADES_QUALIFYING_PROPERTIES_ID, "QualId");
header.put(XmlSignatureConstants.HEADER_XADES_SIGNED_DATA_OBJECT_PROPERTIES_ID, "ObjId");
header.put(XmlSignatureConstants.HEADER_XADES_SIGNED_SIGNATURE_PROPERTIES_ID, "SigId");
header.put(XmlSignatureConstants.HEADER_XADES_DATA_OBJECT_FORMAT_ENCODING, "base64");
XmlSignerEndpoint endpoint = getSignerEndpoint();
XAdESSignatureProperties props = (XAdESSignatureProperties) endpoint.getProperties();
// the following lists must be set to empty because otherwise they would contain XML fragments with a wrong namespace
props.setSigPolicyQualifiers(Collections.<String>emptyList());
props.setSignerClaimedRoles(Collections.<String>emptyList());
props.setCommitmentTypeQualifiers(Collections.<String>emptyList());
Document doc = testEnveloping("direct:enveloping", header);
Map<String, String> prefix2Namespace = new TreeMap<String, String>();
prefix2Namespace.put("ds", XMLSignature.XMLNS);
prefix2Namespace.put("etsi", XAdESSignatureProperties.HTTP_URI_ETSI_ORG_01903_V1_2_2);
XPathExpression expr = getXpath("/ds:Signature/ds:Object/etsi:QualifyingProperties", prefix2Namespace);
Object result = expr.evaluate(doc, XPathConstants.NODE);
assertNotNull(result);
Node node = (Node) result;
assertEquals("ns1", node.getPrefix());
assertEquals(XAdESSignatureProperties.HTTP_URI_ETSI_ORG_01903_V1_2_2, node.getNamespaceURI());
checkXpath(doc, "/ds:Signature/ds:Object/etsi:QualifyingProperties/@Id", prefix2Namespace, "QualId");
checkXpath(doc, "/ds:Signature/ds:Object/etsi:QualifyingProperties/etsi:SignedProperties/etsi:SignedDataObjectProperties/@Id", prefix2Namespace, "ObjId");
checkXpath(doc, "/ds:Signature/ds:Object/etsi:QualifyingProperties/etsi:SignedProperties/etsi:SignedSignatureProperties/@Id", prefix2Namespace, "SigId");
checkXpath(doc, "/ds:Signature/ds:Object/etsi:QualifyingProperties/etsi:SignedProperties/etsi:SignedDataObjectProperties/etsi:DataObjectFormat/etsi:Encoding/text()", prefix2Namespace, "base64");
}
use of javax.xml.xpath.XPathExpression in project camel by apache.
the class BomGeneratorMojo method overwriteDependencyManagement.
private void overwriteDependencyManagement(Document pom, List<Dependency> dependencies) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/project/dependencyManagement/dependencies");
NodeList nodes = (NodeList) expr.evaluate(pom, XPathConstants.NODESET);
if (nodes.getLength() == 0) {
throw new IllegalStateException("No dependencies found in the dependencyManagement section of the current pom");
}
Node dependenciesSection = nodes.item(0);
// cleanup the dependency management section
while (dependenciesSection.hasChildNodes()) {
Node child = dependenciesSection.getFirstChild();
dependenciesSection.removeChild(child);
}
for (Dependency dep : dependencies) {
Element dependencyEl = pom.createElement("dependency");
Element groupIdEl = pom.createElement("groupId");
groupIdEl.setTextContent(dep.getGroupId());
dependencyEl.appendChild(groupIdEl);
Element artifactIdEl = pom.createElement("artifactId");
artifactIdEl.setTextContent(dep.getArtifactId());
dependencyEl.appendChild(artifactIdEl);
Element versionEl = pom.createElement("version");
versionEl.setTextContent(dep.getVersion());
dependencyEl.appendChild(versionEl);
if (!"jar".equals(dep.getType())) {
Element typeEl = pom.createElement("type");
typeEl.setTextContent(dep.getType());
dependencyEl.appendChild(typeEl);
}
if (dep.getClassifier() != null) {
Element classifierEl = pom.createElement("classifier");
classifierEl.setTextContent(dep.getClassifier());
dependencyEl.appendChild(classifierEl);
}
if (dep.getScope() != null && !"compile".equals(dep.getScope())) {
Element scopeEl = pom.createElement("scope");
scopeEl.setTextContent(dep.getScope());
dependencyEl.appendChild(scopeEl);
}
if (dep.getExclusions() != null && !dep.getExclusions().isEmpty()) {
Element exclsEl = pom.createElement("exclusions");
for (Exclusion e : dep.getExclusions()) {
Element exclEl = pom.createElement("exclusion");
Element groupIdExEl = pom.createElement("groupId");
groupIdExEl.setTextContent(e.getGroupId());
exclEl.appendChild(groupIdExEl);
Element artifactIdExEl = pom.createElement("artifactId");
artifactIdExEl.setTextContent(e.getArtifactId());
exclEl.appendChild(artifactIdExEl);
exclsEl.appendChild(exclEl);
}
dependencyEl.appendChild(exclsEl);
}
dependenciesSection.appendChild(dependencyEl);
}
}
use of javax.xml.xpath.XPathExpression in project camel by apache.
the class BomGeneratorMojo method loadBasePom.
private Document loadBasePom() throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document pom = builder.parse(sourcePom);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/project/parent/version");
Node node = (Node) expr.evaluate(pom, XPathConstants.NODE);
if (node != null && node.getTextContent() != null && node.getTextContent().trim().equals("${project.version}")) {
node.setTextContent(project.getVersion());
}
return pom;
}
use of javax.xml.xpath.XPathExpression in project camel by apache.
the class SpringBootStarterMojo method writeXmlFormatted.
private void writeXmlFormatted(Document pom, File destination) throws Exception {
XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//text()[normalize-space(.) = '']");
NodeList emptyNodes = (NodeList) xpath.evaluate(pom, XPathConstants.NODESET);
// Remove empty text nodes
for (int i = 0; i < emptyNodes.getLength(); i++) {
Node emptyNode = emptyNodes.item(i);
emptyNode.getParentNode().removeChild(emptyNode);
}
pom.setXmlStandalone(true);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(pom);
String content;
try (StringWriter out = new StringWriter()) {
StreamResult result = new StreamResult(out);
transformer.transform(source, result);
content = out.toString();
}
// Fix header formatting problem
content = content.replaceFirst("-->", "-->\n").replaceFirst("\\?><!--", "\\?>\n<!--");
writeIfChanged(content, destination);
}
use of javax.xml.xpath.XPathExpression in project camel by apache.
the class XmlSignerProcessor method getContentReferenceUrisForDetachedCase.
private List<String> getContentReferenceUrisForDetachedCase(Message message, Node messageBodyNode) throws XmlSignatureException, XPathExpressionException {
List<XPathFilterParameterSpec> xpathsToIdAttributes = getXpathToIdAttributes(message);
if (xpathsToIdAttributes.isEmpty()) {
// should not happen, has already been checked earlier
throw new IllegalStateException("List of XPATHs to ID attributes is empty in detached signature case");
}
List<ComparableNode> result = new ArrayList<ComparableNode>(xpathsToIdAttributes.size());
for (XPathFilterParameterSpec xp : xpathsToIdAttributes) {
XPathExpression exp;
try {
exp = XmlSignatureHelper.getXPathExpression(xp);
} catch (XPathExpressionException e) {
throw new XmlSignatureException("The configured xpath expression " + xp.getXPath() + " is invalid.", e);
}
NodeList list = (NodeList) exp.evaluate(messageBodyNode, XPathConstants.NODESET);
if (list == null) {
//assume optional element, XSD validation has been done before
LOG.warn("No ID attribute found for xpath expression {}. Therfore this xpath expression will be ignored.", xp.getXPath());
continue;
}
int length = list.getLength();
for (int i = 0; i < length; i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
Attr attr = (Attr) node;
String value = attr.getValue();
// check that attribute is ID attribute
Element element = messageBodyNode.getOwnerDocument().getElementById(value);
if (element == null) {
throw new XmlSignatureException("Wrong configured xpath expression for ID attributes: The evaluation of the xpath expression " + xp.getXPath() + " resulted in an attribute which is not of type ID. The attribute value is " + value + ".");
}
result.add(new ComparableNode(element, "#" + value));
LOG.debug("ID attribute with value {} found for xpath {}", value, xp.getXPath());
} else {
throw new XmlSignatureException("Wrong configured xpath expression for ID attributes: The evaluation of the xpath expression " + xp.getXPath() + " returned a node which was not of type Attribute.");
}
}
}
if (result.size() == 0) {
throw new XmlSignatureException("No element to sign found in the detached case. No node found for the configured xpath expressions " + toString(xpathsToIdAttributes) + ". Either the configuration of the XML signature component is wrong or the incoming message has not the correct structure.");
}
// sort so that elements with deeper hierarchy level are treated first
Collections.sort(result);
return ComparableNode.getReferenceUris(result);
}
Aggregations