]> source.dussan.org Git - vaadin-framework.git/commitdiff
-Enhancement: Application.setSessionExpiredURL now allows the developer to set the...
authorJouni Koivuviita <jouni.koivuviita@itmill.com>
Mon, 7 Apr 2008 09:30:40 +0000 (09:30 +0000)
committerJouni Koivuviita <jouni.koivuviita@itmill.com>
Mon, 7 Apr 2008 09:30:40 +0000 (09:30 +0000)
-Minor modification to the session expired notification (onclick="window.location.reload()" was not working properly).
-Enhancement: "closeApplication" parameter allows Testing Tools to close the application after tests.

svn changeset:4134/svn branch:trunk

src/com/itmill/toolkit/Application.java
src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java

index 5a18903fa10f47196e8532406d047c1d12ad39ab..6a7758f8802ce1c1505591e2760ee4ed2d35b61c 100644 (file)
@@ -163,11 +163,18 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener
     private long lastResourceKeyNumber = 0;
 
     /**
-     * URL the user is redirected to on application close or null if application
-     * is just closed
+     * URL where the user is redirected to on application close, or null if
+     * application is just closed without redirection.
      */
     private String logoutURL = null;
 
+    /**
+     * URL where the user is redirected to when the Toolkit ApplicationServlet
+     * session expires, or null if the application is just closed without
+     * redirection.
+     */
+    private String expiredURL = null;
+
     private Focusable pendingFocus;
 
     /**
@@ -996,7 +1003,7 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener
     }
 
     /**
-     * Returns the URL user is redirected to on application close.If the URL is
+     * Returns the URL user is redirected to on application close. If the URL is
      * <code>null</code>, the application is closed normally as defined by
      * the application running environment.
      * <p>
@@ -1024,6 +1031,31 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener
         this.logoutURL = logoutURL;
     }
 
+    /**
+     * Returns the URL where user is redirected to when the Toolkit
+     * ApplicationServlet session expires. If the URL is <code>null</code>,
+     * the application is closed normally and it shows a notification to the
+     * client.
+     * 
+     * @return the URL.
+     */
+    public String getSessionExpiredURL() {
+        return expiredURL;
+    }
+
+    /**
+     * Sets the URL where user is redirected to when the Toolkit
+     * ApplicationServlet session expires. If the URL is <code>null</code>,
+     * the application is closed normally and it shows a notification to the
+     * client.
+     * 
+     * @param expiredURL
+     *                the expiredURL to set.
+     */
+    public void setSessionExpiredURL(String expiredURL) {
+        this.expiredURL = expiredURL;
+    }
+
     /**
      * <p>
      * Invoked by the terminal on any exception that occurs in application and
index 1edd69fc72981c52dfdd331624afbbcae3c0cec5..56fab0803c3255d823c0a38b062ee3b67ce50df3 100644 (file)
@@ -398,11 +398,11 @@ public class ApplicationServlet extends HttpServlet {
             // Get existing application
             application = getExistingApplication(request, response);
             if (application == null
-                    || request.getParameter("restartApplication") != null) {
-                if (request.getParameter("restartApplication") != null
-                        && application != null) {
+                    || request.getParameter("restartApplication") != null
+                    || request.getParameter("closeApplication") != null) {
+                if (application != null) {
                     application.close();
-                    final HttpSession session = request.getSession();
+                    final HttpSession session = request.getSession(false);
                     if (session != null) {
                         application.close();
                         ApplicationServlet.applicationToAjaxAppMgrMap
@@ -411,6 +411,9 @@ public class ApplicationServlet extends HttpServlet {
                                 .removeApplication(application);
                     }
                 }
+                if (request.getParameter("closeApplication") != null) {
+                    return;
+                }
                 // Not found, creating new application
                 application = getNewApplication(request, response);
             }
@@ -471,7 +474,32 @@ public class ApplicationServlet extends HttpServlet {
 
         } catch (final SessionExpired e) {
             // Session has expired
-            criticalNotification(request, response, "Your session has expired.");
+            // Get new application so we can fetch the sessionExpiredURL
+            Application app = null;
+            try {
+                app = (Application) applicationClass.newInstance();
+                app.init();
+            } catch (InstantiationException e1) {
+                // Should not happen
+                e1.printStackTrace();
+            } catch (IllegalAccessException e1) {
+                // Should not happen
+                e1.printStackTrace();
+            }
+
+            // Redirect if expiredURL is found
+            if (app != null && app.getSessionExpiredURL() != null) {
+                if (UIDLrequest) {
+                    redirectUidlRequest(request, response, app
+                            .getSessionExpiredURL());
+                } else {
+                    response.sendRedirect(app.getSessionExpiredURL());
+                }
+            } else {
+                // Else show a notification to the client
+                criticalNotification(request, response,
+                        "Your session has expired.");
+            }
         } catch (final Throwable e) {
             e.printStackTrace();
             // if this was an UIDL request, response UIDL back to client
@@ -535,6 +563,31 @@ public class ApplicationServlet extends HttpServlet {
         }
     }
 
+    /**
+     * Redirect an UIDL request to move to a new URL.
+     * 
+     * @param request
+     *                the HTTP request instance.
+     * @param response
+     *                the HTTP response to write to.
+     * @param url
+     *                the URL to redirect to.
+     * @throws IOException
+     *                 if the writing failed due to input/output error.
+     */
+    private void redirectUidlRequest(HttpServletRequest request,
+            HttpServletResponse response, String url) throws IOException {
+        // Set the response type
+        response.setContentType("application/json; charset=UTF-8");
+        final ServletOutputStream out = response.getOutputStream();
+        final PrintWriter outWriter = new PrintWriter(new BufferedWriter(
+                new OutputStreamWriter(out, "UTF-8")));
+        outWriter.print("for(;;);[{\"redirect\":{\"url\":\"" + url + "\"}}]");
+        outWriter.flush();
+        outWriter.close();
+        out.flush();
+    }
+
     /**
      * Send notification to client's application. Used to notify client of
      * critical errors and session expiration due to long inactivity. Server has
@@ -566,10 +619,8 @@ public class ApplicationServlet extends HttpServlet {
                 new OutputStreamWriter(out, "UTF-8")));
         outWriter.print("for(;;);[{\"changes\":[], \"meta\" : {"
                 + "\"appError\": {" + "\"caption\":\"" + caption + "\","
-                + "\"message\" : \"<br />Please click <a href=\\\"\\\""
-                + "onclick=\\\"javascript:window.location.reload()\\\" >"
-                + "here</a> to restart your application.<br />"
-                + "You can also click your browser's refresh button.\""
+                + "\"message\" : \"<br />You can click your browser's"
+                + " refresh button to restart your application.\""
                 + "}}, \"resources\": {}, \"locales\":[]}]");
         outWriter.flush();
         outWriter.close();