Attachment Support - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

POJO Mode: Both SOAP with Attachment and MTOM are supported (see example in Payload Mode for enabling MTOM). However, SOAP with Attachment is not tested. Since attachments are marshalled and unmarshalled into POJOs, users typically do not need to deal with the attachment themselves. Attachments are propagated to Camel message's attachments since 2.12.3 . So, it is possible to retrieve attachments by Camel Message API

DataHandler Message.getAttachment(String id)

.

Payload Mode: MTOM is supported since 2.1. Attachments can be retrieved by Camel Message APIs mentioned above. SOAP with Attachment (SwA) is supported and attachments can be retrieved since 2.5. SwA is the default (same as setting the CXF endpoint property "mtom-enabled" to false).

To enable MTOM, set the CXF endpoint property "mtom-enabled" to true.

<cxf:cxfEndpoint id="routerEndpoint" 
   address="http://localhost:9091/jaxws-mtom/hello"
   wsdlURL="mtom.wsdl"
   serviceName="ns:HelloService"
   endpointName="ns:HelloPort"
   xmlns:ns="http://apache.org/camel/cxf/mtom_feature">

   <cxf:properties>
      <!--  enable mtom by setting this property to true -->
      <entry key="mtom-enabled" value="true"/>
         
      <!--  set the camel-cxf endpoint data fromat to PAYLOAD mode -->
      <entry key="dataFormat" value="PAYLOAD"/>
   </cxf:properties>

<cxf:cxfEndpoint>        

You can produce a Camel message with attachment to send to a CXF endpoint in Payload mode.

Exchange exchange = context.createProducerTemplate().send(
   "direct:testEndpoint", new Processor() {

   public void process(Exchange exchange) throws Exception {
      exchange.setPattern(ExchangePattern.InOut);
      List<Element> elements = new ArrayList<Element>();
      
      elements.add(DOMUtils.readXml(
         new StringReader(MtomTestHelper.REQ_MESSAGE)).
         getDocumentElement());
      CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(
         new ArrayList<SoapHeader>(),elements);
         
      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 ele = (Element)xu.getValue(
   "//ns:DetailResponse/ns:photo/xop:Include", 
   out.getBody().get(0),XPathConstants.NODE);
            
String photoId = ele.getAttribute("href").substring(4); // skip "cid:"

ele = (Element)xu.getValue(
   "//ns:DetailResponse/ns:image/xop:Include", 
   out.getBody().get(0),XPathConstants.NODE);
            
String imageId = ele.getAttribute("href").substring(4); // skip "cid:"

DataHandler dr = exchange.getOut().getAttachment(photoId);
Assert.assertEquals("application/octet-stream", dr.getContentType());
MtomTestHelper.assertEquals(
   MtomTestHelper.RESP_PHOTO_DATA, 
   IOUtils.readBytesFromStream(dr.getInputStream()));
   
dr = exchange.getOut().getAttachment(imageId);
Assert.assertEquals("image/jpeg", dr.getContentType());

BufferedImage image = ImageIO.read(dr.getInputStream());
Assert.assertEquals(560, image.getWidth());
Assert.assertEquals(300, image.getHeight());

You can also consume a Camel message received from a CXF endpoint in Payload mode.

public static class MyProcessor implements Processor {

   @SuppressWarnings("unchecked")
   public void process(Exchange exchange) throws Exception {
      CxfPayload<SoapHeader> in = exchange.getIn().getBody(
         CxfPayload.class);
        
      // verify request
      Assert.assertEquals(1, in.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 ele = (Element)
         xu.getValue("//ns:Detail/ns:photo/xop:Include", 
         in.getBody().get(0),XPathConstants.NODE);
                  
      // skip "cid:"
      String photoId = ele.getAttribute("href").substring(4); 
      Assert.assertEquals(MtomTestHelper.REQ_PHOTO_CID, photoId);

      ele = (Element)xu.getValue("//ns:Detail/ns:image/xop:Include", 
         in.getBody().get(0), XPathConstants.NODE);
                  
      // skip "cid:"
      String imageId = ele.getAttribute("href").substring(4); 
      Assert.assertEquals(MtomTestHelper.REQ_IMAGE_CID, imageId);

      DataHandler dr = exchange.getIn().getAttachment(photoId);
      Assert.assertEquals("application/octet-stream", dr.getContentType());
      MtomTestHelper.assertEquals(MtomTestHelper.REQ_PHOTO_DATA, 
         IOUtils.readBytesFromStream(dr.getInputStream()));
   
      dr = exchange.getIn().getAttachment(imageId);
      Assert.assertEquals("image/jpeg", dr.getContentType());
      MtomTestHelper.assertEquals(MtomTestHelper.requestJpeg, 
         IOUtils.readBytesFromStream(dr.getInputStream()));

        // create response
      List<Element> elements = new ArrayList<Element>();
      elements.add(DOMUtils.readXml(new StringReader(
         MtomTestHelper.RESP_MESSAGE)).getDocumentElement());
      CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(
         new ArrayList<SoapHeader>(),elements);
      exchange.getOut().setBody(body);
      exchange.getOut().addAttachment(MtomTestHelper.RESP_PHOTO_CID, 
         new DataHandler(new ByteArrayDataSource(
         MtomTestHelper.RESP_PHOTO_DATA, "application/octet-stream")));

      exchange.getOut().addAttachment(MtomTestHelper.RESP_IMAGE_CID, 
         new DataHandler(new ByteArrayDataSource(
         MtomTestHelper.responseJpeg, "image/jpeg")));
    }
}

Message Mode: Attachments are not supported as it does not process the message at all.