users@jersey.java.net

Re: [Jersey] container request and multipart/form-data

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 03 May 2010 10:44:02 +0200

Hi Geoff,

On May 1, 2010, at 9:47 PM, geoffrey hendrey wrote:

> Hi,
>
> I am implementing a ResourceFilter, and I need to get at multipart/
> form-data form fields. I see that the doc's for
> ContainerRequest.getFormParameters states: "the form parameters, if
> there is a request entity and the content type is "application/x-www-
> form-urlencoded", otherwise an instance containing no parameters
> will be returned."
>
> Is there any way, from my ResourceFilter, that I can get access to
> form parameters that are posted as multipart/form-data?
>

There is no direct support, partly because this functionality is in a
separate module.

In your filter you will need to:

1) Buffer the request entity, because you do not know how the resource
method will consume the request entity;

2) Obtain the the entity as a FormDataMultiPart instance.

For example:

             // Buffer
             InputStream in = request.getEntityInputStream();
             if (in.getClass() != ByteArrayInputStream.class) {
                 // Buffer input
                 ByteArrayOutputStream baos = new
ByteArrayOutputStream();
                 ReaderWriter.writeTo(in, baos);
                 in = new ByteArrayInputStream(baos.toByteArray());
                 request.setEntityInputStream(in);
             }

             // Read entity
             FormDataMultiPart form =
request.getEntity(FormDataMultiPart.class);

             // Reset buffer
             ByteArrayInputStream bais = (ByteArrayInputStream)in;
             bais.reset();


Currently it is not possible to set the FormDataMultiPart instance to
be utilized by the resource method, it would just require a few tweaks
such that the filter could do:

             FormDataMultiPart form =
request.getEntity(FormDataMultiPart.class);
             request.getProperties().put(FORM_MULTIPART_PROPERTY, form);

then the buffering could be avoided. Of course this will only work if
the resource method consumes the request entity as an instance of
FormDataMultiPart. Please log an issue if you want this support.

Paul.