Search in sources :

Example 6 with Transformer

use of org.apache.xml.security.stax.ext.Transformer in project santuario-java by apache.

the class TransformEnvelopedSignatureTest method testXMLSecEventToInputStreamAPI.

@Test
public void testXMLSecEventToInputStreamAPI() throws Exception {
    TransformEnvelopedSignature transformEnvelopedSignature = new TransformEnvelopedSignature();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Transformer transformer = new Transformer() {

        @Override
        public void setOutputStream(OutputStream outputStream) throws XMLSecurityException {
        }

        @Override
        public void setTransformer(Transformer transformer) throws XMLSecurityException {
        }

        @Override
        public void setProperties(Map<String, Object> properties) throws XMLSecurityException {
        }

        @Override
        public XMLSecurityConstants.TransformMethod getPreferredTransformMethod(XMLSecurityConstants.TransformMethod forInput) {
            return XMLSecurityConstants.TransformMethod.InputStream;
        }

        @Override
        public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
            Assert.fail("unexpected call to transform(XMLSecEvent");
        }

        @Override
        public void transform(InputStream inputStream) throws XMLStreamException {
            try {
                XMLSecurityUtils.copy(inputStream, byteArrayOutputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void doFinal() throws XMLStreamException {
        }
    };
    transformEnvelopedSignature.setTransformer(transformer);
    XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("com/phaos/phaos-xmldsig-three/signature-rsa-enveloped.xml"));
    while (xmlSecEventReader.hasNext()) {
        XMLSecEvent xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
        transformEnvelopedSignature.transform(xmlSecEvent);
    }
    transformEnvelopedSignature.doFinal();
    Assert.assertEquals(207, byteArrayOutputStream.size());
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) Transformer(org.apache.xml.security.stax.ext.Transformer) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformEnvelopedSignature(org.apache.xml.security.stax.impl.transformer.TransformEnvelopedSignature) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Map(java.util.Map) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

Example 7 with Transformer

use of org.apache.xml.security.stax.ext.Transformer in project santuario-java by apache.

the class TransformIdentityTest method testXMLSecEventToInputStreamAPI.

@Test
public void testXMLSecEventToInputStreamAPI() throws Exception {
    TransformIdentity transformIdentity = new TransformIdentity();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Transformer transformer = new Transformer() {

        @Override
        public void setOutputStream(OutputStream outputStream) throws XMLSecurityException {
        }

        @Override
        public void setTransformer(Transformer transformer) throws XMLSecurityException {
        }

        @Override
        public void setProperties(Map<String, Object> properties) throws XMLSecurityException {
        }

        @Override
        public XMLSecurityConstants.TransformMethod getPreferredTransformMethod(XMLSecurityConstants.TransformMethod forInput) {
            return XMLSecurityConstants.TransformMethod.InputStream;
        }

        @Override
        public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
            Assert.fail("unexpected call to transform(XMLSecEvent");
        }

        @Override
        public void transform(InputStream inputStream) throws XMLStreamException {
            try {
                XMLSecurityUtils.copy(inputStream, byteArrayOutputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void doFinal() throws XMLStreamException {
        }
    };
    transformIdentity.setTransformer(transformer);
    XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext-base64.xml"));
    XMLSecEvent xmlSecEvent = null;
    while (xmlSecEventReader.hasNext()) {
        xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
        if (xmlSecEvent.isStartElement() && xmlSecEvent.asStartElement().getName().equals(new QName("urn:example:po", "PaymentInfo"))) {
            break;
        }
    }
    while (xmlSecEventReader.hasNext()) {
        transformIdentity.transform(xmlSecEvent);
        if (xmlSecEvent.isEndElement() && xmlSecEvent.asEndElement().getName().equals(new QName("urn:example:po", "PaymentInfo"))) {
            break;
        }
        xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
    }
    transformIdentity.doFinal();
    Assert.assertEquals(255, byteArrayOutputStream.size());
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) TransformIdentity(org.apache.xml.security.stax.impl.transformer.TransformIdentity) Transformer(org.apache.xml.security.stax.ext.Transformer) UnixInputStream(org.apache.xml.security.test.stax.utils.UnixInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Map(java.util.Map) Test(org.junit.Test)

Example 8 with Transformer

use of org.apache.xml.security.stax.ext.Transformer in project santuario-java by apache.

the class TransformIdentityTest method testXMLSecEventToXMLSecEventAPI.

@Test
public void testXMLSecEventToXMLSecEventAPI() throws Exception {
    TransformIdentity transformIdentity = new TransformIdentity();
    final List<XMLSecEvent> xmlSecEvents = new ArrayList<>();
    Transformer transformer = new Transformer() {

        @Override
        public void setOutputStream(OutputStream outputStream) throws XMLSecurityException {
        }

        @Override
        public void setTransformer(Transformer transformer) throws XMLSecurityException {
        }

        @Override
        public void setProperties(Map<String, Object> properties) throws XMLSecurityException {
        }

        @Override
        public XMLSecurityConstants.TransformMethod getPreferredTransformMethod(XMLSecurityConstants.TransformMethod forInput) {
            return XMLSecurityConstants.TransformMethod.XMLSecEvent;
        }

        @Override
        public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
            xmlSecEvents.add(xmlSecEvent);
        }

        @Override
        public void transform(InputStream inputStream) throws XMLStreamException {
            Assert.fail("unexpected call to transform(InputStream");
        }

        @Override
        public void doFinal() throws XMLStreamException {
        }
    };
    transformIdentity.setTransformer(transformer);
    XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext-base64.xml"));
    XMLSecEvent xmlSecEvent = null;
    while (xmlSecEventReader.hasNext()) {
        xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
        if (xmlSecEvent.isStartElement() && xmlSecEvent.asStartElement().getName().equals(new QName("urn:example:po", "PaymentInfo"))) {
            break;
        }
    }
    while (xmlSecEventReader.hasNext()) {
        transformIdentity.transform(xmlSecEvent);
        if (xmlSecEvent.isEndElement() && xmlSecEvent.asEndElement().getName().equals(new QName("urn:example:po", "PaymentInfo"))) {
            break;
        }
        xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
    }
    transformIdentity.doFinal();
    Assert.assertEquals(3, xmlSecEvents.size());
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) TransformIdentity(org.apache.xml.security.stax.impl.transformer.TransformIdentity) Transformer(org.apache.xml.security.stax.ext.Transformer) UnixInputStream(org.apache.xml.security.test.stax.utils.UnixInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayList(java.util.ArrayList) XMLEventReader(javax.xml.stream.XMLEventReader) Map(java.util.Map) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

Example 9 with Transformer

use of org.apache.xml.security.stax.ext.Transformer in project santuario-java by apache.

the class TransformBase64DecodeTest method testXMLSecEventToInputStreamAPI.

@Test
public void testXMLSecEventToInputStreamAPI() throws Exception {
    TransformBase64Decode transformBase64Decode = new TransformBase64Decode();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Transformer transformer = new Transformer() {

        @Override
        public void setOutputStream(OutputStream outputStream) throws XMLSecurityException {
        }

        @Override
        public void setTransformer(Transformer transformer) throws XMLSecurityException {
        }

        @Override
        public void setProperties(Map<String, Object> properties) throws XMLSecurityException {
        }

        @Override
        public XMLSecurityConstants.TransformMethod getPreferredTransformMethod(XMLSecurityConstants.TransformMethod forInput) {
            return XMLSecurityConstants.TransformMethod.InputStream;
        }

        @Override
        public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
            Assert.fail("unexpected call to transform(XMLSecEvent");
        }

        @Override
        public void transform(InputStream inputStream) throws XMLStreamException {
            try {
                XMLSecurityUtils.copy(inputStream, byteArrayOutputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void doFinal() throws XMLStreamException {
        }
    };
    transformBase64Decode.setTransformer(transformer);
    XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext-base64.xml"));
    XMLSecEvent xmlSecEvent = null;
    while (xmlSecEventReader.hasNext()) {
        xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
        if (xmlSecEvent.isStartElement() && xmlSecEvent.asStartElement().getName().equals(new QName("urn:example:po", "PaymentInfo"))) {
            break;
        }
    }
    while (xmlSecEventReader.hasNext()) {
        transformBase64Decode.transform(xmlSecEvent);
        if (xmlSecEvent.isEndElement() && xmlSecEvent.asEndElement().getName().equals(new QName("urn:example:po", "PaymentInfo"))) {
            break;
        }
        xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
    }
    transformBase64Decode.doFinal();
    Assert.assertEquals(153, byteArrayOutputStream.size());
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) Transformer(org.apache.xml.security.stax.ext.Transformer) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) TransformBase64Decode(org.apache.xml.security.stax.impl.transformer.TransformBase64Decode) XMLEventReader(javax.xml.stream.XMLEventReader) Map(java.util.Map) Test(org.junit.Test)

Example 10 with Transformer

use of org.apache.xml.security.stax.ext.Transformer in project santuario-java by apache.

the class TransformBase64DecodeTest method testInputStreamToInputStreamAPI.

@Test
public void testInputStreamToInputStreamAPI() throws Exception {
    TransformBase64Decode transformBase64Decode = new TransformBase64Decode();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Transformer transformer = new Transformer() {

        @Override
        public void setOutputStream(OutputStream outputStream) throws XMLSecurityException {
        }

        @Override
        public void setTransformer(Transformer transformer) throws XMLSecurityException {
        }

        @Override
        public void setProperties(Map<String, Object> properties) throws XMLSecurityException {
        }

        @Override
        public XMLSecurityConstants.TransformMethod getPreferredTransformMethod(XMLSecurityConstants.TransformMethod forInput) {
            return XMLSecurityConstants.TransformMethod.InputStream;
        }

        @Override
        public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
            Assert.fail("unexpected call to transform(XMLSecEvent");
        }

        @Override
        public void transform(InputStream inputStream) throws XMLStreamException {
            try {
                XMLSecurityUtils.copy(inputStream, byteArrayOutputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void doFinal() throws XMLStreamException {
        }
    };
    transformBase64Decode.setTransformer(transformer);
    transformBase64Decode.transform(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/base64.xml"));
    transformBase64Decode.doFinal();
    Assert.assertEquals(615, byteArrayOutputStream.size());
}
Also used : XMLSecurityConstants(org.apache.xml.security.stax.ext.XMLSecurityConstants) TransformBase64Decode(org.apache.xml.security.stax.impl.transformer.TransformBase64Decode) Transformer(org.apache.xml.security.stax.ext.Transformer) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Map(java.util.Map) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 InputStream (java.io.InputStream)14 OutputStream (java.io.OutputStream)14 Map (java.util.Map)14 Transformer (org.apache.xml.security.stax.ext.Transformer)14 XMLSecurityConstants (org.apache.xml.security.stax.ext.XMLSecurityConstants)14 XMLSecEvent (org.apache.xml.security.stax.ext.stax.XMLSecEvent)14 Test (org.junit.Test)14 IOException (java.io.IOException)9 XMLEventReader (javax.xml.stream.XMLEventReader)8 ArrayList (java.util.ArrayList)5 QName (javax.xml.namespace.QName)4 TransformBase64Decode (org.apache.xml.security.stax.impl.transformer.TransformBase64Decode)4 TransformIdentity (org.apache.xml.security.stax.impl.transformer.TransformIdentity)4 Canonicalizer20010315_OmitCommentsTransformer (org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer)4 UnixInputStream (org.apache.xml.security.test.stax.utils.UnixInputStream)4 TransformEnvelopedSignature (org.apache.xml.security.stax.impl.transformer.TransformEnvelopedSignature)2