com.google.gwt.user.server.rpc
Class RemoteServiceServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.google.gwt.user.server.rpc.RemoteServiceServlet
All Implemented Interfaces:
SerializationPolicyProvider, java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class RemoteServiceServlet
extends javax.servlet.http.HttpServlet
implements SerializationPolicyProvider

The servlet base class for your RPC service implementations that automatically deserializes incoming requests from the client and serializes outgoing responses for client/server RPCs.

See Also:
Serialized Form

Constructor Summary
RemoteServiceServlet()
          The default constructor.
 
Method Summary
protected  SerializationPolicy doGetSerializationPolicy(javax.servlet.http.HttpServletRequest request, java.lang.String moduleBaseURL, java.lang.String strongName)
          Gets the SerializationPolicy for given module base URL and strong name if there is one.
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Standard HttpServlet method: handle the POST.
protected  void doUnexpectedFailure(java.lang.Throwable e)
          Override this method to control what should happen when an exception escapes the processCall(String) method.
 SerializationPolicy getSerializationPolicy(java.lang.String moduleBaseURL, java.lang.String strongName)
          Returns a SerializationPolicy for a given module base URL and serialization policy strong name.
protected  javax.servlet.http.HttpServletRequest getThreadLocalRequest()
          Gets the HttpServletRequest object for the current call.
protected  javax.servlet.http.HttpServletResponse getThreadLocalResponse()
          Gets the HttpServletResponse object for the current call.
protected  void onAfterResponseSerialized(java.lang.String serializedResponse)
          Override this method to examine the serialized response that will be returned to the client.
protected  void onBeforeRequestDeserialized(java.lang.String serializedRequest)
          Override this method to examine the serialized version of the request payload before it is deserialized into objects.
 java.lang.String processCall(java.lang.String payload)
          Process a call originating from the given request.
protected  boolean shouldCompressResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String responsePayload)
          Determines whether the response to a given servlet request should or should not be GZIP compressed.
 
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, getServletContext, getServletInfo, getServletName, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RemoteServiceServlet

public RemoteServiceServlet()
The default constructor.

Method Detail

doPost

public final void doPost(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response)
Standard HttpServlet method: handle the POST. This doPost method swallows ALL exceptions, logs them in the ServletContext, and returns a GENERIC_FAILURE_MSG response with status code 500.

Overrides:
doPost in class javax.servlet.http.HttpServlet

getSerializationPolicy

public final SerializationPolicy getSerializationPolicy(java.lang.String moduleBaseURL,
                                                        java.lang.String strongName)
Description copied from interface: SerializationPolicyProvider
Returns a SerializationPolicy for a given module base URL and serialization policy strong name.

Specified by:
getSerializationPolicy in interface SerializationPolicyProvider
Parameters:
moduleBaseURL - the URL for the module
strongName - strong name of the serialization policy for the specified module URL
Returns:
a SerializationPolicy for a given module base URL and RPC strong name; must not return null

processCall

public java.lang.String processCall(java.lang.String payload)
                             throws SerializationException
Process a call originating from the given request. Uses the RPC.invokeAndEncodeResponse(Object, java.lang.reflect.Method, Object[]) method to do the actual work.

Subclasses may optionally override this method to handle the payload in any way they desire (by routing the request to a framework component, for instance). The HttpServletRequest and HttpServletResponse can be accessed via the getThreadLocalRequest() and getThreadLocalResponse() methods.

This is public so that it can be unit tested easily without HTTP.

Parameters:
payload - the UTF-8 request payload
Returns:
a string which encodes either the method's return, a checked exception thrown by the method, or an IncompatibleRemoteServiceException
Throws:
SerializationException - if we cannot serialize the response
UnexpectedException - if the invocation throws a checked exception that is not declared in the service method's signature
java.lang.RuntimeException - if the service method throws an unchecked exception (the exception will be the one thrown by the service)

doGetSerializationPolicy

protected SerializationPolicy doGetSerializationPolicy(javax.servlet.http.HttpServletRequest request,
                                                       java.lang.String moduleBaseURL,
                                                       java.lang.String strongName)
Gets the SerializationPolicy for given module base URL and strong name if there is one. Override this method to provide a SerializationPolicy using an alternative approach.

Parameters:
request - the HTTP request being serviced
moduleBaseURL - as specified in the incoming payload
strongName - a strong name that uniquely identifies a serialization policy file
Returns:
a SerializationPolicy for the given module base URL and strong name, or null if there is none

doUnexpectedFailure

protected void doUnexpectedFailure(java.lang.Throwable e)
Override this method to control what should happen when an exception escapes the processCall(String) method. The default implementation will log the failure and send a generic failure response to the client.

An "expected failure" is an exception thrown by a service method that is declared in the signature of the service method. These exceptions are serialized back to the client, and are not passed to this method. This method is called only for exceptions or errors that are not part of the service method's signature, or that result from SecurityExceptions, SerializationExceptions, or other failures within the RPC framework.

Note that if the desired behavior is to both send the GENERIC_FAILURE_MSG response AND to rethrow the exception, then this method should first send the GENERIC_FAILURE_MSG response itself (using getThreadLocalResponse), and then rethrow the exception. Rethrowing the exception will cause it to escape into the servlet container.

Parameters:
e - the exception which was thrown

getThreadLocalRequest

protected final javax.servlet.http.HttpServletRequest getThreadLocalRequest()
Gets the HttpServletRequest object for the current call. It is stored thread-locally so that simultaneous invocations can have different request objects.


getThreadLocalResponse

protected final javax.servlet.http.HttpServletResponse getThreadLocalResponse()
Gets the HttpServletResponse object for the current call. It is stored thread-locally so that simultaneous invocations can have different response objects.


onAfterResponseSerialized

protected void onAfterResponseSerialized(java.lang.String serializedResponse)
Override this method to examine the serialized response that will be returned to the client. The default implementation does nothing and need not be called by subclasses.


onBeforeRequestDeserialized

protected void onBeforeRequestDeserialized(java.lang.String serializedRequest)
Override this method to examine the serialized version of the request payload before it is deserialized into objects. The default implementation does nothing and need not be called by subclasses.


shouldCompressResponse

protected boolean shouldCompressResponse(javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response,
                                         java.lang.String responsePayload)
Determines whether the response to a given servlet request should or should not be GZIP compressed. This method is only called in cases where the requestor accepts GZIP encoding.

This implementation currently returns true if the response string's estimated byte length is longer than 256 bytes. Subclasses can override this logic.

Parameters:
request - the request being served
response - the response that will be written into
responsePayload - the payload that is about to be sent to the client
Returns:
true if responsePayload should be GZIP compressed, otherwise false.