MTOM/XOP  Locate

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]Important

  • WSO2 SOA Enablement Server for Java MTOM implementation does not support rpc/encoded.

There are many key benefits to using MTOM over the existing technologies:

MTOM Examples  Locate

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]Note

There are four acceptable MTOM modes: auto, always, always-including-faults, and never:

  • auto (the default) means the service will respond according to the return type. If the returned object tree contains byte array(s) only, base64Binary content will be emited. If the returned object tree contains MtomMessageAttachment(s), an MTOM message will be produced.

  • always demands all outgoing messages to be MTOM also in cases when there is no base64 binary element in the outgoing message.

  • always-including-faults demands all outgoing messages including faults to be MTOM also in cases when there is no base64 binary element in the outgoing message. (Use this option e.g. when exchanging messages with WSE 3.0 MTOM enabled service that is in always mode).

  • never demands all outgoing messages will never be MTOM. (Use this option e.g. when exchanging messages with WSE 3.0 MTOM disabled service that is in never mode).

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------------