Search in sources :

Example 1 with SerializationException

use of com.google.gwt.user.client.rpc.SerializationException in project pentaho-platform by pentaho.

the class AbstractGwtRpcTest method testInvokeServiceClassLogsAndThrowsIfRPCThrowsSerializationException.

@Test
public void testInvokeServiceClassLogsAndThrowsIfRPCThrowsSerializationException() throws NoSuchMethodException {
    ServiceClassWhichDoesNotImplementInterface target = new ServiceClassWhichDoesNotImplementInterface();
    Method targetMethod = target.getClass().getMethod("method", String.class);
    ClassLoader targetClassLoader = mock(ClassLoader.class);
    Method serviceSpecialMethod = ServiceInterface.class.getMethod("method", String.class);
    Object[] rpcParameters = new Object[] { "id1" };
    SerializationPolicy policy = mock(SerializationPolicy.class);
    RPCRequest rpcRequest = new RPCRequest(serviceSpecialMethod, rpcParameters, policy, 0);
    HttpServletRequest httpRequestMock = mock(HttpServletRequest.class);
    TestGwtRpc gwtRpcSpy = spy(new TestGwtRpc(httpRequestMock));
    doReturn(target).when(gwtRpcSpy).getTarget();
    doReturn(targetClassLoader).when(gwtRpcSpy).getTargetClassLoader();
    doReturn(rpcRequest).when(gwtRpcSpy).getRequest();
    SerializationException error = new SerializationException();
    try (MockedStatic<RPC> rpc = Mockito.mockStatic(RPC.class)) {
        rpc.when(() -> RPC.invokeAndEncodeResponse(target, targetMethod, rpcParameters, policy)).thenThrow(error);
        try {
            gwtRpcSpy.invoke();
            fail();
        } catch (GwtRpcProxyException ex) {
            assertEquals(error, ex.getCause());
            verify(loggerMock).error(nullable(String.class), eq(error));
        }
    }
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) SerializationException(com.google.gwt.user.client.rpc.SerializationException) RPC(com.google.gwt.user.server.rpc.RPC) Method(java.lang.reflect.Method) HttpServletRequest(javax.servlet.http.HttpServletRequest) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) GwtRpcProxyException(org.pentaho.platform.web.servlet.GwtRpcProxyException)

Example 2 with SerializationException

use of com.google.gwt.user.client.rpc.SerializationException in project pentaho-platform by pentaho.

the class AbstractGwtRpcTest method testInvokeRunsInTargetClassLoader.

@Test
public void testInvokeRunsInTargetClassLoader() 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(targetMethod).when(gwtRpcSpy).getTargetMethod(target.getClass(), rpcRequest);
    doReturn(targetClassLoader).when(gwtRpcSpy).getTargetClassLoader();
    doReturn(rpcRequest).when(gwtRpcSpy).getRequest();
    // Stub invokeCore with:
    doAnswer((Answer<String>) invocationOnMock -> {
        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        assertEquals(targetClassLoader, currentClassLoader);
        return response;
    }).when(gwtRpcSpy).invokeCore(target, targetMethod, rpcRequest);
    gwtRpcSpy.invoke();
    verify(gwtRpcSpy).invokeCore(target, targetMethod, rpcRequest);
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ServletException(javax.servlet.ServletException) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) RunWith(org.junit.runner.RunWith) GwtRpcProxyException(org.pentaho.platform.web.servlet.GwtRpcProxyException) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) RPCServletUtils(com.google.gwt.user.server.rpc.RPCServletUtils) Answer(org.mockito.stubbing.Answer) Mockito.doThrow(org.mockito.Mockito.doThrow) HttpServletRequest(javax.servlet.http.HttpServletRequest) RPC(com.google.gwt.user.server.rpc.RPC) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SerializationPolicyLoader(com.google.gwt.user.server.rpc.SerializationPolicyLoader) Assert.fail(org.junit.Assert.fail) GwtRpcUtil(org.pentaho.platform.web.gwt.rpc.impl.GwtRpcUtil) ParseException(java.text.ParseException) RemoteService(com.google.gwt.user.client.rpc.RemoteService) SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) Method(java.lang.reflect.Method) Mockito.doReturn(org.mockito.Mockito.doReturn) SerializationException(com.google.gwt.user.client.rpc.SerializationException) MalformedURLException(java.net.MalformedURLException) ThrowingSupplier(org.pentaho.platform.web.gwt.rpc.util.ThrowingSupplier) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) MockedStatic(org.mockito.MockedStatic) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Log(org.apache.commons.logging.Log) ServletContext(javax.servlet.ServletContext) LogFactory(org.apache.commons.logging.LogFactory) org.junit(org.junit) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.reset(org.mockito.Mockito.reset) Assert.assertEquals(org.junit.Assert.assertEquals) IncompatibleRemoteServiceException(com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException) InputStream(java.io.InputStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) Method(java.lang.reflect.Method) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 3 with SerializationException

use of com.google.gwt.user.client.rpc.SerializationException in project webprotege by protegeproject.

the class WebProtegeRemoteServiceServlet method doUnexpectedFailure.

@Override
protected void doUnexpectedFailure(Throwable e) {
    HttpServletRequest request = getThreadLocalRequest();
    logger.error(e, getUserInSession(), request);
    if (e instanceof SerializationException) {
        HttpServletResponse response = getThreadLocalResponse();
        response.reset();
        try {
            response.setContentType("text/plain");
            response.sendError(StatusCodes.UPDATED, "WebProtege has been updated. Please refresh your browser");
        } catch (IOException ex) {
            logger.error(ex);
        }
    } else {
        super.doUnexpectedFailure(e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SerializationException(com.google.gwt.user.client.rpc.SerializationException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 4 with SerializationException

use of com.google.gwt.user.client.rpc.SerializationException 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);
        }
    }
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) SerializationException(com.google.gwt.user.client.rpc.SerializationException) IncompatibleRemoteServiceException(com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) StandardSerializationPolicy(com.google.gwt.user.server.rpc.impl.StandardSerializationPolicy) IncompatibleRemoteServiceException(com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException)

Example 5 with SerializationException

use of com.google.gwt.user.client.rpc.SerializationException 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);
    }
}
Also used : SerializationException(com.google.gwt.user.client.rpc.SerializationException) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) Method(java.lang.reflect.Method) GwtRpcProxyException(org.pentaho.platform.web.servlet.GwtRpcProxyException) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

SerializationException (com.google.gwt.user.client.rpc.SerializationException)5 RPCRequest (com.google.gwt.user.server.rpc.RPCRequest)4 Method (java.lang.reflect.Method)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 GwtRpcProxyException (org.pentaho.platform.web.servlet.GwtRpcProxyException)3 IncompatibleRemoteServiceException (com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException)2 RPC (com.google.gwt.user.server.rpc.RPC)2 SerializationPolicy (com.google.gwt.user.server.rpc.SerializationPolicy)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 IOException (java.io.IOException)2 RemoteService (com.google.gwt.user.client.rpc.RemoteService)1 RPCServletUtils (com.google.gwt.user.server.rpc.RPCServletUtils)1 SerializationPolicyLoader (com.google.gwt.user.server.rpc.SerializationPolicyLoader)1 StandardSerializationPolicy (com.google.gwt.user.server.rpc.impl.StandardSerializationPolicy)1 Nullable (edu.umd.cs.findbugs.annotations.Nullable)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 Function (java.util.function.Function)1