org.jboss.resteasy.plugins.providers.jackson

## Class Jackson2JsonpInterceptor

- java.lang.Object
- - org.jboss.resteasy.plugins.providers.jackson.Jackson2JsonpInterceptor

- All Implemented Interfaces:
    WriterInterceptor

      @Provider
      @ConstrainedTo(value=SERVER)
      public class Jackson2JsonpInterceptor
      extends Object
      implements WriterInterceptor

  JSONP is an alternative to normal AJAX requests. Instead of using a XMLHttpRequest a script tag is added to the DOM. The browser will call the corresponding URL and download the JavaScript. The server creates a response which looks like a method call. The parameter is the body of the request. The name of the method to call is normally passed as query parameter. The method has to be present in the current JavaScript environment.

  Jackson JSON processor can produce such an response. This interceptor checks if the media type is a JavaScript one if there is a query parameter with the method name. The default name of this query parameter is "callback". So this interceptor is compatible with jQuery.

  Version:
    $Revision: 1 $
  Author:
    Holger Morch

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static MediaType` | `APPLICATION_JAVASCRIPT_MEDIA_TYPE` "application/javascript" media type.
    `static MediaType` | `APPLICATION_PLUS_JSON_TYPE` "application/*+json" media type.
    `static String` | `DEFAULT_CALLBACK_QUERY_PARAMETER` Default name of the query parameter with the method name.
    `protected static com.fasterxml.jackson.databind.ObjectMapper` | `DEFAULT_MAPPER` Default `ObjectMapper` for type resolution.
    `static MediaTypeMap<String>` | `jsonpCompatibleMediaTypes` If response media type is one of this jsonp response may be created.
    `protected com.fasterxml.jackson.databind.ObjectMapper` | `objectMapper` The `ObjectMapper` used to create typing information.
    `protected Providers` | `providers` The `Providers` used to retrieve the `objectMapper` from.
    `static MediaType` | `TEXT_JAVASCRIPT_MEDIA_TYPE` "text/javascript" media type.
    `static MediaType` | `TEXT_JSON_TYPE` "text/json" media type.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Jackson2JsonpInterceptor()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `aroundWriteTo(WriterInterceptorContext context)` Interceptor method wrapping calls to `MessageBodyWriter.writeTo(T, java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.Object>, java.io.OutputStream)` method.
    `String` | `getCallbackQueryParameter()` Get the name of the query parameter which contains the JavaScript method name.
    `protected com.fasterxml.jackson.databind.ObjectMapper` | `getObjectMapper(Class<?> type, MediaType mediaType)` Search for an `ObjectMapper` for the given class and mediaType
    `void` | `setCallbackQueryParameter(String callbackQueryParameter)` Set callback query parameter.
    `void` | `setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)` Set an fix `ObjectMapper`.
    `void` | `setProviders(Providers providers)` Setter used by RESTeasy to provide the `Providers`
    `void` | `setUri(UriInfo uri)` Setter used by RESTeasy to provide the `UriInfo`.

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

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

- - ### Field Detail

    - #### TEXT_JAVASCRIPT_MEDIA_TYPE

          public static final MediaType TEXT_JAVASCRIPT_MEDIA_TYPE

      "text/javascript" media type. Default media type of script tags.

    - #### APPLICATION_JAVASCRIPT_MEDIA_TYPE

          public static final MediaType APPLICATION_JAVASCRIPT_MEDIA_TYPE

      "application/javascript" media type.

    - #### TEXT_JSON_TYPE

          public static final MediaType TEXT_JSON_TYPE

      "text/json" media type.

    - #### APPLICATION_PLUS_JSON_TYPE

          public static final MediaType APPLICATION_PLUS_JSON_TYPE

      "application/*+json" media type.

    - #### DEFAULT_CALLBACK_QUERY_PARAMETER

          public static final String DEFAULT_CALLBACK_QUERY_PARAMETER

      Default name of the query parameter with the method name.
      See Also:
        Constant Field Values

    - #### jsonpCompatibleMediaTypes

          public static final MediaTypeMap<String> jsonpCompatibleMediaTypes

      If response media type is one of this jsonp response may be created.

    - #### DEFAULT_MAPPER

          protected static final com.fasterxml.jackson.databind.ObjectMapper DEFAULT_MAPPER

      Default `ObjectMapper` for type resolution. Used if none is provided by `Providers`.

    - #### objectMapper

          protected com.fasterxml.jackson.databind.ObjectMapper objectMapper

      The `ObjectMapper` used to create typing information.

    - #### providers

          protected Providers providers

      The `Providers` used to retrieve the `objectMapper` from.

  - ### Constructor Detail

    - #### Jackson2JsonpInterceptor

          public Jackson2JsonpInterceptor()

  - ### Method Detail

    - #### aroundWriteTo

          public void aroundWriteTo(WriterInterceptorContext context)
                             throws IOException,
                                    WebApplicationException

      Interceptor method wrapping calls to `MessageBodyWriter.writeTo(T, java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.Object>, java.io.OutputStream)` method. The parameters of the wrapped method called are available from `context`. Implementations of this method SHOULD explicitly call `WriterInterceptorContext.proceed()` to invoke the next interceptor in the chain, and ultimately the wrapped `MessageBodyWriter.writeTo` method.
      Specified by:
        `aroundWriteTo` in interface `WriterInterceptor`
      Parameters:
        `context` - invocation context.
      Throws:
        `IOException` - if an IO error arises or is thrown by the wrapped `MessageBodyWriter.writeTo` method.
        `WebApplicationException` - thrown by the wrapped `MessageBodyWriter.writeTo` method.

    - #### getObjectMapper

          protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper(Class<?> type,
                                                                    MediaType mediaType)

      Search for an `ObjectMapper` for the given class and mediaType
      Parameters:
        `type` - the `Class` to serialize
        `mediaType` - the response `MediaType`
      Returns:
        the `ObjectMapper`

    - #### setUri

          @Context
          public void setUri(UriInfo uri)

      Setter used by RESTeasy to provide the `UriInfo`.
      Parameters:
        `uri` - the uri to set

    - #### setProviders

          @Context
          public void setProviders(Providers providers)

      Setter used by RESTeasy to provide the `Providers`
      Parameters:
        `providers` -

    - #### setObjectMapper

          public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)

      Set an fix `ObjectMapper`. If this is not set `Providers` are used for lookup. If there are is none too, use a default one.
      Parameters:
        `objectMapper` -

    - #### getCallbackQueryParameter

          public String getCallbackQueryParameter()

      Get the name of the query parameter which contains the JavaScript method name. Default: callback.
      Returns:
        the callbackQueryParameter

    - #### setCallbackQueryParameter

          public void setCallbackQueryParameter(String callbackQueryParameter)

      Set callback query parameter.
      Parameters:
        `callbackQueryParameter` - the callbackQueryParameter to set
      See Also:
        `getCallbackQueryParameter()`

Copyright © 2014. All Rights Reserved.