Search in sources :

Example 1 with NodeSetData

use of javax.xml.crypto.NodeSetData in project syndesis by syndesisio.

the class BaseSwaggerGeneratorExampleTest method c14Xml.

private static String c14Xml(final String xml) {
    if (xml == null) {
        return null;
    }
    try {
        final DocumentBuilder documentBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
        final TransformService transformation = TransformService.getInstance(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, "DOM");
        transformation.init(null);
        final NodeList allElements = document.getElementsByTagName("*");
        final List<Node> elements = new ArrayList<>();
        for (int i = 0; i < allElements.getLength(); i++) {
            elements.add(allElements.item(i));
        }
        final OctetStreamData data = (OctetStreamData) transformation.transform((NodeSetData) elements::iterator, null);
        try (final InputStream stream = data.getOctetStream()) {
            final byte[] buffy = new byte[stream.available()];
            stream.read(buffy);
            return new String(buffy, StandardCharsets.UTF_8);
        }
    } catch (GeneralSecurityException | TransformException | SAXException | IOException | ParserConfigurationException e) {
        throw new AssertionError(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) TransformException(javax.xml.crypto.dsig.TransformException) TransformService(javax.xml.crypto.dsig.TransformService) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) NodeSetData(javax.xml.crypto.NodeSetData) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OctetStreamData(javax.xml.crypto.OctetStreamData)

Example 2 with NodeSetData

use of javax.xml.crypto.NodeSetData in project santuario-java by apache.

the class DOMRetrievalMethod method dereference.

@Override
public Data dereference(XMLCryptoContext context) throws URIReferenceException {
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }
    /*
         * If URIDereferencer is specified in context; use it, otherwise use
         * built-in.
         */
    URIDereferencer deref = context.getURIDereferencer();
    if (deref == null) {
        deref = DOMURIDereferencer.INSTANCE;
    }
    Data data = deref.dereference(this, context);
    // pass dereferenced data through Transforms
    try {
        for (Transform transform : transforms) {
            data = transform.transform(data, context);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
    // guard against RetrievalMethod loops
    if (data instanceof NodeSetData && Utils.secureValidation(context)) {
        NodeSetData nsd = (NodeSetData) data;
        Iterator<?> i = nsd.iterator();
        if (i.hasNext()) {
            Node root = (Node) i.next();
            if ("RetrievalMethod".equals(root.getLocalName())) {
                throw new URIReferenceException("It is forbidden to have one RetrievalMethod point " + "to another when secure validation is enabled");
            }
        }
    }
    return data;
}
Also used : NodeSetData(javax.xml.crypto.NodeSetData) Node(org.w3c.dom.Node) Data(javax.xml.crypto.Data) NodeSetData(javax.xml.crypto.NodeSetData) URIReferenceException(javax.xml.crypto.URIReferenceException) URIDereferencer(javax.xml.crypto.URIDereferencer) Transform(javax.xml.crypto.dsig.Transform) MarshalException(javax.xml.crypto.MarshalException) URISyntaxException(java.net.URISyntaxException) URIReferenceException(javax.xml.crypto.URIReferenceException)

Aggregations

NodeSetData (javax.xml.crypto.NodeSetData)2 Node (org.w3c.dom.Node)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 Data (javax.xml.crypto.Data)1 MarshalException (javax.xml.crypto.MarshalException)1 OctetStreamData (javax.xml.crypto.OctetStreamData)1 URIDereferencer (javax.xml.crypto.URIDereferencer)1 URIReferenceException (javax.xml.crypto.URIReferenceException)1 Transform (javax.xml.crypto.dsig.Transform)1 TransformException (javax.xml.crypto.dsig.TransformException)1 TransformService (javax.xml.crypto.dsig.TransformService)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1