users@jersey.java.net

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

From: Lars Tackmann <lars_at_randompage.org>
Date: Thu, 15 May 2008 14:48:30 +0200

On Thu, May 15, 2008 at 2:13 PM, Jo Størset <jo.storset_at_usit.uio.no> wrote:
> Hi,
>
> 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)?

Good question and I for one would be happy to see a clean solution for
this. Anyway I accomplish this today with a little bit of hacking
outlined below:

--
private String getHtml(...) {
    :
}
private String getXML(...) {
    :
}
@GET
@ProduceMime( {"text/html", "application/xml"} )
public String getStuff(...) {
    if(headers contains html (and xml) then serve html) {
         return getHtml(...);
    } else if (only xml) {
         return getXML(...);
    }
}
--
You can inject the HTTPHeaders and then check them by hand. As I said
this is not pretty but getting into some order and precedens
algorithms is not that pretty either.
-- 
Yours sincerely
Lars Tackmann