Search in sources :

Example 6 with Transform

use of org.apache.xml.security.transforms.Transform in project santuario-java by apache.

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());
            boolean secVal = Utils.secureValidation(xc);
            apacheTransform.setSecureValidation(secVal);
            LOG.debug("Created transform for algorithm: {}", getAlgorithm());
        } catch (Exception ex) {
            throw new TransformException("Couldn't find Transform for: " + getAlgorithm(), ex);
        }
    }
    XMLSignatureInput in;
    if (data instanceof ApacheData) {
        LOG.debug("ApacheData = true");
        in = ((ApacheData) data).getXMLSignatureInput();
    } else if (data instanceof NodeSetData) {
        LOG.debug("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 {
        LOG.debug("isNodeSet() = false");
        try {
            in = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } catch (Exception ex) {
            throw new TransformException(ex);
        }
    }
    boolean secVal = Utils.secureValidation(xc);
    in.setSecureValidation(secVal);
    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(org.apache.xml.security.signature.XMLSignatureInput) Transform(org.apache.xml.security.transforms.Transform) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) TransformException(javax.xml.crypto.dsig.TransformException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException)

Example 7 with Transform

use of org.apache.xml.security.transforms.Transform in project santuario-java by apache.

the class Reference method getNodesetBeforeFirstCanonicalization.

/**
 * This method returns the XMLSignatureInput which represents the node set before
 * some kind of canonicalization is applied for the first time.
 * @return Gets a the node doing everything till the first c14n is needed
 *
 * @throws XMLSignatureException
 */
public XMLSignatureInput getNodesetBeforeFirstCanonicalization() throws XMLSignatureException {
    try {
        XMLSignatureInput input = this.getContentsBeforeTransformation();
        cacheDereferencedElement(input);
        XMLSignatureInput output = input;
        Transforms transforms = this.getTransforms();
        if (transforms != null) {
            for (int i = 0; i < transforms.getLength(); i++) {
                Transform t = transforms.item(i);
                String uri = t.getURI();
                if (uri.equals(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N_WITH_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N11_OMIT_COMMENTS) || uri.equals(Transforms.TRANSFORM_C14N11_WITH_COMMENTS)) {
                    break;
                }
                output = t.performTransform(output, null);
            }
            output.setSourceURI(input.getSourceURI());
        }
        return output;
    } catch (IOException ex) {
        throw new XMLSignatureException(ex);
    } catch (ResourceResolverException ex) {
        throw new XMLSignatureException(ex);
    } catch (CanonicalizationException ex) {
        throw new XMLSignatureException(ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new XMLSignatureException(ex);
    } catch (TransformationException ex) {
        throw new XMLSignatureException(ex);
    } catch (XMLSecurityException ex) {
        throw new XMLSignatureException(ex);
    }
}
Also used : TransformationException(org.apache.xml.security.transforms.TransformationException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException) Transforms(org.apache.xml.security.transforms.Transforms) IOException(java.io.IOException) ResourceResolverException(org.apache.xml.security.utils.resolver.ResourceResolverException) Transform(org.apache.xml.security.transforms.Transform) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Aggregations

Transform (org.apache.xml.security.transforms.Transform)7 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)3 XMLSignatureInput (org.apache.xml.security.signature.XMLSignatureInput)3 Transforms (org.apache.xml.security.transforms.Transforms)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 Set (java.util.Set)2 InvalidCanonicalizerException (org.apache.xml.security.c14n.InvalidCanonicalizerException)2 TransformationException (org.apache.xml.security.transforms.TransformationException)2 Element (org.w3c.dom.Element)2 File (java.io.File)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 TransformException (javax.xml.crypto.dsig.TransformException)1 XPath (javax.xml.xpath.XPath)1 XPathFactory (javax.xml.xpath.XPathFactory)1 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)1 XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)1 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)1 InvalidTransformException (org.apache.xml.security.transforms.InvalidTransformException)1