users@jersey.java.net

Using _at_Produces to match Accept with WebKit advice

From: John Lister <john.lister_at_kickstone.com>
Date: Fri, 30 Apr 2010 11:51:02 +0100

Hi, using Jersey 1.1.5 I've got a set of resources that return
differently formatted data depending on the accept type passed(amongst
other things) using @Produces, this all works fine for most browsers.
However I'm having trouble with webkit based ones (ie safari), safari
lists the accept header as (rightly or wrongly)

application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5


but if I used @Produces ({MediaType.TEXT_HTML,
MediaType.APPLICATION_XML}) then a request for a page in safari that you
would expect to be returned as html, actually comes back as xml which
isn't what a user would expect or probably want.

Does anyone have any suggestions/solutions to this? I've tried a couple
of things but neither seemed to work...

Originally I had a single method taking all types, but I've tried
splitting that into 2, one for xml and the other for the other media
types using the qs qualifier, for example

@Products ({"text/html;qs=1", ...}
public Response getHtml(){
...

@Products ("application/xml;qs=.5")
public Response getXml(){
...
}

but this doesn't seem to work. I'd expect html to have priority 0.9 (eg
multiply cient and server values) and xml to be 0.5 - but I'm still
getting xml. I've even added xhtml media type to the first method, but
the same result..

Secondly I've tried using Request.selectVariant to return the best
matched type from a supplied list which includes application/xhtml+xml
and according to the various things I've read on the web as it is the
most specific at the highest priority it should be matched first, but
again I still get xml. Looking through the source, it seems that for the
same priority the first type listed is returned first which as I imagine
it is almost impossible to know which types are more specific than
others there isn't much more to do.

Therefore any ideas short of checking if the browser is webkit and doing
something different.

On a similar note, is it possible to find out what type @Produces has
matched against - I originally tried to get this using
Request.selectVariant as I couldn't see another way.

Thanks

John