Blog Banner Image

Message Transmission Optimization Mechanism (MTOM)

Web services mainly transmit data in the text form, but with the increasing use and wide scope of web services, they require an elegant handling of binary data transmission.

Message Transmission Optimization Mechanism (MTOM) is a method of efficiently sending binary data to and from Web services.

MTOM provides a mechanism of transmitting binary data, such as images, PDF files, MS Word documents, between systems.

Following diagram shows the steps involved in transmitting data between a Consumer and Producer using MTOM.

Untitled

Step1: The Consumer application begins by sending a SOAP Message that contains complex data in Base64Binary encoded format. Base64Binary data type represents arbitrary data (e.g., Images, PDF files, Word Docs) in 65 textual characters that can be displayed as part of a SOAP Message element.

For the Send SOAP Message Step 1 in the Figure above, a sample SOAP Body with Base64Binary encoded element <tns:data> looks like:

<soap:Body>
<tns:ByteEcho>
<tns:data>JVBERi0xLjYNJeLjz9MNCjE+DQpzdGFyNCjEx0YNCg==</tns:data>
</tns:ByteEcho>
</soap:Body>

Step2: An MTOM-aware web services engine detects the presence of Base64Binary encoded data types, <tns:data> in our example, and makes a decision – typically based on data size – to convert the Base64Binary data to MIME data with an XML-binary Optimization Package (xop) content type. The data conversion, shown in Step 2 of the Figure above, results in replacing the Base64Binary data with an <xop:Include> element that references the original raw bytes of the document being transmitted. The raw bytes are appended to the SOAP Message and are separated by a MIME boundary as shown below:

<soap:Envelope>
<soap:Body>
<tns:ByteEcho>
<tns:data><xop:Include href=”cid:1.633335845875937500@example.org”/></tns:data>
</tns:ByteEcho>
</soap:Body>
</soap:Envelope>
–MIMEBoundary000000
content-id: <1.633335845875937500@example.org>
content-type: application/octet-stream
content-transfer-encoding: binary

The raw binary data along with the SOAP Message and the MIME Boundary is transmitted over the wire to the Producer. The Producer then changes the raw binary data back to Base64Binary encoding for further processing. With this conversion between Base64Binary and raw binary MIME types, MTOM provides two significant advantages:

  1. Efficient Transmission: Base64Binary encoded data is ~33% larger than raw byte transmission using MIME. MTOM therefore reduces data bloat by converting Base64Binary encoding to raw bytes for transmission.
  1. Processing Simplicity: Base64Binary encoded data is composed of 65 textual characters. The data is represented within an element of a SOAP message. Security standards such as WS-Signatures and WS-Encryption can directly be applied to the SOAP Message. Once such operations are performed, the Base64Binary data can be converted to raw bytes for efficient transmission. Securing document transmission via SOAP, therefore, does not require additional standards for securing MIME-based attachments.

Through electronic transmission of documents, corporations can realize significant cost savings and better service levels by eliminating the use of postal mail. Paper-based manual tasks can be replaced with simple and efficient electronic processes where binary data can be transmitted between organizations through standards such as MTOM.

Let’s talk about how we can enable MTOM in JAX-WS web service.

Enabling MTOM for JAX-WS Web services

With Java API for XML-Based Web Services (JAX-WS), you can send binary attachments such as images or files along with Web services requests. JAX-WS adds support for optimized transmission of binary data as specified by the SOAP Message Transmission Optimization Mechanism (MTOM) specification.

JAX-WS supports the use of SOAP Message Transmission Optimized Mechanism (MTOM) for sending binary attachment data. By enabling MTOM, you can send and receive binary data optimally without incurring the cost of data encoding needed to embed the binary data in an XML document.

The application server supports sending attachments using MTOM only for JAX-WS applications. This product also provides the ability to provide attachments with Web services security SOAP messages by using the new MTOM and XOP standards.

JAX-WS applications can send binary data as base64 or hexBinary encoded data contained within the XML document. However, to take advantage of the optimizations provided by MTOM, enable MTOM to send binary base64 data as attachments contained outside the XML document. MTOM optimization is not enabled by default. JAX-WS applications require separate configuration of both the client and the server artifacts to enable MTOM support. For the server, you can enable MTOM on a JavaBeans endpoint only and not on endpoints that implement the javax.xml.ws.Provider interface.

Enable MTOM on Server Side (JavaBeans endpoint)-

To enable MTOM on an endpoint, use the @MTOM (javax.xml.ws.soap.MTOM) annotation on the endpoint. The @MTOM annotation has two parameters, enabled and threshold. The enabled parameter has a boolean value and indicates if MTOM is enabled for the JAX-WS endpoint. The threshold parameter has an integer value, and it specifies the minimum size for messages that are sent using MTOM. When the message size is less than this specified integer, the message is inlined in the XML document as base64 or hexBinary data.

Additionally, you can use the @BindingType (javax.xml.ws.BindingType) annotation on a server endpoint implementation class to specify that the endpoint supports one of the MTOM binding types so that the response messages are MTOM-enabled. The javax.xml.ws.SOAPBinding class defines two different constants, SOAP11HTTP_MTOM_BINDING and SOAP12HTTP_MTOM_BINDING that you can use for the value of the @BindingType annotation.

For example:

// for SOAP version 1.1

@BindingType(value = SOAPBinding.SOAP11HTTP_MTOM_BINDING)

// for SOAP version 1.2

@BindingType(value = SOAPBinding.SOAP12HTTP_MTOM_BINDING)

Enable MTOM on Client Side-

Using either the javax.xml.ws.soap.SOAPBinding or the javax.xml.ws.soap.MTOMFeature APIs. Enabling MTOM on the client optimizes the binary messages that are sent to the server.

1) Enable MTOM on a Dispatch client. The following example uses SOAP version 1.1:

– First method: Using SOAPBinding.setMTOMEnabled()

SOAPBinding binding = (SOAPBinding)dispatch.getBinding();

binding.setMTOMEnabled(true);

– Second method: Using Service.addPort where you specify:

Service svc = Service.create(serviceName); svc.addPort(portName,SOAPBinding.SOAP11HTTP_MTOM_BINDING,endpointUrl);

– Third method: Using MTOMFeature:

MTOMFeature mtom = new MTOMFeature();

Service svc = Service.create(serviceName);

svc.addPort(portName, SOAPBinding.SOAP11_HTTP_BINDING, endpointUrl);

Dispatch<Source> dsp = svc.createDispatch(portName, Source.class, Service.Mode.PAYLOAD, mtom);

2) Enable MTOM on a Dynamic Proxy client.

// Create a BindingProvider bp from a proxy port.

Service svc = Service.create(serviceName);

MtomSample proxy = svc.getPort(portName, MtomSample.class);

BindingProvider bp = (BindingProvider) proxy;

//Enable MTOM using the SOAPBinding

MtomSample proxy = svc.getPort(portName, MtomSample.class);

BindingProvider bp = (BindingProvider) proxy;

SOAPBinding binding = (SOAPBinding) bp.getBinding(); binding.setMTOMEnabled(true);

//Or, enable MTOM with the MTOMFeature

MTOMFeature mtom = new MTOMFeature();

MtomSample proxy = svc.getPort(portName, MtomSample.class, mtom);

Once we are done with enabling MTOM on server and client side, we are ready to transfer the binary data with our web service.

There are so many practical scenarios where we need the binary data transfer in the industries like Medical, Financial, Retail etc. I used binary data transfer in one of the major insurance group which provides insurance to small and medium size businesses and insurer was supposed to embed the business logo of the client in each and every communication they sent to the client. Each business logo (image) was stored in the DB and was required to be fetched by a web service while creating any sort of communication like payment reminder, monthly bill, change of policy terms etc. This is just an example while the real scenarios may be countless.