MTOM With JAX-WS

Hi All, what if you want to send large binary data using web services we have SOAP with attachments i.e. MTOM(Message Transmission Optimization Mechanism).SOAP Message Transmission Optimization Mechanism (MTOM) specifies an optimized method for sending binary data as part of a SOAP message. Unlike SOAP with Attachments, MTOM requires the use of XML-binary Optimized Packaging (XOP) packages for transmitting binary data. Using MTOM to send binary data does not require you to fully define the MIME Multipart/Related message as part of the SOAP binding. It does, however, require that you do the following:

1. Annotate the data that you are going to send as an attachment.
2. You can annotate either your WSDL or the Java class that implements your data.
3. Enable the runtime’s MTOM support.
4. This can be done either programatically or through configuration.
5. Develop a DataHandler for the data being passed as an attachment.

below is the code snippet for MTOM implementation, follow the comments closely in the code.

/**
 * 
 */
package com.webservice.endpoint.interfaces;

import java.awt.Image;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 * @author Sony
 *
 */
@WebService
@SOAPBinding(style = Style.RPC)
public interface BinaryDataDistribution {

	@WebMethod
	public Image downloadBinaryData(String name);
	
	@WebMethod
	public String uploadBinaryData(Image data);
}
/**
 * 
 */
package com.webservice.endpoint.interfaces.impl;

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.MTOM;

import com.webservice.endpoint.interfaces.BinaryDataDistribution;

/**
 * @author Sony
 * 
 */
@MTOM
@WebService(endpointInterface = "com.webservice.endpoint.interfaces.BinaryDataDistribution")
public class BinaryDataDistributionImpl implements BinaryDataDistribution {

	
	 // (non-Javadoc)
	  
	 // @see
	 // com.webservice.endpoint.interfaces.BinaryDataDistribution#downloadBinaryData
	 // (java.lang.String)
	 
	@Override
	@WebMethod
	public Image downloadBinaryData(String name) {
		try {

			File image = new File("c:\\images\\" + name);
			return ImageIO.read(image);

		} catch (IOException e) {

			e.printStackTrace();
			return null;

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.webservice.endpoint.interfaces.BinaryDataDistribution#uploadBinaryData
	 * (java.awt.Image)
	 */
	@Override
	@WebMethod
	public String uploadBinaryData(Image data) {
		if (data != null) {
			// saving logic goes here !
			return "Upload Successful";
		}

		throw new WebServiceException("Upload Failed!");
	}

}
/**
 * 
 */
package com.webservice.publish;

import javax.xml.ws.Endpoint;

import com.webservice.endpoint.interfaces.impl.BinaryDataDistributionImpl;

/**
 * @author Sony
 * 
 */
public class PublishService {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:9999/ws/image",
				new BinaryDataDistributionImpl());

		System.out.println("Server is published!");
	}

}

Now the Wsdl should look as below.

  <?xml version="1.0" encoding="UTF-8" ?> 
- <!--  Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
  --> 
- <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
  --> 
- <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.interfaces.endpoint.webservice.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.interfaces.endpoint.webservice.com/" name="BinaryDataDistributionImplService">
  <import namespace="http://interfaces.endpoint.webservice.com/" location="http://localhost:9999/ws/image?wsdl=1" /> 
- <wsp:Policy wsu:Id="BinaryDataDistributionImplPortBinding_MTOM_Policy">
  <ns1:OptimizedMimeSerialization xmlns:ns1="http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization" wsp:Optional="true" /> 
  </wsp:Policy>
- <binding xmlns:ns2="http://interfaces.endpoint.webservice.com/" name="BinaryDataDistributionImplPortBinding" type="ns2:BinaryDataDistribution">
  <wsp:PolicyReference URI="#BinaryDataDistributionImplPortBinding_MTOM_Policy" /> 
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" /> 
- <operation name="downloadBinaryData">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" namespace="http://interfaces.endpoint.webservice.com/" /> 
  </input>
- <output>
  <soap:body use="literal" namespace="http://interfaces.endpoint.webservice.com/" /> 
  </output>
  </operation>
- <operation name="uploadBinaryData">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" namespace="http://interfaces.endpoint.webservice.com/" /> 
  </input>
- <output>
  <soap:body use="literal" namespace="http://interfaces.endpoint.webservice.com/" /> 
  </output>
  </operation>
  </binding>
- <service name="BinaryDataDistributionImplService">
- <port name="BinaryDataDistributionImplPort" binding="tns:BinaryDataDistributionImplPortBinding">
  <soap:address location="http://localhost:9999/ws/image" /> 
  </port>
  </service>
  </definitions>

Happy coding 🙂 “MTOM

Advertisements

One thought on “MTOM With JAX-WS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s