Reviewers: rjrjr,

Description:
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:


Please review this at http://gwt-code-reviews.appspot.com/1450817/

Affected files:
M samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/client/GaeAuthRequestTransport.java M samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/server/GaeAuthFilter.java M samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/shared/GaeHelper.java


Index: samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/client/GaeAuthRequestTransport.java
===================================================================
--- samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/client/GaeAuthRequestTransport.java (revision 10331) +++ samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/client/GaeAuthRequestTransport.java (working copy)
@@ -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;

@@ -36,14 +37,29 @@
   }

   @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.
              */
Index: samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/server/GaeAuthFilter.java
===================================================================
--- samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/server/GaeAuthFilter.java (revision 10331) +++ samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/server/GaeAuthFilter.java (working copy)
@@ -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 = "/MobileWebApp.html";
+      }
+      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) {
   }
 }
Index: samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/shared/GaeHelper.java
===================================================================
--- samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/shared/GaeHelper.java (revision 10331) +++ samples/mobilewebapp/src/main/java/com/google/gwt/sample/gaerequest/shared/GaeHelper.java (working copy)
@@ -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