org.gwtwidgets.server.spring
Class GWTRPCServiceExporter

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.google.gwt.user.server.rpc.RemoteServiceServlet
              extended by org.gwtwidgets.server.spring.GWTRPCServiceExporter
All Implemented Interfaces:
com.google.gwt.user.server.rpc.SerializationPolicyProvider, java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig, org.springframework.beans.factory.InitializingBean, org.springframework.web.context.ServletContextAware, org.springframework.web.HttpRequestHandler

public class GWTRPCServiceExporter
extends com.google.gwt.user.server.rpc.RemoteServiceServlet
implements org.springframework.web.HttpRequestHandler, org.springframework.beans.factory.InitializingBean, org.springframework.web.context.ServletContextAware

This component publishes an object (see setService(Object)) as a service to the GWT RPC protocol. Service targets can be:

Exceptions directly thrown from the target service are propagated back to the client. For special exception handling you can override the various handle* methods which are invoked by the GWTRPCServiceExporter.

Author:
George Georgovassilis, g.georgovassilis[at]gmail.com, Max Jonas Werner
See Also:
Serialized Form

Field Summary
static int COMPRESSION_AUTO
          Leave default RPC response compression behavior.
static int COMPRESSION_DISABLED
          Disable RPC response compression.
static int COMPRESSION_ENABLED
          Force compression of all RPC responses.
protected  int compressResponse
           
protected  org.apache.commons.logging.Log logger
           
protected  java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> methodCache
           
protected  java.lang.Object service
           
protected  java.lang.Class<?>[] serviceInterfaces
           
protected  javax.servlet.ServletContext servletContext
           
 
Constructor Summary
GWTRPCServiceExporter()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  java.lang.reflect.Method getMethodToInvoke(java.lang.reflect.Method decodedMethod)
          Returns method to invoke on service.
static javax.servlet.http.HttpServletRequest getRequest()
          Deprecated. Use ServletUtils.getRequest()
static javax.servlet.http.HttpServletResponse getResponse()
          Deprecated. Use ServletUtils.getResponse()
 java.lang.Object getService()
          Return target service.
 javax.servlet.ServletContext getServletContext()
           
protected  java.lang.String handleExporterProcessingException(java.lang.Exception e)
          Invoked by processCall(String) for an exception if no suitable exception handler was found.
protected  java.lang.String handleIllegalAccessException(java.lang.IllegalAccessException e, java.lang.Object service, java.lang.reflect.Method targetMethod, com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
          Handles an exception which is raised when a method access is attempted to a method which is not part of the RPC interface.
protected  java.lang.String handleIllegalArgumentException(java.lang.IllegalArgumentException e, java.lang.Object service, java.lang.reflect.Method targetMethod, com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
          Handles an exception which is raised when a method invocation with bad arguments is attempted.
protected  java.lang.String handleIncompatibleRemoteServiceException(com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException e)
          Invoked by processCall(String) when RPC throws an IncompatibleRemoteServiceException.
protected  java.lang.String handleInvocationTargetException(java.lang.reflect.InvocationTargetException e, java.lang.Object service, java.lang.reflect.Method targetMethod, com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
          Handles exceptions thrown by the target service, which are wrapped in InvocationTargetExceptions due to invocation by reflection.
 void handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
protected  java.lang.String handleServiceException(java.lang.Exception e, java.lang.Object service, java.lang.reflect.Method targetMethod, com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
          Handles exceptions thrown during a service invocation that are not handled by other exception handlers.
protected  java.lang.String handleUndeclaredThrowableException(java.lang.Exception e, java.lang.Object service, java.lang.reflect.Method targetMethod, com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
          Handles UndeclaredThrowableExceptions which are thrown by the target service.
protected  java.lang.String invokeMethodOnService(java.lang.Object service, java.lang.reflect.Method targetMethod, java.lang.Object[] targetParameters, com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
          Handles method invocation on a service and is invoked by processCall(String).
protected  void onAfterResponseSerialized(java.lang.String serializedResponse)
           
protected  void onBeforeRequestDeserialized(java.lang.String serializedRequest)
           
 java.lang.String processCall(java.lang.String payload)
          Overridden from RemoteServiceServlet and invoked by the servlet code.
protected  void setCompressResponse(int compressResponse)
          Enables or disables compression of RPC output.
 void setService(java.lang.Object service)
          Set the service service.
 void setServiceInterfaces(java.lang.Class<?>[] serviceInterfaces)
          Specifies the interfaces which must be implemented by the service bean.
 void setServletContext(javax.servlet.ServletContext servletContext)
          Implementation of ServletContextAware, is invoked by the Spring application context.
protected  boolean shouldCompressResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String responsePayload)
           
 
Methods inherited from class com.google.gwt.user.server.rpc.RemoteServiceServlet
doGetSerializationPolicy, doPost, doUnexpectedFailure, getSerializationPolicy, getThreadLocalRequest, getThreadLocalResponse
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletInfo, getServletName, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPRESSION_DISABLED

public static final int COMPRESSION_DISABLED
Disable RPC response compression. Value is 0.

See Also:
Constant Field Values

COMPRESSION_AUTO

public static final int COMPRESSION_AUTO
Leave default RPC response compression behavior. Value is 1.

See Also:
Constant Field Values

COMPRESSION_ENABLED

public static final int COMPRESSION_ENABLED
Force compression of all RPC responses. Value is 2.

See Also:
Constant Field Values

logger

protected org.apache.commons.logging.Log logger

serviceInterfaces

protected java.lang.Class<?>[] serviceInterfaces

service

protected java.lang.Object service

servletContext

protected javax.servlet.ServletContext servletContext

compressResponse

protected int compressResponse

methodCache

protected java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> methodCache
Constructor Detail

GWTRPCServiceExporter

public GWTRPCServiceExporter()
Method Detail

setServletContext

public void setServletContext(javax.servlet.ServletContext servletContext)
Implementation of ServletContextAware, is invoked by the Spring application context.

Specified by:
setServletContext in interface org.springframework.web.context.ServletContextAware
Parameters:
servletContext -

getServletContext

public javax.servlet.ServletContext getServletContext()
Specified by:
getServletContext in interface javax.servlet.ServletConfig
Overrides:
getServletContext in class javax.servlet.GenericServlet

getRequest

public static javax.servlet.http.HttpServletRequest getRequest()
Deprecated. Use ServletUtils.getRequest()

Return the request which invokes the service. Valid only if used in the dispatching thread.

Returns:
the servlet request

getResponse

public static javax.servlet.http.HttpServletResponse getResponse()
Deprecated. Use ServletUtils.getResponse()

Return the response which accompanies the request. Valid only if used in the dispatching thread.

Returns:
the servlet response

onAfterResponseSerialized

protected void onAfterResponseSerialized(java.lang.String serializedResponse)
Overrides:
onAfterResponseSerialized in class com.google.gwt.user.server.rpc.RemoteServiceServlet

onBeforeRequestDeserialized

protected void onBeforeRequestDeserialized(java.lang.String serializedRequest)
Overrides:
onBeforeRequestDeserialized in class com.google.gwt.user.server.rpc.RemoteServiceServlet

invokeMethodOnService

protected java.lang.String invokeMethodOnService(java.lang.Object service,
                                                 java.lang.reflect.Method targetMethod,
                                                 java.lang.Object[] targetParameters,
                                                 com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
                                          throws java.lang.Exception
Handles method invocation on a service and is invoked by processCall(String).

Parameters:
service - Service to invoke method on
targetMethod - Method to invoke.
targetParameters - Parameters to pass to method. Can be null for no arguments.
rpcRequest - RPCRequest instance for this request
Returns:
Return RPC encoded result.
Throws:
java.lang.Exception

handleIllegalArgumentException

protected java.lang.String handleIllegalArgumentException(java.lang.IllegalArgumentException e,
                                                          java.lang.Object service,
                                                          java.lang.reflect.Method targetMethod,
                                                          com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
Handles an exception which is raised when a method invocation with bad arguments is attempted. This implementation throws a SecurityException. For details on arguments please consult invokeMethodOnService(Object, Method, Object[], RPCRequest).

Parameters:
e - Exception thrown
service -
targetMethod -
Returns:
RPC encoded response (such as an RPC client exception)

handleIllegalAccessException

protected java.lang.String handleIllegalAccessException(java.lang.IllegalAccessException e,
                                                        java.lang.Object service,
                                                        java.lang.reflect.Method targetMethod,
                                                        com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
Handles an exception which is raised when a method access is attempted to a method which is not part of the RPC interface. This method is invoked by processCall(String). This implementation throws a SecurityException. For details on arguments please consult invokeMethodOnService(Object, Method, Object[], RPCRequest).

Parameters:
e - Exception thrown
service -
targetMethod -
Returns:
RPC encoded response (such as an RPC client exception)

handleInvocationTargetException

protected java.lang.String handleInvocationTargetException(java.lang.reflect.InvocationTargetException e,
                                                           java.lang.Object service,
                                                           java.lang.reflect.Method targetMethod,
                                                           com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
                                                    throws java.lang.Exception
Handles exceptions thrown by the target service, which are wrapped in InvocationTargetExceptions due to invocation by reflection. This method is invoked by processCall(String). This implementation encodes exceptions as RPC errors and returns them. For details on arguments please consult invokeMethodOnService(Object, Method, Object[], RPCRequest).

Parameters:
e - Exception thrown
service -
targetMethod -
rpcRequest -
Returns:
RPC payload
Throws:
java.lang.Exception

handleServiceException

protected java.lang.String handleServiceException(java.lang.Exception e,
                                                  java.lang.Object service,
                                                  java.lang.reflect.Method targetMethod,
                                                  com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
                                           throws java.lang.Exception
Handles exceptions thrown during a service invocation that are not handled by other exception handlers. processCall(String) on exceptions which have escaped the other exception handlers such as handleIllegalAccessException(IllegalAccessException, Object, Method, RPCRequest) etc. This implementation re-casts 'e'. For details on arguments please consult invokeMethodOnService(Object, Method, Object[], RPCRequest).

Parameters:
e - Exception thrown
service -
targetMethod -
rpcRequest -
Returns:
RPC payload
Throws:
java.lang.Exception

handleUndeclaredThrowableException

protected java.lang.String handleUndeclaredThrowableException(java.lang.Exception e,
                                                              java.lang.Object service,
                                                              java.lang.reflect.Method targetMethod,
                                                              com.google.gwt.user.server.rpc.RPCRequest rpcRequest)
                                                       throws java.lang.Exception
Handles UndeclaredThrowableExceptions which are thrown by the target service. This method This method is invoked by processCall(String). This implementation re-casts 'e'. For details on arguments please consult invokeMethodOnService(Object, Method, Object[], RPCRequest).

Parameters:
e - Exception thrown
service -
targetMethod -
rpcRequest -
Returns:
RPC payload
Throws:
java.lang.Exception

getMethodToInvoke

protected java.lang.reflect.Method getMethodToInvoke(java.lang.reflect.Method decodedMethod)
                                              throws java.lang.NoSuchMethodException
Returns method to invoke on service. This implementation calls ReflectionUtils.getRPCMethod(Object, Class[], Method)

Parameters:
decodedMethod - Method as determined by RPC
Returns:
Throws:
java.lang.NoSuchMethodException

processCall

public java.lang.String processCall(java.lang.String payload)
                             throws com.google.gwt.user.client.rpc.SerializationException
Overridden from RemoteServiceServlet and invoked by the servlet code.

Overrides:
processCall in class com.google.gwt.user.server.rpc.RemoteServiceServlet
Throws:
com.google.gwt.user.client.rpc.SerializationException

handleIncompatibleRemoteServiceException

protected java.lang.String handleIncompatibleRemoteServiceException(com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException e)
                                                             throws com.google.gwt.user.client.rpc.SerializationException
Invoked by processCall(String) when RPC throws an IncompatibleRemoteServiceException. This implementation propagates the exception back to the client via RPC.

Parameters:
e - Exception thrown
Returns:
RPC encoded failure response
Throws:
com.google.gwt.user.client.rpc.SerializationException

handleExporterProcessingException

protected java.lang.String handleExporterProcessingException(java.lang.Exception e)
Invoked by processCall(String) for an exception if no suitable exception handler was found. This is the outermost exception handler, catching any exceptions not caught by other exception handlers or even thrown by those handlers. This implementation wraps 'e' in a RuntimeException which is then thrown.

Parameters:
e -
Returns:
RPC encoded failure response

setService

public void setService(java.lang.Object service)
Set the service service. RPC requests are decoded and the corresponding method of the service object is invoked.

Parameters:
service -

handleRequest

public void handleRequest(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response)
                   throws javax.servlet.ServletException,
                          java.io.IOException
Specified by:
handleRequest in interface org.springframework.web.HttpRequestHandler
Throws:
javax.servlet.ServletException
java.io.IOException

setServiceInterfaces

public void setServiceInterfaces(java.lang.Class<?>[] serviceInterfaces)
Specifies the interfaces which must be implemented by the service bean. If not specified then any interface extending RemoteService which is implemented by the service bean is assumed.

Parameters:
serviceInterfaces -

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception

getService

public java.lang.Object getService()
Return target service. Each GWTRPCServiceExporter has a single target service which it redirects RPC to.

Returns:
Object

shouldCompressResponse

protected boolean shouldCompressResponse(javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response,
                                         java.lang.String responsePayload)
Overrides:
shouldCompressResponse in class com.google.gwt.user.server.rpc.RemoteServiceServlet

setCompressResponse

protected void setCompressResponse(int compressResponse)
Enables or disables compression of RPC output. Defaults to COMPRESSION_AUTO. Allowed values are COMPRESSION_ENABLED, COMPRESSION_DISABLED and COMPRESSION_AUTO.

Parameters:
compressResponse -