Over a million developers have joined DZone.

SOAP & REST Attachments in Mule

DZone's Guide to

SOAP & REST Attachments in Mule

· Integration Zone
Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

[This article was written by Albin Kjellin.]

I was recently working on a project where we had to handle SOAP attachments. Working with SOAP attachments is the kind of thing that you work on every 3-5 years and then 10 seconds after you are done you forget all about them. All the information required is available in our docs but it can still be good to have a complete end to end example as a reference. Esteban Robles Luna’s (former MuleSoft colleague) blogpost, Working with SOAP attachments using Mule’s CXF module, from 2011 was most helpful.

When working on this project I also wanted to see if how this could be implemented using RAML and REST services instead of SOAP. When researching the topic it seems like there is no complete consensus for how to do this, but I found this discussion – How do I upload a file with metadata using a REST web service? – quite interesting.

The use case is very straightforward, sending and receiving a PDF file as a SOAP attachment and as a REST attachment. The application has four different flows:

  • Read a PDF file from disc and then add it as a SOAP attachment to the request.
  • Expose a SOAP service that is capable of receiving a SOAP request with an attachment.
  • Read a PDF file from disc and add that as an attachment to a REST call.
  • Expose a REST service using APIkit and RAML that is able to handle a request with an attachment.

1. SOAP Attachment – Client

This is probably the most complicated flow since it requires some 4 lines of code in order to create the attachment and uses the CXF module to configure the client piece. Trigger the flow by copying the file src/test/resources/esb.pdf to the folder src/test/resources/soap/attachment/in. The configuration file can be found here:

<flow name="file2soap" doc:description="Reads a file and sends that as a SOAP attachment to a SOAP service.">
<file:inbound-endpoint path="src/test/resources/soap/attachment/in" responseTimeout="10000" doc:name="Read File" />
<processor-chain doc:name="Processor Chain">
<scripting:transformer doc:name="Create SOAP Attachement">
<scripting:script engine="Groovy"><![CDATA[def attachment = new org.apache.cxf.attachment.AttachmentImpl(originalFilename)
def source = new org.apache.axiom.attachments.ByteArrayDataSource(payload.getBytes(),'application/pdf');
attachment.setDataHandler(new org.apache.axiom.attachments.ConfigurableDataHandler(source));
return payload

<set-payload value="#[['FirstName', 'LastName', '123'].toArray()]" doc:name="Create Payload Map" />
<cxf:jaxws-client operation="contact" serviceClass="org.mule.demo.soap.Contact" doc:name="SOAP Client">
<spring:bean class="org.mule.module.cxf.support.CopyAttachmentOutInterceptor" />
<http:request config-ref="SOAP-Service" path="contacts" method="POST" doc:name="Call SOAP Service" />

2. SOAP Attachment – Server

This flow exposes a SOAP web service and retrieves the SOAP attachment from the request and writes that to disk. The service is triggered when triggering the client, if you want to test it it individually just use SOAP UI and point it to the endpoint (http://localhost:8883/contacts). The configuration file for this web service can be found here:

<http:listener-config name="SOAP-in" host="" port="8883" doc:name="HTTP Listener Configuration" />

<flow name="soap2file">
<http:listener config-ref="SOAP-in" path="/contacts" doc:name="Receive SOAP Request" parseRequest="false" />
<cxf:jaxws-service serviceClass="org.mule.demo.soap.Contact" doc:name="Parse SOAP Request">
<spring:bean class="org.mule.module.cxf.support.CopyAttachmentInInterceptor" />
<choice doc:name="Choice">
<when expression="#[flowVars.containsKey('cxf_attachments')]">
<set-payload value="#[cxf_attachments.iterator().next().getDataHandler().getContent()]" doc:name="Retrive Attachment" />
<file:outbound-endpoint path="src/test/resources/soap/attachment/out" outputPattern="#[server.dateTime.toString()].pdf" responseTimeout="10000" doc:name="Write File to Disc" />
<logger message="********************** No SOAP Attachement Found! **********************" level="INFO" doc:name="Log Missing Attachment" />

<set-payload value="Success" doc:name="Generate Response" />

3. REST Attachment – Client

This implementation is very straightforward, just use the attachment message processor and the outbound HTTP call to make the call. You can trigger the flow by copying the file src/test/resources/esb.pdf to the folder src/test/resources/rest/attachment/in. The configuration file for this can be found here:

<http:request-config name="HTTP_Request_Configuration" host="localhost" basePath="api" port="8884" doc:name="HTTP Request Configuration"/>
<flow name="file2rest" doc:description="Reads a file from your desktop and sends that to a rest service.">
<file:inbound-endpoint path="src/test/resources/rest/attachment/in" responseTimeout="10000" doc:name="File"/>
<file:file-to-byte-array-transformer doc:name="File to Byte Array"/>
<set-attachment attachmentName="#[originalFilename]" value="#[payload]" contentType="multipart/form-data" doc:name="Attachment"/>
<http:request config-ref="HTTP_Request_Configuration" path="contact/abc/datasheet" method="POST" doc:name="HTTP" parseResponse="false"/>

4. REST Attachment – Server

The REST service accepting is autogenerated using a RAML file that can be found here. The API has some additional methods not used in this example. The example can be triggered by the client described in step 3 by copying the file to the right folder or by any REST console that can take an attachement by posting to this URL: http://localhost:8884/api/contact/abc/datasheet. The configuration for this API can be found here:

<flow name="main">
<http:inbound-endpoint address="http://localhost:8884/api" doc:name="HTTP" exchange-pattern="request-response" />
<apikit:router config-ref="apiConfig" doc:name="APIkit Router" />

<flow name="post:/contact/{contactId}/datasheet:apiConfig">
<set-payload value="#[message.inboundAttachments]" doc:name="Retrieve Attachments"/>
<foreach doc:name="For Each">
<set-payload value="#[payload.getInputStream() ]" doc:name="Get Inputstream from Payload"/>
<file:outbound-endpoint path="src/test/resources/rest/attachment/out" responseTimeout="10000" doc:name="File" outputPattern="#[server.dateTime.toString()].pdf"/>
<set-payload value="{"status":"success"}" doc:name="Generate JSON Response" />

The complete project is available in github:

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

enterprise-integration ,web services ,soap ,rest ,integration ,mule

Published at DZone with permission of Ross Mason, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}