Example usage for javax.xml.crypto.dsig TransformException TransformException

List of usage examples for javax.xml.crypto.dsig TransformException TransformException

Introduction

In this page you can find the example usage for javax.xml.crypto.dsig TransformException TransformException.

Prototype

public TransformException(String message, Throwable cause) 

Source Link

Document

Constructs a new TransformException with the specified detail message and cause.

Usage

From source file:be.fedict.eid.applet.service.signer.ooxml.RelationshipTransformService.java

public Data transform(Data data, XMLCryptoContext context) throws TransformException {
    LOG.debug("transform(data,context)");
    LOG.debug("data java type: " + data.getClass().getName());
    OctetStreamData octetStreamData = (OctetStreamData) data;
    LOG.debug("URI: " + octetStreamData.getURI());
    InputStream octetStream = octetStreamData.getOctetStream();
    Document relationshipsDocument;
    try {//from   ww w. j av a  2s . c  o  m
        relationshipsDocument = loadDocument(octetStream);
    } catch (Exception e) {
        throw new TransformException(e.getMessage(), e);
    }
    try {
        LOG.debug("relationships document: " + toString(relationshipsDocument));
    } catch (TransformerException e) {
        throw new TransformException(e.getMessage(), e);
    }
    Element nsElement = relationshipsDocument.createElement("ns");
    nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns",
            "http://schemas.openxmlformats.org/package/2006/relationships");
    Element relationshipsElement = relationshipsDocument.getDocumentElement();
    NodeList childNodes = relationshipsElement.getChildNodes();
    for (int nodeIdx = 0; nodeIdx < childNodes.getLength(); nodeIdx++) {
        Node childNode = childNodes.item(nodeIdx);
        if (Node.ELEMENT_NODE != childNode.getNodeType()) {
            LOG.debug("removing node");
            relationshipsElement.removeChild(childNode);
            nodeIdx--;
            continue;
        }
        Element childElement = (Element) childNode;
        String idAttribute = childElement.getAttribute("Id");
        String typeAttribute = childElement.getAttribute("Type");
        LOG.debug("Relationship id attribute: " + idAttribute);
        LOG.debug("Relationship type attribute: " + typeAttribute);
        if (false == this.sourceIds.contains(idAttribute)
                && false == this.sourceTypes.contains(typeAttribute)) {
            LOG.debug("removing Relationship element: " + idAttribute);
            relationshipsElement.removeChild(childNode);
            nodeIdx--;
        }
        /*
         * See: ISO/IEC 29500-2:2008(E) - 13.2.4.24 Relationships Transform
         * Algorithm.
         */
        if (null == childElement.getAttributeNode("TargetMode")) {
            childElement.setAttribute("TargetMode", "Internal");
        }
    }
    LOG.debug("# Relationship elements: " + relationshipsElement.getElementsByTagName("*").getLength());
    sortRelationshipElements(relationshipsElement);
    try {
        return toOctetStreamData(relationshipsDocument);
    } catch (TransformerException e) {
        throw new TransformException(e.getMessage(), e);
    }
}

From source file:org.apache.jcp.xml.dsig.internal.dom.ApacheCanonicalizer.java

public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
    if (apacheCanonicalizer == null) {
        try {/*from  ww  w  .  ja  va2s  . c om*/
            apacheCanonicalizer = Canonicalizer.getInstance(getAlgorithm());
            if (log.isDebugEnabled()) {
                log.debug("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.isDebugEnabled()) {
                log.debug("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);
    }
}

From source file:org.jcp.xml.dsig.internal.dom.ApacheCanonicalizer.java

public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException {
    if (apacheCanonicalizer == null) {
        try {//  w  w  w .j  a v  a2 s  .  c o  m
            apacheCanonicalizer = Canonicalizer.getInstance(getAlgorithm());
            if (log.isDebugEnabled()) {
                log.debug("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
            nodeSet = Utils.toNodeSet(nsd.iterator());
            if (log.isDebugEnabled()) {
                log.debug("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);
    }
}