Google Web Toolkit (Beta)
Search this site:

# Class RemoteServiceServlet

public class RemoteServiceServlet
extends HttpServlet
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.

## Constructors

RemoteServiceServlet() | The default constructor.

## Methods

doPost(HttpServletRequest, HttpServletResponse) | Standard HttpServlet method: handle the POST.
doUnexpectedFailure(Throwable) | Override this method to control what should happen when an exception escapes the processCall(String) method.
getThreadLocalRequest() | Gets the `HttpServletRequest` object for the current call.
getThreadLocalResponse() | Gets the `HttpServletResponse` object for the current call.
onAfterResponseSerialized(String) | Override this method to examine the serialized response that will be returned to the client.
onBeforeRequestDeserialized(String) | Override this method to examine the serialized version of the request payload before it is deserialized into objects.
processCall(String) | Process a call originating from the given request.
shouldCompressResponse(HttpServletRequest, HttpServletResponse, String) | Determines whether the response to a given servlet request should or should not be GZIP compressed.

## Constructor Detail

### RemoteServiceServlet

public RemoteServiceServlet()
The default constructor.

## Method Detail

### doPost

public final void doPost(HttpServletRequest request, 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.

#### Parameters

request
response

### doUnexpectedFailure

protected void doUnexpectedFailure(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 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 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(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.

#### Parameters

serializedResponse

### onBeforeRequestDeserialized

protected void onBeforeRequestDeserialized(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.

#### Parameters

serializedRequest

### processCall

public String processCall(String payload)
throws SerializationException
Process a call originating from the given request. Uses the 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

#### Return Value

a string which encodes either the method's return, a checked exception thrown by the method, or an IncompatibleRemoteServiceException

### shouldCompressResponse

protected boolean shouldCompressResponse(HttpServletRequest request, HttpServletResponse response, 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

#### Return Value

`true` if responsePayload should be GZIP compressed, otherwise `false`.