How to do MTOM Interop

In my JavaOne session yesterday I showed (what I believe to be) the first MTOM Interop demo between .NET and Java using publicly available toolkits.  For those that don't know MTOM (Message Transmission Optimization Mechanism) is the new specification for optimizing the transmission and/or wire format of SOAP messages.  Primarily this means that we have a new standard that allows the sending of attachments over Web Services - one that the industry agrees on, and one that is composable with the other WS-* specifications.

 

The demo showed WSE 3.0 CTP (which can be downloaded here) returning a PowerPoint deck to a Java client using JAX-WS EA 2.0 (which can be downloaded here).  This is how it works:

 

The WSE service defines a method called GetFileBinary:

 

[WebMethod]

public byte[] GetFileBinary(String fileName)

{
String filePath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\" + fileName;

  return File.ReadAllBytes(filePath);

}

 

As you can see the method returns an array of bytes.  To enable MTOM encoding we use the following switch in the web.config file:

 

<mtom clientMode="On" />

 

When we call the method the binary representation of the attachment appears within the body of the SOAP response (although actually this is an abstract representation of the multiple MIME messages that are physically sent on the wire). 

 

So, how do we call this in Java?  Well, first we create a new instance of the service using JAX-WS EA 2.0 and call the GetFileBinary method:

 

ServiceFactory serviceFactory = ServiceFactory.newInstance();

Service service = (Service)serviceFactory.createService(new URL("https://localhost:8080/dotNETWS/Service.asmx?WSDL"), Service.class);

ServiceSoap soap = service.getServiceSoap();

SOAPBinding binding = (SOAPBinding)((BindingProvider)soap).getBinding();

binding.setMTOMEnabled(true);

 

Notice how we also set the MTOMEnabled flag on the binding of the service.  When the method is called, an array of bytes is returned.  We simply output to a FileOutputStream:

 

byte[] fileData = soap.getFileBinary("JavaOne.ppt");

FileOutputStream fos = new FileOutputStream("C:\\Documents and Settings\\Administrator\\Desktop\\JavaOne.ppt");

fos.write(fileData);

fos.flush();

fos.close();

 

..and there we have the attachment!  Anyone that has done anything with SwA or DIME interop in the past will hopefully recognize the progress that we are making in this area - testament I believe to how Microsoft and Sun has been working together on these specifications.  From a presenter's point of view, it was also nice to see how easy this demo was to put together (even if it was done just a few hours before my presentation ;-)

 

What's next?  I am working with Arun Gupta from Sun to formally write up the demo into a whitepaper and sample download that we'll post on both sites.  After that we'll be working on showing Secure MTOM (WS-Security + MTOM) to demonstrate how attachments can be sent securely between Web Service endpoints.