use of com.google.gwt.user.server.rpc.RPCRequest in project pentaho-platform by pentaho.
the class AbstractGwtRpcRequestMatcherTest method createGwtRpcMock.
@NonNull
private AbstractGwtRpc createGwtRpcMock(@NonNull String serviceMethodName) throws NoSuchMethodException {
Method serviceMethodMock = this.getClass().getMethod(serviceMethodName);
RPCRequest rpcRequestMock = new RPCRequest(serviceMethodMock, new Object[] {}, null, 0);
AbstractGwtRpc gwtRpcMock = mock(AbstractGwtRpc.class);
when(gwtRpcMock.getRequest()).thenReturn(rpcRequestMock);
return gwtRpcMock;
}
use of com.google.gwt.user.server.rpc.RPCRequest in project pentaho-platform by pentaho.
the class AbstractGwtRpcProxyServlet method processCall.
@Override
public String processCall(String payload) throws SerializationException {
Map<Class<?>, Boolean> whitelist = new HashMap<Class<?>, Boolean>();
whitelist.put(GwtRpcProxyException.class, Boolean.TRUE);
Map<Class<?>, String> obfuscatedTypeIds = new HashMap<Class<?>, String>();
StandardSerializationPolicy policy = new StandardSerializationPolicy(whitelist, whitelist, obfuscatedTypeIds);
String servletContextPath = getServletContextPath();
Object target = null;
try {
target = resolveDispatchTarget(servletContextPath);
} catch (GwtRpcProxyException ex) {
logger.error(Messages.getInstance().getErrorString("AbstractGwtRpcProxyServlet.ERROR_0001_FAILED_TO_RESOLVE_DISPATCH_TARGET", servletContextPath), // $NON-NLS-1$
ex);
return RPC.encodeResponseForFailure(null, ex, policy);
}
final ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
final ClassLoader altLoader = target.getClass().getClassLoader();
try {
// class specified in the request
if (altLoader != origLoader) {
Thread.currentThread().setContextClassLoader(altLoader);
}
RPCRequest rpcRequest = RPC.decodeRequest(payload, null, this);
onAfterRequestDeserialized(rpcRequest);
// don't require the server side to implement the service interface
Method method = rpcRequest.getMethod();
try {
Method m = target.getClass().getMethod(method.getName(), method.getParameterTypes());
if (m != null) {
method = m;
}
} catch (Exception e) {
e.printStackTrace();
}
return RPC.invokeAndEncodeResponse(target, method, rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
} catch (IncompatibleRemoteServiceException ex) {
logger.error(Messages.getInstance().getErrorString("AbstractGwtRpcProxyServlet.ERROR_0003_RPC_INVOCATION_FAILED", target.getClass().getName()), // $NON-NLS-1$
ex);
return RPC.encodeResponseForFailure(null, ex);
} finally {
// reset the context classloader if necessary
if ((altLoader != origLoader) && origLoader != null) {
Thread.currentThread().setContextClassLoader(origLoader);
}
}
}
use of com.google.gwt.user.server.rpc.RPCRequest in project pentaho-platform by pentaho.
the class AbstractGwtRpc method invoke.
// endregion
// region Invocation
/**
* Makes the remote call on the target object and returns the serialized response.
*
* @return The serialized response.
* @throws GwtRpcProxyException if the call does not succeed.
*/
@NonNull
public String invoke() {
Object target = getTarget();
Class<?> targetClass = target.getClass();
RPCRequest rpcRequest = getRequest();
try {
Method targetMethod = getTargetMethod(targetClass, rpcRequest);
return GwtRpcUtil.withClassLoaderThrowing(// Making it this way, effectively repeating getTarget(), makes it easier to test.
getTargetClassLoader(), () -> invokeCore(target, targetMethod, rpcRequest));
} catch (NoSuchMethodException | SerializationException ex) {
String message = Messages.getInstance().getErrorString("AbstractGwtRpcProxyServlet.ERROR_0003_RPC_INVOCATION_FAILED", targetClass.getName());
logger.error(message, ex);
throw new GwtRpcProxyException(message, ex);
}
}
use of com.google.gwt.user.server.rpc.RPCRequest in project pentaho-platform by pentaho.
the class AbstractGwtRpcTest method testInvokeServiceClassDoesNotNeedToImplementServiceInterface.
@Test
public void testInvokeServiceClassDoesNotNeedToImplementServiceInterface() throws NoSuchMethodException, SerializationException {
ServiceClassWhichDoesNotImplementInterface target = new ServiceClassWhichDoesNotImplementInterface();
Method targetMethod = target.getClass().getMethod("method", String.class);
ClassLoader targetClassLoader = mock(ClassLoader.class);
Method serviceMethod = ServiceInterface.class.getMethod("method", String.class);
Object[] rpcParameters = new Object[] { "id1" };
SerializationPolicy policy = mock(SerializationPolicy.class);
RPCRequest rpcRequest = new RPCRequest(serviceMethod, rpcParameters, policy, 0);
HttpServletRequest httpRequestMock = mock(HttpServletRequest.class);
String response = "rpc response";
TestGwtRpc gwtRpcSpy = spy(new TestGwtRpc(httpRequestMock));
doReturn(target).when(gwtRpcSpy).getTarget();
doReturn(targetClassLoader).when(gwtRpcSpy).getTargetClassLoader();
doReturn(rpcRequest).when(gwtRpcSpy).getRequest();
// Proven if the correct targetMethod is received at this call.
try (MockedStatic<RPC> rpc = Mockito.mockStatic(RPC.class)) {
rpc.when(() -> RPC.invokeAndEncodeResponse(target, targetMethod, rpcParameters, policy)).thenReturn(response);
String result = gwtRpcSpy.invoke();
assertEquals(response, result);
rpc.verify(() -> RPC.invokeAndEncodeResponse(target, targetMethod, rpcParameters, policy));
RPC.invokeAndEncodeResponse(target, targetMethod, rpcParameters, policy);
}
}
use of com.google.gwt.user.server.rpc.RPCRequest in project pentaho-platform by pentaho.
the class AbstractGwtRpcTest method testGetRequestRunsWithTargetClassLoader.
@Test
public void testGetRequestRunsWithTargetClassLoader() {
String requestPayload = "REQUEST_PAYLOAD";
ClassLoader targetClassLoader = mock(ClassLoader.class);
RPCRequest rpcRequest = new RPCRequest(null, null, null, 0);
HttpServletRequest httpRequestMock = mock(HttpServletRequest.class);
TestGwtRpc gwtRpcSpy = spy(new TestGwtRpc(httpRequestMock));
doReturn(targetClassLoader).when(gwtRpcSpy).getTargetClassLoader();
doReturn(requestPayload).when(gwtRpcSpy).getRequestPayload();
// Stub getRequestCore with:
doAnswer((Answer<RPCRequest>) invocationOnMock -> {
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
assertEquals(targetClassLoader, currentClassLoader);
return rpcRequest;
}).when(gwtRpcSpy).getRequestCore(requestPayload);
gwtRpcSpy.getRequest();
verify(gwtRpcSpy).getRequestCore(requestPayload);
}
Aggregations