简体   繁体   中英

Before every GWT RPC method call -> execute some check and throw a checked exception

I have all the logic in place to check if a user is logged in and therefore is allowed to execute a RPC call. I can place this check in all my RPC methods and throw a throw new MyLoginException() if the user is not logged in. In the client side I just check for this exception and handle it accordingly.

The problem now is: I don't want to put this in every RPC method. I want a generic solution for this.

I tried to override methods like com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall() which is called before every RPC call. Problem here: I can only throw unchecked exception because the method signature does not allow me to throw my own exception. The unchecked exception result then in generic exception in the client code and I can not handle them correctly because I do not know anymore which exception was thrown.

Where can I put my login check? Is there any clean solution for this problem?

I found the solution in the link posted by Baz: https://groups.google.com/forum/#!topic/google-web-toolkit/D2MPaD2EpOQ

Instead of throwing the exception like this (Which is not possible since the signature only allows SerializationException :

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

    if(!userIsAuthorized()){
        throw new MyAuthorizationException();
    }

    return super.processCall(payload);
}

The exception has to be returned encoded in the response like this:

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

    if(!userIsAuthorized()){
        return RPC.encodeResponseForFailure(null, new MyAuthorizationException());
    }

    return super.processCall(payload);
}

This will be called every time a RPC call is invoked and the method will not be executed if the user is not authorized and the checked exception will be transported to the front end where it can be handled accordingly.

Edit @Baz: This is how the client handling looks like for me. But I think if you get a IncompatibleRemoteServiceException you have a problem that some old stuff is cached in your browser und you need to clean your cache.

@Override
public void onFailure(Throwable caught) {
    try {
        throw caught;
    }
        catch (SessionExpiredException ex){
            onSessionExpired();
    }catch(Throwable e){
        MyCallback.this.onFailure(e);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM