Search in sources :

Example 1 with URIReferenceException

use of javax.xml.crypto.URIReferenceException in project poi by apache.

the class OOXMLURIDereferencer method dereference.

public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
    if (baseUriDereferencer == null) {
        baseUriDereferencer = signatureConfig.getSignatureFactory().getURIDereferencer();
    }
    if (null == uriReference) {
        throw new NullPointerException("URIReference cannot be null");
    }
    if (null == context) {
        throw new NullPointerException("XMLCrytoContext cannot be null");
    }
    URI uri;
    try {
        uri = new URI(uriReference.getURI());
    } catch (URISyntaxException e) {
        throw new URIReferenceException("could not URL decode the uri: " + uriReference.getURI(), e);
    }
    PackagePart part = findPart(uri);
    if (part == null) {
        LOG.log(POILogger.DEBUG, "cannot resolve, delegating to base DOM URI dereferencer", uri);
        return this.baseUriDereferencer.dereference(uriReference, context);
    }
    InputStream dataStream;
    try {
        dataStream = part.getInputStream();
        // workaround for office 2007 pretty-printed .rels files
        if (part.getPartName().toString().endsWith(".rels")) {
            // although xmlsec has an option to ignore line breaks, currently this
            // only affects .rels files, so we only modify these
            // http://stackoverflow.com/questions/4728300
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            for (int ch; (ch = dataStream.read()) != -1; ) {
                if (ch == 10 || ch == 13)
                    continue;
                bos.write(ch);
            }
            dataStream = new ByteArrayInputStream(bos.toByteArray());
        }
    } catch (IOException e) {
        throw new URIReferenceException("I/O error: " + e.getMessage(), e);
    }
    return new OctetStreamData(dataStream, uri.toString(), null);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) URIReferenceException(javax.xml.crypto.URIReferenceException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) URI(java.net.URI) OctetStreamData(javax.xml.crypto.OctetStreamData)

Example 2 with URIReferenceException

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

the class DOMRetrievalMethod method dereferenceAsXMLStructure.

public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context) throws URIReferenceException {
    DocumentBuilder db = null;
    boolean secVal = Utils.secureValidation(context);
    ApacheData data = (ApacheData) dereference(context);
    try (InputStream is = new ByteArrayInputStream(data.getXMLSignatureInput().getBytes())) {
        db = XMLUtils.createDocumentBuilder(false, secVal);
        Document doc = db.parse(is);
        Element kiElem = doc.getDocumentElement();
        if (kiElem.getLocalName().equals("X509Data") && XMLSignature.XMLNS.equals(kiElem.getNamespaceURI())) {
            return new DOMX509Data(kiElem);
        } else {
            // unsupported
            return null;
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    } finally {
        if (db != null) {
            XMLUtils.repoolDocumentBuilder(db);
        }
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) URIReferenceException(javax.xml.crypto.URIReferenceException) Document(org.w3c.dom.Document) MarshalException(javax.xml.crypto.MarshalException) URISyntaxException(java.net.URISyntaxException) URIReferenceException(javax.xml.crypto.URIReferenceException)

Example 3 with URIReferenceException

use of javax.xml.crypto.URIReferenceException 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

URISyntaxException (java.net.URISyntaxException)3 URIReferenceException (javax.xml.crypto.URIReferenceException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 MarshalException (javax.xml.crypto.MarshalException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Data (javax.xml.crypto.Data)1 NodeSetData (javax.xml.crypto.NodeSetData)1 OctetStreamData (javax.xml.crypto.OctetStreamData)1 URIDereferencer (javax.xml.crypto.URIDereferencer)1 Transform (javax.xml.crypto.dsig.Transform)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1