The SOAP Message Transmission Optimization Mechanism (MTOM) is a W3C recommendation that replaces DIME and WS-Attachments as the mechanism for sending large amounts of binary data. MTOM enables the sending of binary data more efficiently as part of a SOAP message.
![]() | Important |
---|---|
|
There are many key benefits to using MTOM over the existing technologies:
Dimimished Raw Message Size With MTOM enabled, binary data are sent on the wire as a MIME attachments. These are referenced from the SOAP body of the SOAP message. The consequential base64-encoded data has a length 1/3 greater than the original binary data.
Handlers and Security MTOM composes with WSO2 SOA Enablement Server for Java handlers and WS-Security because it provides MTOM content to be visible as base64 encoded text. The data is so secure as well as the SOAP message. With other attachment technologies the attachments are not secure unless you used transport-lever security.
First, let's look at a simple Web service that echoes string parameter as MTOM:
Example 247. String Parameter Echoed as MTOM
package service; import org.systinet.wasp.webservice.Current; import org.systinet.wasp.webservice.CallContext; public class MtomAsByteArrayService { public byte[] echo( String s ) { Current.getCallContext() .getContextData() .put( CallContext.USE_MTOM , "always" ); // force MTOM return s.getBytes(); // return binary representation } }
This service expects a string and returns its binary representation to the client. Before the binary representation is returned, MTOM is forced to be used instead of base64Binary content.
![]() | Note |
---|---|
There are four acceptable MTOM modes: auto, always, always-including-faults, and never:
|
Next, we'll look at the client-side code to call this MTOM-enabled service:
Example 248. Client-Side Code for Calling MTOM-Enabled Service
package client; import org.systinet.wasp.webservice.Registry; public class Client { private static final String SERVER_URL = "http://localhost:6060"; private static final String SERVICE_PATH = "/MtomAsByteArray"; private static final String SERVICE_WSDL = SERVER_URL + SERVICE_PATH + "/wsdl"; public static void main ( String[] args ) throws Exception { // lookup service MtomAsByteArrayService service = (MtomAsByteArrayService ) Registry.lookup( SERVICE_WSDL , MtomAsByteArrayService.class ); // invoke method byte[] mtomResponse = service.echo( "hello" ); // print result to console System.out.println( new String( mtomResponse ) ); } }
Example 249 shows the sample response message body content that goes back to the client:
Example 249. Client-Side Code for Calling MTOM-Enabled Service
------------MULTIPART_BOUNDARY_10760a9f0281---------- Content-Transfer-Encoding: binary Content-Type: application/xop+xml; type="text/xml"; charset=UTF-8 X-WASP-Message-ID: 11e-FLxtbx/tDgWbei5OeHEwbw== Content-ID: <133749a0532da12c-6e6723724546f190-abd79665de154d4-9864ea6e6be49004> <?xml version="1.0" encoding="UTF-8"?> <e:Envelope xmlns:e="http://schemas.xmlsoap.org/soap/envelope/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:wn0="http://systinet.com/xsd/SchemaTypes/" xmlns:wn1="http://idoox.com/interface"> <e:Body> <wn0:base64Binary_Response> <n0:Include href="cid:133749a0532da12c-6e6723724546f190-abd79665de154d4-9864ea6e6be49003" xmlns:n0="http://www.w3.org/2004/08/xop/include"/> </wn0:base64Binary_Response> </e:Body> </e:Envelope> ------------MULTIPART_BOUNDARY_10760a9f0281---------- Content-Transfer-Encoding: binary Content-Type: application/octet-stream Content-Id: <133749a0532da12c-6e6723724546f190-abd79665de154d4-9864ea6e6be49003> hello ------------MULTIPART_BOUNDARY_10760a9f0281------------