Search in sources :

Example 26 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class StaxConverterTest method testToInputSreamByXmlStreamReader.

public void testToInputSreamByXmlStreamReader() throws Exception {
    StringReader src = new StringReader(TEST_XML_7000);
    XMLStreamReader xreader = null;
    InputStream in = null;
    try {
        xreader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, src);
        in = context.getTypeConverter().mandatoryConvertTo(InputStream.class, xreader);
        // verify
        InputStream expected = new ByteArrayInputStream(TEST_XML_7000.getBytes("utf-8"));
        byte[] tmp1 = new byte[512];
        byte[] tmp2 = new byte[512];
        for (; ; ) {
            int n1 = 0;
            int n2 = 0;
            try {
                n1 = expected.read(tmp1, 0, tmp1.length);
                n2 = in.read(tmp2, 0, tmp2.length);
            } catch (IOException e) {
                fail("unable to read data");
            }
            assertEquals(n1, n2);
            if (n2 < 0) {
                break;
            }
            assertTrue(Arrays.equals(tmp1, tmp2));
        }
    } finally {
        if (xreader != null) {
            xreader.close();
        }
        if (in != null) {
            in.close();
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 27 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class StAX2SAXSourceTest method testDefaultPrefixInRootElementWithCopyTransformer.

public void testDefaultPrefixInRootElementWithCopyTransformer() throws Exception {
    TransformerFactory trf = TransformerFactory.newInstance();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLStreamReader reader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, new StringReader(TEST_XML));
    // ensure UTF-8 encoding
    Exchange exchange = new DefaultExchange(context);
    exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
    XMLStreamWriter writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, baos);
    StAX2SAXSource staxSource = new StAX2SAXSource(reader);
    StreamSource templateSource = new StreamSource(getClass().getResourceAsStream("/xslt/common/copy.xsl"));
    Transformer transformer = trf.newTransformer(templateSource);
    log.info("Used transformer: {}", transformer.getClass().getName());
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(staxSource, new StreamResult(baos));
    writer.flush();
    baos.flush();
    assertThat(new String(baos.toByteArray()), equalTo(TEST_XML));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) TransformerFactory(javax.xml.transform.TransformerFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 28 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class CxfPayloadConverter method convertTo.

@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // CxfPayloads from other types
    if (type.isAssignableFrom(CxfPayload.class)) {
        try {
            if (!value.getClass().isArray()) {
                Source src = null;
                // directly
                if (value instanceof InputStream) {
                    src = new StreamSource((InputStream) value);
                } else if (value instanceof Reader) {
                    src = new StreamSource((Reader) value);
                } else if (value instanceof String) {
                    src = new StreamSource(new StringReader((String) value));
                } else if (value instanceof Node) {
                    src = new DOMSource((Node) value);
                } else if (value instanceof Source) {
                    src = (Source) value;
                }
                if (src == null) {
                    // assuming staxsource is preferred, otherwise use the
                    // one preferred
                    TypeConverter tc = registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
                    if (tc == null) {
                        tc = registry.lookup(Source.class, value.getClass());
                    }
                    if (tc != null) {
                        src = tc.convertTo(Source.class, exchange, value);
                    }
                }
                if (src != null) {
                    return (T) sourceToCxfPayload(src, exchange);
                }
            }
            TypeConverter tc = registry.lookup(NodeList.class, value.getClass());
            if (tc != null) {
                NodeList nodeList = tc.convertTo(NodeList.class, exchange, value);
                return (T) nodeListToCxfPayload(nodeList, exchange);
            }
            tc = registry.lookup(Document.class, value.getClass());
            if (tc != null) {
                Document document = tc.convertTo(Document.class, exchange, value);
                return (T) documentToCxfPayload(document, exchange);
            }
            // maybe we can convert via an InputStream
            CxfPayload<?> p;
            p = convertVia(InputStream.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
            // String is the converter of last resort
            p = convertVia(String.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
        } catch (RuntimeCamelException e) {
        // the internal conversion to XML can throw an exception if the content is not XML
        // ignore this and return Void.TYPE to indicate that we cannot convert this
        }
        // no we could not do it currently
        return (T) Void.TYPE;
    }
    // Convert a CxfPayload into something else
    if (CxfPayload.class.isAssignableFrom(value.getClass())) {
        CxfPayload<?> payload = (CxfPayload<?>) value;
        int size = payload.getBodySources().size();
        if (size == 1) {
            if (type.isAssignableFrom(Document.class)) {
                Source s = payload.getBodySources().get(0);
                Document d;
                try {
                    d = StaxUtils.read(s);
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
                return type.cast(d);
            }
            // CAMEL-8410 Just make sure we get the Source object directly from the payload body source
            Source s = payload.getBodySources().get(0);
            if (type.isInstance(s)) {
                return type.cast(s);
            }
            TypeConverter tc = registry.lookup(type, XMLStreamReader.class);
            if (tc != null && (s instanceof StaxSource || s instanceof StAXSource)) {
                XMLStreamReader r = (s instanceof StAXSource) ? ((StAXSource) s).getXMLStreamReader() : ((StaxSource) s).getXMLStreamReader();
                if (payload.getNsMap() != null) {
                    r = new DelegatingXMLStreamReader(r, payload.getNsMap());
                }
                return tc.convertTo(type, exchange, r);
            }
            tc = registry.lookup(type, Source.class);
            if (tc != null) {
                XMLStreamReader r = null;
                if (payload.getNsMap() != null) {
                    if (s instanceof StaxSource) {
                        r = ((StaxSource) s).getXMLStreamReader();
                    } else if (s instanceof StAXSource) {
                        r = ((StAXSource) s).getXMLStreamReader();
                    }
                    if (r != null) {
                        s = new StAXSource(new DelegatingXMLStreamReader(r, payload.getNsMap()));
                    }
                }
                return tc.convertTo(type, exchange, s);
            }
        }
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            Object result = tc.convertTo(type, exchange, cxfPayloadToNodeList((CxfPayload<?>) value, exchange));
            if (result == null) {
                // no we could not do it currently, and we just abort the convert here
                return (T) Void.TYPE;
            } else {
                return (T) result;
            }
        }
        // we cannot convert a node list, so we try the first item from the
        // node list
        tc = registry.lookup(type, Node.class);
        if (tc != null) {
            NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) value, exchange);
            if (nodeList.getLength() > 0) {
                return tc.convertTo(type, exchange, nodeList.item(0));
            } else {
                // no we could not do it currently
                return (T) Void.TYPE;
            }
        } else {
            if (size == 0) {
                // empty size so we cannot convert
                return (T) Void.TYPE;
            }
        }
    }
    return null;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) CxfPayload(org.apache.camel.component.cxf.CxfPayload) Node(org.w3c.dom.Node) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) StringReader(java.io.StringReader) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) StAXSource(javax.xml.transform.stax.StAXSource) TypeConverter(org.apache.camel.TypeConverter) XMLStreamException(javax.xml.stream.XMLStreamException) StaxSource(org.apache.cxf.staxutils.StaxSource) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FallbackConverter(org.apache.camel.FallbackConverter)

Example 29 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class CxfMessageHelperTest method testGetCxfInMessage.

// setup the default context for testing
@Test
public void testGetCxfInMessage() throws Exception {
    HeaderFilterStrategy headerFilterStrategy = new CxfHeaderFilterStrategy();
    org.apache.camel.Exchange exchange = new DefaultExchange(context);
    // String
    exchange.getIn().setBody("hello world");
    org.apache.cxf.message.Message message = CxfMessageHelper.getCxfInMessage(headerFilterStrategy, exchange, false);
    // test message
    InputStream is = message.getContent(InputStream.class);
    assertNotNull("The input stream should not be null", is);
    assertEquals("Don't get the right message", toString(is), "hello world");
    // DOMSource
    URL request = this.getClass().getResource("RequestBody.xml");
    File requestFile = new File(request.toURI());
    FileInputStream inputStream = new FileInputStream(requestFile);
    XMLStreamReader xmlReader = StaxUtils.createXMLStreamReader(inputStream);
    DOMSource source = new DOMSource(StaxUtils.read(xmlReader));
    exchange.getIn().setBody(source);
    message = CxfMessageHelper.getCxfInMessage(headerFilterStrategy, exchange, false);
    is = message.getContent(InputStream.class);
    assertNotNull("The input stream should not be null", is);
    assertEquals("Don't get the right message", toString(is), REQUEST_STRING);
    // File
    exchange.getIn().setBody(requestFile);
    message = CxfMessageHelper.getCxfInMessage(headerFilterStrategy, exchange, false);
    is = message.getContent(InputStream.class);
    assertNotNull("The input stream should not be null", is);
    assertEquals("Don't get the right message", toString(is), REQUEST_STRING);
    // transport header's case insensitiveness
    // String
    exchange.getIn().setBody("hello world");
    exchange.getIn().setHeader("soapAction", "urn:hello:world");
    message = CxfMessageHelper.getCxfInMessage(headerFilterStrategy, exchange, false);
    // test message
    Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
    // verify there is no duplicate
    assertNotNull("The headers must be present", headers);
    assertTrue("There must be one header entry", headers.size() == 1);
    // verify the soapaction can be retrieved in case-insensitive ways
    verifyHeader(headers, "soapaction", "urn:hello:world");
    verifyHeader(headers, "SoapAction", "urn:hello:world");
    verifyHeader(headers, "SOAPAction", "urn:hello:world");
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) CxfHeaderFilterStrategy(org.apache.camel.component.cxf.common.header.CxfHeaderFilterStrategy) Message(org.apache.cxf.message.Message) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CxfHeaderFilterStrategy(org.apache.camel.component.cxf.common.header.CxfHeaderFilterStrategy) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) URL(java.net.URL) FileInputStream(java.io.FileInputStream) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 30 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project camel by apache.

the class CachedCxfPayloadTest method testCachedCxfPayloadStaxSource.

@Test
public void testCachedCxfPayloadStaxSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException {
    XMLStreamReader streamReader = StaxUtils.createXMLStreamReader(new StreamSource(new StringReader(PAYLOAD)));
    StaxSource source = new StaxSource(streamReader);
    doTest(source, PAYLOAD);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)1074 Test (org.junit.Test)486 InputStream (java.io.InputStream)451 ByteArrayInputStream (java.io.ByteArrayInputStream)379 ByteArrayOutputStream (java.io.ByteArrayOutputStream)334 Document (org.w3c.dom.Document)311 XMLStreamException (javax.xml.stream.XMLStreamException)288 ArrayList (java.util.ArrayList)270 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)242 XMLInputFactory (javax.xml.stream.XMLInputFactory)211 QName (javax.xml.namespace.QName)208 DOMSource (javax.xml.transform.dom.DOMSource)206 StringReader (java.io.StringReader)196 SecretKey (javax.crypto.SecretKey)188 StreamResult (javax.xml.transform.stream.StreamResult)183 DocumentBuilder (javax.xml.parsers.DocumentBuilder)178 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)160 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)155 IOException (java.io.IOException)144 Key (java.security.Key)103