Example usage for com.google.gwt.user.server.rpc RPC encodeResponseForFailure

List of usage examples for com.google.gwt.user.server.rpc RPC encodeResponseForFailure

Introduction

In this page you can find the example usage for com.google.gwt.user.server.rpc RPC encodeResponseForFailure.

Prototype

public static String encodeResponseForFailure(Method serviceMethod, Throwable cause)
        throws SerializationException 

Source Link

Document

Returns a string that encodes an exception.

Usage

From source file:cc.alcina.framework.servlet.servlet.CommonRemoteServiceServlet.java

License:Apache License

@Override
public String processCall(String payload) throws SerializationException {
    RPCRequest rpcRequest = null;/*from  w  w w .ja  v a 2s  .co  m*/
    String threadName = Thread.currentThread().getName();
    try {
        LooseContext.push();
        initUserStateWithCookie(getThreadLocalRequest(), getThreadLocalResponse());
        LooseContext.set(CONTEXT_THREAD_LOCAL_HTTP_REQUEST, getThreadLocalRequest());
        LooseContext.set(CommonPersistenceBase.CONTEXT_CLIENT_IP_ADDRESS,
                ServletLayerUtils.robustGetRemoteAddr(getThreadLocalRequest()));
        LooseContext.set(CommonPersistenceBase.CONTEXT_CLIENT_INSTANCE_ID,
                SessionHelper.getAuthenticatedSessionClientInstanceId(getThreadLocalRequest()));
        rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
        if (rpcRequest.getSerializationPolicy() instanceof LegacySerializationPolicy) {
            throw new IncompatibleRemoteServiceException();
        }
        getThreadLocalRequest().setAttribute(THRD_LOCAL_RPC_RQ, rpcRequest);
        getThreadLocalRequest().setAttribute(THRD_LOCAL_RPC_PAYLOAD, payload);
        String name = rpcRequest.getMethod().getName();
        RPCRequest f_rpcRequest = rpcRequest;
        Thread.currentThread().setName(Ax.format("gwt-rpc:%s", name));
        onAfterAlcinaAuthentication(name);
        LooseContext.set(CONTEXT_RPC_USER_ID, PermissionsManager.get().getUserId());
        InternalMetrics.get().startTracker(rpcRequest, () -> describeRpcRequest(f_rpcRequest, ""),
                InternalMetricTypeAlcina.client, Thread.currentThread().getName(), () -> true);
        Method method;
        try {
            method = this.getClass().getMethod(name, rpcRequest.getMethod().getParameterTypes());
            if (method.isAnnotationPresent(WebMethod.class)) {
                WebMethod webMethod = method.getAnnotation(WebMethod.class);
                AnnotatedPermissible ap = new AnnotatedPermissible(webMethod.customPermission());
                if (!PermissionsManager.get().isPermissible(ap)) {
                    WebException wex = new WebException("Action not permitted: " + name);
                    logRpcException(wex, LogMessageType.PERMISSIONS_EXCEPTION.toString());
                    return RPC.encodeResponseForFailure(null, wex);
                }
                if (!webMethod.readonlyPermitted()) {
                    AppPersistenceBase.checkNotReadOnly();
                }
            }
        } catch (SecurityException ex) {
            RPC.encodeResponseForFailure(null, ex);
        } catch (NoSuchMethodException ex) {
            RPC.encodeResponseForFailure(null, ex);
        }
        return invokeAndEncodeResponse(rpcRequest);
    } catch (IncompatibleRemoteServiceException ex) {
        getServletContext().log("An IncompatibleRemoteServiceException was thrown while processing this call.",
                ex);
        return RPC.encodeResponseForFailure(null, ex);
    } catch (UnexpectedException ex) {
        logRpcException(ex);
        throw ex;
    } catch (OutOfMemoryError e) {
        handleOom(payload, e);
        throw e;
    } catch (RuntimeException rex) {
        logRpcException(rex);
        throw rex;
    } finally {
        Thread.currentThread().setName(threadName);
        InternalMetrics.get().endTracker(rpcRequest);
        if (TransformManager.hasInstance()) {
            if (CommonUtils
                    .bv((Boolean) getThreadLocalRequest().getAttribute(PUSH_TRANSFORMS_AT_END_OF_REUQEST))) {
                Sx.commit();
            }
            ThreadlocalTransformManager.cast().resetTltm(null);
            LooseContext.pop();
        } else {
            try {
                LooseContext.pop();
            } catch (Exception e) {// squelch, probably webapp undeployed
            }
        }
    }
}

From source file:cc.kune.core.server.rack.filters.gwts.DelegatedRemoteServlet.java

License:GNU Affero Public License

@Override
public String processCall(final String payload) throws SerializationException, DefaultException {
    try {/*from   w ww.j ava  2  s.c om*/
        final RPCRequest rpcRequest = RPC.decodeRequest(payload, service.getClass());
        return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(), rpcRequest.getParameters());
    } catch (final IncompatibleRemoteServiceException ex) {
        return RPC.encodeResponseForFailure(null, ex);
    }
}

From source file:com.apress.progwt.server.gwt.GWTController.java

License:Apache License

@Override
public String processCall(String payload) throws SerializationException {
    try {//from ww w  .  j a v  a  2  s .c o  m

        RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
        ServerSerializationStreamWriter_1_5_3 writer = getWriter(rpcRequest);

        return RPC1524.invokeAndEncodeResponse(this, rpcRequest.getMethod(), rpcRequest.getParameters(),
                writer);

    } catch (IncompatibleRemoteServiceException ex) {
        log.error("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
        return RPC.encodeResponseForFailure(null, ex);
    } catch (Exception e) {
        log.error("An Exception was thrown while processing this call.", e);
        return RPC.encodeResponseForFailure(null, e);
    }
}

From source file:com.brightedu.server.BrightServlet.java

License:Apache License

/**
 * Process a call originating from the given request. Uses the
 * {@link RPC#invokeAndEncodeResponse(Object, java.lang.reflect.Method, Object[])}
 * method to do the actual work.//from w ww .jav  a 2  s . c  o  m
 * <p>
 * Subclasses may optionally override this method to handle the payload in
 * any way they desire (by routing the request to a framework component, for
 * instance). The {@link HttpServletRequest} and {@link HttpServletResponse}
 * can be accessed via the {@link #getThreadLocalRequest()} and
 * {@link #getThreadLocalResponse()} methods.
 * </p>
 * This is public so that it can be unit tested easily without HTTP.
 * 
 * @param payload
 *            the UTF-8 request payload
 * @return a string which encodes either the method's return, a checked
 *         exception thrown by the method, or an
 *         {@link IncompatibleRemoteServiceException}
 * @throws SerializationException
 *             if we cannot serialize the response
 * @throws UnexpectedException
 *             if the invocation throws a checked exception that is not
 *             declared in the service method's signature
 * @throws RuntimeException
 *             if the service method throws an unchecked exception (the
 *             exception will be the one thrown by the service)
 */
public String processCall(String payload) throws SerializationException {
    // First, check for possible XSRF situation
    checkPermutationStrongName();

    try {
        RPCRequest rpcRequest = RPC.decodeRequest(payload, delegate.getClass(), this);
        onAfterRequestDeserialized(rpcRequest);
        return RPC.invokeAndEncodeResponse(delegate, rpcRequest.getMethod(), rpcRequest.getParameters(),
                rpcRequest.getSerializationPolicy(), rpcRequest.getFlags());
    } catch (IncompatibleRemoteServiceException ex) {
        log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
        return RPC.encodeResponseForFailure(null, ex);
    } catch (RpcTokenException tokenException) {
        log("An RpcTokenException was thrown while processing this call.", tokenException);
        return RPC.encodeResponseForFailure(null, tokenException);
    }
}

From source file:com.cubusmail.gwtui.server.services.MailboxService.java

License:Open Source License

@Override
public String processCall(String payload) throws SerializationException {

    if (this.applicationContext == null) {
        this.applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    }//from  w w  w . j  ava 2  s .c om

    if (SessionManager.isLoggedIn()) {
        try {
            return super.processCall(payload);
        } catch (SerializationException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    } else {
        RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
        return RPC.encodeResponseForFailure(rpcRequest.getMethod(), new GWTInvalidSessionException());
    }
}

From source file:com.cubusmail.gwtui.server.services.UserAccountService.java

License:Open Source License

@Override
public String processCall(String payload) throws SerializationException {

    if (SessionManager.isLoggedIn()) {
        try {/*from   w  w  w  .j  av a 2s . co  m*/
            return super.processCall(payload);
        } catch (SerializationException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    } else {
        RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
        return RPC.encodeResponseForFailure(rpcRequest.getMethod(), new GWTInvalidSessionException());
    }
}

From source file:com.dotspots.rpcplus.flexiblerpc.FlexibleRemoteServiceServlet.java

License:Apache License

/**
 * Process a call originating from the given request. Uses the
 * {@link RPC#invokeAndEncodeResponse(Object, java.lang.reflect.Method, Object[])} method to do the actual work.
 * <p>// www  .  j av a2s .c o  m
 * Subclasses may optionally override this method to handle the payload in any way they desire (by routing the
 * request to a framework component, for instance). The {@link HttpServletRequest} and {@link HttpServletResponse}
 * can be accessed via the {@link #getThreadLocalRequest()} and {@link #getThreadLocalResponse()} methods.
 * </p>
 * This is public so that it can be unit tested easily without HTTP.
 * 
 * @param payload
 *            the UTF-8 request payload
 * @return a string which encodes either the method's return, a checked exception thrown by the method, or an
 *         {@link IncompatibleRemoteServiceException}
 * @throws SerializationException
 *             if we cannot serialize the response
 * @throws UnexpectedException
 *             if the invocation throws a checked exception that is not declared in the service method's signature
 * @throws RuntimeException
 *             if the service method throws an unchecked exception (the exception will be the one thrown by the
 *             service)
 */
public String processCall(String payload) throws SerializationException {
    try {
        RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
        onAfterRequestDeserialized(rpcRequest);
        return RPC.invokeAndEncodeResponse(this, rpcRequest.getMethod(), rpcRequest.getParameters(),
                rpcRequest.getSerializationPolicy());
    } catch (IncompatibleRemoteServiceException ex) {
        log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
        return RPC.encodeResponseForFailure(null, ex);
    }
}

From source file:com.foo.server.rpc230.GWTController.java

License:Apache License

@Override
public String processCall(String payload) throws SerializationException {
    try {/*from   w  ww .  j  a v a  2 s . c  om*/
        RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
        ServerSerializationStreamWriterSenasa writer = getWriter(rpcRequest);

        return CustomRPC.invokeAndEncodeResponse(this, rpcRequest.getMethod(), rpcRequest.getParameters(),
                writer);

    } catch (IncompatibleRemoteServiceException ex) {
        logger.error("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
        return RPC.encodeResponseForFailure(null, ex);
    } catch (Exception e) {
        logger.error("An Exception was thrown while processing this call.", e);

        return RPC.encodeResponseForFailure(null, e);
    }
}

From source file:com.google.gwt.examples.rpc.server.AdvancedExample.java

License:Apache License

/**
 * An example of how you could integrate GWTs RPC functionality without using
 * the {@link com.google.gwt.user.server.rpc.RemoteServiceServlet}. Note that
 * it also shows how mapping between and RPC interface and some other POJO
 * could be performed.// ww w . j  a  v  a2 s  .  com
 */
@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);
    }
}

From source file:com.google.gwt.examples.rpc.server.CanonicalExample.java

License:Apache License

/**
 * Process the RPC request encoded into the payload string and return a string
 * that encodes either the method return or an exception thrown by it.
 *///from   w  ww . jav a  2  s  .co m
@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);
    }
}