Hello Avi,
I've made some tests using Restlet 2.0 and 2.1. From what I notice, in this
case, the order is not significant.
I get the "xhtml" representation instead of the "html" one in both cases :
@Get(“html|xhtml”) and @Get(“xhtml|html”)
Here is my explanation : "html" is a shortcut for "text/html" and "xhtml"
stands for "application/xhtml+xml".
When the client provides no preference, then the default one set on the
MetadataService is applied : by default "application/octet-stream".
The curent score algorithm gives a better affinity between
"application/xhtml+xml" and "application/octet-stream" than between
"text/html" and "application/octet-stream", because of "application/"...
The scores are very low : 0.0006 and 0.0003 but the difference is real.
Good news anyway, you can set the default media type of the application's
MetadataService to a neutral one : "*/*" :
getMetadataService().setDefaultMediaType(MediaType.ALL);
In this case, the order is significant, once again.
Best regards,
Thierry Boileau
> The proper/standard way in HTTP to do this is to correctly set the
> > preferences of your clients (via the “Accept” header typically)…
> Otherwise,
> > the order of the annotated methods declaration might be taken into
> account
> > by Restlet when deciding how to dispatch the method call, but I wouldn’t
> say
> > it is a safe bet to solely rely on this default behavior.
> >
> > You could customize the ConnegService attached to your parent
> application to
> > force the variant when client preferences are not explicitly given
> > (MediaType.ALL), or plug a custom filter in the routing chain to enforce
> > your policy.
>
> Jerome,
>
> I’m using 2.0, and I need a way to set the default variant of my
> resources. I’m frustrated because I’d think this’d be a common need
> and it really should be easier and clearer how to do this. I need to
> support requests which don’t include an Accept header, and I need to
> be able to set the default variant concisely on a resource-by-resource
> basis — not in a centralized Service or Filter.
>
> With the resource I’m working on now, I first tried to just use a
> single annotation:
>
> @Get(“html|xhtml”)
> public Representation getRep(Variant variant) { … }
>
> I assumed that when a request didn’t include an Accept header, the
> conneg algorithm would take into account the order specified and use
> html as the default. Unfortunately it does not — for some reason,
> xhtml is chosen as the default.
>
> Unsurprisingly, the same is true if I use two separate methods:
>
> @Get(“html”)
> public Representation getHtml() { … }
>
> @Get(“xhtml”)
> public Representation getXhtml() { … }
>
> IIRC, in Restlet 1 this was fully supported — the first variant passed
> to getVariants().add() was considered the default. I still need this
> functionality.
>
> I don’t know if it’s too late to change this for 2.0 (I’d love to
> submit a patch…) but I’d at least like to see this improved for 2.1 —
> I think the order specified in the annotation parameter should be
> significant, with the first one specified used as the default.
>
> For now, I’m not even sure how I’m going to make this work in the
> resource I’m working on today — I might just do something hacky like
> this:
>
> if (acceptHeader != null &&
> acceptHeader.contains("application/xhtml+xml") && !
> acceptHeader.contains("text/html"))
> representation.setMediaType(MediaType.APPLICATION_XHTML);
>
> Thanks,
> Avi
>
> Avi Flax » Arc90 » http://arc90.com
> Kindling » Innovation through Collaboration » http://kindlingapp.com
> Readability » Enjoy Reading, Support Writing » http://readability.com
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2916957
>
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2925400