Search in sources :

Example 1 with TransformException

use of javax.xml.crypto.dsig.TransformException in project poi by apache.

the class RelationshipTransformService method transform.

public Data transform(Data data, XMLCryptoContext context) throws TransformException {
    LOG.log(POILogger.DEBUG, "transform(data,context)");
    LOG.log(POILogger.DEBUG, "data java type: " + data.getClass().getName());
    OctetStreamData octetStreamData = (OctetStreamData) data;
    LOG.log(POILogger.DEBUG, "URI: " + octetStreamData.getURI());
    InputStream octetStream = octetStreamData.getOctetStream();
    RelationshipsDocument relDoc;
    try {
        relDoc = RelationshipsDocument.Factory.parse(octetStream, DEFAULT_XML_OPTIONS);
    } catch (Exception e) {
        throw new TransformException(e.getMessage(), e);
    }
    LOG.log(POILogger.DEBUG, "relationships document", relDoc);
    CTRelationships rels = relDoc.getRelationships();
    List<CTRelationship> relList = rels.getRelationshipList();
    Iterator<CTRelationship> relIter = rels.getRelationshipList().iterator();
    while (relIter.hasNext()) {
        CTRelationship rel = relIter.next();
        /*
             * See: ISO/IEC 29500-2:2008(E) - 13.2.4.24 Relationships Transform
             * Algorithm.
             */
        if (!this.sourceIds.contains(rel.getId())) {
            LOG.log(POILogger.DEBUG, "removing element: " + rel.getId());
            relIter.remove();
        } else {
            if (!rel.isSetTargetMode()) {
                rel.setTargetMode(STTargetMode.INTERNAL);
            }
        }
    }
    // TODO: remove non element nodes ???
    LOG.log(POILogger.DEBUG, "# Relationship elements", relList.size());
    XmlSort.sort(rels, new Comparator<XmlCursor>() {

        public int compare(XmlCursor c1, XmlCursor c2) {
            String id1 = ((CTRelationship) c1.getObject()).getId();
            String id2 = ((CTRelationship) c2.getObject()).getId();
            return id1.compareTo(id2);
        }
    });
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XmlOptions xo = new XmlOptions();
        xo.setSaveNoXmlDecl();
        relDoc.save(bos, xo);
        return new OctetStreamData(new ByteArrayInputStream(bos.toByteArray()));
    } catch (IOException e) {
        throw new TransformException(e.getMessage(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XmlOptions(org.apache.xmlbeans.XmlOptions) TransformException(javax.xml.crypto.dsig.TransformException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MarshalException(javax.xml.crypto.MarshalException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TransformException(javax.xml.crypto.dsig.TransformException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) XmlCursor(org.apache.xmlbeans.XmlCursor) CTRelationship(org.openxmlformats.schemas.xpackage.x2006.relationships.CTRelationship) RelationshipsDocument(org.openxmlformats.schemas.xpackage.x2006.relationships.RelationshipsDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) CTRelationships(org.openxmlformats.schemas.xpackage.x2006.relationships.CTRelationships) OctetStreamData(javax.xml.crypto.OctetStreamData)

Example 2 with TransformException

use of javax.xml.crypto.dsig.TransformException in project jdk8u_jdk by JetBrains.

the class ApacheCanonicalizer method transform.

public Data transform(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
    if (data == null) {
        throw new NullPointerException("data must not be null");
    }
    if (os == null) {
        throw new NullPointerException("output stream must not be null");
    }
    if (ownerDoc == null) {
        throw new TransformException("transform must be marshalled");
    }
    if (apacheTransform == null) {
        try {
            apacheTransform = new Transform(ownerDoc, getAlgorithm(), transformElem.getChildNodes());
            apacheTransform.setElement(transformElem, xc.getBaseURI());
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "Created transform for algorithm: " + getAlgorithm());
            }
        } catch (Exception ex) {
            throw new TransformException("Couldn't find Transform for: " + getAlgorithm(), ex);
        }
    }
    XMLSignatureInput in;
    if (data instanceof ApacheData) {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "ApacheData = true");
        }
        in = ((ApacheData) data).getXMLSignatureInput();
    } else if (data instanceof NodeSetData) {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "isNodeSet() = true");
        }
        if (data instanceof DOMSubTreeData) {
            DOMSubTreeData subTree = (DOMSubTreeData) data;
            in = new XMLSignatureInput(subTree.getRoot());
            in.setExcludeComments(subTree.excludeComments());
        } else {
            @SuppressWarnings("unchecked") Set<Node> nodeSet = Utils.toNodeSet(((NodeSetData) data).iterator());
            in = new XMLSignatureInput(nodeSet);
        }
    } else {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "isNodeSet() = false");
        }
        try {
            in = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } catch (Exception ex) {
            throw new TransformException(ex);
        }
    }
    try {
        in = apacheTransform.performTransform(in, os);
        if (!in.isNodeSet() && !in.isElement()) {
            return null;
        }
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception ex) {
        throw new TransformException(ex);
    }
}
Also used : Set(java.util.Set) TransformException(javax.xml.crypto.dsig.TransformException) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) Transform(com.sun.org.apache.xml.internal.security.transforms.Transform) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TransformException(javax.xml.crypto.dsig.TransformException)

Example 3 with TransformException

use of javax.xml.crypto.dsig.TransformException in project jdk8u_jdk by JetBrains.

the class ApacheCanonicalizer method canonicalize.

public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
    if (apacheCanonicalizer == null) {
        try {
            apacheCanonicalizer = Canonicalizer.getInstance(getAlgorithm());
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "Created canonicalizer for algorithm: " + getAlgorithm());
            }
        } catch (InvalidCanonicalizerException ice) {
            throw new TransformException("Couldn't find Canonicalizer for: " + getAlgorithm() + ": " + ice.getMessage(), ice);
        }
    }
    if (os != null) {
        apacheCanonicalizer.setWriter(os);
    } else {
        apacheCanonicalizer.setWriter(new ByteArrayOutputStream());
    }
    try {
        Set<Node> nodeSet = null;
        if (data instanceof ApacheData) {
            XMLSignatureInput in = ((ApacheData) data).getXMLSignatureInput();
            if (in.isElement()) {
                if (inclusiveNamespaces != null) {
                    return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(in.getSubNode(), inclusiveNamespaces)));
                } else {
                    return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(in.getSubNode())));
                }
            } else if (in.isNodeSet()) {
                nodeSet = in.getNodeSet();
            } else {
                return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalize(Utils.readBytesFromStream(in.getOctetStream()))));
            }
        } else if (data instanceof DOMSubTreeData) {
            DOMSubTreeData subTree = (DOMSubTreeData) data;
            if (inclusiveNamespaces != null) {
                return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(subTree.getRoot(), inclusiveNamespaces)));
            } else {
                return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeSubtree(subTree.getRoot())));
            }
        } else if (data instanceof NodeSetData) {
            NodeSetData nsd = (NodeSetData) data;
            // convert Iterator to Set
            @SuppressWarnings("unchecked") Set<Node> ns = Utils.toNodeSet(nsd.iterator());
            nodeSet = ns;
            if (log.isLoggable(java.util.logging.Level.FINE)) {
                log.log(java.util.logging.Level.FINE, "Canonicalizing " + nodeSet.size() + " nodes");
            }
        } else {
            return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalize(Utils.readBytesFromStream(((OctetStreamData) data).getOctetStream()))));
        }
        if (inclusiveNamespaces != null) {
            return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeXPathNodeSet(nodeSet, inclusiveNamespaces)));
        } else {
            return new OctetStreamData(new ByteArrayInputStream(apacheCanonicalizer.canonicalizeXPathNodeSet(nodeSet)));
        }
    } catch (Exception e) {
        throw new TransformException(e);
    }
}
Also used : Node(org.w3c.dom.Node) TransformException(javax.xml.crypto.dsig.TransformException) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TransformException(javax.xml.crypto.dsig.TransformException) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidCanonicalizerException(com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)

Aggregations

InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 TransformException (javax.xml.crypto.dsig.TransformException)3 InvalidCanonicalizerException (com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException)2 XMLSignatureInput (com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Transform (com.sun.org.apache.xml.internal.security.transforms.Transform)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Set (java.util.Set)1 MarshalException (javax.xml.crypto.MarshalException)1 OctetStreamData (javax.xml.crypto.OctetStreamData)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1 XmlOptions (org.apache.xmlbeans.XmlOptions)1 CTRelationship (org.openxmlformats.schemas.xpackage.x2006.relationships.CTRelationship)1 CTRelationships (org.openxmlformats.schemas.xpackage.x2006.relationships.CTRelationships)1 RelationshipsDocument (org.openxmlformats.schemas.xpackage.x2006.relationships.RelationshipsDocument)1 Node (org.w3c.dom.Node)1