Hi


RECEIVING (client -> server)

What's the conventional wisdom on how best to let somebody send (upload) a file in a REST application (one built on JSR-311 annotations, in particular)? Seems like you could just let them PUT or POST the raw data, as an application/octet-stream... Is that better/worse than something that uses multipart/mixed? Need it be base-64 encoded? Anyway, looking for a best practice, if you will. How's this generally handled?

Uploading multiparts is also supported, see

http://cxf.apache.org/docs/jax-rs.html#JAX-RS-SupportforMultiparts

Using multipart/mixed will be more efficient for uploading large attachments, as user-configured temp directories can be used to store them...

If it is not a binary content then you can tell CXF what is the content type of individual types and it will use a registered JAXRS MessageBodyReader to read it.

I haven't actually tested uploading base64-encoded parts, but CXF JAXRS just relies on the core runtime to handle it all so I presume it will just work - but may be there's no need to do base64 - it won't protect anyway so just use HTTPs if needed and you can expect some serious message-level security support from CXF in the longer term.


SENDING (server -> client)

On the flip side (sending a file back to the user when they GET the corresponding resource), is there anything you can do other than tag it as "application/octet-stream" (i.e. can you cause the right MIME type to be used)? Seems like the code has to advertise that it @Produces something specific, in which case application/octet-stream seems the most generic/safe. Not sure how you would determine (and alter) the MIME type on the fly -- especially since the data at the given resource could be any kind of file (in my case, the resource points to an arbitrary, previously-uploaded file).

See http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Writingattachments for a number of options on how you can specify types for indiv parts when writing multiparts. So one option is to use CXF MultipartRequest object on teh receive side which will let you to crceate a map of types-to-filenames (ex, when uploading from the form) and then when a user requests GET on some file you can dynamically set a content type for an outbound multipart/mixed root part, etc

cheers, Sergey


Thanks!


Reply via email to