From 0e7ac388039cf8140f37df9d83cf62b9efc95bb8 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 13 Jan 2014 13:50:34 +0200 Subject: Method for retrieving all VaadinSessions inside a HTTP session (#13053) Change-Id: I8e612ccd521d99f6b7ec6d10c72331b4d4373abe --- WebContent/statictestfiles/vaadinsessions.jsp | 54 +++++++++++ server/src/com/vaadin/server/VaadinSession.java | 29 ++++++ .../tests/integration/JSPIntegrationTest.java | 100 +++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 WebContent/statictestfiles/vaadinsessions.jsp create mode 100644 uitest/src/com/vaadin/tests/integration/JSPIntegrationTest.java diff --git a/WebContent/statictestfiles/vaadinsessions.jsp b/WebContent/statictestfiles/vaadinsessions.jsp new file mode 100644 index 0000000000..b22787a203 --- /dev/null +++ b/WebContent/statictestfiles/vaadinsessions.jsp @@ -0,0 +1,54 @@ + +<%@page import="com.vaadin.ui.UI"%> +<%@page import="com.vaadin.server.VaadinSession"%> + + +JSP integration + + + + + + + + + + + + + + + <% + HttpSession httpSession = request.getSession(false); + for (VaadinSession vs : VaadinSession.getAllSessions(httpSession)) { + try { + vs.lock(); + for (UI ui : vs.getUIs()) { + out.append(""); + out.append(""); + out.append(""); + out.append(""); + out.append(""); + out.append(""); + out.append(""); + + } + } finally { + vs.unlock(); + } + + } + %> +
Available UIs:
Service NameCSRF tokenUI idUI typeMain content
" + vs.getService().getServiceName() + + "" + vs.getCsrfToken() + "" + ui.getUIId() + "" + ui.getClass().getName() + "" + ui.getContent().getClass().getName() + "
+ + \ No newline at end of file diff --git a/server/src/com/vaadin/server/VaadinSession.java b/server/src/com/vaadin/server/VaadinSession.java index f34721944a..265d18b859 100644 --- a/server/src/com/vaadin/server/VaadinSession.java +++ b/server/src/com/vaadin/server/VaadinSession.java @@ -22,12 +22,15 @@ import java.io.Serializable; import java.lang.reflect.Method; import java.util.Collection; import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Queue; +import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutionException; @@ -422,6 +425,32 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { return vaadinSession; } + /** + * Retrieves all {@link VaadinSession}s which are stored in the given HTTP + * session + * + * @since 7.2 + * @param httpSession + * the HTTP session + * @return the found VaadinSessions + */ + public static Collection getAllSessions( + HttpSession httpSession) { + Set sessions = new HashSet(); + Enumeration attributeNames = httpSession.getAttributeNames(); + + while (attributeNames.hasMoreElements()) { + String attributeName = attributeNames.nextElement(); + if (attributeName.startsWith(VaadinSession.class.getName() + ".")) { + Object value = httpSession.getAttribute(attributeName); + if (value instanceof VaadinSession) { + sessions.add((VaadinSession) value); + } + } + } + return sessions; + } + /** * Removes this VaadinSession from the HTTP session. * diff --git a/uitest/src/com/vaadin/tests/integration/JSPIntegrationTest.java b/uitest/src/com/vaadin/tests/integration/JSPIntegrationTest.java new file mode 100644 index 0000000000..c5d6a65d87 --- /dev/null +++ b/uitest/src/com/vaadin/tests/integration/JSPIntegrationTest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2000-2013 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.integration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.tests.tb3.PrivateTB3Configuration; + +public class JSPIntegrationTest extends PrivateTB3Configuration { + + final String appRunnerTestUrl = getBaseURL() + "/run/Buttons"; + final String jspUrl = getBaseURL() + "/statictestfiles/vaadinsessions.jsp"; + final String integrationUrl = getBaseURL() + "/integration"; + + @Test + public void listVaadinSessions() { + + assertUICount(0); + + // Open a new UI + getDriver().get(integrationUrl); + assertUICount(1); + + // Open a new UI + getDriver().get(integrationUrl); + + // Should now have two UIs for the same service with different uiIds + List twoUIs = getUIs(); + assertEquals(2, twoUIs.size()); + assertNotEquals(twoUIs.get(0).uiId, twoUIs.get(1).uiId); + assertEquals(twoUIs.get(0).serviceName, twoUIs.get(1).serviceName); + + getDriver().get(appRunnerTestUrl); + // Should now have two services with 2 + 1 UIs + List threeUIs = getUIs(); + assertEquals(3, threeUIs.size()); + Set serviceNames = new HashSet(); + Set uiIds = new HashSet(); + for (UIData uiData : threeUIs) { + serviceNames.add(uiData.serviceName); + uiIds.add(uiData.uiId); + } + assertGreaterOrEqual( + "There should be at least two unique service names", + serviceNames.size(), 2); + assertGreaterOrEqual("There should be at least two unique ui ids", + uiIds.size(), 2); + } + + private static class UIData { + private String serviceName; + private int uiId; + } + + private List getUIs() { + List uis = new ArrayList(); + + getDriver().get(jspUrl); + List rows = getDriver().findElements( + By.xpath("//tr[@class='uirow']")); + for (WebElement row : rows) { + UIData data = new UIData(); + List tds = row.findElements(By.xpath("./td")); + + data.serviceName = tds.get(0).getText(); + data.uiId = Integer.parseInt(tds.get(2).getText()); + + uis.add(data); + } + + return uis; + } + + private void assertUICount(int i) { + assertEquals(i, getUIs().size()); + } +} -- cgit v1.2.3