How does MTOM work + sample code

zengr picture zengr · Apr 12, 2010 · Viewed 30.8k times · Source

I am trying to make a very simple web-service which does the following:

  1. The client hits the web service requesting a file.
  2. The web service's service class queries a hashtable which has the key (search query) and the value as the base64encoded value of a file (say a pdf)
  3. Now,I need to use MTOM to return the base64encoded value stored in the hashtable to the client.

It's upto the client to decode it and convert it to pdf.

So, here are my questions:

  1. I understand we encode files to base64 for transmission via web service, but where and how does MTOM come into the picture there?

  2. Can some one provide me a simple method which uses MTOM and sends the data back. Do we need to specify something in the WSDL too? or a simple String return type would suffice? Why/Why not?

Thanks

I have seen this code. It uses a lot of annotations, I just need a simple java code using MTOM. New to J2EE HERE :)

UPDATE 1

Decent enough explanation here, now I need to understand it from the implementation level. Good info here, but for .Net

UPDATE 2

What should be the return type of the method "createMTOM(int id)"??

Answer

bmargulies picture bmargulies · Apr 12, 2010

You seem to be mixing up implementation and protocol.

The specification for MTOM is here. This page has sample messages.

If you are using a JAX-WS environment, you most assuredly use Annotations to turn it on. If you are rolling soap messages by hand, you write whatever Java code you need to write to produce it. Essentially, MTOM means that the content of the data move to an attachment, which is a separate MIME part of the message, and is referenced through an xop:Include element with an href to the MIME part.

The schema has to say base64Binary as the element type, but additional attributes give hints as to the interpretation of the bytes: xmime:contentType and xmime:expectedContentType. In my experience, the JAX-B reference implementation has some limitations on the use of these. You can't, for example, get it to automatically convert text back and forth to bytes. In a typical kit like CXF or Glassfish, you put data into a DataHandler and the kit turns it into the attachment.