Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

SOAP & REST Attachments in Mule

DZone's Guide to

SOAP & REST Attachments in Mule

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

[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));
message.setInvocationProperty('cxf_attachments',[attachment])
return payload
]]></scripting:script>
</scripting:transformer>

<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">
<cxf:outInterceptors>
<spring:bean class="org.mule.module.cxf.support.CopyAttachmentOutInterceptor" />
</cxf:outInterceptors>
</cxf:jaxws-client>
<http:request config-ref="SOAP-Service" path="contacts" method="POST" doc:name="Call SOAP Service" />
</processor-chain>
</flow>

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="0.0.0.0" 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">
<cxf:inInterceptors>
<spring:bean class="org.mule.module.cxf.support.CopyAttachmentInInterceptor" />
</cxf:inInterceptors>
</cxf:jaxws-service>
<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" />
</when>
<otherwise>
<logger message="********************** No SOAP Attachement Found! **********************" level="INFO" doc:name="Log Missing Attachment" />
</otherwise>
</choice>

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

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"/>
</flow>

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>

<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"/>
        </foreach>
<set-payload value="{"status":"success"}" doc:name="Generate JSON Response" />
</flow>

The complete project is available in github:
https://github.com/albinkjellin/soap-rest-attachments

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}