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.AbstractRemoteServiceServlet
              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, RPCServiceExporter, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.web.context.ServletConfigAware, org.springframework.web.context.ServletContextAware, org.springframework.web.servlet.mvc.Controller
Direct Known Subclasses:
TracingExporter

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

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
protected  java.lang.String beanName
           
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  boolean disableResponseCaching
           
protected  org.apache.commons.logging.Log logger
           
protected  java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> methodCache
           
protected  ModulePathTranslation modulePathTranslation
           
protected  int serializationFlags
           
protected  SerializationPolicyProvider serializationPolicyProvider
           
protected  java.lang.Object service
           
protected  java.lang.Class<?>[] serviceInterfaces
           
protected  javax.servlet.ServletContext servletContext
           
protected  boolean shouldCheckPermutationStrongName
           
protected  boolean throwUndeclaredExceptionToServletContainer
           
 
Fields inherited from class com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet
perThreadRequest, perThreadResponse
 
Constructor Summary
GWTRPCServiceExporter()
           
 
Method Summary
 void afterPropertiesSet()
          Should be invoked after all properties have been set.
protected  void checkPermutationStrongName()
           
protected  com.google.gwt.user.server.rpc.SerializationPolicy doGetSerializationPolicy(javax.servlet.http.HttpServletRequest request, java.lang.String moduleBaseURL, java.lang.String strongName)
           
protected  void doUnexpectedFailure(java.lang.Throwable e)
           
protected  java.lang.String encodeResponseForFailure(com.google.gwt.user.server.rpc.RPCRequest rpcRequest, java.lang.Throwable cause, java.lang.reflect.Method targetMethod, java.lang.Object[] targetParameters)
          Wrapper around RPC utility invocation
protected  java.lang.reflect.Method getMethodToInvoke(java.lang.reflect.Method decodedMethod)
          Returns method to invoke on service.
 int getSerializationFlags()
          Return the set serialization flags (see AbstractSerializationStream.getFlags()
 SerializationPolicyProvider getSerializationPolicyProvider()
          Returns the installed serialization policy provider.
 java.lang.Object getService()
          Return target service.
 javax.servlet.ServletContext getServletContext()
          Returns the servlet context
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 cause)
          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, java.lang.Object[] parameters, 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.
 org.springframework.web.servlet.ModelAndView handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Implementation of inherited interface
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)
           
protected  void preprocessHTTP(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Disables HTTP response caching by modifying response headers for browsers.
 java.lang.String processCall(java.lang.String payload)
          Overridden from RemoteServiceServlet and invoked by the servlet code.
protected  java.lang.String processResponse(java.lang.String response)
          Post-processes an RPC response.
 void setBeanName(java.lang.String beanName)
           
protected  void setCompressResponse(int compressResponse)
          Enables or disables compression of RPC output.
 void setModulePathTranslation(ModulePathTranslation modulePathTranslation)
           
 void setResponseCachingDisabled(boolean disableResponseCaching)
          Can be used to set HTTP response headers that explicitly disable caching on the browser side.
 void setSerializationFlags(int serializationFlags)
          Set serialization flags (see AbstractSerializationStream.getFlags().
 void setSerializationPolicyProvider(SerializationPolicyProvider serializationPolicyProvider)
          Assign a new serialization policy provider.
 void setService(java.lang.Object service)
          Set the wrapped service bean.
 void setServiceInterfaces(java.lang.Class<com.google.gwt.user.client.rpc.RemoteService>[] serviceInterfaces)
          Specifies the interfaces which must be implemented by the service bean.
 void setServletConfig(javax.servlet.ServletConfig servletConfig)
          Setter for servlet configuration
 void setServletContext(javax.servlet.ServletContext servletContext)
          Implementation of ServletContextAware, is invoked by the Spring application context.
 void setShouldCheckPermutationStrongName(boolean shouldCheckPermutationStrongName)
          Specify whether RemoteServiceServlet.checkPermutationStrongName() should be invoked for the current request.
 void setThrowUndeclaredExceptionToServletContainer(boolean throwUndeclaredExceptionToServletContainer)
          When enabled will throw exceptions which originate from the service and have not been declared in the RPC interface back to the servlet container.
protected  boolean shouldCompressResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String responsePayload)
           
 java.lang.String toString()
           
 
Methods inherited from class com.google.gwt.user.server.rpc.RemoteServiceServlet
getSerializationPolicy, processPost
 
Methods inherited from class com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet
doPost, getPermutationStrongName, getThreadLocalRequest, getThreadLocalResponse, onAfterRequestDeserialized, readContent
 
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, 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

disableResponseCaching

protected boolean disableResponseCaching

throwUndeclaredExceptionToServletContainer

protected boolean throwUndeclaredExceptionToServletContainer

beanName

protected java.lang.String beanName

serializationPolicyProvider

protected SerializationPolicyProvider serializationPolicyProvider

serializationFlags

protected int serializationFlags

shouldCheckPermutationStrongName

protected boolean shouldCheckPermutationStrongName

modulePathTranslation

protected ModulePathTranslation modulePathTranslation

methodCache

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

GWTRPCServiceExporter

public GWTRPCServiceExporter()
Method Detail

setModulePathTranslation

public void setModulePathTranslation(ModulePathTranslation modulePathTranslation)

checkPermutationStrongName

protected void checkPermutationStrongName()
Overrides:
checkPermutationStrongName in class com.google.gwt.user.server.rpc.RemoteServiceServlet

setShouldCheckPermutationStrongName

public void setShouldCheckPermutationStrongName(boolean shouldCheckPermutationStrongName)
Description copied from interface: RPCServiceExporter
Specify whether RemoteServiceServlet.checkPermutationStrongName() should be invoked for the current request. Disabled by default for backwards compatibility with older SL versions.

Specified by:
setShouldCheckPermutationStrongName in interface RPCServiceExporter

getSerializationFlags

public int getSerializationFlags()
Return the set serialization flags (see AbstractSerializationStream.getFlags()

Returns:

setSerializationFlags

public void setSerializationFlags(int serializationFlags)
Set serialization flags (see AbstractSerializationStream.getFlags(). Default value is AbstractSerializationStream.DEFAULT_FLAGS

Parameters:
serializationFlags -

processResponse

protected java.lang.String processResponse(java.lang.String response)
Post-processes an RPC response. Default method returns the method's argument

Parameters:
response - Response
Returns:
Possibly changed response

doUnexpectedFailure

protected void doUnexpectedFailure(java.lang.Throwable e)
Overrides:
doUnexpectedFailure in class com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet

preprocessHTTP

protected void preprocessHTTP(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
Disables HTTP response caching by modifying response headers for browsers. Can be overridden by extending classes to change behaviour.

Parameters:
request -
response -

getSerializationPolicyProvider

public SerializationPolicyProvider getSerializationPolicyProvider()
Returns the installed serialization policy provider. If none other was specified, the DefaultSerializationPolicyProvider is used

Returns:

setSerializationPolicyProvider

public void setSerializationPolicyProvider(SerializationPolicyProvider serializationPolicyProvider)
Assign a new serialization policy provider.

Parameters:
serializationPolicyProvider -

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()
Returns the servlet context

Specified by:
getServletContext in interface javax.servlet.ServletConfig
Overrides:
getServletContext in class javax.servlet.GenericServlet
Returns:
ServletContext

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)

encodeResponseForFailure

protected java.lang.String encodeResponseForFailure(com.google.gwt.user.server.rpc.RPCRequest rpcRequest,
                                                    java.lang.Throwable cause,
                                                    java.lang.reflect.Method targetMethod,
                                                    java.lang.Object[] targetParameters)
                                             throws com.google.gwt.user.client.rpc.SerializationException
Wrapper around RPC utility invocation

Parameters:
rpcRequest - RPCRequest
cause - Exception to handle
targetMethod - Method which threw the exception
targetParameters - Method arguments
Returns:
RPC payload
Throws:
java.lang.Exception
com.google.gwt.user.client.rpc.SerializationException

handleInvocationTargetException

protected java.lang.String handleInvocationTargetException(java.lang.reflect.InvocationTargetException e,
                                                           java.lang.Object service,
                                                           java.lang.reflect.Method targetMethod,
                                                           java.lang.Object[] parameters,
                                                           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 -
parameters -
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 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:
Method to invoke.
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 cause)
                                                             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 wrapped service bean. RPC requests are decoded and the corresponding method of the service object is invoked.

Specified by:
setService in interface RPCServiceExporter
Parameters:
service - Service to which the decoded requests are forwarded

handleRequest

public org.springframework.web.servlet.ModelAndView handleRequest(javax.servlet.http.HttpServletRequest request,
                                                                  javax.servlet.http.HttpServletResponse response)
                                                           throws javax.servlet.ServletException,
                                                                  java.io.IOException
Implementation of inherited interface

Specified by:
handleRequest in interface org.springframework.web.servlet.mvc.Controller
Throws:
javax.servlet.ServletException
java.io.IOException
See Also:
HttpRequestHandler#handleRequest(HttpServletRequest, HttpServletResponse)}

setServiceInterfaces

public void setServiceInterfaces(java.lang.Class<com.google.gwt.user.client.rpc.RemoteService>[] 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. Implementation note: as methods are only lazily bound to the service implementation you may get away with mismatches between the specified interfaces and the actual implementation as long as no method is invoked which has a different/missing signature in the interface and the service implementation.

Specified by:
setServiceInterfaces in interface RPCServiceExporter
Parameters:
serviceInterfaces -

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Should be invoked after all properties have been set. Normally invoked by the Spring application context setup.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception
See Also:
InitializingBean.afterPropertiesSet()

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 -

setResponseCachingDisabled

public void setResponseCachingDisabled(boolean disableResponseCaching)
Can be used to set HTTP response headers that explicitly disable caching on the browser side. Note that due to the additional headers the response size increases.

Specified by:
setResponseCachingDisabled in interface RPCServiceExporter
Parameters:
responseCaching -

setThrowUndeclaredExceptionToServletContainer

public void setThrowUndeclaredExceptionToServletContainer(boolean throwUndeclaredExceptionToServletContainer)
When enabled will throw exceptions which originate from the service and have not been declared in the RPC interface back to the servlet container.

Specified by:
setThrowUndeclaredExceptionToServletContainer in interface RPCServiceExporter
Parameters:
throwUndeclaredExceptionToServletContainer - Defaults to false

setServletConfig

public void setServletConfig(javax.servlet.ServletConfig servletConfig)
Setter for servlet configuration

Specified by:
setServletConfig in interface org.springframework.web.context.ServletConfigAware

setBeanName

public void setBeanName(java.lang.String beanName)
Specified by:
setBeanName in interface org.springframework.beans.factory.BeanNameAware

doGetSerializationPolicy

protected com.google.gwt.user.server.rpc.SerializationPolicy doGetSerializationPolicy(javax.servlet.http.HttpServletRequest request,
                                                                                      java.lang.String moduleBaseURL,
                                                                                      java.lang.String strongName)
Overrides:
doGetSerializationPolicy in class com.google.gwt.user.server.rpc.RemoteServiceServlet

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2011. All Rights Reserved.