Class Jackson2JsonpInterceptor
- java.lang.Object
-
- org.jboss.resteasy.plugins.providers.jackson.Jackson2JsonpInterceptor
-
- All Implemented Interfaces:
javax.ws.rs.ext.WriterInterceptor
@Provider @ConstrainedTo(SERVER) public class Jackson2JsonpInterceptor extends java.lang.Object implements javax.ws.rs.ext.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.
It is possible to wrap the generated javascript function call in a try-catch block. You can enable it either by setting the
wrapInTryCatch
property of the provider instance totrue
or by setting theresteasy.jsonp.silent
context-param to true:<context-param> <param-name>resteasy.jsonp.silent</param-name> <param-value>true</param-value> </context-param>
- Version:
- $Revision: 1 $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Jackson2JsonpInterceptor.DoNotCloseDelegateOutputStream
This subclass ofCommitHeaderOutputStream
overrides theJackson2JsonpInterceptor.DoNotCloseDelegateOutputStream.close()
method so it would commit the headers only, without actually calling theJackson2JsonpInterceptor.DoNotCloseDelegateOutputStream.close()
method of the delegateOutputStream
-
Field Summary
Fields Modifier and Type Field Description static javax.ws.rs.core.MediaType
APPLICATION_JAVASCRIPT_MEDIA_TYPE
"application/javascript" media type.static javax.ws.rs.core.MediaType
APPLICATION_PLUS_JSON_TYPE
"application/*+json" media type.private java.lang.String
callbackQueryParameter
static java.lang.String
DEFAULT_CALLBACK_QUERY_PARAMETER
Default name of the query parameter with the method name.protected static com.fasterxml.jackson.databind.ObjectMapper
DEFAULT_MAPPER
DefaultObjectMapper
for type resolution.private boolean
enabled
Is this interceptor enabled.static MediaTypeMap<java.lang.String>
jsonpCompatibleMediaTypes
If response media type is one of this jsonp response may be created.protected com.fasterxml.jackson.databind.ObjectMapper
objectMapper
TheObjectMapper
used to create typing information.protected javax.ws.rs.ext.Providers
providers
TheProviders
used to retrieve theobjectMapper
from.static javax.ws.rs.core.MediaType
TEXT_JAVASCRIPT_MEDIA_TYPE
"text/javascript" media type.static javax.ws.rs.core.MediaType
TEXT_JSON_TYPE
"text/json" media type.private javax.ws.rs.core.UriInfo
uri
private boolean
wrapInTryCatch
-
Constructor Summary
Constructors Constructor Description Jackson2JsonpInterceptor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
aroundWriteTo(javax.ws.rs.ext.WriterInterceptorContext context)
java.lang.String
getCallbackQueryParameter()
Get the name of the query parameter which contains the JavaScript method name.protected com.fasterxml.jackson.databind.ObjectMapper
getObjectMapper(java.lang.Class<?> type, javax.ws.rs.core.MediaType mediaType)
Search for anObjectMapper
for the given class and mediaTypeboolean
isWrapInTryCatch()
Check is the JSONP callback will be wrapped with try-catch blockvoid
setCallbackQueryParameter(java.lang.String callbackQueryParameter)
Set callback query parameter.void
setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Set an fixObjectMapper
.void
setProviders(javax.ws.rs.ext.Providers providers)
Setter used by RESTeasy to provide theProviders
void
setUri(javax.ws.rs.core.UriInfo uri)
Setter used by RESTeasy to provide theUriInfo
.void
setWrapInTryCatch(boolean wrapInTryCatch)
Enables or disables wrapping the JSONP callback try try-catch block
-
-
-
Field Detail
-
TEXT_JAVASCRIPT_MEDIA_TYPE
public static final javax.ws.rs.core.MediaType TEXT_JAVASCRIPT_MEDIA_TYPE
"text/javascript" media type. Default media type of script tags.
-
APPLICATION_JAVASCRIPT_MEDIA_TYPE
public static final javax.ws.rs.core.MediaType APPLICATION_JAVASCRIPT_MEDIA_TYPE
"application/javascript" media type.
-
TEXT_JSON_TYPE
public static final javax.ws.rs.core.MediaType TEXT_JSON_TYPE
"text/json" media type.
-
APPLICATION_PLUS_JSON_TYPE
public static final javax.ws.rs.core.MediaType APPLICATION_PLUS_JSON_TYPE
"application/*+json" media type.
-
DEFAULT_CALLBACK_QUERY_PARAMETER
public static final java.lang.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<java.lang.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
DefaultObjectMapper
for type resolution. Used if none is provided byProviders
.
-
uri
private javax.ws.rs.core.UriInfo uri
-
callbackQueryParameter
private java.lang.String callbackQueryParameter
-
wrapInTryCatch
private boolean wrapInTryCatch
-
objectMapper
protected com.fasterxml.jackson.databind.ObjectMapper objectMapper
TheObjectMapper
used to create typing information.
-
providers
protected javax.ws.rs.ext.Providers providers
TheProviders
used to retrieve theobjectMapper
from.
-
enabled
private boolean enabled
Is this interceptor enabled.
-
-
Method Detail
-
aroundWriteTo
public void aroundWriteTo(javax.ws.rs.ext.WriterInterceptorContext context) throws java.io.IOException, javax.ws.rs.WebApplicationException
- Specified by:
aroundWriteTo
in interfacejavax.ws.rs.ext.WriterInterceptor
- Throws:
java.io.IOException
javax.ws.rs.WebApplicationException
-
getObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper(java.lang.Class<?> type, javax.ws.rs.core.MediaType mediaType)
Search for anObjectMapper
for the given class and mediaType- Parameters:
type
- theClass
to serializemediaType
- the responseMediaType
- Returns:
- the
ObjectMapper
-
setUri
@Context public void setUri(javax.ws.rs.core.UriInfo uri)
Setter used by RESTeasy to provide theUriInfo
.- Parameters:
uri
- the uri to set
-
setProviders
@Context public void setProviders(javax.ws.rs.ext.Providers providers)
Setter used by RESTeasy to provide theProviders
- Parameters:
providers
-
-
setObjectMapper
public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Set an fixObjectMapper
. If this is not setProviders
are used for lookup. If there are is none too, use a default one.- Parameters:
objectMapper
-
-
getCallbackQueryParameter
public java.lang.String getCallbackQueryParameter()
Get the name of the query parameter which contains the JavaScript method name. Default: callback.- Returns:
- the callbackQueryParameter
-
setCallbackQueryParameter
public void setCallbackQueryParameter(java.lang.String callbackQueryParameter)
Set callback query parameter.- Parameters:
callbackQueryParameter
- the callbackQueryParameter to set- See Also:
getCallbackQueryParameter()
-
isWrapInTryCatch
public boolean isWrapInTryCatch()
Check is the JSONP callback will be wrapped with try-catch block- Returns:
- true if try-catch block is generated; false otherwise
-
setWrapInTryCatch
public void setWrapInTryCatch(boolean wrapInTryCatch)
Enables or disables wrapping the JSONP callback try try-catch block- Parameters:
wrapInTryCatch
- true if you want to wrap the result with try-catch block; false otherwise
-
-