7

In my GWT app. I overrode RemoteServiceServlet to check if the session is valid right before the service method is being called. I am trying to throw a RuntimeException("expired session") from the server and I would like the client to catch this exception from the asynccallback onFailure...

In the client I would like to: Asynccallback:

@Override
    public void onFailure(Throwable caught) {
        final String message = caught.getMessage();

        if (!isNullOrEmptyString(message) && message.contains("expired session")) {
            com.google.gwt.user.client.Window.Location.reload();
        }

    }

However, in the client, the caught object is still a StatusCodeException and the message is still the default "...Exception in the server...". how can I override the exception at least the default message to compare if it was a session expired message I sent from the server?

thanks

Hi Gursel, Here's my code: -> Custom RemoteServiceServlet. I'm trying to "intercept" every method before it's invoked. I check the session and throw a RuntimeException if it's already expired. So basically, it is not the declared method that throws the exception but the custom RemoteServiceServlet. It still goes to the "onFailure" in the client async but the Throwable object is still of type "StatusCodeException" without the EXPIRED_SESSION_MSG message. Don;t know how to make this work. Thanks!

public class XRemoteServiceServlet extends RemoteServiceServlet {
    private final static String EXPIRED_SESSION_MSG = "ERROR: Application has expired session.";   
    @Override
    protected void onAfterRequestDeserialized(RPCRequest rpcRequest) {
        HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
        HttpSession session = httpServletRequest.getSession(false);
        if (session != null) {
            final String sessionIdFromRequestHeader = getSessionIdFromHeader();
            if (!isNullOrEmptyString(sessionIdFromRequestHeader)) {
                final String sessionId = session.getId();

                if (!sessionId.equals(sessionIdFromRequestHeader)) {
                    throw new RuntimeException(EXPIRED_SESSION_MSG);
                }
            }

2 Answers 2

5

All RuntimeExceptions thrown by Server side of gwt application has been wrapped as StatusCodeException if you did not declare them at your remote method declaration.

EDIT :

After, Thomas Broyer comment, I have learned that all exceptions (checked or unchecked) that are declared at remote method declaration are propagated to gwt client. Therefore all you have to do is just declare your remote method such as :

public void myRemoteMethod() throws RuntimeException;
Sign up to request clarification or add additional context in comments.

6 Comments

Not sure about that one but… can't you propagate RuntimeExceptions to the client if you have explicit throws declarations for them on your interface?
I found this tutorial: roussev.org/2010/02/gwt-dynamic-exception-handling/...Looks like this fits my needs...I'm still testing though
could you post your synch and asynch remote interface code?. What I have said, has been tested and it is working.
Hi Gursel..I posted the code...Please let me know if you have a solution for it? Thanks.
@mark, there is no easy way to accomplish what you want . I suggest you to check expired session at your remote methods, not at onAfterRequestDeserialized.
|
0

The post looks too old, still here is the solution I have come up with. Override processCall(String payload) of RemoveServiceServlet, if the session is invalid execute the blow code else call super.processCall(payload).

//Pass the exception to Client
RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this);
onAfterRequestDeserialized(rpcRequest);
return RPC.encodeResponseForFailure(rpcRequest.getMethod(), new CustomException("Invalid Session!"),rpcRequest.getSerializationPolicy(), rpcRequest.getFlags());

All the GWT services are servlets so GWT serializes the custom exception and sends to client as string, we follow the same :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.