Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle "multipart/related" in java servlet

A Servlet, running under Jetty 8, receives the following request:

Header:
Content-Type = multipart/related; boundary=example

Data:

--example
content-type: text/xml; charset=UTF-8

data1here

--example
content-type: text/xml; charset=UTF-8

data2here

--example--
  • Is there a convenient way of getting "data1here" and "data2here" from this kind of a request?
  • Do Java servlets support it, natively?
  • Or are there any additional libraries that support it?
like image 309
user1985273 Avatar asked Jan 29 '23 16:01

user1985273


1 Answers

Consumes Annotation

Consume the event using a JAX-RS annotation provided by Apache CXF:

@Consumes("multipart/related")

From the JAX-RS documentation:

It is now possible (since 2.2.5) to have individual multipart/form-data parts read by registered JAX-RS MessageBodyReaders, something that is already possible to do for types like multipart/mixed or multipart/related.

See also:

  • Spring boot multipart/related mime type support
  • Jersey: How to register MultiPartConfigProvider class

Note that Jersey, which is used by GlassFish, has no mention of related in its source code.

HTTP Client

Google offers an API for an HTTP client that parses multipart/related messages as per the RFC.

RESTeasy

The RESTeasy project can parse multipart/related content via JAX-RS.

JavaMail API

With some stream contortions, it may be possible to use the JavaMail API to parse a MimeMultipart message:

The default multipart subtype is "mixed". The other multipart subtypes, such as "alternative", "related", and so on, can be implemented as subclasses of MimeMultipart with additional methods to implement the additional semantics of that type of multipart content.

The JavaMail FAQ offers a few more details:

As described above, there are more complex cases to consider as well. In particular, messages may have arbitrary nesting of multipart/mixed and multipart/alternative parts and may include multipart/related parts for embedded HTML and multipart/signed and/or multipart/encrypted parts for secure messages.

I'd advise against this approach because it mixes metaphors (it conflates MIME over HTTP/web with MIME over SMTP/mail). That is, JavaMail, conceptually, is used for reading and writing messages over SMTP/IMAP, which could leave future maintainers wondering why JavaMail is being used to parse MIME messages received via Servlets, especially when there are annotation-based solutions available. That said, documenting the code with the reason for its usage would be a way to avoid any confusion.

Container

Depending on the container, it may or may not handle all pertinent RFCs. You may have to try (or peruse the source code of) different containers to see what ones implement this feature.

Jetty

The source code has a few spots related to parsing input streams, which are limited to multipart/form-data:

  • MultiPartFilter
  • MultiPartInputStreamParser

Additionally, the unit tests do not include RFC 2387, which is a strong indicator that the container does not handle related parts under the Servlet 3.0 API. As such, JAX-RS is probably the best approach.

Tomcat

Tomcat has not implemented multipart/related as part of the Servlet 3.0 specification, although there exists a patched version of Tomcat 7.0.47 that does.

like image 60
Dave Jarvis Avatar answered Feb 02 '23 11:02

Dave Jarvis