ContentNegotiationManagerFactoryBean (Spring Framework 3.2.1.RELEASE API)
Date: 2013-01-24

org.springframework.web.accept

## Class ContentNegotiationManagerFactoryBean

- java.lang.Object
- - org.springframework.web.accept.ContentNegotiationManagerFactoryBean

- All Implemented Interfaces:
    Aware, FactoryBean<ContentNegotiationManager>, InitializingBean, ServletContextAware

      public class ContentNegotiationManagerFactoryBean
      extends Object
      implements FactoryBean<ContentNegotiationManager>, InitializingBean, ServletContextAware

  A factory providing convenient access to a `ContentNegotiationManager` configured with one or more `ContentNegotiationStrategy` instances.

  By default strategies for checking the extension of the request path and the `Accept` header are registered. The path extension check will perform lookups through the `ServletContext` and the Java Activation Framework (if present) unless media types are configured.

  Since:
    3.2
  Author:
    Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ContentNegotiationManagerFactoryBean()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addMediaType(String fileExtension, MediaType mediaType)` Add a mapping from a file extension to a media type.
    `void` | `addMediaTypes(Map<String,MediaType> mediaTypes)` Add mappings from file extensions to media types.
    `void` | `afterPropertiesSet()` Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
    `ContentNegotiationManager` | `getObject()` Return an instance (possibly shared or independent) of the object managed by this factory.
    `Class<?>` | `getObjectType()` Return the type of object that this FactoryBean creates, or `null` if not known in advance.
    `boolean` | `isSingleton()` Is the object managed by this factory a singleton? That is, will `FactoryBean.getObject()` always return the same object (a reference that can be cached)?
    `void` | `setDefaultContentType(MediaType defaultContentType)` Set the default content type.
    `void` | `setFavorParameter(boolean favorParameter)` Indicate whether a request parameter should be used to determine the requested media type with the 2nd highest priority, i.e.
    `void` | `setFavorPathExtension(boolean favorPathExtension)` Indicate whether the extension of the request path should be used to determine the requested media type with the highest priority.
    `void` | `setIgnoreAcceptHeader(boolean ignoreAcceptHeader)` Indicate whether the HTTP `Accept` header should be ignored altogether.
    `void` | `setMediaTypes(Properties mediaTypes)` Add mappings from file extensions to media types represented as strings.
    `void` | `setParameterName(String parameterName)` Set the parameter name that can be used to determine the requested media type if the `setFavorParameter(boolean)` property is `true`.
    `void` | `setServletContext(ServletContext servletContext)` Set the `ServletContext` that this object runs in.
    `void` | `setUseJaf(boolean useJaf)` Indicate whether to use the Java Activation Framework as a fallback option to map from file extensions to media types.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ContentNegotiationManagerFactoryBean

          public ContentNegotiationManagerFactoryBean()

  - ### Method Detail

    - #### setFavorPathExtension

          public void setFavorPathExtension(boolean favorPathExtension)

      Indicate whether the extension of the request path should be used to determine the requested media type with the highest priority.

      By default this value is set to `true` in which case a request for `/hotels.pdf` will be interpreted as a request for `"application/pdf"` regardless of the `Accept` header.

    - #### setMediaTypes

          public void setMediaTypes(Properties mediaTypes)

      Add mappings from file extensions to media types represented as strings.

      When this mapping is not set or when an extension is not found, the Java Action Framework, if available, may be used if enabled via `setFavorPathExtension(boolean)`.

      See Also:
        `addMediaType(String, MediaType)`, `addMediaTypes(Map)`

    - #### addMediaType

          public void addMediaType(String fileExtension,
                          MediaType mediaType)

      Add a mapping from a file extension to a media type.

      If no mapping is added or when an extension is not found, the Java Action Framework, if available, may be used if enabled via `setFavorPathExtension(boolean)`.

    - #### addMediaTypes

          public void addMediaTypes(Map<String,MediaType> mediaTypes)

      Add mappings from file extensions to media types.

      If no mappings are added or when an extension is not found, the Java Action Framework, if available, may be used if enabled via `setFavorPathExtension(boolean)`.

    - #### setUseJaf

          public void setUseJaf(boolean useJaf)

      Indicate whether to use the Java Activation Framework as a fallback option to map from file extensions to media types. This is used only when `setFavorPathExtension(boolean)` is set to `true`.

      The default value is `true`.

      See Also:
        `parameterName`, `setMediaTypes(Properties)`

    - #### setFavorParameter

          public void setFavorParameter(boolean favorParameter)

      Indicate whether a request parameter should be used to determine the requested media type with the 2nd highest priority, i.e. after path extensions but before the `Accept` header.

      The default value is `false`. If set to to `true`, a request for `/hotels?format=pdf` will be interpreted as a request for `"application/pdf"` regardless of the `Accept` header.

      To use this option effectively you must also configure the MediaType type mappings via `setMediaTypes(Properties)`.

      See Also:
        `setParameterName(String)`

    - #### setParameterName

          public void setParameterName(String parameterName)

      Set the parameter name that can be used to determine the requested media type if the `setFavorParameter(boolean)` property is `true`.

      The default parameter name is `"format"`.

    - #### setIgnoreAcceptHeader

          public void setIgnoreAcceptHeader(boolean ignoreAcceptHeader)

      Indicate whether the HTTP `Accept` header should be ignored altogether. If set the `Accept` header is checked at the 3rd highest priority, i.e. after the request path extension and possibly a request parameter if configured.

      By default this value is set to `false`.

    - #### setDefaultContentType

          public void setDefaultContentType(MediaType defaultContentType)

      Set the default content type.

      This content type will be used when neither the request path extension, nor a request parameter, nor the `Accept` header could help determine the requested content type.

    - #### setServletContext

          public void setServletContext(ServletContext servletContext)

      Description copied from interface: `ServletContextAware`
      Set the `ServletContext` that this object runs in.

      Invoked after population of normal bean properties but before an init callback like InitializingBean's `afterPropertiesSet` or a custom init-method. Invoked after ApplicationContextAware's `setApplicationContext`.

      Specified by:
        `setServletContext` in interface `ServletContextAware`
      Parameters:
        `servletContext` - ServletContext object to be used by this object
      See Also:
        `InitializingBean.afterPropertiesSet()`, `ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)`

    - #### afterPropertiesSet

          public void afterPropertiesSet()
                                  throws Exception

      Description copied from interface: `InitializingBean`
      Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

      This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

      Specified by:
        `afterPropertiesSet` in interface `InitializingBean`
      Throws:
        `Exception` - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

    - #### getObjectType

          public Class<?> getObjectType()

      Description copied from interface: `FactoryBean`
      Return the type of object that this FactoryBean creates, or `null` if not known in advance.

      This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

      In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

      This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

      NOTE: Autowiring will simply ignore FactoryBeans that return `null` here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

      Specified by:
        `getObjectType` in interface `FactoryBean<ContentNegotiationManager>`
      Returns:
        the type of object that this FactoryBean creates, or `null` if not known at the time of the call
      See Also:
        `ListableBeanFactory.getBeansOfType(java.lang.Class<T>)`

    - #### isSingleton

          public boolean isSingleton()

      Description copied from interface: `FactoryBean`
      Is the object managed by this factory a singleton? That is, will `FactoryBean.getObject()` always return the same object (a reference that can be cached)?

      NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from `getObject()` might get cached by the owning BeanFactory. Hence, do not return `true` unless the FactoryBean always exposes the same reference.

      The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

      NOTE: This method returning `false` does not necessarily indicate that returned objects are independent instances. An implementation of the extended `SmartFactoryBean` interface may explicitly indicate independent instances through its `SmartFactoryBean.isPrototype()` method. Plain `FactoryBean` implementations which do not implement this extended interface are simply assumed to always return independent instances if the `isSingleton()` implementation returns `false`.

      Specified by:
        `isSingleton` in interface `FactoryBean<ContentNegotiationManager>`
      Returns:
        whether the exposed object is a singleton
      See Also:
        `FactoryBean.getObject()`, `SmartFactoryBean.isPrototype()`

    - #### getObject

          public ContentNegotiationManager getObject()
                                              throws Exception

      Description copied from interface: `FactoryBean`
      Return an instance (possibly shared or independent) of the object managed by this factory.

      As with a `BeanFactory`, this allows support for both the Singleton and Prototype design pattern.

      If this FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding `FactoryBeanNotInitializedException`.

      As of Spring 2.0, FactoryBeans are allowed to return `null` objects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.

      Specified by:
        `getObject` in interface `FactoryBean<ContentNegotiationManager>`
      Returns:
        an instance of the bean (can be `null`)
      Throws:
        `Exception` - in case of creation errors
      See Also:
        `FactoryBeanNotInitializedException`