Overriding processCall to intercept RPC calls

303 views
Skip to first unread message

joe kolba

unread,
Jan 3, 2012, 10:14:23 AM1/3/12
to google-we...@googlegroups.com
I needed a solution to intercept RPC calls to make sure the user session hasn't expired.  Here is my code:


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

        boolean validSession = (!getThreadLocalRequest().getSession().isNew());
           
        RPCRequest rpcRequest =  RPC.decodeRequest(payload, this.remoteServiceClass);
           
        if(!validSession && !rpcRequest.getMethod().getName().equalsIgnoreCase("isUserLoggedIn")){
            return RPC.encodeResponseForFailure(null, new IncompatibleRemoteServiceException("Logged out"));
        }else{
            return super.processCall(payload);
        }
       
      }

Everything works however I had to use the IncompatibleRemoteServiceException rather then a custom exception.  Whenever I tried to use my custom exception or IllegalArgumentException with RPC.encodeResponseForFailure() I would get a 500 response from my RPC call.  Anyone have any information that could help me return a custom exception?

Paul Stockley

unread,
Jan 3, 2012, 1:22:11 PM1/3/12
to google-we...@googlegroups.com
In your service interface you have do declare each function throws your custom exception otherwise you will get that error.

joe kolba

unread,
Jan 3, 2012, 1:25:53 PM1/3/12
to google-we...@googlegroups.com
I am throwing IllegalArgumentException in my service interface and i still get the 500 error.

On Tue, Jan 3, 2012 at 1:22 PM, Paul Stockley <pstoc...@gmail.com> wrote:
In your service interface you have do declare each function throws your custom exception otherwise you will get that error.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/KuhUBhSNs6wJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

joe kolba

unread,
Jan 3, 2012, 1:32:54 PM1/3/12
to google-we...@googlegroups.com
return RPC.encodeResponseForFailure(null, new IllegalArgumentException("Logged out"));


service interface...

public interface GreetingService extends RemoteService {
        boolean isUserLoggedIn() throws IllegalArgumentException;

Ashton Thomas

unread,
Jan 3, 2012, 1:39:16 PM1/3/12
to google-we...@googlegroups.com
May also be a good idea to have all your services declare "throws SerializationException" and have all your custom exceptions extend SerExc

Joe, did you fix your problem? if not, can you provide more details?


joe kolba

unread,
Jan 3, 2012, 1:41:14 PM1/3/12
to google-we...@googlegroups.com
I solved it by creating a custom exception that extended IncompatibleRemoveService exception... it seems that is the only exception that RPC can serialize for failure.

On Tue, Jan 3, 2012 at 1:39 PM, Ashton Thomas <ash...@acrinta.com> wrote:
May also be a good idea to have all your services declare "throws SerializationException" and have all your custom exceptions extend SerExc

Joe, did you fix your problem? if not, can you provide more details?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

joe kolba

unread,
Jan 3, 2012, 1:42:42 PM1/3/12
to google-we...@googlegroups.com
im sorry i meant i extended RuntimeException not IncompatibleRemoteService

Jens

unread,
Jan 3, 2012, 2:00:56 PM1/3/12
to google-we...@googlegroups.com
Instead of this I would just extend RemoteServiceServlet and add a public static method that makes the session available to everyone (just like RequestFactoryServlet does), e.g.

public static HttpSession getThreadLocalSession() {
  return getThreadLocalRequest().getSession();
}

Then in your service method:

public boolean isUserLoggedIn() throws IllegalArgumentException {
  HttpSession session = YourExtendedRemoteServiceServlet.getThreadLocalSession();
  if(session.isNew()) {
    throw new IllegalArgumentException("Logged out");
  }
}


That way you actually see the session check at the correct place and not hidden in your RemoteServiceServlet implementation.

-- J.

joe kolba

unread,
Jan 3, 2012, 2:04:06 PM1/3/12
to google-we...@googlegroups.com
yeah i was thinking about doing that but im going to have to call that static method for every rpc method, which I have around 15.  Thanks I will consider changing it to this.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

Jens

unread,
Jan 3, 2012, 2:35:12 PM1/3/12
to google-we...@googlegroups.com
You could also implement it using a ServletFilter. Take a look at the mobile web app example from GWT which uses a ServletFilter to authenticate users against Google App Engine:


Then simply add the Filter to your web.xml.

-- J.
Reply all
Reply to author
Forward
0 new messages