Search in sources :

Example 1 with UnsyncByteArrayOutputStream

use of org.apache.xml.security.utils.UnsyncByteArrayOutputStream in project santuario-java by apache.

the class CanonicalizerBase method engineCanonicalizeSubTree.

/**
 * Canonicalizes a Subtree node.
 *
 * @param rootNode
 *            the root of the subtree to canonicalize
 * @param excludeNode
 *            a node to be excluded from the canonicalize operation
 * @return The canonicalize stream.
 * @throws CanonicalizationException
 */
protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode) throws CanonicalizationException {
    this.excludeNode = excludeNode;
    try {
        NameSpaceSymbTable ns = new NameSpaceSymbTable();
        int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
        if (rootNode != null && Node.ELEMENT_NODE == rootNode.getNodeType()) {
            // Fills the nssymbtable with the definitions of the parent of the root subnode
            getParentNameSpaces((Element) rootNode, ns);
            nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
        }
        this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel);
        this.writer.flush();
        if (this.writer instanceof ByteArrayOutputStream) {
            byte[] result = ((ByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((ByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return result;
        } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
            byte[] result = ((UnsyncByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((UnsyncByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return result;
        } else {
            this.writer.close();
        }
        return null;
    } catch (UnsupportedEncodingException ex) {
        throw new CanonicalizationException(ex);
    } catch (IOException ex) {
        throw new CanonicalizationException(ex);
    }
}
Also used : UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with UnsyncByteArrayOutputStream

use of org.apache.xml.security.utils.UnsyncByteArrayOutputStream in project santuario-java by apache.

the class TransformBase64Decode method transform.

@Override
public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
    int eventType = xmlSecEvent.getEventType();
    switch(eventType) {
        case XMLStreamConstants.CHARACTERS:
            if (getOutputStream() != null) {
                // encoding shouldn't matter here, because the data is Base64 encoded and is therefore in the ASCII range.
                try {
                    getOutputStream().write(xmlSecEvent.asCharacters().getData().getBytes());
                } catch (IOException e) {
                    throw new XMLStreamException(e);
                }
            } else {
                // we have a child transformer
                if (childOutputMethod == null) {
                    final XMLSecurityConstants.TransformMethod preferredChildTransformMethod = getTransformer().getPreferredTransformMethod(XMLSecurityConstants.TransformMethod.XMLSecEvent);
                    switch(preferredChildTransformMethod) {
                        case XMLSecEvent:
                            {
                                childOutputMethod = new ChildOutputMethod() {

                                    private UnsyncByteArrayOutputStream byteArrayOutputStream;

                                    private Base64OutputStream base64OutputStream;

                                    @Override
                                    public void transform(Object object) throws XMLStreamException {
                                        if (base64OutputStream == null) {
                                            byteArrayOutputStream = new UnsyncByteArrayOutputStream();
                                            base64OutputStream = new Base64OutputStream(byteArrayOutputStream, false);
                                        }
                                        try {
                                            base64OutputStream.write((byte[]) object);
                                        } catch (IOException e) {
                                            throw new XMLStreamException(e);
                                        }
                                    }

                                    @Override
                                    public void doFinal() throws XMLStreamException {
                                        try {
                                            base64OutputStream.close();
                                        } catch (IOException e) {
                                            throw new XMLStreamException(e);
                                        }
                                        try (InputStream is = new UnsyncByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
                                            XMLEventReaderInputProcessor xmlEventReaderInputProcessor = new XMLEventReaderInputProcessor(null, getXmlInputFactory().createXMLStreamReader(is));
                                            XMLSecEvent xmlSecEvent;
                                            do {
                                                xmlSecEvent = xmlEventReaderInputProcessor.processNextEvent(null);
                                                getTransformer().transform(xmlSecEvent);
                                            } while (xmlSecEvent.getEventType() != XMLStreamConstants.END_DOCUMENT);
                                        } catch (XMLSecurityException | IOException e) {
                                            throw new XMLStreamException(e);
                                        }
                                        getTransformer().doFinal();
                                    }
                                };
                                break;
                            }
                        case InputStream:
                            {
                                childOutputMethod = new ChildOutputMethod() {

                                    private UnsyncByteArrayOutputStream byteArrayOutputStream;

                                    private Base64OutputStream base64OutputStream;

                                    @Override
                                    public void transform(Object object) throws XMLStreamException {
                                        if (base64OutputStream == null) {
                                            byteArrayOutputStream = new UnsyncByteArrayOutputStream();
                                            base64OutputStream = new Base64OutputStream(byteArrayOutputStream, false);
                                        }
                                        try {
                                            base64OutputStream.write((byte[]) object);
                                        } catch (IOException e) {
                                            throw new XMLStreamException(e);
                                        }
                                    }

                                    @Override
                                    public void doFinal() throws XMLStreamException {
                                        try {
                                            base64OutputStream.close();
                                        } catch (IOException e) {
                                            throw new XMLStreamException(e);
                                        }
                                        try (InputStream is = new UnsyncByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
                                            getTransformer().transform(is);
                                            getTransformer().doFinal();
                                        } catch (IOException ex) {
                                            throw new XMLStreamException(ex);
                                        }
                                    }
                                };
                                break;
                            }
                    }
                }
                childOutputMethod.transform(xmlSecEvent.asCharacters().getData().getBytes());
            }
            break;
    }
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) XMLEventReaderInputProcessor(org.apache.xml.security.stax.impl.processor.input.XMLEventReaderInputProcessor) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) XMLStreamException(javax.xml.stream.XMLStreamException) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream)

Example 3 with UnsyncByteArrayOutputStream

use of org.apache.xml.security.utils.UnsyncByteArrayOutputStream in project santuario-java by apache.

the class CanonicalizerBase method setTransformer.

@Override
public void setTransformer(Transformer transformer) throws XMLSecurityException {
    // we support only transformers which takes an InputStream otherwise we will break the C14N
    setOutputStream(new UnsyncByteArrayOutputStream());
    super.setTransformer(transformer);
}
Also used : UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream)

Example 4 with UnsyncByteArrayOutputStream

use of org.apache.xml.security.utils.UnsyncByteArrayOutputStream in project santuario-java by apache.

the class CanonicalizerBase method doFinal.

@Override
public void doFinal() throws XMLStreamException {
    if (getTransformer() != null) {
        UnsyncByteArrayOutputStream baos = (UnsyncByteArrayOutputStream) getOutputStream();
        try (InputStream is = new UnsyncByteArrayInputStream(baos.toByteArray())) {
            getTransformer().transform(is);
            getTransformer().doFinal();
        } catch (IOException ex) {
            throw new XMLStreamException(ex);
        }
    }
}
Also used : UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream)

Example 5 with UnsyncByteArrayOutputStream

use of org.apache.xml.security.utils.UnsyncByteArrayOutputStream in project santuario-java by apache.

the class TransformIdentity method transform.

@Override
public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
    if (getXmlEventWriterForOutputStream() != null) {
        // we have an output stream
        getXmlEventWriterForOutputStream().add(xmlSecEvent);
    } else {
        // we have a child transformer
        if (childOutputMethod == null) {
            final XMLSecurityConstants.TransformMethod preferredChildTransformMethod = getTransformer().getPreferredTransformMethod(XMLSecurityConstants.TransformMethod.XMLSecEvent);
            switch(preferredChildTransformMethod) {
                case XMLSecEvent:
                    {
                        childOutputMethod = new ChildOutputMethod() {

                            @Override
                            public void transform(Object object) throws XMLStreamException {
                                getTransformer().transform((XMLSecEvent) object);
                            }

                            @Override
                            public void doFinal() throws XMLStreamException {
                                getTransformer().doFinal();
                            }
                        };
                        break;
                    }
                case InputStream:
                    {
                        childOutputMethod = new ChildOutputMethod() {

                            private UnsyncByteArrayOutputStream baos;

                            private XMLEventWriter xmlEventWriter;

                            @Override
                            public void transform(Object object) throws XMLStreamException {
                                if (xmlEventWriter == null) {
                                    baos = new UnsyncByteArrayOutputStream();
                                    xmlEventWriter = getXmlOutputFactory().createXMLEventWriter(baos);
                                }
                                xmlEventWriter.add((XMLSecEvent) object);
                            }

                            @Override
                            public void doFinal() throws XMLStreamException {
                                xmlEventWriter.close();
                                try (InputStream is = new UnsyncByteArrayInputStream(baos.toByteArray())) {
                                    getTransformer().transform(is);
                                    getTransformer().doFinal();
                                } catch (IOException ex) {
                                    throw new XMLStreamException(ex);
                                }
                            }
                        };
                        break;
                    }
            }
        }
        if (childOutputMethod != null) {
            childOutputMethod.transform(xmlSecEvent);
        }
    }
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream)

Aggregations

UnsyncByteArrayOutputStream (org.apache.xml.security.utils.UnsyncByteArrayOutputStream)7 UnsyncByteArrayInputStream (org.apache.xml.security.utils.UnsyncByteArrayInputStream)4 IOException (java.io.IOException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)2 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)2 XMLSecurityConstants (org.apache.xml.security.stax.ext.XMLSecurityConstants)2 XMLSecEvent (org.apache.xml.security.stax.ext.stax.XMLSecEvent)2 InputStream (java.io.InputStream)1 JAXBElement (javax.xml.bind.JAXBElement)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Base64InputStream (org.apache.commons.codec.binary.Base64InputStream)1 Base64OutputStream (org.apache.commons.codec.binary.Base64OutputStream)1 SignedInfoType (org.apache.xml.security.binding.xmldsig.SignedInfoType)1 XMLEventReaderInputProcessor (org.apache.xml.security.stax.impl.processor.input.XMLEventReaderInputProcessor)1