Search in sources :

Example 1 with CxfPayload

use of org.apache.camel.component.cxf.CxfPayload 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 2 with CxfPayload

use of org.apache.camel.component.cxf.CxfPayload in project camel by apache.

the class CxfPayloadConverterTest method testCxfPayloadToStreamCache.

@Test
public void testCxfPayloadToStreamCache() {
    StreamCache streamCache = CxfPayloadConverter.cxfPayLoadToStreamCache(payload, exchange);
    assertNotNull(streamCache);
    assertTrue(streamCache instanceof CxfPayload);
}
Also used : StreamCache(org.apache.camel.StreamCache) CxfPayload(org.apache.camel.component.cxf.CxfPayload) Test(org.junit.Test)

Example 3 with CxfPayload

use of org.apache.camel.component.cxf.CxfPayload in project camel by apache.

the class CxfMessageHeadersRelayTest method testInoutHeaderCXFClientNoServiceClassNoRelay.

@Test
public void testInoutHeaderCXFClientNoServiceClassNoRelay() throws Exception {
    // TODO: Fix this test later
    QName qname = QName.valueOf("{http://apache.org/camel/component/cxf/soap/headers}SOAPHeaderInfo");
    String uri = "cxf:bean:routerNoRelayNoServiceClassEndpoint?headerFilterStrategy=#dropAllMessageHeadersStrategy";
    String requestHeader = "<ns2:SOAPHeaderInfo xmlns:ns2=\"http://apache.org/camel/" + "component/cxf/soap/headers\"><originator>CxfSoapHeaderRoutePropagationTest.testInOutHeader Requestor" + "</originator><message>Invoking CxfSoapHeaderRoutePropagationTest.testInOutHeader() Request" + "</message></ns2:SOAPHeaderInfo>";
    String requestBody = "<ns2:inoutHeader xmlns:ns2=\"http://apache.org/camel/component/cxf/soap/headers\">" + "<requestType>CXF user</requestType></ns2:inoutHeader>";
    List<Source> elements = new ArrayList<Source>();
    elements.add(new DOMSource(StaxUtils.read(new StringReader(requestBody)).getDocumentElement()));
    final List<SoapHeader> headers = new ArrayList<SoapHeader>();
    headers.add(new SoapHeader(qname, StaxUtils.read(new StringReader(requestHeader)).getDocumentElement()));
    final CxfPayload<SoapHeader> cxfPayload = new CxfPayload<SoapHeader>(headers, elements, null);
    Exchange exchange = template.request(uri, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(cxfPayload);
            exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "inoutHeader");
            exchange.getIn().setHeader(Header.HEADER_LIST, headers);
        }
    });
    CxfPayload<?> out = exchange.getOut().getBody(CxfPayload.class);
    assertEquals(1, out.getBodySources().size());
    assertTrue(out.getBodySources().get(0) instanceof DOMSource);
    assertEquals(0, out.getHeaders().size());
    String responseExp = "<ns2:inoutHeaderResponse xmlns:ns2=\"http://apache.org/camel/" + "component/cxf/soap/headers\"><responseType>pass</responseType>" + "</ns2:inoutHeaderResponse>";
    String response = StaxUtils.toString(out.getBody().get(0));
    //REVISIT use a more reliable comparison to tolerate some namespaces being added to the root element
    assertTrue(response, response.startsWith(responseExp.substring(0, 87)) && response.endsWith(responseExp.substring(88, responseExp.length())));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) JAXBException(javax.xml.bind.JAXBException) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Test(org.junit.Test)

Example 4 with CxfPayload

use of org.apache.camel.component.cxf.CxfPayload in project camel by apache.

the class CxfMtomDisabledProducerPayloadModeTest method testProducer.

@Override
public void testProducer() throws Exception {
    if (MtomTestHelper.isAwtHeadless(logger, null)) {
        return;
    }
    Exchange exchange = context.createProducerTemplate().send("direct:testEndpoint", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            List<Source> elements = new ArrayList<Source>();
            elements.add(new DOMSource(StaxUtils.read(new StringReader(MtomTestHelper.MTOM_DISABLED_REQ_MESSAGE)).getDocumentElement()));
            CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null);
            exchange.getIn().setBody(body);
            exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, "application/octet-stream")));
            exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.requestJpeg, "image/jpeg")));
        }
    });
    // process response - verify response attachments
    CxfPayload<?> out = exchange.getOut().getBody(CxfPayload.class);
    Assert.assertEquals(1, out.getBody().size());
    DataHandler dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_PHOTO_CID);
    Assert.assertEquals("application/octet-stream", dr.getContentType());
    MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
    dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_IMAGE_CID);
    Assert.assertEquals("image/jpeg", dr.getContentType());
    BufferedImage image = ImageIO.read(dr.getInputStream());
    Assert.assertEquals(560, image.getWidth());
    Assert.assertEquals(300, image.getHeight());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) AttachmentDataSource(org.apache.cxf.attachment.AttachmentDataSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) BufferedImage(java.awt.image.BufferedImage) Exchange(org.apache.camel.Exchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 5 with CxfPayload

use of org.apache.camel.component.cxf.CxfPayload in project camel by apache.

the class CxfPayloadConverter method nodeListToCxfPayload.

@Converter
public static <T> CxfPayload<T> nodeListToCxfPayload(NodeList nodeList, Exchange exchange) {
    List<T> headers = new ArrayList<T>();
    List<Element> body = new ArrayList<Element>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        // add all nodes to the body that are elements
        if (Element.class.isAssignableFrom(node.getClass())) {
            body.add((Element) node);
        }
    }
    return new CxfPayload<T>(headers, body);
}
Also used : CxfPayload(org.apache.camel.component.cxf.CxfPayload) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) FallbackConverter(org.apache.camel.FallbackConverter) Converter(org.apache.camel.Converter) TypeConverter(org.apache.camel.TypeConverter)

Aggregations

CxfPayload (org.apache.camel.component.cxf.CxfPayload)6 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 Source (javax.xml.transform.Source)4 DOMSource (javax.xml.transform.dom.DOMSource)4 Exchange (org.apache.camel.Exchange)3 Processor (org.apache.camel.Processor)3 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)3 Test (org.junit.Test)3 BufferedImage (java.awt.image.BufferedImage)2 List (java.util.List)2 DataHandler (javax.activation.DataHandler)2 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)2 FallbackConverter (org.apache.camel.FallbackConverter)2 TypeConverter (org.apache.camel.TypeConverter)2 XmlConverter (org.apache.camel.converter.jaxp.XmlConverter)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1