|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.gwt.user.server.rpc.RPC
public final class RPC
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.
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); } }
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); } }
Method Summary | |
---|---|
static RPCRequest |
decodeRequest(java.lang.String encodedRequest)
Returns an RPCRequest that is built by decoding the contents of an
encoded RPC request. |
static RPCRequest |
decodeRequest(java.lang.String encodedRequest,
java.lang.Class type)
Returns an RPCRequest that is built by decoding the contents of an
encoded RPC request and optionally validating that type can handle the
request. |
static RPCRequest |
decodeRequest(java.lang.String encodedRequest,
java.lang.Class type,
SerializationPolicyProvider serializationPolicyProvider)
Returns an RPCRequest that is built by decoding the contents of an
encoded RPC request and optionally validating that type can handle the
request. |
static java.lang.String |
encodeResponseForFailure(java.lang.reflect.Method serviceMethod,
java.lang.Throwable cause)
Returns a string that encodes an exception. |
static java.lang.String |
encodeResponseForFailure(java.lang.reflect.Method serviceMethod,
java.lang.Throwable cause,
SerializationPolicy serializationPolicy)
Returns a string that encodes an exception. |
static java.lang.String |
encodeResponseForSuccess(java.lang.reflect.Method serviceMethod,
java.lang.Object object)
Returns a string that encodes the object. |
static java.lang.String |
encodeResponseForSuccess(java.lang.reflect.Method serviceMethod,
java.lang.Object object,
SerializationPolicy serializationPolicy)
Returns a string that encodes the object. |
static SerializationPolicy |
getDefaultSerializationPolicy()
Returns a default serialization policy. |
static java.lang.String |
invokeAndEncodeResponse(java.lang.Object target,
java.lang.reflect.Method serviceMethod,
java.lang.Object[] args)
Returns a string that encodes the result of calling a service method, which could be the value returned by the method or an exception thrown by it. |
static java.lang.String |
invokeAndEncodeResponse(java.lang.Object target,
java.lang.reflect.Method serviceMethod,
java.lang.Object[] args,
SerializationPolicy serializationPolicy)
Returns a string that encodes the result of calling a service method, which could be the value returned by the method or an exception thrown by it. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static RPCRequest decodeRequest(java.lang.String encodedRequest)
RPCRequest
that is built by decoding the contents of an
encoded RPC request.
This method is equivalent to calling decodeRequest(String, Class)
with null
for the type parameter.
encodedRequest
- a string that encodes the RemoteService
interface, the service method to call, and the arguments to for
the service method
RPCRequest
instance
IncompatibleRemoteServiceException
- if any of the following
conditions apply:
ClassLoader
acquired from
Thread.currentThread().getContextClassLoader()
cannot load the service interface or any of the types specified
in the encodedRequestRemoteService
null
and is not
assignable to the requested RemoteService
interface
public static RPCRequest decodeRequest(java.lang.String encodedRequest, java.lang.Class type)
RPCRequest
that is built by decoding the contents of an
encoded RPC request and optionally validating that type can handle the
request. If the type parameter is not null
, the
implementation checks that the type is assignable to the
RemoteService
interface requested in the encoded request string.
Invoking this method with null
for the type parameter,
decodeRequest(encodedRequest, null)
, is equivalent to
calling decodeRequest(encodedRequest)
.
encodedRequest
- a string that encodes the RemoteService
interface, the service method, and the arguments to pass to the
service methodtype
- if not null
, the implementation checks that the
type is assignable to the RemoteService
interface encoded
in the encoded request string.
RPCRequest
instance
java.lang.NullPointerException
- if the encodedRequest is null
java.lang.IllegalArgumentException
- if the encodedRequest is an empty string
IncompatibleRemoteServiceException
- if any of the following
conditions apply:
ClassLoader
acquired from
Thread.currentThread().getContextClassLoader()
cannot load the service interface or any of the types specified
in the encodedRequestRemoteService
null
and is not
assignable to the requested RemoteService
interface
public static RPCRequest decodeRequest(java.lang.String encodedRequest, java.lang.Class type, SerializationPolicyProvider serializationPolicyProvider)
RPCRequest
that is built by decoding the contents of an
encoded RPC request and optionally validating that type can handle the
request. If the type parameter is not null
, the
implementation checks that the type is assignable to the
RemoteService
interface requested in the encoded request string.
If the serializationPolicyProvider parameter is not null
, it is
asked for a SerializationPolicy
to use to restrict the set of types
that can be decoded from the request. If this parameter is
null
, then only subtypes of
IsSerializable
or
types which have custom field serializers can be decoded.
Invoking this method with null
for the type parameter,
decodeRequest(encodedRequest, null)
, is equivalent to
calling decodeRequest(encodedRequest)
.
encodedRequest
- a string that encodes the RemoteService
interface, the service method, and the arguments to pass to the
service methodtype
- if not null
, the implementation checks that the
type is assignable to the RemoteService
interface encoded
in the encoded request string.serializationPolicyProvider
- if not null
, the
implementation asks this provider for a
SerializationPolicy
which will be used to restrict the set
of types that can be decoded from this request
RPCRequest
instance
java.lang.NullPointerException
- if the encodedRequest is null
java.lang.IllegalArgumentException
- if the encodedRequest is an empty string
IncompatibleRemoteServiceException
- if any of the following
conditions apply:
ClassLoader
acquired from
Thread.currentThread().getContextClassLoader()
cannot load the service interface or any of the types specified
in the encodedRequestRemoteService
null
and is not
assignable to the requested RemoteService
interface
public static java.lang.String encodeResponseForFailure(java.lang.reflect.Method serviceMethod, java.lang.Throwable cause) throws SerializationException
null
, it is an error if the exception is not in the
method's list of checked exceptions.
serviceMethod
- the method that threw the exception, may be
null
cause
- the Throwable
that was thrown
java.lang.NullPointerException
- if the the cause is null
SerializationException
- if the result cannot be serialized
UnexpectedException
- if the result was an unexpected exception (a
checked exception not declared in the serviceMethod's signature)public static java.lang.String encodeResponseForFailure(java.lang.reflect.Method serviceMethod, java.lang.Throwable cause, SerializationPolicy serializationPolicy) throws SerializationException
null
, it is an error if the exception is not in the
method's list of checked exceptions.
If the serializationPolicy parameter is not null
, it is used to
determine what types can be encoded as part of this response. If this
parameter is null
, then only subtypes of
IsSerializable
or
types which have custom field serializers may be encoded.
serviceMethod
- the method that threw the exception, may be
null
cause
- the Throwable
that was thrownserializationPolicy
- determines the serialization policy to be used
java.lang.NullPointerException
- if the the cause or the serializationPolicy
are null
SerializationException
- if the result cannot be serialized
UnexpectedException
- if the result was an unexpected exception (a
checked exception not declared in the serviceMethod's signature)public static java.lang.String encodeResponseForSuccess(java.lang.reflect.Method serviceMethod, java.lang.Object object) throws SerializationException
serviceMethod
- the method whose result we are encodingobject
- the instance that we wish to encode
java.lang.IllegalArgumentException
- if the result is not assignable to the
service method's return type
java.lang.NullPointerException
- if the service method is null
SerializationException
- if the result cannot be serializedpublic static java.lang.String encodeResponseForSuccess(java.lang.reflect.Method serviceMethod, java.lang.Object object, SerializationPolicy serializationPolicy) throws SerializationException
If the serializationPolicy parameter is not null
, it is used to
determine what types can be encoded as part of this response. If this
parameter is null
, then only subtypes of
IsSerializable
or
types which have custom field serializers may be encoded.
serviceMethod
- the method whose result we are encodingobject
- the instance that we wish to encodeserializationPolicy
- determines the serialization policy to be used
java.lang.IllegalArgumentException
- if the result is not assignable to the
service method's return type
java.lang.NullPointerException
- if the serviceMethod or the
serializationPolicy are null
SerializationException
- if the result cannot be serializedpublic static SerializationPolicy getDefaultSerializationPolicy()
public static java.lang.String invokeAndEncodeResponse(java.lang.Object target, java.lang.reflect.Method serviceMethod, java.lang.Object[] args) throws SerializationException
This method does no security checking; security checking must be done on the method prior to this invocation.
target
- instance on which to invoke the serviceMethodserviceMethod
- the method to invokeargs
- arguments used for the method invocation
java.lang.SecurityException
- if the method cannot be accessed or if the number
or type of actual and formal arguments differ
SerializationException
- if an object could not be serialized by the
stream
UnexpectedException
- if the serviceMethod throws a checked exception
that is not declared in its signaturepublic static java.lang.String invokeAndEncodeResponse(java.lang.Object target, java.lang.reflect.Method serviceMethod, java.lang.Object[] args, SerializationPolicy serializationPolicy) throws SerializationException
If the serializationPolicy parameter is not null
, it is used to
determine what types can be encoded as part of this response. If this
parameter is null
, then only subtypes of
IsSerializable
or
types which have custom field serializers may be encoded.
This method does no security checking; security checking must be done on the method prior to this invocation.
target
- instance on which to invoke the serviceMethodserviceMethod
- the method to invokeargs
- arguments used for the method invocationserializationPolicy
- determines the serialization policy to be used
java.lang.NullPointerException
- if the serviceMethod or the
serializationPolicy are null
java.lang.SecurityException
- if the method cannot be accessed or if the number
or type of actual and formal arguments differ
SerializationException
- if an object could not be serialized by the
stream
UnexpectedException
- if the serviceMethod throws a checked exception
that is not declared in its signature
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |