@Provider public class JsonpEncoder extends JsonEncoder implements javax.ws.rs.ext.MessageBodyWriter
The JSONP serializer of the Java object.
It is used to serialize a Java object to a JSONP string.
Modifier and Type | Class and Description |
---|---|
static class |
JsonpEncoder.JsonpRepresentation
The JSONP format representation.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
defaultCallBack |
Constructor and Description |
---|
JsonpEncoder()
The constructor.
|
JsonpEncoder(HttpServletRequest request)
The constructor that constructs a JsonpEncoder object according to the HTTP request.
|
Modifier and Type | Method and Description |
---|---|
protected java.util.List<MediaType> |
createSupportedMediaTypes()
Creates supported media types.
|
java.lang.String |
getCallBack()
Gets the name of the callback function.
|
protected java.lang.String |
getCallBackForRequest(HttpServletRequest request2)
Gets the name of the callback function from the HTTP request.
|
static boolean |
isCallBackValid(java.lang.String callBack)
Checks if the name of the callback function is valid
|
boolean |
isWriteable(java.lang.Class arg0, java.lang.reflect.Type arg1, java.lang.annotation.Annotation[] arg2, javax.ws.rs.core.MediaType arg3)
Determines whether the encoder supports the representation of the specified media type.
|
void |
setCallBack(java.lang.String callBack)
Sets the name of the callback function.
|
void |
setStatus(int code)
Sets the corresponding status code.
|
protected java.lang.String |
toFormatedString(java.lang.Object resultObj)
Converts an object to JSONP string.
|
Representation |
toRepresentation(MediaType mediaType, java.lang.Object resourceObj)
Serializes a Java object to JSONP format representation.
|
void |
writeTo(java.lang.Object arg0, java.lang.Class arg1, java.lang.reflect.Type arg2, java.lang.annotation.Annotation[] arg3, javax.ws.rs.core.MediaType arg4, javax.ws.rs.core.MultivaluedMap arg5, java.io.OutputStream arg6)
Generates an HTTP response for the specified media type.
|
getSize, isindent, toFormatedObject
getSupportedMediaTypes, isEncodeAllow
public JsonpEncoder()
The constructor.
public JsonpEncoder(@Context HttpServletRequest request)
The constructor that constructs a JsonpEncoder object according to the HTTP request.
request
- HTTP request.protected java.util.List<MediaType> createSupportedMediaTypes()
Creates supported media types. Here it is JSONP, i.e., "application/jsonp".
createSupportedMediaTypes
in class JsonEncoder
public java.lang.String getCallBack()
Gets the name of the callback function.
public void setCallBack(java.lang.String callBack)
Sets the name of the callback function.
callBack
- The name of the callback function.public static boolean isCallBackValid(java.lang.String callBack)
Checks if the name of the callback function is valid
The callback function should be valid name of javascript function, this check is to forbide cross-site scripting and avoid users submitting invalid string. Three are three legal return situations: functionName({JSON}) obj.functionName({JSON}) obj['functionName']({JSON})
callBack
- The name of callback function to be checked.public Representation toRepresentation(MediaType mediaType, java.lang.Object resourceObj)
Serializes a Java object to JSONP format representation.
toRepresentation
in class JsonEncoder
mediaType
- The media type.resourceObj
- The Java object to be serialized.protected java.lang.String toFormatedString(java.lang.Object resultObj)
Converts an object to JSONP string.
toFormatedString
in class JsonEncoder
resultObj
- The object to be converted.public boolean isWriteable(java.lang.Class arg0, java.lang.reflect.Type arg1, java.lang.annotation.Annotation[] arg2, javax.ws.rs.core.MediaType arg3)
Determines whether the encoder supports the representation of the specified media type.
isWriteable
in interface javax.ws.rs.ext.MessageBodyWriter
isWriteable
in class JsonEncoder
arg0
- The object to generate representation.arg1
- The specified media type.arg2
- The annotation array. Methods of the resource contain these annotations return the object to be written.arg3
- The media type of the expected response.protected java.lang.String getCallBackForRequest(HttpServletRequest request2)
Gets the name of the callback function from the HTTP request.
request2
- HTTP request.public void setStatus(int code)
Sets the corresponding status code.
code
- The status code.public void writeTo(java.lang.Object arg0, java.lang.Class arg1, java.lang.reflect.Type arg2, java.lang.annotation.Annotation[] arg3, javax.ws.rs.core.MediaType arg4, javax.ws.rs.core.MultivaluedMap arg5, java.io.OutputStream arg6) throws java.io.IOException, javax.ws.rs.WebApplicationException
JsonEncoder
Generates an HTTP response for the specified media type.
writeTo
in interface javax.ws.rs.ext.MessageBodyWriter
writeTo
in class JsonEncoder
arg0
- To generate an expression object.arg1
- The Java class that corresponds to the object to be generated.arg2
- To generate an expression object type.arg3
- annotation array, the resource has these annotation methods to return to the object to be written.arg4
- HTTP The media type of the response.arg5
- HTTP Response to the message header.arg6
- HTTP response.java.io.IOException
- When the read and write process is wrong.javax.ws.rs.WebApplicationException
- When the HTTP response fails.