org.glassfish.jersey.server.filter
Class UriConnegFilter

java.lang.Object
  extended by org.glassfish.jersey.server.filter.UriConnegFilter
All Implemented Interfaces:
ContainerRequestFilter

@PreMatching
public class UriConnegFilter
extends java.lang.Object
implements ContainerRequestFilter

A URI-based content negotiation filter mapping a dot-declared suffix in URI to media type that is the value of the Accept header or a language that is the value of the Accept-Language header.

This filter may be used when the acceptable media type and acceptable language need to be declared in the URI.

This class may be extended to declare the mappings and the extending class, foo.MyUriConnegFilter say, can be registered as a container request filter.

If a suffix of "atom" is registered with a media type of "application/atom+xml" then a GET request of:

GET /resource.atom

is transformed to:

GET /resource
Accept: application/atom+xml
Any existing "Accept" header value will be replaced.

If a suffix of "english" is registered with a language of "en" then a GET request of:

GET /resource.english

is transformed to:

GET /resource
Accept-Language: en
Any existing "Accept-Language"header value will be replaced.

The media type mappings are processed before the language type mappings.

Author:
Paul Sandoz, Martin Matula (martin.matula at oracle.com)

Field Summary
protected  java.util.Map<java.lang.String,java.lang.String> languageMappings
           
protected  java.util.Map<java.lang.String,MediaType> mediaTypeMappings
           
 
Constructor Summary
UriConnegFilter(java.util.Map<java.lang.String,MediaType> mediaTypeMappings, java.util.Map<java.lang.String,java.lang.String> languageMappings)
          Create a filter with suffix to media type mappings and suffix to language mappings.
UriConnegFilter(ResourceConfig rc)
          Create a filter that reads the configuration (media type and language mappings) from the provided ResourceConfig instance.
 
Method Summary
static void enableFor(ResourceConfig rc, java.util.Map<java.lang.String,MediaType> mediaTypeMappings, java.util.Map<java.lang.String,java.lang.String> languageMappings)
          Registers this filter into the passed ResourceConfig instance and configures it.
static void enableFor(ResourceConfig rc, java.lang.String mediaTypeMappings, java.lang.String languageMappings)
           
 void filter(ContainerRequestContext rc)
          Filter method called before a request has been dispatched to a resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mediaTypeMappings

protected final java.util.Map<java.lang.String,MediaType> mediaTypeMappings

languageMappings

protected final java.util.Map<java.lang.String,java.lang.String> languageMappings
Constructor Detail

UriConnegFilter

public UriConnegFilter(@Context
                       ResourceConfig rc)
Create a filter that reads the configuration (media type and language mappings) from the provided ResourceConfig instance. This constructor will be called by the Jersey runtime when the filter class is returned from Application.getClasses(). The ResourceConfig instance will get auto-injected.

Parameters:
rc - ResourceConfig instance that holds the configuration for the filter.

UriConnegFilter

public UriConnegFilter(java.util.Map<java.lang.String,MediaType> mediaTypeMappings,
                       java.util.Map<java.lang.String,java.lang.String> languageMappings)
Create a filter with suffix to media type mappings and suffix to language mappings.

Parameters:
mediaTypeMappings - the suffix to media type mappings.
languageMappings - the suffix to language mappings.
Method Detail

enableFor

public static void enableFor(ResourceConfig rc,
                             java.util.Map<java.lang.String,MediaType> mediaTypeMappings,
                             java.util.Map<java.lang.String,java.lang.String> languageMappings)
Registers this filter into the passed ResourceConfig instance and configures it.

Parameters:
rc - ResourceConfig instance where the filter should be registered.
mediaTypeMappings - the suffix to media type mappings.
languageMappings - the suffix to language mappings.

enableFor

public static void enableFor(ResourceConfig rc,
                             java.lang.String mediaTypeMappings,
                             java.lang.String languageMappings)

filter

public void filter(ContainerRequestContext rc)
            throws java.io.IOException
Description copied from interface: ContainerRequestFilter
Filter method called before a request has been dispatched to a resource.

Filters in the filter chain are ordered according to their binding priority (see BindingPriority). If a request filter produces a response by calling ContainerRequestContext.abortWith(javax.ws.rs.core.Response) method, the execution of the (either pre-match or post-match) request filter chain is stopped and the response is passed to the corresponding response filter chain (either pre-match or post-match). For example, a pre-match caching filter may produce a response in this way, which would effectively skip any post-match request filters as well as post-match response filters. Note however that a responses produced in this manner would still be processed by the pre-match response filter chain.

Specified by:
filter in interface ContainerRequestFilter
Parameters:
rc - request context.
Throws:
java.io.IOException - if an I/O exception occurs.
See Also:
PreMatching


Copyright © 2007-2012 Oracle Corporation. All Rights Reserved. Use is subject to license terms.