Web Services Attachment with MTOM

Intro

I was preparing for a blog about binary attachment using web services. Just this week, I had to write a client code to help one of my co-workers test uploading binary data via Document Content Web Services for his newly installed I/PM framework. What a coincidence!

This post shows how you can upload and download attachment via web services using the SOAP Message Transmission Optimization Mechanism/XML-binary Optimized Packaging (MTOM/XOP) specification. MTOM/XOP defines a method for optimizing the transmission of XML data of type xs:base64Binary or xs:hexBinary in SOAP messages.

Project Setup and Code

Let’s create a project for the web services in JDeveloper first. We will name it FaMtomAttachment. Click on File menu –> select New –> under General select Projects –> select Generic Project –> click OK button.
Create Project

Let’s add the interface and implementation for the web services in its appropriate packages. Click on File menu –> select New –> under General select Java –> select Java Interface –> click OK button. Repeat same steps but select Java Class this time. MTOM support is standard in JAX-WS. Enabling server to send attachment via MTOM is fairly straight forward, use JWS annotation of javax.xml.ws.soap.MTOM in implementation class.

Interface

Now that we have defined 2 methods in the interface, attachFile() and downloadFile(), here is how the implementation look like.
File upload code:

File download code is rather simple:

Deployment

Once we are done deploying the services, we will test those from JDeveloper proxy project and the soapUI tool. Use JDeveloper to create WAR and EAR profiles and use Deployment Wizard to deploy the application. To create WAR file; right click on the FaMtomAttachment project –> select Project Properties –> select Deployment –> click New button –> select WAR File as Archive Type –> type FaMtomAttachment as Name –> click OK button. For the sake of simplicity Specify Java EE Web Context Root as faws. Click OK twice to exit the dialog and this will create FaMtomAttachment.war deployment profile.

To create EAR file; click Application menu –> select Application Properties –> select Deployment –> click New button –> select EAR File as Archive Type –> type FaMtomAttachment as Name –> click OK button. Select Application Assembly –> select FaMtomAttachment project –> click OK twice and this will create FaMtomAttachment.ear deployment profile.

Using JDeveloper deploy the FaMtomAttachment application. Click Application menu –> select Deploy –> select FaMtomAttachment application –> run through the deployment wizard and click Finish.

Nice! now you see reference to MTOM policy in the deployed services. Screen shots below.

wsdl

Testing Setup

Create a proxy project to test the services now. Click on File menu –> select New –> under General select Projects –> select Generic Project –> click OK button. Name this project FaMtomAttachmentProxy. Click File –> New –> select Web Services under Business Tier –> select Web Service Proxy –> go through proxy creation wizard to populate the supporting types for testing the services. We will add our test code in the generated FaWsAttachmentServiceImplPortClient class.
Create Proxy
Test Client

Testing Output

Below you see that the file system is empty at first, ie /home/oracle/download/. To run the client right click on source editor for FaWsAttachmentServiceImplPortClient.java and select Run. The results below show successful attachment of a binary file utilizing MTOM from exposed operations.
Test client code:

Empty File System
After Upload

Testing Output from soapUI tool

SoapUI Upload
SoapUI Upload Result
SoapUI Download
SoapUI Download Result

And that is how we implement web services with attachment.

Sanjib

Director at Fusion Applied
Sanjib is an Oracle Middleware Architectwith over 15 years' experience developing software and managing projects in challenging, fast-paced consulting environments. An experienced OO developer with expertise in J2EE and Oracle Fusion Middleware technologies. His current focus is on helping customers build solutions using the Oracle Fusion Middleware stack including Oracle SOA Suite 11g, BPM, ADF & WebCenter Suite.
Sanjib

Leave a Reply

avatar
  Subscribe  
Notify of