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

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

Introduction

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

Prototype

public static String encodeResponseForSuccess(Method serviceMethod, Object object,
            SerializationPolicy serializationPolicy, int flags) throws SerializationException 

Source Link

Usage

From source file:com.colinalworth.gwt.websockets.server.GwtWebService.java

License:Apache License

private void callback(int callbackId, Object response, boolean isSuccess, WebSocketConnection connection) {
    ClientCallbackInvocation callbackInvocation = new ClientCallbackInvocation(callbackId, response, isSuccess);

    try {/*from   ww  w. j ava2 s  .  co m*/
        // Encode and send the message
        int flags = 0;
        String message = RPC.encodeResponseForSuccess(CALLBACK_RPC_METHOD, callbackInvocation, makePolicy(),
                flags);
        connection.send(message);
    } catch (SerializationException e) {
        throw new RuntimeException("Failed to send callback to client, " + e);
    }

}

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.  j  a v a  2s . c  o  m

    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 /*w w  w. j av  a 2s .com*/
 * 
 * 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 invokeAndEncodeResponse(HttpServletRequest servletRequest, HttpServletResponse response,
        Object service, RPCRequest rpcRequest)
        throws ServletException, IOException, SecurityException, SerializationException {
    String responsePayload;/* w ww .  ja v a2  s  . c om*/

    try {
        final Object invocationResult;

        invocationResult = rpcRequest.getMethod().invoke(service, rpcRequest.getParameters());
        responsePayload = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), invocationResult,
                rpcRequest.getSerializationPolicy(), rpcRequest.getFlags());

        if (CwtRpcUtils.getRpcSessionInvalidationPolicy(servletRequest).isInvalidateAfterInvocation()) {
            invalidateSession(servletRequest);
        }
    } catch (IllegalAccessException e) {
        throw new SecurityException("Illegal access detected when invoking method " + rpcRequest.getMethod()
                + " on service " + service.getClass().getName() + " (as requested by client)", e);
    } catch (IllegalArgumentException e) {
        throw new SecurityException(
                "Illegal argument types detected when invoking method " + rpcRequest.getMethod()
                        + " with arguments \"" + createTypeNameString(rpcRequest.getParameters())
                        + "\" on service " + service.getClass().getName() + " (as requested by client)",
                e);
    } catch (InvocationTargetException e) {
        responsePayload = processInvocationException(servletRequest, service, rpcRequest, e.getCause());
    }

    return responsePayload;
}

From source file:de.itsvs.cwtrpc.security.AbstractRpcSuccessHandler.java

License:Apache License

protected void sendResponse(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    final RPCRequest rpcRequest;
    final String responsePayload;

    rpcRequest = AbstractRpcAuthenticationProcessingFilter.getRpcRequest(request);
    if (rpcRequest == null) {
        throw new CwtRpcException("RPC request has not been stored in request ("
                + AbstractRpcAuthenticationProcessingFilter.class.getName() + " must be used)");
    }/*from  www .  java 2s .  c om*/

    try {
        addNoCacheResponseHeaders(request, response);
        responsePayload = RPC.encodeResponseForSuccess(rpcRequest.getMethod(),
                getResponse(request, rpcRequest, authentication), rpcRequest.getSerializationPolicy(),
                rpcRequest.getFlags());
        RPCServletUtils.writeResponse(getServletContext(), response, responsePayload, false);
    } catch (SerializationException e) {
        log.error("Serialization error while processing service request", e);
        if (!response.isCommitted()) {
            RPCServletUtils.writeResponseForUnexpectedFailure(getServletContext(), response, e);
        }
    }
}

From source file:org.atmosphere.gwt.poll.AtmospherePollService.java

License:Apache License

static String encodeResponse(RPCRequest rpcRequest, Object message) throws SerializationException {
    if (rpcRequest == null) {
        throw new NullPointerException("rpcRequest");
    }//w  ww.j  av  a2 s  .  c o  m

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

    String responsePayload;

    responsePayload = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), message,
            rpcRequest.getSerializationPolicy(), rpcRequest.getFlags());

    return responsePayload;
}

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

License:Apache License

/**
 * Handles method invocation on a service and is invoked by
 * {@link #processCall(String)}.//from w  ww.ja v a  2  s  . c  o m
 * 
 * @param service
 *            Service to invoke method on
 * @param targetMethod
 *            Method to invoke.
 * @param targetParameters
 *            Parameters to pass to method. Can be null for no arguments.
 * @param rpcRequest
 *            RPCRequest instance for this request
 * @return Return RPC encoded result.
 * @throws Exception
 */
protected String invokeMethodOnService(Object service, Method targetMethod, Object[] targetParameters,
        RPCRequest rpcRequest) throws Exception {
    Object result = targetMethod.invoke(service, targetParameters);
    SerializationPolicy serializationPolicy = getSerializationPolicyProvider()
            .getSerializationPolicyForSuccess(rpcRequest, service, targetMethod, targetParameters, result);
    String encodedResult = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), result, serializationPolicy,
            serializationFlags);
    return encodedResult;
}

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");
    }// w w  w.  ja v a  2s .  c  om

    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:play.modules.gwt2.GWT2Invoker.java

License:Apache License

/**
 * Invoke a service//w  ww.j a  va  2s  .  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);
        }
    }
}

From source file:sk.seges.acris.rpc.GWTRPCServiceExporter.java

License:Apache License

/**
 * Handles method invocation on a service and is invoked by
 * {@link #processCall(String)}./* w  w  w.  java 2 s  .c o  m*/
 * 
 * @param service
 *            Service to invoke method on
 * @param targetMethod
 *            Method to invoke.
 * @param targetParameters
 *            Parameters to pass to method. Can be null for no arguments.
 * @param rpcRequest
 *            RPCRequest instance for this request
 * @return Return RPC encoded result.
 * @throws Exception
 */
protected String invokeMethodOnService(Object service, Method targetMethod, Object[] targetParameters,
        RPCRequest rpcRequest) throws Exception {
    Object result = targetMethod.invoke(service, targetParameters);
    SerializationPolicy serializationPolicy = getSerializationPolicyProvider()
            .getSerializationPolicyForSuccess(rpcRequest, service, targetMethod, targetParameters, result);

    Object ehCacheStatus = ServletUtils.getRequest().getAttribute(EHCACHE_ENABLED);

    if (disableResponseCaching || ehCacheStatus == null || ehCacheStatus.equals(false)) {
        return RPC.encodeResponseForSuccess(rpcRequest.getMethod(), result, serializationPolicy,
                serializationFlags);
    }
    return CacheableRPC.encodeResponseForSuccess(rpcRequest.getMethod(), result, serializationPolicy,
            serializationFlags);
}