Class RPC
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);
}
}
-
Method Summary
Modifier and TypeMethodDescriptionstatic RPCRequestdecodeRequest(String encodedRequest) Returns anRPCRequestthat is built by decoding the contents of an encoded RPC request.static RPCRequestdecodeRequest(String encodedRequest, Class<?> type) Returns anRPCRequestthat is built by decoding the contents of an encoded RPC request and optionally validating that type can handle the request.static RPCRequestdecodeRequest(String encodedRequest, Class<?> type, SerializationPolicyProvider serializationPolicyProvider) Returns anRPCRequestthat is built by decoding the contents of an encoded RPC request and optionally validating that type can handle the request.static StringencodeResponseForFailedRequest(RPCRequest rpcRequest, Throwable cause) Returns a string that encodes an exception.static StringencodeResponseForFailure(Method serviceMethod, Throwable cause) Returns a string that encodes an exception.static StringencodeResponseForFailure(Method serviceMethod, Throwable cause, SerializationPolicy serializationPolicy) Returns a string that encodes an exception.static StringencodeResponseForFailure(Method serviceMethod, Throwable cause, SerializationPolicy serializationPolicy, int flags) static StringencodeResponseForSuccess(Method serviceMethod, Object object) Returns a string that encodes the object.static StringencodeResponseForSuccess(Method serviceMethod, Object object, SerializationPolicy serializationPolicy) Returns a string that encodes the object.static StringencodeResponseForSuccess(Method serviceMethod, Object object, SerializationPolicy serializationPolicy, int flags) static SerializationPolicyReturns a default serialization policy.static StringinvokeAndEncodeResponse(Object target, Method serviceMethod, 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 StringinvokeAndEncodeResponse(Object target, Method serviceMethod, 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.static StringinvokeAndEncodeResponse(Object target, Method serviceMethod, Object[] args, SerializationPolicy serializationPolicy, int flags)
-
Method Details
-
decodeRequest
Returns anRPCRequestthat is built by decoding the contents of an encoded RPC request.This method is equivalent to calling
decodeRequest(String, Class)withnullfor the type parameter.- Parameters:
encodedRequest- a string that encodes theRemoteServiceinterface, the service method to call, and the arguments to for the service method- Returns:
- an
RPCRequestinstance - Throws:
IncompatibleRemoteServiceException- if any of the following conditions apply:- if the types in the encoded request cannot be deserialized
- if the
ClassLoaderacquired fromThread.currentThread().getContextClassLoader()cannot load the service interface or any of the types specified in the encodedRequest - the requested interface is not assignable to
RemoteService - the service method requested in the encodedRequest is not a member of the requested service interface
- the type parameter is not
nulland is not assignable to the requestedRemoteServiceinterface
-
decodeRequest
Returns anRPCRequestthat 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 notnull, the implementation checks that the type is assignable to theRemoteServiceinterface requested in the encoded request string.Invoking this method with
nullfor the type parameter,decodeRequest(encodedRequest, null), is equivalent to callingdecodeRequest(encodedRequest).- Parameters:
encodedRequest- a string that encodes theRemoteServiceinterface, the service method, and the arguments to pass to the service methodtype- if notnull, the implementation checks that the type is assignable to theRemoteServiceinterface encoded in the encoded request string.- Returns:
- an
RPCRequestinstance - Throws:
NullPointerException- if the encodedRequest isnullIllegalArgumentException- if the encodedRequest is an empty stringIncompatibleRemoteServiceException- if any of the following conditions apply:- if the types in the encoded request cannot be deserialized
- if the
ClassLoaderacquired fromThread.currentThread().getContextClassLoader()cannot load the service interface or any of the types specified in the encodedRequest - the requested interface is not assignable to
RemoteService - the service method requested in the encodedRequest is not a member of the requested service interface
- the type parameter is not
nulland is not assignable to the requestedRemoteServiceinterface
-
decodeRequest
public static RPCRequest decodeRequest(String encodedRequest, Class<?> type, SerializationPolicyProvider serializationPolicyProvider) Returns anRPCRequestthat 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 notnull, the implementation checks that the type is assignable to theRemoteServiceinterface requested in the encoded request string.If the serializationPolicyProvider parameter is not
null, it is asked for aSerializationPolicyto use to restrict the set of types that can be decoded from the request. If this parameter isnull, then only subtypes ofIsSerializableor types which have custom field serializers can be decoded.Invoking this method with
nullfor the type parameter,decodeRequest(encodedRequest, null), is equivalent to callingdecodeRequest(encodedRequest).- Parameters:
encodedRequest- a string that encodes theRemoteServiceinterface, the service method, and the arguments to pass to the service methodtype- if notnull, the implementation checks that the type is assignable to theRemoteServiceinterface encoded in the encoded request string.serializationPolicyProvider- if notnull, the implementation asks this provider for aSerializationPolicywhich will be used to restrict the set of types that can be decoded from this request- Returns:
- an
RPCRequestinstance - Throws:
NullPointerException- if the encodedRequest isnullIllegalArgumentException- if the encodedRequest is an empty stringIncompatibleRemoteServiceException- if any of the following conditions apply:- if the types in the encoded request cannot be deserialized
- if the
ClassLoaderacquired fromThread.currentThread().getContextClassLoader()cannot load the service interface or any of the types specified in the encodedRequest - the requested interface is not assignable to
RemoteService - the service method requested in the encodedRequest is not a member of the requested service interface
- the type parameter is not
nulland is not assignable to the requestedRemoteServiceinterface
-
encodeResponseForFailedRequest
public static String encodeResponseForFailedRequest(RPCRequest rpcRequest, Throwable cause) throws SerializationException Returns a string that encodes an exception. IfrpcRequestisnulla default serialization policy and default request flags will be used. Otherwise these information are taken fromrpcRequest.This method should be used if the RPC request could not be decoded or could not be executed because of an exception thrown, e.g.
IncompatibleRemoteServiceException,invalid reference
RpcTokenException- Parameters:
rpcRequest- the RPCRequest that failed to execute, may be nullcause- theThrowablethat was thrown- Returns:
- a String that encodes the exception
- Throws:
SerializationException- if the result cannot be serialized
-
encodeResponseForFailure
public static String encodeResponseForFailure(Method serviceMethod, Throwable cause) throws SerializationException Returns a string that encodes an exception. If method is notnull, it is an error if the exception is not in the method's list of checked exceptions.- Parameters:
serviceMethod- the method that threw the exception, may benullcause- theThrowablethat was thrown- Returns:
- a string that encodes the exception
- Throws:
NullPointerException- if the cause isnullSerializationException- if the result cannot be serializedUnexpectedException- if the result was an unexpected exception (a checked exception not declared in the serviceMethod's signature)
-
encodeResponseForFailure
public static String encodeResponseForFailure(Method serviceMethod, Throwable cause, SerializationPolicy serializationPolicy) throws SerializationException Returns a string that encodes an exception. If method is notnull, 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 isnull, then only subtypes ofIsSerializableor types which have custom field serializers may be encoded.- Parameters:
serviceMethod- the method that threw the exception, may benullcause- theThrowablethat was thrownserializationPolicy- determines the serialization policy to be used- Returns:
- a string that encodes the exception
- Throws:
NullPointerException- if the cause or the serializationPolicy arenullSerializationException- if the result cannot be serializedUnexpectedException- if the result was an unexpected exception (a checked exception not declared in the serviceMethod's signature)
-
encodeResponseForFailure
public static String encodeResponseForFailure(Method serviceMethod, Throwable cause, SerializationPolicy serializationPolicy, int flags) throws SerializationException - Throws:
SerializationException
-
encodeResponseForSuccess
public static String encodeResponseForSuccess(Method serviceMethod, Object object) throws SerializationException Returns a string that encodes the object. It is an error to try to encode an object that is not assignable to the service method's return type.- Parameters:
serviceMethod- the method whose result we are encodingobject- the instance that we wish to encode- Returns:
- a string that encodes the object, if the object is compatible with the service method's declared return type
- Throws:
IllegalArgumentException- if the result is not assignable to the service method's return typeNullPointerException- if the service method isnullSerializationException- if the result cannot be serialized
-
encodeResponseForSuccess
public static String encodeResponseForSuccess(Method serviceMethod, Object object, SerializationPolicy serializationPolicy) throws SerializationException Returns a string that encodes the object. It is an error to try to encode an object that is not assignable to the service method's return type.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 isnull, then only subtypes ofIsSerializableor types which have custom field serializers may be encoded.- Parameters:
serviceMethod- the method whose result we are encodingobject- the instance that we wish to encodeserializationPolicy- determines the serialization policy to be used- Returns:
- a string that encodes the object, if the object is compatible with the service method's declared return type
- Throws:
IllegalArgumentException- if the result is not assignable to the service method's return typeNullPointerException- if the serviceMethod or the serializationPolicy arenullSerializationException- if the result cannot be serialized
-
encodeResponseForSuccess
public static String encodeResponseForSuccess(Method serviceMethod, Object object, SerializationPolicy serializationPolicy, int flags) throws SerializationException - Throws:
SerializationException
-
getDefaultSerializationPolicy
Returns a default serialization policy.- Returns:
- the default serialization policy.
-
invokeAndEncodeResponse
public static String invokeAndEncodeResponse(Object target, Method serviceMethod, Object[] args) throws SerializationException 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.This method does no security checking; security checking must be done on the method prior to this invocation.
- Parameters:
target- instance on which to invoke the serviceMethodserviceMethod- the method to invokeargs- arguments used for the method invocation- Returns:
- a string which encodes either the method's return or a checked exception thrown by the method
- Throws:
SecurityException- if the method cannot be accessed or if the number or type of actual and formal arguments differSerializationException- if an object could not be serialized by the streamUnexpectedException- if the serviceMethod throws a checked exception that is not declared in its signature
-
invokeAndEncodeResponse
public static String invokeAndEncodeResponse(Object target, Method serviceMethod, Object[] args, SerializationPolicy serializationPolicy) throws SerializationException 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.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 isnull, then only subtypes ofIsSerializableor 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.
- Parameters:
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- Returns:
- a string which encodes either the method's return or a checked exception thrown by the method
- Throws:
NullPointerException- if the serviceMethod or the serializationPolicy arenullSecurityException- if the method cannot be accessed or if the number or type of actual and formal arguments differSerializationException- if an object could not be serialized by the streamUnexpectedException- if the serviceMethod throws a checked exception that is not declared in its signature
-
invokeAndEncodeResponse
public static String invokeAndEncodeResponse(Object target, Method serviceMethod, Object[] args, SerializationPolicy serializationPolicy, int flags) throws SerializationException - Throws:
SerializationException
-