use of org.apache.xml.security.transforms.params.XPath2FilterContainer in project xades4j by luisgoncalves.
the class XPath2FilterTransformParamsMarshaller method marshalParameters.
@Override
public List<Node> marshalParameters(XPath2FilterTransform alg, Document doc) {
List<XPath2Filter> filters = alg.getFilters();
List<Node> params = new ArrayList<Node>(filters.size());
Set<Map.Entry<String, String>> namespaces = alg.getNamespaces().entrySet();
for (XPath2Filter filter : filters) {
XPath2FilterContainer c = null;
String filterType = filter.getFilterType();
if (XPath2FilterContainer.INTERSECT.equals(filterType)) {
c = XPath2FilterContainer.newInstanceIntersect(doc, filter.getXPath());
} else if (XPath2FilterContainer.SUBTRACT.equals(filterType)) {
c = XPath2FilterContainer.newInstanceSubtract(doc, filter.getXPath());
} else if (XPath2FilterContainer.UNION.equals(filterType)) {
c = XPath2FilterContainer.newInstanceUnion(doc, filter.getXPath());
} else {
throw new IllegalArgumentException(filterType);
}
// different filters.
for (Map.Entry<String, String> ns : namespaces) {
try {
c.setXPathNamespaceContext(ns.getKey(), ns.getValue());
} catch (XMLSecurityException ex) {
throw new IllegalArgumentException("Invalid namespaces for XPath query", ex);
}
}
params.add(c.getElement());
}
return params;
}
use of org.apache.xml.security.transforms.params.XPath2FilterContainer in project santuario-java by apache.
the class CreateSignatureTest method testXFilter2Signature.
@org.junit.Test
public void testXFilter2Signature() throws Exception {
Document doc = db.newDocument();
doc.appendChild(doc.createComment(" Comment before "));
Element root = doc.createElementNS("", "RootElement");
doc.appendChild(root);
root.appendChild(doc.createTextNode("Some simple text\n"));
// Sign
XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_SIGNATURE_DSA);
root.appendChild(sig.getElement());
Transforms transforms = new Transforms(doc);
String filter = "here()/ancestor::ds.Signature/parent::node()/descendant-or-self::*";
XPath2FilterContainer xpathC = XPath2FilterContainer.newInstanceIntersect(doc, filter);
xpathC.setXPathNamespaceContext("dsig-xpath", Transforms.TRANSFORM_XPATH2FILTER);
Element node = xpathC.getElement();
transforms.addTransform(Transforms.TRANSFORM_XPATH2FILTER, node);
sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream fis = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
fis = new FileInputStream(BASEDIR + SEP + "src/test/resources/org/apache/xml/security/samples/input/keystore.jks");
} else {
fis = new FileInputStream("src/test/resources/org/apache/xml/security/samples/input/keystore.jks");
}
ks.load(fis, "xmlsecurity".toCharArray());
PrivateKey privateKey = (PrivateKey) ks.getKey("test", "xmlsecurity".toCharArray());
sig.sign(privateKey);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLUtils.outputDOMc14nWithComments(doc, bos);
String signedDoc = new String(bos.toByteArray());
// Now Verify
try (InputStream is = new ByteArrayInputStream(signedDoc.getBytes())) {
doc = db.parse(is);
}
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:Signature[1]";
Element sigElement = (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
XMLSignature signature = new XMLSignature(sigElement, "");
assertTrue(signature.checkSignatureValue(ks.getCertificate("test").getPublicKey()));
}
use of org.apache.xml.security.transforms.params.XPath2FilterContainer in project santuario-java by apache.
the class XPath2NodeFilter method enginePerformTransform.
/**
* Method enginePerformTransform
* {@inheritDoc}
* @param input
*
* @throws TransformationException
*/
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws TransformationException {
try {
List<NodeList> unionNodes = new ArrayList<>();
List<NodeList> subtractNodes = new ArrayList<>();
List<NodeList> intersectNodes = new ArrayList<>();
Element[] xpathElements = XMLUtils.selectNodes(transformObject.getElement().getFirstChild(), XPath2FilterContainer.XPathFilter2NS, XPath2FilterContainer._TAG_XPATH2);
if (xpathElements.length == 0) {
Object[] exArgs = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" };
throw new TransformationException("xml.WrongContent", exArgs);
}
Document inputDoc = null;
if (input.getSubNode() != null) {
inputDoc = XMLUtils.getOwnerDocument(input.getSubNode());
} else {
inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
}
for (int i = 0; i < xpathElements.length; i++) {
Element xpathElement = xpathElements[i];
XPath2FilterContainer xpathContainer = XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI());
String str = XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode());
XPathFactory xpathFactory = XPathFactory.newInstance();
XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI();
NodeList subtreeRoots = xpathAPIInstance.selectNodeList(inputDoc, xpathContainer.getXPathFilterTextNode(), str, xpathContainer.getElement());
if (xpathContainer.isIntersect()) {
intersectNodes.add(subtreeRoots);
} else if (xpathContainer.isSubtract()) {
subtractNodes.add(subtreeRoots);
} else if (xpathContainer.isUnion()) {
unionNodes.add(subtreeRoots);
}
}
input.addNodeFilter(new XPath2NodeFilter(unionNodes, subtractNodes, intersectNodes));
input.setNodeSet(true);
return input;
} catch (TransformerException ex) {
throw new TransformationException(ex);
} catch (DOMException ex) {
throw new TransformationException(ex);
} catch (CanonicalizationException ex) {
throw new TransformationException(ex);
} catch (InvalidCanonicalizerException ex) {
throw new TransformationException(ex);
} catch (XMLSecurityException ex) {
throw new TransformationException(ex);
} catch (SAXException ex) {
throw new TransformationException(ex);
} catch (IOException ex) {
throw new TransformationException(ex);
} catch (ParserConfigurationException ex) {
throw new TransformationException(ex);
}
}
Aggregations