From 6b3a109b3af8b9adfa48a95407a872eeaedd8ef0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 16 Oct 2012 11:43:02 +0300 Subject: [PATCH] Add VaadinService.closeSession + test (#9859) Also test that the HttpSession can be invalidated (#6093) Change-Id: I4e415fe15d7a734db81562e24a5ab6a7fbc5304b --- .../src/com/vaadin/server/VaadinService.java | 18 +++ .../vaadin/server/VaadinServiceSession.java | 20 ++++ .../applicationcontext/CloseSession.html | 90 +++++++++++++++ .../applicationcontext/CloseSession.java | 105 ++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 uitest/src/com/vaadin/tests/applicationcontext/CloseSession.html create mode 100644 uitest/src/com/vaadin/tests/applicationcontext/CloseSession.java diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index 9d78d4c107..44e82b5898 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -737,4 +737,22 @@ public abstract class VaadinService implements Serializable { */ public abstract String getMainDivId(VaadinServiceSession session, VaadinRequest request, Class uiClass); + + /** + * Closes the VaadinServiceSession and discards all associated UI state. + * After the session has been discarded, any UIs that have been left open + * will give an Out of sync error ( + * {@link SystemMessages#getOutOfSyncCaption()}) error and a new session + * will be created for serving new UIs. + *

+ * To avoid causing out of sync errors, you should typically redirect to + * some other page using {@link Page#setLocation(String)} to make the + * browser unload the invalidated UI. + * + * @param session + * the session to close + */ + public void closeSession(VaadinServiceSession session) { + session.removeFromSession(this); + } } diff --git a/server/src/com/vaadin/server/VaadinServiceSession.java b/server/src/com/vaadin/server/VaadinServiceSession.java index 62a11c710a..de5accbfd0 100644 --- a/server/src/com/vaadin/server/VaadinServiceSession.java +++ b/server/src/com/vaadin/server/VaadinServiceSession.java @@ -1036,4 +1036,24 @@ public class VaadinServiceSession implements HttpSessionBindingListener, return service; } + /** + * Closes this session and discards all associated UI state. After the + * session has been discarded, any UIs that have been left open will give an + * Out of sync error ({@link SystemMessages#getOutOfSyncCaption()}) error + * and a new session will be created for serving new UIs. + *

+ * To avoid causing out of sync errors, you should typically redirect to + * some other page using {@link Page#setLocation(String)} to make the + * browser unload the invalidated UI. + *

+ * This method is just a shorthand to + * {@link VaadinService#closeSession(VaadinServiceSession)} + * + * @see VaadinService#closeSession(VaadinServiceSession) + * + */ + public void close() { + getService().closeSession(this); + } + } diff --git a/uitest/src/com/vaadin/tests/applicationcontext/CloseSession.html b/uitest/src/com/vaadin/tests/applicationcontext/CloseSession.html new file mode 100644 index 0000000000..45be36eac7 --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationcontext/CloseSession.html @@ -0,0 +1,90 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickAndWaitvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[2]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_2exact:4. Same hash as current? false
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_0exact:6. Same WrappedSession id? true
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickAndWaitvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[4]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_2exact:4. Same hash as current? false
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_0exact:6. Same WrappedSession id? false
clickAndWaitvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VButton[0]/domChild[0]/domChild[0]
assertLocationhttps://www.google.com/https://www.google.com/
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[3]/VButton[0]/domChild[0]/domChild[0]
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[3]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::Root/VNotification[0]/HTML[0]/domChild[0]Session Expired
+ + diff --git a/uitest/src/com/vaadin/tests/applicationcontext/CloseSession.java b/uitest/src/com/vaadin/tests/applicationcontext/CloseSession.java new file mode 100644 index 0000000000..0278190084 --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationcontext/CloseSession.java @@ -0,0 +1,105 @@ +/* + * Copyright 2012 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.tests.applicationcontext; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.server.VaadinService; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.util.Log; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; + +public class CloseSession extends AbstractTestUI { + private static final String OLD_HASH_PARAM = "oldHash"; + private static final String OLD_SESSION_ID_PARAM = "oldSessionId"; + + private final Log log = new Log(6); + + @Override + protected void setup(VaadinRequest request) { + final int sessionHash = getSession().hashCode(); + final String sessionId = request.getWrappedSession().getId(); + + log.log("Current session hashcode: " + sessionHash); + log.log("Current WrappedSession id: " + sessionId); + + // Log previous values to make it easier to see what has changed + String oldHashValue = request.getParameter(OLD_HASH_PARAM); + if (oldHashValue != null) { + log.log("Old session hashcode: " + oldHashValue); + log.log("Same hash as current? " + + oldHashValue.equals(Integer.toString(sessionHash))); + } + + String oldSessionId = request.getParameter(OLD_SESSION_ID_PARAM); + if (oldSessionId != null) { + log.log("Old WrappedSession id: " + oldSessionId); + log.log("Same WrappedSession id? " + oldSessionId.equals(sessionId)); + } + + // Add parameters to help see what has changed + final String reopenUrl = getPage().getLocation().getPath() + "?" + + OLD_HASH_PARAM + "=" + sessionHash + "&" + + OLD_SESSION_ID_PARAM + "=" + sessionId; + + addComponent(log); + addComponent(new Button( + "Close VaadinServiceSession and redirect to Google", + new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + getPage().setLocation("https://www.google.com"); + getSession().close(); + } + })); + addComponent(new Button("Close VaadinServiceSession and reopen page", + new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + getPage().setLocation(reopenUrl); + getSession().close(); + } + })); + addComponent(new Button("Just close session", + new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + getSession().close(); + } + })); + addComponent(new Button("Invalidate HttpSession and reopen page", + new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + getPage().setLocation(reopenUrl); + VaadinService.getCurrentRequest().getWrappedSession() + .invalidate(); + } + })); + } + + @Override + protected String getTestDescription() { + return "Test for closing the session and redirecting the user"; + } + + @Override + protected Integer getTicketNumber() { + return Integer.valueOf(9859); + } + +} -- 2.39.5