users@jersey.java.net

Re: [Jersey] Handling of mutiple mime types : How to know which mime type has been chosen ?

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Fri, 3 Sep 2010 15:11:18 +0200

On Sep 3, 2010, at 1:57 PM, Raphaël Jolivet wrote:

>
> Hello,
>
> I have written a Jersey REST web service which get and put stream of
> numerical data over the cloud.
> For now, the data is converted to CSV stream.
>
> I have added a parser / writer to binary format, to speed up things
> and to
> reduce the bandwidth usage.
>
> So, for now, I have something like :
>
> @GET @Path(METHOD_GET_DATA)
> @RolesAllowed(ROLE_READ)
> @Produces({CSV_MIME_TYPE, MY_BINARY_MIME_TYPE})
> public StreamingOutput getData(...) {...};
>
> and
>
> @POST @Path(METHOD_PUT_DATA)
> @RolesAllowed(ROLE_WRITE)
> @Consumes({CSV_MIME_TYPE, MY_BINARY_MIME_TYPE})
> public String putData(...) { ...}
>
> How do I know which mime type has been "agreed" with the client ?
> For the "putData" method, I guess I just need to get the
> ServletRequest#getMimeType(), to know
> what is the format of the inputstream.
>

For the putData see:

   https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/core/HttpHeaders.html
#getMediaType%28%29


For the getData, this is unfortunately an oversight in the JAX-RS API
and you need to do something like this:

   Request r = ...
   Variant v = r.selectVariant(Variant.mediaTypes(csvType,
myBinaryType).build());
   MediaType m = v.getMediaType();


Or alternatively split your method up like this:

    @GET @Path(METHOD_GET_DATA)
    @RolesAllowed(ROLE_READ)
    @Produces(CSV_MIME_TYPE)
    public StreamingOutput getDataCSV(...) {...};

    @GET @Path(METHOD_GET_DATA)
    @RolesAllowed(ROLE_READ)
    @Produces({MY_BINARY_MIME_TYPE)
    public StreamingOutput getDataMyBinaryType(...) {...};


Paul.

> But for the "getData", how do I know which mime type is accepted by
> the
> client ?
> Is there a convenience method to get the result of the "agreement"
> between
> the server and the client : The first mime type supported by both ?
>
> Thanks in advance,
>
> Raphael
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Handling-of-mutiple-mime-types-How-to-know-which-mime-type-has-been-chosen-tp5495061p5495061.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>