com.supermap.services.rest.encoders

Class 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 class  JsonpEncoder.JsonpRepresentation
      The JSONP format representation.
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      static java.lang.String defaultCallBack 
    • 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.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface javax.ws.rs.ext.MessageBodyWriter

        getSize
    • Field Detail

      • defaultCallBack

        public static final java.lang.String defaultCallBack
        
    • 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:
        createSupportedMediaTypes in class JsonEncoder
        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:
        toRepresentation in class JsonEncoder
        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:
        toFormatedString in class JsonEncoder
        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:
        isWriteable in interface javax.ws.rs.ext.MessageBodyWriter
        Overrides:
        isWriteable in class JsonEncoder
        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.WebApplicationException
        
        Description copied from class: JsonEncoder

        Generates an HTTP response for the specified media type.

        Specified by:
        writeTo in interface javax.ws.rs.ext.MessageBodyWriter
        Overrides:
        writeTo in class JsonEncoder
        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.