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) throws SerializationException 

Source Link

Document

Returns a string that encodes the object.

Usage

From source file:com.edgenius.wiki.gwt.server.handler.GWTSpringController.java

License:Open Source License

public String processCall(String payload) throws SerializationException {
    try {// w  ww  .j a va2  s.co  m
        // Copy & pasted & edited from the GWT 1.4.3 RPC documentation
        RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);

        Method targetMethod = rpcRequest.getMethod();
        Object[] targetParameters = rpcRequest.getParameters();
        SerializationPolicy policy = rpcRequest.getSerializationPolicy();
        try {
            Object result = targetMethod.invoke(this, targetParameters);
            String encodedResult;
            String redir = getRedir();
            if (redir != null) {
                Throwable ae;
                if (redir.endsWith(WikiConstants.URL_CAPTCHA_VERIFIED_ERROR)) {
                    ae = new CaptchaVerifiedException(redir);
                } else if (redir.endsWith(WikiConstants.URL_ACCESS_DENIED)) {
                    ae = new ClientAccessDeniedException(redir);
                } else {
                    //OK, maybe accessDenied or authentication or other error, then let Gwt redirect it anyway.
                    ae = new ClientAuthenticationException(redir);
                }
                log.info("Send redir value " + redir);
                encodedResult = RPC.encodeResponseForFailure(null, ae, policy);
            } else {
                if (result instanceof GeneralModel) {
                    //set current user info,so that client can check if its session is expired for each request
                    User user = WikiUtil.getUser();
                    ((GeneralModel) result).loginUserFullname = user.getFullname();
                    ((GeneralModel) result).loginUsername = user.getUsername();
                    ((GeneralModel) result).loginUserUid = user.getUid();
                }
                //return correct result
                encodedResult = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), result, policy);
            }
            return encodedResult;
        } catch (IllegalArgumentException e) {
            log.error("Handle ajax call for service " + this + " with exception.", e);
            SecurityException securityException = new SecurityException(
                    "Blocked attempt to invoke method " + targetMethod);
            securityException.initCause(e);
            throw securityException;
        } catch (IllegalAccessException e) {
            log.error("Handle ajax call for service " + this + " with exception.", e);
            SecurityException securityException = new SecurityException(
                    "Blocked attempt to access inaccessible method " + targetMethod
                            + (this != null ? " on service " + this : ""));
            securityException.initCause(e);
            throw securityException;
        } catch (InvocationTargetException e) {
            log.error("Handle ajax call for service " + this + " with exception.", e);
            Throwable cause = e.getCause();
            log.warn("Get RPC InvocationTargetException exception, the root cause is " + cause);
            //following handle are not exactly response if redir value is null: accessDenied is only for login user, here does not check this. 
            //so MethodExceptionHandler.handleException() give more exactly response.
            //MethodSecurityInterceptor.invoke() may throw Authentication and AccessDenied Exception, if targetMethod does not
            //capture these exception, then they will go to here!! The example is PageControllerImpl.viewPage(), it handle the accessDenied
            //exception, if it does not, then here will handle it...
            if (cause instanceof AuthenticationException) {
                String redir = getRedir();
                if (redir == null)
                    //system default value
                    redir = WikiConstants.URL_LOGIN;
                log.info("Send Authentication redirect URL " + redir);
                ClientAuthenticationException ae = new ClientAuthenticationException(redir);
                return RPC.encodeResponseForFailure(null, ae, policy);
            } else if (cause instanceof AccessDeniedException) {
                String redir = getRedir();
                if (redir == null)
                    //system default value
                    redir = WikiConstants.URL_ACCESS_DENIED;
                log.info("Send AccessDenied redirect URL " + redir);
                ClientAccessDeniedException ae = new ClientAccessDeniedException(redir);
                return RPC.encodeResponseForFailure(null, ae, policy);
            }

            log.info("Return unexpected exception to client side " + cause);
            String failurePayload = RPC.encodeResponseForFailure(rpcRequest.getMethod(), cause, policy);
            return failurePayload;
        }
    } catch (IncompatibleRemoteServiceException e) {
        log.warn(e.getMessage());
        return RPC.encodeResponseForFailure(null, e);
    } catch (Exception e) {
        log.error("Ajax call failed", e);
        throw new RuntimeException(e);
    }
}

From source file:com.fullmetalgalaxy.server.Serializer.java

License:Open Source License

/**
 * serialize PresenceRoom to send to client.
 * @param p_room/*from  ww w.  j  a  v a 2s . c  o  m*/
 * @return null if any error occur.
 */
public static String toClient(PresenceRoom p_room) {
    String response = null;
    try {
        response = RPC.encodeResponseForSuccess(s_getRoom, p_room, FmgSerializationPolicy.getPolicy());
    } catch (SerializationException e) {
        log.error(e);
    }
    return response;
}

From source file:com.fullmetalgalaxy.server.Serializer.java

License:Open Source License

/**
 * serialize ChatMessage to send to client.
 * @param p_msg//from   w w w  .ja  v  a  2 s .c o m
 * @return null if any error occur.
 */
public static String toClient(ChatMessage p_msg) {
    String response = null;
    try {
        response = RPC.encodeResponseForSuccess(s_getChatMessage, p_msg, FmgSerializationPolicy.getPolicy());
    } catch (SerializationException e) {
        log.error(e);
    }
    return response;
}

From source file:com.fullmetalgalaxy.server.Serializer.java

License:Open Source License

/**
 * serialize ModelFmpUpdate to send to client.
 * @param p_modelUpdate//from   w  w  w.j av a2  s  .  c o m
 * @return null if any error occur.
 */
public static String toClient(ModelFmpUpdate p_modelUpdate) {
    String response = null;
    try {
        response = RPC.encodeResponseForSuccess(s_getModelFmpUpdate, p_modelUpdate,
                FmgSerializationPolicy.getPolicy());
    } catch (SerializationException e) {
        log.error(e);
    }
    return response;
}

From source file:com.fullmetalgalaxy.server.Serializer.java

License:Open Source License

/**
 * serialize EbGame to send to client.//from   w  ww.java 2 s.  c om
 * @param p_model
 * @return null if any error occur.
 */
public static String toClient(ModelFmpInit p_model) {
    String response = null;
    try {
        response = RPC.encodeResponseForSuccess(s_getModelFmpInit, p_model, FmgSerializationPolicy.getPolicy());
    } catch (SerializationException e) {
        log.error(e);
    }
    return response;
}

From source file:com.google.gwt.sample.dynatable.server.ChannelServiceImpl.java

public static void send(Notify message) {
    try {//w w w .j  a  v a 2  s .  com
        Method serviceMethod = GetMessageService.class.getMethod("getMessage", Message.class);

        // Yes, the SerializationPolicy is a hack
        String serialized = RPC.encodeResponseForSuccess(serviceMethod, message, new SerializationPolicy() {
            @Override
            public void validateSerialize(Class<?> clazz) throws SerializationException {
            }

            @Override
            public void validateDeserialize(Class<?> clazz) throws SerializationException {
            }

            @Override
            public boolean shouldSerializeFields(Class<?> clazz) {
                return false;
            }

            @Override
            public boolean shouldDeserializeFields(Class<?> clazz) {
                return false;
            }
        });
        SERVICE.sendMessage(new ChannelMessage(Notify.CHANNEL_NAME, serialized));
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (SerializationException e) {
        e.printStackTrace();
    }
}

From source file:com.metadot.book.connectr.server.utils.ChannelServer.java

License:Apache License

private static String encodeMessage(Message msg) {
    try {/*from w  w  w. j  a va  2s.co  m*/
        return RPC.encodeResponseForSuccess(dummyMethod, msg, serializationPolicy);
    } catch (SerializationException e) {
        throw new RuntimeException("Unable to encode a message for push.\n" + msg, e);
    }
}

From source file:net.sf.gilead.comet.CometRemoteService.java

License:Apache License

/**
 * if a PendingRequest class was created, you should call
 * 'invokeService(PendingRequest)'//from   ww w.  jav  a2s.  co m
 * 
 * @param p_rpcRequest
 * @param p_event
 * @throws IOException
 */
protected void invokeService(RPCRequest p_rpcRequest, CometEvent p_event) throws IOException {
    //   Precondition checking
    //
    if (p_rpcRequest == null || p_event == null) {
        return;
    }

    if (_log.isDebugEnabled()) {
        _log.debug("invokeService: " + p_rpcRequest.getMethod().getName());
    }

    // store perThreadRequest/Response to make this methods re-entrant
    //
    HttpServletRequest oldRequest = getPerThreadRequest().get();
    HttpServletResponse oldResponse = getPerThreadResponse().get();
    getPerThreadRequest().set(p_event.getHttpServletRequest());
    getPerThreadResponse().set(p_event.getHttpServletResponse());

    try {
        String responsePayload = null;
        try {
            try {
                // invoke service
                // 
                Object returnValue = p_rpcRequest.getMethod().invoke(this, p_rpcRequest.getParameters());

                // Encode response
                //  
                responsePayload = RPC.encodeResponseForSuccess(p_rpcRequest.getMethod(), returnValue,
                        p_rpcRequest.getSerializationPolicy());

            } catch (IllegalAccessException e) {
                // TODO message
                // SecurityException securityException = new
                // SecurityException( RPC
                // .formatIllegalAccessErrorMessage( this,
                // p_rpcRequest.getMethod() )
                // );
                SecurityException securityException = new SecurityException(
                        "IllegalAccessError : " + this + "." + p_rpcRequest.getMethod());
                securityException.initCause(e);
                throw securityException;
            } catch (InvocationTargetException e) {
                _log.error("Invocation exception : " + e.getMessage(), e);

                responsePayload = RPC.encodeResponseForFailure(p_rpcRequest.getMethod(), e.getCause());
            }
        } catch (SerializationException e) {
            _log.error("Serialization exception : " + e.getMessage(), e);
            responsePayload = "server error: " + e.getMessage();
        }
        writeResponse(p_event.getHttpServletResponse(), responsePayload);
        p_event.close();
    } finally {
        getPerThreadRequest().set(oldRequest);
        getPerThreadResponse().set(oldResponse);
    }
}

From source file:net.sf.gilead.comet.PersistentCometService.java

License:Apache License

/**
 * if a PendingRequest class was created, you should call
 * 'invokeService(PendingRequest)'/*from  w w  w  .j  a va2s.com*/
 * 
 * @param p_rpcRequest
 * @param p_event
 * @throws IOException
 */
@Override
protected void invokeService(RPCRequest p_rpcRequest, CometEvent p_event) throws IOException {
    if (p_rpcRequest == null || p_event == null) {
        return;
    }

    if (_log.isDebugEnabled()) {
        _log.debug("invokeService: " + p_rpcRequest.getMethod().getName());
    }
    // store perThreadRequest/Response to make this methods re-entrant
    //
    HttpServletRequest oldRequest = getPerThreadRequest().get();
    HttpServletResponse oldResponse = getPerThreadResponse().get();
    getPerThreadRequest().set(p_event.getHttpServletRequest());
    getPerThreadResponse().set(p_event.getHttpServletResponse());

    try {
        String responsePayload = null;
        try {
            try {
                Object returnValue = p_rpcRequest.getMethod().invoke(this, p_rpcRequest.getParameters());

                returnValue = GileadRPCHelper.parseReturnValue(returnValue, _beanManager);

                // Encode response
                //  
                responsePayload = RPC.encodeResponseForSuccess(p_rpcRequest.getMethod(), returnValue,
                        p_rpcRequest.getSerializationPolicy());

            } catch (IllegalAccessException e) {
                SecurityException securityException = new SecurityException(
                        "Blocked attempt to access inaccessible method " + p_rpcRequest.getMethod()
                                + " on target " + this);
                securityException.initCause(e);
                throw securityException;
            } catch (InvocationTargetException e) {
                // Clone exception if needed
                Exception exception = (Exception) GileadRPCHelper.parseReturnValue(e.getCause(), _beanManager);

                responsePayload = RPC.encodeResponseForFailure(p_rpcRequest.getMethod(), exception,
                        p_rpcRequest.getSerializationPolicy());
            }
        } catch (SerializationException e) {
            _log.error("Serialization exception : " + e.getMessage(), e);
            e.printStackTrace();
            responsePayload = "server error: " + e.getMessage();
        }
        writeResponse(p_event.getHttpServletResponse(), responsePayload);
        p_event.close();
    } finally {
        getPerThreadRequest().set(oldRequest);
        getPerThreadResponse().set(oldResponse);
    }
}

From source file:net.sf.gilead.gwt.PersistentRemoteService.java

License:Apache License

/**
 * Override of the RemoteServletService main method
 *//*from w  w w . ja v a  2  s  .c o m*/
@Override
public String processCall(String payload) throws SerializationException {
    // Normal processing
    //
    RPCRequest rpcRequest = null;
    try {
        // Decode request
        //
        rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);

        if (rpcRequest == null) {
            throw new NullPointerException("No rpc request");
        }

        // Invoke method
        //
        GileadRPCHelper.parseInputParameters(rpcRequest, _beanManager, getThreadLocalRequest().getSession());
        Object returnValue = rpcRequest.getMethod().invoke(this, rpcRequest.getParameters());

        returnValue = GileadRPCHelper.parseReturnValue(returnValue, _beanManager);

        // Encode response
        //
        return RPC.encodeResponseForSuccess(rpcRequest.getMethod(), returnValue,
                rpcRequest.getSerializationPolicy());
    } catch (IllegalArgumentException e) {
        SecurityException securityException = new SecurityException(
                "Blocked attempt to invoke method " + rpcRequest.getMethod());
        securityException.initCause(e);
        throw securityException;
    } catch (IllegalAccessException e) {
        SecurityException securityException = new SecurityException(
                "Blocked attempt to access inaccessible method " + rpcRequest.getMethod() + " on target "
                        + this);
        securityException.initCause(e);
        throw securityException;
    } catch (InvocationTargetException e) {
        // Clone exception if needed
        Exception exception = (Exception) GileadRPCHelper.parseReturnValue(e.getCause(), _beanManager);

        return RPC.encodeResponseForFailure(rpcRequest.getMethod(), exception,
                rpcRequest.getSerializationPolicy());
    } catch (IncompatibleRemoteServiceException ex) {
        // Clone exception if needed
        Exception exception = (Exception) GileadRPCHelper.parseReturnValue(ex, _beanManager);
        return RPC.encodeResponseForFailure(null, exception, rpcRequest.getSerializationPolicy());
    }
}