Handle multipart / related in a Java servlet

A servlet running on 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 to get the data "data1here" and "data2here" from this type of request?
  • Do they support Java servlets natively?
  • Or are there any additional libraries that support it?
+5
source share
2 answers

requires annotation

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

 @Consumes("multipart/related") 

From the JAX-RS documentation:

Now it is possible (starting from version 2.2.5) to have separate multipart/form-data parts registered with registered JAX-RS MessageBodyReaders, which can already be done for types such as multipart/mixed or multipart/related .

See also:

Note that the jersey that GlassFish uses does not mention related in its source code .

HTTP client

Google offers an API for the HTTP client , which parses multipart/related messages according to the RFC .

Resteasy

The RESTeasy project can analyze multipart/related through JAX-RS.

JavaMail API

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

By default, the multi-party subtype is "mixed." Other multiple subtypes, such as "alternative", "related", etc., can be implemented as subclasses of MimeMultipart with additional methods to implement additional semantics of this type of multi-part content.

The JavaMail FAQ contains some additional information:

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

I would advise against this approach because it mixes metaphors (it combines MIME via HTTP / web with MIME via SMTP / mail). That is, JavaMail is conceptually used for reading and writing messages through SMTP / IMAP, which may leave future companions wondering why JavaMail is used to parse MIME messages received through Servlets, especially if there are annotation-based solutions available. However, documenting the code for its use would be a way to avoid confusion.

Container

Depending on the container, it may or may not process all relevant RFCs. You may need to try (or view the source code) of different containers to see which ones implement this function.

Jetty

the source code contains several points related to the analysis of input streams, which are limited to multipart/form-data :

In addition, tags do not include RFC 2387 , which is a strong indicator that the container does not process related parts in the Servlet 3.0 API. So JAX-RS is probably the best approach.

Tomcat

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

+4
source

You can use the JavaMail library and read the data as mail. You get MimeMessage and can go through BodyParts for processing. The usage is pretty straight forward, I used this approach to implement an AS2 server, and I saw that other AS2 implementations do the same, so this is a fairly common approach.

I do not see a conflict of standards here. The entire request format is MIME, so processing it with a library designed to handle MIME-encoded messages is, in my eyes, the right way to handle things.

-1
source

Source: https://habr.com/ru/post/1271471/


All Articles