* Rename closeIdleUIs to closeIdleSessions, isIdleUICleanupEnabled to isCloseIdleSessions (#10252)
* Close the whole session at the same time as well
* Move UI lastUidlRequestTime to VaadinSession lastRequestTimestamp (#10253)
* Rename VaadinSession lastRequestTime to lastRequestDuration, totalSessionTime to cumulativeRequestDuration (#10253)
* Rename UI lastHeartbeatTime to lastHeartbeatTimestamp
* Show "Session Expired" notification when a heartbeat fails because of this
Change-Id: If3d4bd9add9995f435c29812eec00b3a3a92a6e6
<param-value>3601</param-value>
</init-param>
<init-param>
- <param-name>closeIdleUIs</param-name>
+ <param-name>closeIdleSessions</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
private boolean standalone;
private ErrorMessage communicationError;
private ErrorMessage authorizationError;
+ private ErrorMessage sessionExpiredError;
private int heartbeatInterval;
private HashMap<Integer, String> unknownComponents;
return authorizationError;
}
+ public ErrorMessage getSessionExpiredError() {
+ return sessionExpiredError;
+ }
+
/**
* Reads the configuration values defined by the bootstrap javascript.
*/
communicationError = jsoConfiguration.getConfigError("comErrMsg");
authorizationError = jsoConfiguration.getConfigError("authErrMsg");
+ sessionExpiredError = jsoConfiguration.getConfigError("sessExpMsg");
// boostrap sets initPending to false if it has sent the browser details
if (jsoConfiguration.getConfigBoolean("initPending") == Boolean.FALSE) {
*/
protected void showCommunicationError(String details, int statusCode) {
VConsole.error("Communication error: " + details);
- ErrorMessage communicationError = configuration.getCommunicationError();
- showError(details, communicationError.getCaption(),
- communicationError.getMessage(), communicationError.getUrl());
+ showError(details, configuration.getCommunicationError());
}
/**
*/
protected void showAuthenticationError(String details) {
VConsole.error("Authentication error: " + details);
- ErrorMessage authorizationError = configuration.getAuthorizationError();
- showError(details, authorizationError.getCaption(),
- authorizationError.getMessage(), authorizationError.getUrl());
+ showError(details, configuration.getAuthorizationError());
+ }
+
+ /**
+ * Shows the session expiration notification.
+ *
+ * @param details
+ * Optional details for debugging.
+ */
+ protected void showSessionExpiredError(String details) {
+ VConsole.error("Session expired: " + details);
+ showError(details, configuration.getSessionExpiredError());
+ }
+
+ /**
+ * Shows an error notification.
+ *
+ * @param details
+ * Optional details for debugging.
+ * @param message
+ * An ErrorMessage describing the error.
+ */
+ protected void showError(String details, ErrorMessage message) {
+ showError(details, message.getCaption(), message.getMessage(),
+ message.getUrl());
}
/**
if (html.length() > 0) {
// Add error description
- html.append("<br/><p><I style=\"font-size:0.7em\">");
- html.append(details);
- html.append("</I></p>");
+ if (details != null) {
+ html.append("<p><i style=\"font-size:0.7em\">");
+ html.append(details);
+ html.append("</i></p>");
+ }
VNotification n = VNotification.createNotification(1000 * 60 * 45,
uIConnector.getWidget());
if (meta != null) {
if (meta.containsKey("appError")) {
ValueMap error = meta.getValueMap("appError");
- String html = "";
- if (error.containsKey("caption")
- && error.getString("caption") != null) {
- html += "<h1>" + error.getAsString("caption")
- + "</h1>";
- }
- if (error.containsKey("message")
- && error.getString("message") != null) {
- html += "<p>" + error.getAsString("message")
- + "</p>";
- }
- String url = null;
- if (error.containsKey("url")) {
- url = error.getString("url");
- }
- if (html.length() != 0) {
- /* 45 min */
- VNotification n = VNotification.createNotification(
- 1000 * 60 * 45, uIConnector.getWidget());
- n.addEventListener(new NotificationRedirect(url));
- n.show(html, VNotification.CENTERED_TOP,
- VNotification.STYLE_SYSTEM);
- } else {
- redirect(url);
- }
+ showError(null, error.getString("caption"),
+ error.getString("message"),
+ error.getString("url"));
+
applicationRunning = false;
}
if (validatingLayouts) {
* <p>
* Heartbeat requests are used to inform the server that the client-side is
* still alive. If the client page is closed or the connection lost, the
- * server will eventually close the inactive Root.
+ * server will eventually close the inactive UI.
* <p>
* <b>TODO</b>: Improved error handling, like in doUidlRequest().
*
// TODO Permit retry in some error situations
VConsole.log("Heartbeat response OK");
scheduleHeartbeat();
+ } else if (status == Response.SC_GONE) {
+ showSessionExpiredError(null);
} else {
- VConsole.error("Heartbeat request failed with status code "
+ VConsole.error("Failed sending heartbeat to server. Error code: "
+ status);
}
}
@Override
public void onError(Request request, Throwable exception) {
- VConsole.error("Heartbeat request resulted in exception");
- VConsole.error(exception);
+ VConsole.error("Exception sending heartbeat: " + exception);
}
};
return;
}
- // Keep the UI alive
- uI.setLastUidlRequestTime(System.currentTimeMillis());
+ session.setLastRequestTimestamp(System.currentTimeMillis());
// Change all variables based on request parameters
if (!handleVariables(request, response, callback, session, uI)) {
*/
private void writePerformanceData(final PrintWriter outWriter) {
outWriter.write(String.format(", \"timings\":[%d, %d]",
- session.getTotalSessionTime(), session.getLastRequestTime()));
+ session.getCumulativeRequestDuration(), session.getLastRequestDuration()));
}
private void legacyPaint(PaintTarget paintTarget,
// null-check below handles this as well
}
if (ui != null) {
- ui.setLastHeartbeatTime(System.currentTimeMillis());
+ ui.setLastHeartbeatTimestamp(System.currentTimeMillis());
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "UI not found");
}
authErrMsg.put("url", systemMessages.getAuthenticationErrorURL());
appConfig.put("authErrMsg", authErrMsg);
+
+ JSONObject sessExpMsg = new JSONObject();
+ sessExpMsg
+ .put("caption", systemMessages.getSessionExpiredCaption());
+ sessExpMsg
+ .put("message", systemMessages.getSessionExpiredMessage());
+ sessExpMsg.put("url", systemMessages.getSessionExpiredURL());
+
+ appConfig.put("sessExpMsg", sessExpMsg);
}
// getStaticFileLocation documented to never end with a slash
static final String SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION = "disable-xsrf-protection";
static final String SERVLET_PARAMETER_RESOURCE_CACHE_TIME = "resourceCacheTime";
static final String SERVLET_PARAMETER_HEARTBEAT_INTERVAL = "heartbeatInterval";
- static final String SERVLET_PARAMETER_CLOSE_IDLE_UIS = "closeIdleUIs";
+ static final String SERVLET_PARAMETER_CLOSE_IDLE_SESSIONS = "closeIdleSessions";
static final String SERVLET_PARAMETER_UI_PROVIDER = "UIProvider";
// Configurable parameter names
private boolean xsrfProtectionEnabled;
private int resourceCacheTime;
private int heartbeatInterval;
- private boolean idleUICleanupEnabled;
+ private boolean closeIdleSessions;
private final Class<?> systemPropertyBaseClass;
/**
checkXsrfProtection();
checkResourceCacheTime();
checkHeartbeatInterval();
- checkIdleUICleanup();
+ checkCloseIdleSessions();
}
@Override
}
@Override
- public boolean isIdleUICleanupEnabled() {
- return idleUICleanupEnabled;
+ public boolean isCloseIdleSessions() {
+ return closeIdleSessions;
}
/**
}
}
- private void checkIdleUICleanup() {
- idleUICleanupEnabled = getApplicationOrSystemProperty(
- Constants.SERVLET_PARAMETER_CLOSE_IDLE_UIS, "false").equals(
- "true");
+ private void checkCloseIdleSessions() {
+ closeIdleSessions = getApplicationOrSystemProperty(
+ Constants.SERVLET_PARAMETER_CLOSE_IDLE_SESSIONS, "false")
+ .equals("true");
}
private Logger getLogger() {
public int getHeartbeatInterval();
/**
- * Returns whether UIs that have no other activity than heartbeat requests
- * should be removed from the session after they have been idle the maximum
- * inactivity time enforced by the session.
+ * Returns whether a session should be closed when all its open UIs have
+ * been idle for longer than its configured maximum inactivity time.
+ * <p>
+ * A UI is idle if it is open on the client side but has no activity other
+ * than heartbeat requests. If {@code isCloseIdleSessions() == false},
+ * heartbeat requests cause the session to stay open for as long as there
+ * are open UIs on the client side. If it is {@code true}, the session is
+ * eventually closed if the open UIs do not have any user interaction.
*
* @see WrappedSession#getMaxInactiveInterval()
*
* @since 7.0.0
*
- * @return True if UIs receiving only heartbeat requests are eventually
- * removed; false if heartbeat requests extend UI lifetime
- * indefinitely.
+ * @return True if UIs and sessions receiving only heartbeat requests are
+ * eventually closed; false if heartbeat requests extend UI and
+ * session lifetime indefinitely.
*/
- public boolean isIdleUICleanupEnabled();
+ public boolean isCloseIdleSessions();
/**
* Gets the properties configured for the deployment, e.g. as init
// The timings must be stored in the context, since a new
// RequestTimer is created for every request.
- context.setLastRequestTime(time);
+ context.setLastRequestDuration(time);
}
}
private AbstractCommunicationManager communicationManager;
- private long totalSessionTime = 0;
+ private long cumulativeRequestDuration = 0;
- private long lastRequestTime = -1;
+ private long lastRequestDuration = -1;
+
+ private long lastRequestTimestamp = System.currentTimeMillis();
private transient WrappedSession session;
}
/**
- * @return The total time spent servicing requests in this session.
+ * @return The total time spent servicing requests in this session, in
+ * milliseconds.
*/
- public long getTotalSessionTime() {
- return totalSessionTime;
+ public long getCumulativeRequestDuration() {
+ return cumulativeRequestDuration;
}
/**
* the total time spent servicing requests in this session.
*
* @param time
- * the time spent in the last request.
+ * The time spent in the last request, in milliseconds.
+ */
+ public void setLastRequestDuration(long time) {
+ lastRequestDuration = time;
+ cumulativeRequestDuration += time;
+ }
+
+ /**
+ * @return The time spent servicing the last request in this session, in
+ * milliseconds.
*/
- public void setLastRequestTime(long time) {
- lastRequestTime = time;
- totalSessionTime += time;
+ public long getLastRequestDuration() {
+ return lastRequestDuration;
}
/**
- * @return the time spent servicing the last request in this session.
+ * Sets the time when the last UIDL request was serviced in this session.
+ *
+ * @param timestamp
+ * The time when the last request was handled, in milliseconds
+ * since the epoch.
+ *
*/
- public long getLastRequestTime() {
- return lastRequestTime;
+ public void setLastRequestTimestamp(long timestamp) {
+ lastRequestTimestamp = timestamp;
}
/**
*/
@Deprecated
public void cleanupInactiveUIs() {
- for (UI ui : new ArrayList<UI>(uIs.values())) {
- if (!isUIAlive(ui)) {
- cleanupUI(ui);
- getLogger().fine(
- "Closed UI #" + ui.getUIId() + " due to inactivity");
+ if (getUidlRequestTimeout() >= 0
+ && System.currentTimeMillis() - lastRequestTimestamp > 1000 * getUidlRequestTimeout()) {
+ close();
+ } else {
+ for (UI ui : new ArrayList<UI>(uIs.values())) {
+ if (!isUIAlive(ui)) {
+ cleanupUI(ui);
+ getLogger()
+ .fine("Closed UI #" + ui.getUIId()
+ + " due to inactivity");
+ }
}
}
}
* otherwise heartbeat requests are enough to extend UI lifetime
* indefinitely.
*
- * @see DeploymentConfiguration#isIdleUICleanupEnabled()
+ * @see DeploymentConfiguration#isCloseIdleSessions()
* @see #getHeartbeatTimeout()
* @see #cleanupInactiveUIs()
*
* timeout never occurs.
*/
protected int getUidlRequestTimeout() {
- return configuration.isIdleUICleanupEnabled() ? getSession()
+ return configuration.isCloseIdleSessions() ? getSession()
.getMaxInactiveInterval() : -1;
}
protected boolean isUIAlive(UI ui) {
long now = System.currentTimeMillis();
if (getHeartbeatTimeout() >= 0
- && now - ui.getLastHeartbeatTime() > 1000 * getHeartbeatTimeout()) {
- return false;
- }
- if (getUidlRequestTimeout() >= 0
- && now - ui.getLastUidlRequestTime() > 1000 * getUidlRequestTimeout()) {
+ && now - ui.getLastHeartbeatTimestamp() > 1000 * getHeartbeatTimeout()) {
return false;
}
return true;
* current time whenever the application receives a heartbeat or UIDL
* request from the client for this UI.
*/
- private long lastHeartbeat = System.currentTimeMillis();
-
- private long lastUidlRequest = System.currentTimeMillis();
+ private long lastHeartbeatTimestamp = System.currentTimeMillis();
/**
* Creates a new empty UI without a caption. The content of the UI must be
}
/**
- * Returns the timestamp (milliseconds since the epoch) of the last received
- * heartbeat for this UI.
+ * Returns the timestamp of the last received heartbeat for this UI.
*
* @see #heartbeat()
* @see VaadinSession#cleanupInactiveUIs()
*
- * @return The time the last heartbeat request occurred.
- */
- public long getLastHeartbeatTime() {
- return lastHeartbeat;
- }
-
- /**
- * Returns the timestamp (milliseconds since the epoch) of the last received
- * UIDL request for this UI.
- *
- * @return
+ * @return The time the last heartbeat request occurred, in milliseconds
+ * since the epoch.
*/
- public long getLastUidlRequestTime() {
- return lastUidlRequest;
+ public long getLastHeartbeatTimestamp() {
+ return lastHeartbeatTimestamp;
}
/**
* Sets the last heartbeat request timestamp for this UI. Called by the
* framework whenever the application receives a valid heartbeat request for
* this UI.
+ *
+ * @param lastHeartbeat
+ * The time the last heartbeat request occurred, in milliseconds
+ * since the epoch.
*/
- public void setLastHeartbeatTime(long lastHeartbeat) {
- this.lastHeartbeat = lastHeartbeat;
- }
-
- /**
- * Sets the last UIDL request timestamp for this UI. Called by the framework
- * whenever the application receives a valid UIDL request for this UI.
- */
- public void setLastUidlRequestTime(long lastUidlRequest) {
- this.lastUidlRequest = lastUidlRequest;
+ public void setLastHeartbeatTimestamp(long lastHeartbeat) {
+ lastHeartbeatTimestamp = lastHeartbeat;
}
/**
</tr>
<tr>
<td>assertTextPresent</td>
- <td>exact:closeIdleUIs: true</td>
+ <td>exact:closeIdleSessions: true</td>
<td></td>
</tr>
<tr>