Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.



 

 

 

 

...

Span
stylefont-size:2em;font-weight:bold
JAX-RS : Support for Multiparts

...

 

 

 

...



Table of Contents

Reading attachments

...

Code Block
java
java
WebClient client = WebClient.create("http://books");t
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
Attachment att = new Attachment("root", imageInputStream, cd);
client.post(new MultipartBody(att));

// or just post the attachment if it's a single part request only
client.post(att);

// or just use a file
client.post(getClass().getResource("image.png").getFile());

...

Note that if a given attachment exceeds the maximum size property (default is no-limit) then HTTP 413 status will be returned. For more information on these configuration properties, please see the documentation on Securing CXF Services.

Forms and multiparts

The Forms in HTML documents recommendation suggests that multipart/form-data requests should mainly be used to upload files.

...

Note that once a request has more than two parts then one needs to start using @Mutipart@Multipart, the values can refer to either ContentId header or to ContentDisposition/name. Note that at the moment using @Multipart is preferred to using @FormParam unless a plain name/value submission is dealt with. The reason is that @Multipart can also specify an expected media type of the individual part and thus act similarly to a @Consume annotation.

...

Please see RFC 6266 and this unit test for more information. 

Content-Type

If the content type (Content-Type) of the attachment is not set, it is assumed to be "application/octet-stream". This default could be overridden.

Property
org.apache.cxf.attachment.content-type

The default value for AttachmentDataSource content type in case when "Content-Type" header is not present.

XOP support

CXF JAXRS clients and endpoints can support XML-binary Optimized Packaging (XOP).
What it means at a practical level is that a JAXB bean containing binary data is serialized using a multipart packaging, with the root part containing non-binary data only but also linking to co-located parts containing the actual binary payloads. Next it is deserialized into a JAXB bean on the server side.

If you'd like to experiment with XOP then you need to set an "mtom-enabled" property on CXF jaxrs endpoints and clients.
Please see JAXRSMultipartTest (testXopWebClient) and MultipartStore (addBookXop) for more details.

Multipart Filters

...


It is possible to intercept the attachment write or read process starting from CXF 3.1.12.

...

These filters can be registered from JAX-RS 2.0 client or container request/response filters or CXF in/out interceptors. MultipartInputFilter can be added to the list of the input filters which is identified by a "multipart.input.filters" property on the current CXF message. Likewise, MultipartOutputFilter can be added to the list of the output filters which is identified by a "multipart.output.filters" property on the current CXF message. 

Signing Multiparts

See this section for more information.

Note about Struts

If you are using CXF and Struts2 within the same application and expecting CXF to process multipart/form-data payloads then you need to make sure Struts2 dispatcher is not consuming the request input stream.

...