Class JsonpEncoder
- java.lang.Object
-
- com.supermap.services.rest.encoders.Encoder
-
- com.supermap.services.rest.encoders.JsonEncoder
-
- com.supermap.services.rest.encoders.JsonpEncoder
-
- All Implemented Interfaces:
- javax.ws.rs.ext.MessageBodyWriter
@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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classJsonpEncoder.JsonpRepresentationThe JSONP format representation.
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.StringdefaultCallBack
-
Constructor Summary
Constructors Constructor and Description JsonpEncoder()The constructor.JsonpEncoder(HttpServletRequest request)The constructor that constructs a JsonpEncoder object according to the HTTP request.
-
Method Summary
Methods Modifier and Type Method and Description protected java.util.List<MediaType>createSupportedMediaTypes()Creates supported media types.java.lang.StringgetCallBack()Gets the name of the callback function.protected java.lang.StringgetCallBackForRequest(HttpServletRequest request2)Gets the name of the callback function from the HTTP request.static booleanisCallBackValid(java.lang.String callBack)Checks if the name of the callback function is validbooleanisWriteable(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.voidsetCallBack(java.lang.String callBack)Sets the name of the callback function.voidsetStatus(int code)Sets the corresponding status code.protected java.lang.StringtoFormatedString(java.lang.Object resultObj)Converts an object to JSONP string.RepresentationtoRepresentation(MediaType mediaType, java.lang.Object resourceObj)Serializes a Java object to JSONP format representation.voidwriteTo(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.-
Methods inherited from class com.supermap.services.rest.encoders.JsonEncoder
getSize, isindent, toFormatedObject
-
Methods inherited from class com.supermap.services.rest.encoders.Encoder
getSupportedMediaTypes, isEncodeAllow
-
-
-
-
Constructor Detail
-
JsonpEncoder
public JsonpEncoder()
The constructor.
-
JsonpEncoder
public JsonpEncoder(@Context HttpServletRequest request)The constructor that constructs a JsonpEncoder object according to the HTTP request.
- Parameters:
request- HTTP request.
-
-
Method Detail
-
createSupportedMediaTypes
protected java.util.List<MediaType> createSupportedMediaTypes()
Creates supported media types. Here it is JSONP, i.e., "application/jsonp".
- Overrides:
createSupportedMediaTypesin classJsonEncoder- Returns:
- The media type list.
-
getCallBack
public java.lang.String getCallBack()
Gets the name of the callback function.
- Returns:
- The name of the callback function.
-
setCallBack
public void setCallBack(java.lang.String callBack)
Sets the name of the callback function.
- Parameters:
callBack- The name of the callback function.
-
isCallBackValid
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})
- Parameters:
callBack- The name of callback function to be checked.- Returns:
- The callBack name is valid or not.
-
toRepresentation
public Representation toRepresentation(MediaType mediaType, java.lang.Object resourceObj)Serializes a Java object to JSONP format representation.
- Overrides:
toRepresentationin classJsonEncoder- Parameters:
mediaType- The media type.resourceObj- The Java object to be serialized.- Returns:
- The JSONP format representation of the specified object
-
toFormatedString
protected java.lang.String toFormatedString(java.lang.Object resultObj)
Converts an object to JSONP string.
- Overrides:
toFormatedStringin classJsonEncoder- Parameters:
resultObj- The object to be converted.- Returns:
- The JSONP string.
-
isWriteable
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.
- Specified by:
isWriteablein interfacejavax.ws.rs.ext.MessageBodyWriter- Overrides:
isWriteablein classJsonEncoder- Parameters:
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.- Returns:
- true if the encoder supports the representation of the specified media type, or false otherwise.
-
getCallBackForRequest
protected java.lang.String getCallBackForRequest(HttpServletRequest request2)
Gets the name of the callback function from the HTTP request.
- Parameters:
request2- HTTP request.- Returns:
- The name of the callback function
-
setStatus
public void setStatus(int code)
Sets the corresponding status code.
- Parameters:
code- The status code.
-
writeTo
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.WebApplicationExceptionDescription copied from class:JsonEncoderGenerates an HTTP response for the specified media type.
- Specified by:
writeToin interfacejavax.ws.rs.ext.MessageBodyWriter- Overrides:
writeToin classJsonEncoder- Parameters:
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.- Throws:
java.io.IOException- When the read and write process is wrong.javax.ws.rs.WebApplicationException- When the HTTP response fails.
-
-