Revision: 10333
Author:   [email protected]
Date:     Tue Jun 14 07:50:16 2011
Log: Fixing login redirect in MobileWebApp sample. We now send a redirect URL with every request, and use it if the user isn't logged in. We calculate the redirect URL on the client because the client browser has built in support for parsing the URL components.

Example: http://jlabanca-testing.appspot.com/#tl:

Review at http://gwt-code-reviews.appspot.com/1450817

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=10333

Modified:
/trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/client/GaeAuthRequestTransport.java /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/server/GaeAuthFilter.java /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/shared/GaeHelper.java

=======================================
--- /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/client/GaeAuthRequestTransport.java Wed Jun 1 07:45:02 2011 +++ /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/client/GaeAuthRequestTransport.java Tue Jun 14 07:50:16 2011
@@ -17,10 +17,11 @@

 import com.google.gwt.event.shared.EventBus;
 import com.google.gwt.http.client.Request;
+import com.google.gwt.http.client.RequestBuilder;
 import com.google.gwt.http.client.RequestCallback;
 import com.google.gwt.http.client.Response;
 import com.google.gwt.sample.gaerequest.shared.GaeHelper;
-import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.Window.Location;
import com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport;
 import com.google.web.bindery.requestfactory.shared.ServerFailure;

@@ -34,16 +35,31 @@
   public GaeAuthRequestTransport(EventBus eventBus) {
     this.eventBus = eventBus;
   }
+
+  @Override
+  protected void configureRequestBuilder(RequestBuilder builder) {
+    super.configureRequestBuilder(builder);
+
+    /*
+     * Add the redirect URL in case the user is logged out.
+     *
+     * /MobileWebApp.html?parem0=value0&param1=value1#hash
+     */
+ String redirectUrl = Location.getPath() + Location.getQueryString() + Location.getHash(); + builder.setHeader(GaeHelper.REDIRECT_URL_HTTP_HEADER_NAME, redirectUrl);
+  }

   @Override
protected RequestCallback createRequestCallback(final TransportReceiver receiver) { final RequestCallback superCallback = super.createRequestCallback(receiver);

     return new RequestCallback() {
+      @Override
       public void onError(Request request, Throwable exception) {
         superCallback.onError(request, exception);
       }

+      @Override
       public void onResponseReceived(Request request, Response response) {
         /*
* The GaeAuthFailure filter responds with Response.SC_UNAUTHORIZED and
@@ -51,15 +67,11 @@
* receive that combo, post an event so that the app can handle things
          * as it sees fit.
          */
-
         if (Response.SC_UNAUTHORIZED == response.getStatusCode()) {
           String loginUrl = response.getHeader("login");
           if (loginUrl != null) {
-            // Replace the redirect url placeholder with the current url.
- loginUrl = loginUrl.replace(GaeHelper.REDIRECT_URL_TOKEN, Window.Location.getHref());
-
             /*
-             * Hand the receiver a non-fatal callback, so that *
+             * Hand the receiver a non-fatal callback, so that
* com.google.web.bindery.requestfactory.shared.Receiver will not
              * post a runtime exception.
              */
=======================================
--- /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/server/GaeAuthFilter.java Wed Jun 1 07:45:02 2011 +++ /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/server/GaeAuthFilter.java Tue Jun 14 07:50:16 2011
@@ -35,9 +35,11 @@
  */
 public class GaeAuthFilter implements Filter {

+  @Override
   public void destroy() {
   }

+  @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
       FilterChain filterChain) throws IOException, ServletException {
     UserService userService = UserServiceFactory.getUserService();
@@ -45,7 +47,12 @@
     HttpServletResponse response = (HttpServletResponse) servletResponse;

     if (!userService.isUserLoggedIn()) {
- response.setHeader("login", userService.createLoginURL(GaeHelper.REDIRECT_URL_TOKEN)); + String redirectUrl = request.getHeader(GaeHelper.REDIRECT_URL_HTTP_HEADER_NAME);
+      if (redirectUrl == null || redirectUrl.length() == 0) {
+ // Default to the root page if the redirecturl isn't specified in the request.
+        redirectUrl = "/";
+      }
+      response.setHeader("login", userService.createLoginURL(redirectUrl));
       response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
       return;
     }
@@ -53,6 +60,7 @@
     filterChain.doFilter(request, response);
   }

+  @Override
   public void init(FilterConfig config) {
   }
 }
=======================================
--- /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/shared/GaeHelper.java Wed Jun 1 07:45:02 2011 +++ /trunk/samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/shared/GaeHelper.java Tue Jun 14 07:50:16 2011
@@ -21,9 +21,8 @@
 public interface GaeHelper {

   /**
- * The placeholder token added to the login URL. The client replaces the token
-   * with the current href, which only the client knows.
+ * The name of the HTTP header name used to specify the redirct url when login
+   * is required.
    */
- /* Prefixed with http:// to ensure that GAE doesn't automatically prefix it. */
-  String REDIRECT_URL_TOKEN = "http%3A%2F%2FREDIRECTURL";
-}
+  String REDIRECT_URL_HTTP_HEADER_NAME = "redirecturl";
+}

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to