On Thu, Jan 19, 2012 at 05:20, Jerome Louvel <[email protected]> wrote:
> 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

Reply via email to