Class UriConnegFilter

java.lang.Object
org.glassfish.jersey.server.filter.UriConnegFilter
All Implemented Interfaces:
javax.ws.rs.container.ContainerRequestFilter

@PreMatching @Priority(3000) public final class UriConnegFilter extends Object implements javax.ws.rs.container.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.

  • Field Details

    • mediaTypeMappings

      protected final Map<String,javax.ws.rs.core.MediaType> mediaTypeMappings
    • languageMappings

      protected final Map<String,String> languageMappings
  • Constructor Details

    • UriConnegFilter

      public UriConnegFilter(@Context javax.ws.rs.core.Configuration 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(Map<String,javax.ws.rs.core.MediaType> mediaTypeMappings, Map<String,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 Details

    • filter

      public void filter(javax.ws.rs.container.ContainerRequestContext rc) throws IOException
      Specified by:
      filter in interface javax.ws.rs.container.ContainerRequestFilter
      Throws:
      IOException
    • extractMediaTypeMappings

      private static Map<String,javax.ws.rs.core.MediaType> extractMediaTypeMappings(Object mappings)
    • extractLanguageMappings

      private static Map<String,String> extractLanguageMappings(Object mappings)
    • parseAndValidateMappings

      private static <T> Map<String,T> parseAndValidateMappings(String property, Object mappings, UriConnegFilter.TypeParser<T> parser)
    • parseMappings

      private static <T> void parseMappings(String property, String mappings, Map<String,T> mappingsMap, UriConnegFilter.TypeParser<T> parser)
    • encodeKeys

      private static <T> void encodeKeys(Map<String,T> map)