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,
            SerializationPolicy serializationPolicy, int flags) throws SerializationException 

Source Link

Usage

From source file:com.tasktop.c2c.server.common.web.server.AbstractAutowiredRemoteServiceServlet.java

License:Open Source License

private String invokeAndEncodeResponse(Method serviceMethod, Object[] args,
        SerializationPolicy serializationPolicy, int flags) throws SerializationException {
    if (serviceMethod == null) {
        throw new NullPointerException("serviceMethod");
    }// w  w w .ja va  2  s .c  om

    if (serializationPolicy == null) {
        throw new NullPointerException("serializationPolicy");
    }

    String responsePayload;
    try {
        Object result = serviceMethod.invoke(this, args);
        responsePayload = RPC.encodeResponseForSuccess(serviceMethod, result, serializationPolicy, flags);
    } catch (IllegalAccessException e) {
        SecurityException securityException = new SecurityException("Cannot access " + serviceMethod.getName());
        securityException.initCause(e);
        throw securityException;
    } catch (IllegalArgumentException e) {
        SecurityException securityException = new SecurityException("Cannot access " + serviceMethod.getName());
        securityException.initCause(e);
        throw securityException;
    } catch (InvocationTargetException e) {
        // Try to encode the caught exception
        //
        Throwable cause = e.getCause();
        cause = convertException(cause);
        responsePayload = RPC.encodeResponseForFailure(serviceMethod, cause, serializationPolicy, flags);
    }

    return responsePayload;
}

From source file:controllers.GWT2Controller.java

License:Apache License

/**
 * Invoke a Service //from w w  w  . java  2s . c om
 * 
 * This way GWT2Service is allowed to use GWT2Chain
 * 
 * @param mod
 * @return
 * @throws Exception 
 */
private static String invoke(GWT2Service service, RPCRequest rpcRequest) throws Exception {
    String responseload = null;

    try {
        // Get GWt2Invoker
        GWT2Invoker invoker = new GWT2Invoker(service, request, response, session, rpcRequest);

        // run 
        boolean run = true;
        Object result = null;
        Object chainResult = null;
        boolean firstcall = true;
        GWT2ChainRuntime chain = null;
        while (run) {
            try {
                if (firstcall) {
                    firstcall = false;
                    // call service
                    result = invoker.invoke();
                    // get a result stop request
                    run = false;
                } else if (chain != null) {
                    // call callback
                    result = chain.getChain().chain(chainResult);
                    // get a result stop request
                    run = false;
                } else {
                    // no callback to call stop process
                    run = false;
                }
            } catch (GWT2ChainAsync casync) {
                // async chain call
                chain = casync;
                chainResult = chainAwait(chain);
            } catch (GWT2ChainSync csync) {
                chain = csync;
                chainResult = chainDo(chain);
            } catch (InvocationTargetException ite) {
                if (ite.getTargetException() instanceof GWT2ChainAsync) {
                    // async chain call
                    chain = (GWT2ChainAsync) ite.getTargetException();
                    chainResult = chainAwait(chain);
                } else if (ite.getTargetException() instanceof GWT2ChainSync) {
                    chain = (GWT2ChainSync) ite.getTargetException();
                    chainResult = chainDo(chain);
                } else
                    throw ite;
            }
        }

        // encode result
        responseload = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), result,
                rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);

        return responseload;

    } catch (Exception ex) {
        if (ex instanceof PlayException)
            // Rethrow the enclosed exception
            throw (PlayException) ex;

        // if not gwt rpc rethrow
        if (!isGWT())
            throw ex;

        try {
            // else encode error
            responseload = RPC.encodeResponseForFailure(rpcRequest.getMethod(), ex,
                    rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);

        } catch (SerializationException e) {
            StackTraceElement element = PlayException.getInterestingStrackTraceElement(e);
            if (element != null) {
                throw new JavaExecutionException(Play.classes.getApplicationClass(element.getClassName()),
                        element.getLineNumber(), e);
            }
            throw new JavaExecutionException(e);
        }
    }

    return responseload;
}

From source file:de.itsvs.cwtrpc.controller.RemoteServiceControllerServlet.java

License:Apache License

protected String processExpectedException(HttpServletRequest servletRequest, Object service,
        RPCRequest rpcRequest, Throwable exception)
        throws ServletException, IOException, SerializationException {
    final String responsePayload;

    if (CwtRpcUtils.getRpcSessionInvalidationPolicy(servletRequest).isInvalidateOnExpectedException()) {
        invalidateSession(servletRequest);
    }//from  w  w  w .  j  a v a2s .co  m
    responsePayload = RPC.encodeResponseForFailure(rpcRequest.getMethod(), exception,
            rpcRequest.getSerializationPolicy(), rpcRequest.getFlags());

    return responsePayload;
}

From source file:org.gwtwidgets.server.spring.GWTRPCServiceExporter.java

License:Apache License

/**
 * Wrapper around RPC utility invocation
 * @param rpcRequest RPCRequest//from www  . j av  a 2 s.c o  m
 * @param cause Exception to handle
 * @param targetMethod Method which threw the exception
 * @param targetParameters Method arguments  
 * @return RPC payload
 * @throws Exception
 */
protected String encodeResponseForFailure(RPCRequest rpcRequest, Throwable cause, Method targetMethod,
        Object[] targetParameters) throws SerializationException {
    SerializationPolicy serializationPolicy = getSerializationPolicyProvider()
            .getSerializationPolicyForFailure(rpcRequest, service, targetMethod, targetParameters, cause);
    return RPC.encodeResponseForFailure(rpcRequest.getMethod(), cause, serializationPolicy, serializationFlags);
}

From source file:org.spring4gwt.server.RPCHelper.java

License:Apache License

public static String invokeAndEncodeResponse(Object target, Method serviceMethod, Object[] args,
        SerializationPolicy serializationPolicy, int flags) throws SerializationException {
    if (serviceMethod == null) {
        throw new NullPointerException("serviceMethod");
    }//from   w w  w.  j  a v  a2 s.  c  o  m

    if (serializationPolicy == null) {
        throw new NullPointerException("serializationPolicy");
    }

    String responsePayload;
    try {
        Object result = serviceMethod.invoke(target, args);

        responsePayload = RPC.encodeResponseForSuccess(serviceMethod, result, serializationPolicy, flags);
    } catch (IllegalAccessException e) {
        SecurityException securityException = new SecurityException(
                formatIllegalAccessErrorMessage(target, serviceMethod));
        securityException.initCause(e);
        throw securityException;
    } catch (IllegalArgumentException e) {
        SecurityException securityException = new SecurityException(
                formatIllegalArgumentErrorMessage(target, serviceMethod, args));
        securityException.initCause(e);
        throw securityException;
    } catch (InvocationTargetException e) {
        // Try to encode the caught exception
        //
        Throwable cause = e.getCause();

        LOG.error("Unexpected exception occured while invoking service method - "
                + (serviceMethod != null ? serviceMethod.getName() : "null"), e);

        responsePayload = RPC.encodeResponseForFailure(serviceMethod, cause, serializationPolicy, flags);
    }

    return responsePayload;
}

From source file:org.zoxweb.server.shiro.servlet.ShiroBaseGWTRPC.java

License:Apache License

@Override
public String processCall(String payload) throws SerializationException {
    // First, check for possible XSRF situation
    checkPermutationStrongName();/*from  w  w  w  .  j  a v a 2 s  .  c om*/

    try {
        RPCRequest rpcRequest = RPC.decodeRequest(payload, localDelegate.getClass(), this);
        onAfterRequestDeserialized(rpcRequest);

        // invoke the security check here at this level

        try {
            checkSecurity(localDelegate, rpcRequest.getMethod(), rpcRequest.getParameters());
        } catch (AccessException e) {
            return RPC.encodeResponseForFailure(rpcRequest.getMethod(), e, rpcRequest.getSerializationPolicy(),
                    rpcRequest.getFlags());
        }

        return RPC.invokeAndEncodeResponse(localDelegate, 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:play.modules.gwt2.GWT2Invoker.java

License:Apache License

/**
 * Invoke a service/*from   www  .ja v  a2s  .  c  o m*/
 * @param service
 */
public void doJob() {

    result = null;
    try {
        // invoke
        result = invoke();
        // encode result
        responseload = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), result,
                rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);

    } catch (Exception ex) {
        if (ex instanceof PlayException)
            // Rethrow the enclosed exception
            throw (PlayException) ex;

        try {
            if (GWT2ChainRuntime.class.isAssignableFrom(ex.getClass())) {
                responseload = RPC.encodeResponseForFailure(rpcRequest.getMethod(),
                        new JavaExecutionException(
                                "Chain call is not allowed when runngin Service in async mode", ex),
                        rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);
            } else {
                responseload = RPC.encodeResponseForFailure(rpcRequest.getMethod(), ex,
                        rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);
            }
        } catch (SerializationException e) {
            StackTraceElement element = PlayException.getInterestingStrackTraceElement(e);
            if (element != null) {
                throw new JavaExecutionException(Play.classes.getApplicationClass(element.getClassName()),
                        element.getLineNumber(), e);
            }
            throw new JavaExecutionException(e);
        }
    }
}