From: Joonas Lehtinen Date: Fri, 26 Sep 2008 14:04:26 +0000 (+0000) Subject: Fixed #2106 : Reviewed, tested, merged (and a bit fixed) Maunos patch for automatic... X-Git-Tag: 6.7.0.beta1~4078 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=404a76e8db84ae86e517931d23876783d1de4aec;p=vaadin-framework.git Fixed #2106 : Reviewed, tested, merged (and a bit fixed) Maunos patch for automatic expiration forward when the session ends. svn changeset:5528/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/Application.java b/src/com/itmill/toolkit/Application.java index adbf587299..6eadaf8e8e 100644 --- a/src/com/itmill/toolkit/Application.java +++ b/src/com/itmill/toolkit/Application.java @@ -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; diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ApplicationConnection.java b/src/com/itmill/toolkit/terminal/gwt/client/ApplicationConnection.java index c06e2b129c..6102d151d1 100755 --- a/src/com/itmill/toolkit/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ApplicationConnection.java @@ -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"); diff --git a/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java b/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java index 9a8ba47b2a..abcc93b1da 100644 --- a/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java +++ b/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java @@ -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 index 0000000000..743aaf0969 --- /dev/null +++ b/src/com/itmill/toolkit/tests/tickets/Ticket2106.java @@ -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())); + } + })); + } + +}