Search in sources :

Example 1 with TransformIdentity

use of org.apache.xml.security.stax.impl.transformer.TransformIdentity 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 2 with TransformIdentity

use of org.apache.xml.security.stax.impl.transformer.TransformIdentity in project santuario-java by apache.

the class TransformIdentityTest method testXMLSecEventToOutputStreamStreamAPI.

@Test
public void testXMLSecEventToOutputStreamStreamAPI() throws Exception {
    TransformIdentity transformIdentity = new TransformIdentity();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    transformIdentity.setOutputStream(byteArrayOutputStream);
    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 : TransformIdentity(org.apache.xml.security.stax.impl.transformer.TransformIdentity) QName(javax.xml.namespace.QName) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

Example 3 with TransformIdentity

use of org.apache.xml.security.stax.impl.transformer.TransformIdentity 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 4 with TransformIdentity

use of org.apache.xml.security.stax.impl.transformer.TransformIdentity in project santuario-java by apache.

the class TransformIdentityTest method testInputStreamToOutputStreamStreamAPI.

@Test
public void testInputStreamToOutputStreamStreamAPI() throws Exception {
    TransformIdentity transformIdentity = new TransformIdentity();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    transformIdentity.setOutputStream(byteArrayOutputStream);
    transformIdentity.transform(new UnixInputStream(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/xml-stylesheet.b64")));
    Assert.assertEquals(17786, byteArrayOutputStream.size());
}
Also used : TransformIdentity(org.apache.xml.security.stax.impl.transformer.TransformIdentity) UnixInputStream(org.apache.xml.security.test.stax.utils.UnixInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with TransformIdentity

use of org.apache.xml.security.stax.impl.transformer.TransformIdentity in project santuario-java by apache.

the class TransformIdentityTest method testInputStreamToXMLSecEventAPI.

@Test
public void testInputStreamToXMLSecEventAPI() 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);
    transformIdentity.transform(new UnixInputStream(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext-base64.xml")));
    transformIdentity.doFinal();
    Assert.assertEquals(25, 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) UnixInputStream(org.apache.xml.security.test.stax.utils.UnixInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayList(java.util.ArrayList) Map(java.util.Map) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

Aggregations

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