]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixed #2106 : Reviewed, tested, merged (and a bit fixed) Maunos patch for automatic...
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>
Fri, 26 Sep 2008 14:04:26 +0000 (14:04 +0000)
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>
Fri, 26 Sep 2008 14:04:26 +0000 (14:04 +0000)
svn changeset:5528/svn branch:trunk

src/com/itmill/toolkit/Application.java
src/com/itmill/toolkit/terminal/gwt/client/ApplicationConnection.java
src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java
src/com/itmill/toolkit/tests/tickets/Ticket2106.java [new file with mode: 0644]

index adbf58729988843a60a9617d0d3f1c77cefce54c..6eadaf8e8e78e15cf7d28e1631acf796c76b35f4 100644 (file)
@@ -1393,10 +1393,11 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener
 
         /**
          * Enables or disables the notification. If disabled, the set URL (or
-         * current) is loaded directly.
+         * current) is loaded directly when next transaction between server and
+         * client happens.
          * 
          * @param sessionExpiredNotificationEnabled
-         *            true = enabled, false = disabled
+         *                true = enabled, false = disabled
          */
         public void setSessionExpiredNotificationEnabled(
                 boolean sessionExpiredNotificationEnabled) {
@@ -1405,7 +1406,9 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener
 
         /**
          * Sets the caption of the notification. Set to null for no caption. If
-         * both caption and message is null, the notification is disabled;
+         * both caption and message are null, client automatically forwards to
+         * sessionExpiredUrl after timeout timer expires. Timer uses value read
+         * from HTTPSession.getMaxInactiveInterval()
          * 
          * @param sessionExpiredCaption
          *            the caption
@@ -1416,10 +1419,12 @@ public abstract class Application implements URIHandler, Terminal.ErrorListener
 
         /**
          * Sets the message of the notification. Set to null for no message. If
-         * both caption and message is null, the notification is disabled;
+         * both caption and message are null, client automatically forwards to
+         * sessionExpiredUrl after timeout timer expires. Timer uses value read
+         * from HTTPSession.getMaxInactiveInterval()
          * 
          * @param sessionExpiredMessage
-         *            the message
+         *                the message
          */
         public void setSessionExpiredMessage(String sessionExpiredMessage) {
             this.sessionExpiredMessage = sessionExpiredMessage;
index c06e2b129c405da007bada58e600cc5063f5de9c..6102d151d18611920091dacb4869260e9feeb0bf 100755 (executable)
@@ -108,6 +108,12 @@ public class ApplicationConnection {
 
     /** List of pending variable change bursts that must be submitted in order */
     private final Vector pendingVariableBursts = new Vector();
+    
+    /** Timer for automatic refirect to SessionExpiredURL */
+    private Timer redirectTimer; 
+    
+    /** redirectTimer scheduling interval in seconds */ 
+    private int sessionExpirationInterval; 
 
     public ApplicationConnection(WidgetSet widgetSet,
             ApplicationConfiguration cnf) {
@@ -500,8 +506,19 @@ public class ApplicationConnection {
                 idToPaintable.clear();
                 paintableToId.clear();
             }
+            if (meta.containsKey("timedRedirect")) {
+                final JSONObject timedRedirect = meta.get("timedRedirect").isObject();
+                redirectTimer = new Timer() { 
+                    public void run() { 
+                        redirect(timedRedirect.get("url").isString().stringValue()); 
+                    }
+                };                
+                sessionExpirationInterval = Integer.parseInt(timedRedirect.get("interval").toString());
+            }
+        }
+        if (redirectTimer != null) {
+            redirectTimer.schedule(1000 * sessionExpirationInterval);
         }
-
         // Process changes
         final JSONArray changes = (JSONArray) ((JSONObject) json)
                 .get("changes");
index 9a8ba47b2af2d77d59d6fb0208267893173cb0d3..abcc93b1dae6e65c2c848d5979fe373fa6bfcfe7 100644 (file)
@@ -12,6 +12,7 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.text.DateFormatSymbols;
 import java.text.SimpleDateFormat;
@@ -104,6 +105,8 @@ public class CommunicationManager implements Paintable.RepaintRequestListener {
 
     private int pendingLocalesIndex;
 
+    private int timeoutInterval = -1;
+
     public CommunicationManager(Application application,
             ApplicationServlet applicationServlet) {
         this.application = application;
@@ -440,6 +443,42 @@ public class CommunicationManager implements Paintable.RepaintRequestListener {
                 outWriter.write("\"repaintAll\":true");
             }
 
+            SystemMessages ci = null;
+            try {
+                Method m = application.getClass().getMethod(
+                        "getSystemMessages", null);
+                ci = (Application.SystemMessages) m.invoke(null, null);
+            } catch (NoSuchMethodException e1) {
+                e1.printStackTrace();
+            } catch (IllegalArgumentException e) {
+                e.printStackTrace();
+            } catch (IllegalAccessException e) {
+                e.printStackTrace();
+            } catch (InvocationTargetException e) {
+                e.printStackTrace();
+            }
+
+            // meta instruction for client to enable auto-forward to
+            // sessionExpiredURL after timer expires.
+            if (ci != null && ci.getSessionExpiredMessage() == null
+                    && ci.getSessionExpiredCaption() == null
+                    && ci.isSessionExpiredNotificationEnabled()) {
+                int newTimeoutInterval = request.getSession()
+                        .getMaxInactiveInterval();
+                if (repaintAll || (timeoutInterval != newTimeoutInterval)) {
+                    String escapedURL = ci.getSessionExpiredURL() == null ? ""
+                            : ci.getSessionExpiredURL().replace("/", "\\/");
+                    if (metaOpen) {
+                        outWriter.write(",");
+                    }
+                    outWriter.write("\"timedRedirect\":{\"interval\":"
+                            + (newTimeoutInterval + 15) + ",\"url\":\""
+                            + escapedURL + "\"}");
+                    metaOpen = true;
+                }
+                timeoutInterval = newTimeoutInterval;
+            }
+
             // add meta instruction for client to set focus if it is set
             final Paintable f = (Paintable) application.consumeFocus();
             if (f != null) {
diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket2106.java b/src/com/itmill/toolkit/tests/tickets/Ticket2106.java
new file mode 100644 (file)
index 0000000..743aaf0
--- /dev/null
@@ -0,0 +1,37 @@
+package com.itmill.toolkit.tests.tickets;
+
+import java.util.Date;
+
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+
+public class Ticket2106 extends Application {
+
+    private static CustomizedSystemMessages msgs = new Application.CustomizedSystemMessages();
+    static {
+        // We will forward the user to www.itmill.com when the session expires
+        msgs.setSessionExpiredURL("http://www.itmill.com");
+        msgs.setSessionExpiredMessage(null);
+        msgs.setSessionExpiredCaption(null);
+    }
+
+    public static Application.SystemMessages getSystemMessages() {
+        return msgs;
+    }
+
+    public void init() {
+        setMainWindow(new Window("#2106"));
+        getMainWindow().addComponent(
+                new Button("Do nothing", new Button.ClickListener() {
+                    public void buttonClick(ClickEvent event) {
+                        getMainWindow().addComponent(
+                                new Label("Last time did nothing: "
+                                        + new Date()));
+                    }
+                }));
+    }
+
+}