Class RPC

java.lang.Object
com.google.gwt.user.server.rpc.RPC

public final class RPC extends Object
Utility class for integrating with the RPC system. This class exposes methods for decoding of RPC requests, encoding of RPC responses, and invocation of RPC calls on service objects. The operations exposed by this class can be reused by framework implementors such as Spring and G4jsf to support a wide range of service invocation policies.

Canonical Example

The following example demonstrates the canonical way to use this class.
@Override
public String processCall(String payload) throws SerializationException {
  try {
    RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass());
    return RPC.invokeAndEncodeResponse(this, rpcRequest.getMethod(),
        rpcRequest.getParameters());
  } catch (IncompatibleRemoteServiceException ex) {
    return RPC.encodeResponseForFailure(null, ex);
  }
}

Advanced Example

The following example shows a more advanced way of using this class to create an adapter between GWT RPC entities and POJOs.
@Override
public void doPost(HttpServletRequest httpRequest,
    HttpServletResponse httpResponse) {
  String payload = readPayloadAsUtf8(httpRequest);

  try {
    try {
      RPCRequest rpcRequest = RPC.decodeRequest(payload);

      Object targetInstance = getInstanceToHandleRequest(httpRequest,
          rpcRequest);

      Method targetMethod = maybeMapRequestedMethod(targetInstance,
          rpcRequest.getMethod());

      Object[] targetParameters = maybeMapParameters(rpcRequest.getParameters());

      try {
        Object result = targetMethod.invoke(targetInstance, targetParameters);

        result = maybeMapResult(rpcRequest.getMethod(), result);

        /*
         * Encode the object that will be given to the client code's
         * AsyncCallback::onSuccess(Object) method.
         */
        String encodedResult = RPC.encodeResponseForSuccess(
            rpcRequest.getMethod(), result);

        sendResponseForSuccess(httpResponse, encodedResult);
      } catch (IllegalArgumentException e) {
        SecurityException securityException = new SecurityException(
            "Blocked attempt to invoke method " + targetMethod);
        securityException.initCause(e);
        throw securityException;
      } catch (IllegalAccessException e) {
        SecurityException securityException = new SecurityException(
            "Blocked attempt to access inaccessible method "
                + targetMethod
                + (targetInstance != null ? " on target " + targetInstance
                    : ""));
        securityException.initCause(e);
        throw securityException;
      } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();

        Throwable mappedThrowable = maybeMapThrowable(cause,
            rpcRequest.getMethod());

        /*
         * Encode the exception that will be passed back to the client's
         * client code's AsyncCallback::onFailure(Throwable) method.
         */
        String failurePayload = RPC.encodeResponseForFailure(
            rpcRequest.getMethod(), mappedThrowable);

        sendResponseForFailure(httpResponse, failurePayload);
      }
    } catch (IncompatibleRemoteServiceException e) {
      sendResponseForFailure(httpResponse, RPC.encodeResponseForFailure(null,
          e));
    }
  } catch (Throwable e) {
    /*
     * Return a generic error which will be passed to the client code's
     * AsyncCallback::onFailure(Throwable) method.
     */
    sendResponseForGenericFailure(httpResponse);
  }
}