Search in sources :

Example 36 with DSNamespaceContext

use of org.apache.xml.security.test.dom.DSNamespaceContext in project santuario-java by apache.

the class Santuario273Test method testC14n11Base.

@org.junit.Test
public void testC14n11Base() throws Exception {
    DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(true);
    documentBuilder.setErrorHandler(new org.apache.xml.security.utils.IgnoreAllErrorHandler());
    Document doc = null;
    try (InputStream is = new ByteArrayInputStream(input.getBytes())) {
        doc = documentBuilder.parse(is);
    }
    Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    xPath.setNamespaceContext(new DSNamespaceContext());
    Node signedInfo = (Node) xPath.evaluate("//ds:SignedInfo[1]", doc, XPathConstants.NODE);
    byte[] output = c14n.canonicalizeSubtree(signedInfo);
    assertEquals(new String(output, java.nio.charset.StandardCharsets.UTF_8), expectedResult);
}
Also used : XPath(javax.xml.xpath.XPath) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) Canonicalizer(org.apache.xml.security.c14n.Canonicalizer)

Example 37 with DSNamespaceContext

use of org.apache.xml.security.test.dom.DSNamespaceContext in project santuario-java by apache.

the class BaltimoreEncTest method retrieveCCNumber.

/**
 * Method retrieveCCNumber
 *
 * Retrieve the credit card number from the payment info document
 *
 * @param doc The document to retrieve the card number from
 * @return The retrieved credit card number
 * @throws XPathExpressionException
 */
private static String retrieveCCNumber(Document doc) throws javax.xml.transform.TransformerException, XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    Map<String, String> namespace = new HashMap<>();
    namespace.put("x", "urn:example:po");
    DSNamespaceContext context = new DSNamespaceContext(namespace);
    xpath.setNamespaceContext(context);
    String expression = "//x:Number/text()";
    Node ccnumElt = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
    if (ccnumElt != null) {
        return ccnumElt.getNodeValue();
    }
    return null;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) HashMap(java.util.HashMap) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext) Node(org.w3c.dom.Node)

Aggregations

XPath (javax.xml.xpath.XPath)37 XPathFactory (javax.xml.xpath.XPathFactory)37 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)37 Element (org.w3c.dom.Element)23 XMLSignature (org.apache.xml.security.signature.XMLSignature)18 Document (org.w3c.dom.Document)18 NodeList (org.w3c.dom.NodeList)14 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 DocumentBuilder (javax.xml.parsers.DocumentBuilder)11 KeyInfo (org.apache.xml.security.keys.KeyInfo)8 Node (org.w3c.dom.Node)8 File (java.io.File)7 X509Certificate (java.security.cert.X509Certificate)7 Transforms (org.apache.xml.security.transforms.Transforms)7 PublicKey (java.security.PublicKey)6 HashMap (java.util.HashMap)6 XMLCipher (org.apache.xml.security.encryption.XMLCipher)6 FileInputStream (java.io.FileInputStream)5 KeyStore (java.security.KeyStore)5