How to redirect to login page after session expire in GWT RPC call

Nav picture Nav · Sep 7, 2010 · Viewed 15.3k times · Source

I am using GWT and RPC in my app. after session expires when I do a RPC call, because of my login-filter the request redirect to login.jsp, but my problem is client doen't show me login.jsp instead the RPC's onFailure raised.

It means I should handle all my rpc's onFailure events for redirecting to login page ?!!!!

Thanks

Answer

Piotr picture Piotr · Sep 8, 2010

I agree with pathed that you should do redirecting in your AsyncCallbacks. However, you don't need to explicitly use your custom MyAsyncCallback callbacks instead of standard GWT AsyncCallback. This is important for example when you already have a lot of code that uses standard callbacks.

When you invoke GWT.create(MyService.class) GWT generates proxy for your MyServiceAsync service interface. This proxy is responsible for communicating with the server and invoking your callbacks when it gets data from the server. Proxies are generated using GWT code generators mechanism and by default GWT uses ServiceInterfaceProxyGenerator class to generate these proxies.

You can extend this default generator (ServiceInterfaceProxyGenerator class) to automatically use your custom MyAsyncCallbacks in all callbacks invocations. We recently did exactly that in a project. Below there is source code which we used.

Code for MyAsyncCallback, it is identical to the one presented by pathed:

package my.package.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class MyAsyncCallback<T> implements AsyncCallback<T> {

    private final AsyncCallback<T> asyncCallback;

    public MyAsyncCallback(AsyncCallback<T> asyncCallback) {
        this.asyncCallback = asyncCallback;
    }

    @Override
    public void onFailure(Throwable caught) {
        if (caught instanceof SessionTimeoutException) {
            // redirect
            return;
        }

        asyncCallback.onFailure(caught);
    }

    @Override
    public void onSuccess(T result) {
        asyncCallback.onSuccess(result);
    }

}

Code for GWT code generator (MyRpcRemoteProxyGenerator):

package my.package.server;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.user.rebind.rpc.ProxyCreator;
import com.google.gwt.user.rebind.rpc.ServiceInterfaceProxyGenerator;

public class MyRpcRemoteProxyGenerator extends ServiceInterfaceProxyGenerator {

    @Override
    protected ProxyCreator createProxyCreator(JClassType remoteService) {
        return new MyProxyCreator(remoteService);
    }
}

And generator helper class (MyProxyCreator):

package my.package.server;

import java.util.Map;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.user.rebind.SourceWriter;
import com.google.gwt.user.rebind.rpc.ProxyCreator;
import com.google.gwt.user.rebind.rpc.SerializableTypeOracle;


public class MyProxyCreator extends ProxyCreator {

    private final String methodStrTemplate = "@Override\n"
            + "protected <T> com.google.gwt.http.client.Request doInvoke(ResponseReader responseReader, "
            + "String methodName, int invocationCount, String requestData, "
            + "com.google.gwt.user.client.rpc.AsyncCallback<T> callback) {\n"
            + "${method-body}" + "}\n";

    public MyProxyCreator(JClassType serviceIntf) {
        super(serviceIntf);
    }

    @Override
    protected void generateProxyMethods(SourceWriter w,
            SerializableTypeOracle serializableTypeOracle,
            Map<JMethod, JMethod> syncMethToAsyncMethMap) {
        // generate standard proxy methods
        super.generateProxyMethods(w, serializableTypeOracle,
                syncMethToAsyncMethMap);

        // generate additional method
        overrideDoInvokeMethod(w);
    }

    private void overrideDoInvokeMethod(SourceWriter w) {
        StringBuilder methodBody = new StringBuilder();
        methodBody
                .append("final com.google.gwt.user.client.rpc.AsyncCallback newAsyncCallback = new my.package.client.MyAsyncCallback(callback);\n");
        methodBody
                .append("return super.doInvoke(responseReader, methodName, invocationCount, requestData, newAsyncCallback);\n");

        String methodStr = methodStrTemplate.replace("${method-body}",
                methodBody);
        w.print(methodStr);
    }

}

Finally you need to register the new code generator to be used for generating proxies for async services. This is done by adding this to your GWT configuration file (gwt.xml file):

<generate-with
    class="my.package.server.MyRpcRemoteProxyGenerator">
    <when-type-assignable class="com.google.gwt.user.client.rpc.RemoteService" />
</generate-with>

At the beginning it may seem to be a very complicated solution :) but it has its strengths:

  • You can still use standard GWT AsyncCallbacks
  • You can enforce redirecting when session times out globally for your application
  • You can easily tun it on and off (by adding or removing generate-with in your GWT config files)