12

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

Nav
  • 4,450
  • 10
  • 53
  • 84

8 Answers8

21

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)
Community
  • 1
  • 1
Piotr
  • 5,543
  • 1
  • 31
  • 37
  • 1
    This is devilishly clever. I think it would dumbfound a new developer on a project, as there would be no hard references navigable via the IDE to discover how the MyAsyncCallback gets called instead of the AsyncCallback. However I really like that it is a single cutpoint for applying the fix rather than having to always use the custom callback lest a bug resurface. – David Mann May 02 '14 at 01:58
  • @Piotr Is `SessionTimeoutException` a custom exception class that you wrote? If so, would you mind sharing it? – Anish Sana Jan 11 '18 at 11:54
  • @AnishSana Sorry, it was quite a long time ago. I don't have access to this code at the moment. – Piotr Jan 11 '18 at 20:51
5

Yes you should handle session timeout in a onFailure(in my opinion). But there are simple ways to do that.

  1. Implement your own async callback.

    public abstract class MyAsyncCallback<T> implements AsyncCallback<T> {
    
    @Override
    public void onFailure(Throwable arg0) {
        if arg0 is SessionTimeout
            redirect to loginpage
        else
            failure(Throwable ar0)
    }
    
    @Override
    public void onSuccess(T arg0) {
        success(arg0);
    }
    
    public abstract void success(T arg0);
    
    public abstract void failure(Throwable arg0);
    

    }

  2. Use some library like gwt-dispatcher where all rpc-calls go through the same serviceasync and gives u one place to handle onFailures.

pathed
  • 669
  • 4
  • 6
  • I am using MVP pattern so I found it is better to inherits from DefaultDispatchAsync and override execute and check for last execution time, Thanks for your answer. – Nav Sep 07 '10 at 13:17
3

I put a little fix to MyProxyCreator's @Piotr version, adpated to GWT 2.5

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.core.ext.typeinfo.TypeOracle;
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, RpcStatsContext statsContext, 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, TypeOracle typeOracle, Map<JMethod, JMethod> syncMethToAsyncMethMap) {
        // generate standard proxy methods
        super.generateProxyMethods(w, serializableTypeOracle, typeOracle, 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, statsContext, requestData, newAsyncCallback);\n");

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

}

It changed the methods signs for generateProxyMethods and doInvoke.

Best Regards.

iVieL

Vielinko
  • 1,659
  • 1
  • 13
  • 17
2

Why don't you you have a GWT timer (http://google-web-toolkit.googlecode.com/svn/javadoc/2.4/com/google/gwt/user/client/Timer.html) running instead that checks if the session is active/expired and then either prompt the user to extend the session or proceed to logout page. Why are you doing this only on RPC calls?

Jay
  • 21
  • 2
1

Client: All Callbacks extend a Abstract Callback where you implement the onFailur()

public abstract class AbstrCallback<T> implements AsyncCallback<T> {

  @Override
  public void onFailure(Throwable caught) {
    //SessionData Expired Redirect
    if (caught.getMessage().equals("500 " + YourConfig.ERROR_MESSAGE_NOT_LOGGED_IN)) {
      Window.Location.assign(ConfigStatic.LOGIN_PAGE);
    }
    // else{}: Other Error, if you want you could log it on the client
  }
}

Server: All your ServiceImplementations extend AbstractServicesImpl where you have access to your SessionData. Override onBeforeRequestDeserialized(String serializedRequest) and check the SessionData there. If the SessionData has expire then write a spacific error message to the client. This error message is getting checkt in your AbstrCallback and redirect to the Login Page.

public abstract class AbstractServicesImpl extends RemoteServiceServlet {

  protected ServerSessionData sessionData;

  @Override
  protected void onBeforeRequestDeserialized(String serializedRequest) {

    sessionData = getYourSessionDataHere()

    if (this.sessionData == null){ 
      // Write error to the client, just copy paste
      this.getThreadLocalResponse().reset();
      ServletContext servletContext = this.getServletContext();
      HttpServletResponse response = this.getThreadLocalResponse();
      try {
        response.setContentType("text/plain");
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        try {
          response.getOutputStream().write(
            ConfigStatic.ERROR_MESSAGE_NOT_LOGGED_IN.getBytes("UTF-8"));
          response.flushBuffer();
        } catch (IllegalStateException e) {
          // Handle the (unexpected) case where getWriter() was previously used
          response.getWriter().write(YourConfig.ERROR_MESSAGE_NOT_LOGGED_IN);
          response.flushBuffer();
        }
      } catch (IOException ex) {
        servletContext.log(
          "respondWithUnexpectedFailure failed while sending the previous failure to the client",
          ex);
      }
      //Throw Exception to stop the execution of the Servlet
      throw new NullPointerException();
    }
  }

}

In Addition you can also Override doUnexpectedFailure(Throwable t) to avoid logging the thrown NullPointerException.

@Override
protected void doUnexpectedFailure(Throwable t) {
  if (this.sessionData != null) {
    super.doUnexpectedFailure(t);
  }
}
Hollerweger
  • 975
  • 1
  • 13
  • 32
  • It's a workaround, far from elegant, for several reasons: improper use of HTTP codes, improper generation of exceptions, does not use the GWT RPC, etc.. However, considering that GWT hides any possibility to implement a filter that responds using failure messages via RPC, it's an acceptable solution. It saved my day! Thanks. – Italo Borssatto Jun 19 '14 at 01:18
0

You will get com.google.gwt.user.client.rpc.InvocationException when service method is not called because of session expired. You can check that in onFailure method and simply redirect user to login page.


    public void onFailure(Throwable caught) {
    if (caught instanceof InvocationException) {
                            SC.warn("Your session has expired, Please login again.",
                                    value -> com.google.gwt.user.client.Window.Location.replace("/login.jsp"));
    }else{...
    }
    }

Deepti-l
  • 153
  • 1
  • 10
0

I used the following with GWT 2.2 to handle the new doInvoke method:

public class MyProxyCreator extends ProxyCreator {

    private final String methodStrTemplate = "@Override\n"
            + "protected <T> com.google.gwt.http.client.Request doInvoke(ResponseReader responseReader, "
            + "String methodName, com.google.gwt.user.client.rpc.impl.RpcStatsContext statsContext, 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,
            TypeOracle typeOracle,
            Map<JMethod, JMethod> syncMethToAsyncMethMap) {
        // generate standard proxy methods
        super.generateProxyMethods(w, serializableTypeOracle, typeOracle, 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 com.mydomain.client.MyAsyncCallback(callback);\n");
        methodBody
                .append("return super.doInvoke(responseReader, methodName, statsContext, requestData, newAsyncCallback);\n");

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

}
Rori Stumpf
  • 1,912
  • 19
  • 26
0

@Vielinko's update was useful with MyProxyCreator's @Piotr's solution.

For variety, this is an alternative solution to the one provided by @Piotr but it's also quite similar. I found this also worked after implementing with @Piotr's solution to begin with:

Note: update the package name as required.

package com.google.gwt.sample.stockwatcher.server;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.sample.stockwatcher.client.MyRemoteServiceProxy;
import com.google.gwt.user.client.rpc.impl.RemoteServiceProxy;
import com.google.gwt.user.rebind.rpc.ProxyCreator;


public class MyProxyCreator extends ProxyCreator {

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

    /**
     * This proxy creator extends the default GWT {@link ProxyCreator} and replaces {@link RemoteServiceProxy} as base class
     * of proxies with {@link MyRemoteServiceProxy}.
     */
    @Override
    protected Class<? extends RemoteServiceProxy> getProxySupertype() {
        return MyRemoteServiceProxy.class;
    }

}

Create a MyRemoteServiceProxy.java class in your client package:

package com.google.gwt.sample.stockwatcher.client;

import com.google.gwt.user.client.rpc.impl.Serializer;

import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.impl.RemoteServiceProxy;
import com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter;
import com.google.gwt.user.client.rpc.impl.RpcStatsContext;

/**
 * The remote service proxy extends default GWT {@link RemoteServiceProxy} and
 * proxies the {@link AsyncCallback} with the {@link AsyncCallbackProxy}.
 */
public class MyRemoteServiceProxy extends RemoteServiceProxy {

    public MyRemoteServiceProxy(String moduleBaseURL, String remoteServiceRelativePath, String serializationPolicyName,
            Serializer serializer) {
        super(moduleBaseURL, remoteServiceRelativePath, serializationPolicyName, serializer);
    }

    @Override
    protected <T> RequestCallback doCreateRequestCallback(RequestCallbackAdapter.ResponseReader responseReader,
            String methodName, RpcStatsContext statsContext, AsyncCallback<T> callback) {
        return super.doCreateRequestCallback(responseReader, methodName, statsContext,
                new MyAsyncCallback<T>(callback));
    }
}

This is an alternate solution to having the MyProxyCreator and MyRpcRemoteProxyGenerator in @Piotr's solution. I've tested that it works. RPC calls reroute to this function first before they are called. Keep the MyAsyncCallback for handling the session time out. :)

May
  • 29
  • 7