Wednesday, August 30, 2006

sending/receiving files with JAX-RPC & Axis 1.3

Use javax.activication.DataHandler
because it can stream: reduces memory usage (don't have to hold file in core), and is faster (can start to send/receive before whole object is created/read).

MIME TYPE: versus MIME Type
MIME is our standard for encoding DataHandler streams. The actual over-the-wire XML is a multipart MIME message, but unlike one might have thought, the SOAP response is one attachment and the attachment is another attachment, rather than the file being an attachment to the SOAP message.

In any event, Axis 1.3 doesn't handle it, invents its own DataHandler type instead. No good for non-Axis clients.

The Transferable interface, spot the difference:
1.new DataHandler(new String("foo"), "text/csv"))
new DataHandler(new String("foo"), "text/plain"))

This is silly. The client code chooses the object to decode the data handler based on its MIME type, not the type of the object that it was created with. These are both Strings and could be de-serialized in exactly the same way. But noooo, a built-in handler exists for text/plain (String), but not for text/csv.

I ended up using
new DataHandler(new org.apache.commons.mail.ByteArrayDataSource(os.toByteArray(), "text/csv"));

even though this doesn't really take advantage of the streaming capability or reduce the memory requirements. Should really make an inputstream out of the outputstream, and stream the stream.

No comments: