users@jersey.java.net

Re: [Jersey] http and xml on the same path

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 15 May 2008 15:23:33 +0200

Hi Jo,

Unfortunately there is no clean way of doing this at the moment. I have
no idea why the browsers do this as it really does not make any sense to
me. Firefox's Accept header is the following:

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

Perhaps it is because some servers return xml that is actually xhtml+xml?

I bet if you produce "application/xhtml+xml" instead of "text/html" with
the following:

   @ProduceMime( {"application/xhtml+xml", // order is important here
                  "text/html",
                  "application/xml"} )

Lars's solution is one way. Lars, you can get the ordered list of
acceptable media types as follows:

   @Context HttpHeaders h;

   @GET
   @ProduceMime( {"text/html", "application/xml"} )
   public String getStuff() {
     if (hasHtml(h))
       return HTML
     else
       return XML
   }

   private boolean hasHtml() {
     List<MediaType> as = h.getAcceptableMediaTypes();
     for (a : as)
        if (a.isCompatible(MediaType.TEXT_HTML_TYPE))
           return true;
     return false;
   }

Another solution is to check the User-Agent header.

I am reluctant to put logic directly in Jersey that does not obey the
Accept processing rules.

Perhaps the cleanest solution is to write a filter that checks the
User-Agent header and modifies the Accept header accordingly (by simply
prefixing "text/html," to the Accept header).

Can this easily be done using a servlet filter?

But... we should probably add a filter concept to Jersey...to make it
really easy to modify the request or response.

Paul.

Jo Størset wrote:
>
> Den 15. mai. 2008 kl. 14.13 skrev Jo Størset:
>
>> I wan't to serve text/html by default to browsers, but also be able to
>> deliver application/xml on the same url,
>> is there any way to achieve this with jersey?
>>
>> It seems that browser clients (at least firefox/safari) prefer xml
>> over html by default in the accept header, so I need some way of
>> tweaking jerseys matching algorithm (?)
>>
>> Would it be possible to get jersey to choose @ProduceMime("text/html")
>> over @ProduceMime("application/xml") if the client says it accepts
>> html (even if it's less weighted than xml)?
>>
>> The http spec says that I SHOULD respect the weight, by I don't see
>> any better ways to handle this case of wanting browsers to get html by
>> default. Does anyone know of a better solution?
>
>
> I should probably add that I use a template processor to generate the
> html, so I can't do the conneg in my own code. I need seperate methods
> for xml and html, where the former return my jaxb domain object and the
> latter a Viewable.
>
> Jo
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109