Search in sources :

Example 6 with CxfPayload

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

the class CxfMtomProducerPayloadModeTest method testProducer.

@SuppressWarnings("unchecked")
@Test
public void testProducer() throws Exception {
    if (MtomTestHelper.isAwtHeadless(logger, null)) {
        return;
    }
    // START SNIPPET: producer
    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.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 
    CxfPayload<SoapHeader> out = exchange.getOut().getBody(CxfPayload.class);
    Assert.assertEquals(1, out.getBody().size());
    Map<String, String> ns = new HashMap<String, String>();
    ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS);
    ns.put("xop", MtomTestHelper.XOP_NS);
    XPathUtils xu = new XPathUtils(ns);
    Element oute = new XmlConverter().toDOMElement(out.getBody().get(0));
    Element ele = (Element) xu.getValue("//ns:DetailResponse/ns:photo/xop:Include", oute, XPathConstants.NODE);
    // skip "cid:"
    String photoId = ele.getAttribute("href").substring(4);
    ele = (Element) xu.getValue("//ns:DetailResponse/ns:image/xop:Include", oute, XPathConstants.NODE);
    // skip "cid:"
    String imageId = ele.getAttribute("href").substring(4);
    DataHandler dr = exchange.getOut().getAttachment(decodingReference(photoId));
    Assert.assertEquals("application/octet-stream", dr.getContentType());
    MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
    dr = exchange.getOut().getAttachment(decodingReference(imageId));
    Assert.assertEquals("image/jpeg", dr.getContentType());
    BufferedImage image = ImageIO.read(dr.getInputStream());
    Assert.assertEquals(560, image.getWidth());
    Assert.assertEquals(300, image.getHeight());
// END SNIPPET: producer
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Source(javax.xml.transform.Source) BufferedImage(java.awt.image.BufferedImage) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Exchange(org.apache.camel.Exchange) XPathUtils(org.apache.cxf.helpers.XPathUtils) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

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