DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • API Development - Glossaries
  • Why Is REST API Architecture Gaining Popularity in the Digital Industry?
  • WSDL vs REST/JSON — The Troll Fight
  • The Evolution of Systems Integration

Trending

  • Best Practices for Writing Clean Java Code
  • Spring WebFlux Retries
  • Exploring Edge Computing: Delving Into Amazon and Facebook Use Cases
  • Causes and Remedies of Poison Pill in Apache Kafka
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. SOAP & REST Attachments in Mule

SOAP & REST Attachments in Mule

Ross Mason user avatar by
Ross Mason
·
May. 07, 15 · Interview
Like (0)
Save
Tweet
Share
12.51K Views

Join the DZone community and get the full member experience.

Join For Free

[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

SOAP Web Protocols REST

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

Opinions expressed by DZone contributors are their own.

Related

  • API Development - Glossaries
  • Why Is REST API Architecture Gaining Popularity in the Digital Industry?
  • WSDL vs REST/JSON — The Troll Fight
  • The Evolution of Systems Integration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: