dev@jsr311.java.net

Re: Content Negotation vs. Extensions

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 26 Apr 2007 12:38:55 +0200

Stefan Tilkov wrote:
> I know the "right" way to do return different representations of a
> resource is via content negotiation. Still, in many cases it's perceived
> to be easier to use different extensions for different types.
>
> E.g. I could do a GET on http://example.org/customers/4711 to get the
> default representation, say in HTML, and use
> http://example.org/customers/4711.xml to get an XML representation.
> Whether this is "good" or "bad" doesn't really matter IMO; it's going to
> definitely a common approach. How do we address this?
>

Good question.


We could do this with sub-resources for separate methods in much the
same way as there could be different methods on the same resource for
different productions. For example:

@UriTemplate("{customer_id}")
class Customer {

   @HttpMethod
   @ProduceMime("application/xml")
   Customer getAsXML() { return getAsXMLResource(); }

   @HttpMethod
   @ProduceMime("application/json")
   Customer getAsJSON() { return getAsJSONResource(); }

   @UriTemplate(".xml")
   @HttpMethod
   Customer getAsXMLResource() { ... }

   @UriTemplate(".json")
   @HttpMethod
   JSONObject getAsJSONResource() { ... }

}


Maybe there is a case for compressing the methods like the following:

@UriTemplate("{customer_id}")
class Customer {

   @UriTemplates("", ".xml")
   @HttpMethod
   @ProduceMime("application/xml")
   Customer getAsXML() { return ... }

   @UriTemplates("", ".json")
   @HttpMethod
   @ProduceMime("application/json")
   Customer getAsJSON() { return ... }
}

Although i do like the way '_at_UriTemplates("", ".xml")' expresses things
but could not think of anything better in the few minutes of thought
about it.


However, the current matching algorithm i proposed (in the attached HTML
document sent in a previous email) would not work as sub-resources are
separated as path segments in the URI path hierarchy as the paths would be:

   /customers/4711/.xml
   /customers/4711/.json

not good!

So we would need a way to distinguish between path segments and non-path
segments for HTTP methods that are marked as sub-resources, perhaps
requiring things to being with a "/" ? (which just for URI
template-based HTTP methods is a very easy fix to the implementation of
the algorithm).

Paul.

> Stefan
> --
> Stefan Tilkov, http://www.innoq.com/blog/st/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jsr311.dev.java.net
> For additional commands, e-mail: dev-help_at_jsr311.dev.java.net
>

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