Java Reference
In-Depth Information
the language desired by the client. You can obtain this information easily through the Ht-
tpHeaders.getAcceptableLanguages() method.
Negotiation by URI Patterns
Conneg is a powerful feature of HTTP. The problem is that some clients, specifically
browsers, do not support it. For example, the Firefox browser hardcodes the Accept header it
sends to the web server it connects to as follows:
text / html , application / xhtml + xml , application / xml ; q = 0.9 ,*/*; q = 0.8
If you wanted to view a JSON representation of a specific URI through your browser, you
would not be able to if JSON is not one of the preferred formats that your browser is hard-
coded to accept.
A common pattern to support such clients is to embed conneg information within the URI in-
stead of passing it along within an Accept header. Two examples are:
/ customers / en - US / xml / 3323
/ customers / 3323 . xml . en - US
The content information is embedded within separate paths of the URI or as filename suf-
fixes. In these examples, the client is asking for XML translated into English. You could
model this within your JAX-RS resource methods by creating simple path parameter patterns
within your @Path expressions. For example:
@Path ( "/customers/{id}.{type}.{language}" )
public Customer getCustomer ( @PathParam ( "id" ) int
int id ,
@PathParam ( "type" ) String type ,
@PathParam ( "language" ) String language ) {...}
Before the JAX-RS specification went final, a facility revolving around the filename suffix
pattern was actually defined as part of the specification. Unfortunately, the expert group
could not agree on the full semantics of the feature, so it was removed. Many JAX-RS imple-
mentations still support this feature, so I think it is important to go over how it works.
The way the specification worked and the way many JAX-RS implementations now work is
that you define a mapping between file suffixes, media types, and languages. An xml suffix
maps to application/xml . An en suffix maps to en-US . When a request comes in, the JAX-
RS implementation extracts the suffix and uses that information as the conneg data instead of
any incoming Accept or Accept-Language header. Consider this JAX-RS resource class:
@Path ( "/customers" )
public class
class CustomerResource
CustomerResource {
Search WWH ::

Custom Search