From 48c19777e566dd8cd5e496ea364de8aea447abbf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Mon, 8 Oct 2012 13:17:07 +0300 Subject: [PATCH] Remove VaadinServiceSession.getURL (#9884) * Give an URL to LegacyApplication when initializing * Update LoginForm to use DynamicConnectorResource instead of RequestHandler * Make CustomUIClassLoader work again (including previous issues not caused by this change) * Update some other tests to use more sensible URLs Change-Id: I53ed5e9be3b44ed1b62f9762507b0007d53f15b7 --- server/src/com/vaadin/LegacyApplication.java | 6 +- .../server/LegacyApplicationUIProvider.java | 12 +- .../src/com/vaadin/server/VaadinService.java | 13 +- .../vaadin/server/VaadinServiceSession.java | 132 ++---------- server/src/com/vaadin/ui/LoginForm.java | 203 ++++++++---------- server/src/com/vaadin/ui/UI.java | 10 +- .../component/root/CustomUIClassLoader.java | 15 +- uitest/src/com/vaadin/tests/TestBench.java | 2 +- .../tests/components/ui/LazyInitUIs.java | 12 +- .../minitutorials/v7a1/DynamicImageUI.java | 12 +- 10 files changed, 136 insertions(+), 281 deletions(-) diff --git a/server/src/com/vaadin/LegacyApplication.java b/server/src/com/vaadin/LegacyApplication.java index a7f29bf4b1..8526bc3310 100644 --- a/server/src/com/vaadin/LegacyApplication.java +++ b/server/src/com/vaadin/LegacyApplication.java @@ -55,6 +55,7 @@ public abstract class LegacyApplication implements ErrorListener { * application is just closed without redirection. */ private String logoutURL = null; + private URL url; /** * Sets the main window of this application. Setting window as a main window @@ -80,7 +81,8 @@ public abstract class LegacyApplication implements ErrorListener { this.mainWindow = mainWindow; } - public void doInit() { + public void doInit(URL url) { + this.url = url; VaadinServiceSession.getCurrent().setErrorHandler(this); init(); } @@ -241,7 +243,7 @@ public abstract class LegacyApplication implements ErrorListener { } public URL getURL() { - return VaadinServiceSession.getCurrent().getURL(); + return url; } /** diff --git a/server/src/com/vaadin/server/LegacyApplicationUIProvider.java b/server/src/com/vaadin/server/LegacyApplicationUIProvider.java index c3e450fd7a..bedab32105 100644 --- a/server/src/com/vaadin/server/LegacyApplicationUIProvider.java +++ b/server/src/com/vaadin/server/LegacyApplicationUIProvider.java @@ -16,6 +16,8 @@ package com.vaadin.server; +import java.net.MalformedURLException; +import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -124,7 +126,15 @@ public abstract class LegacyApplicationUIProvider extends UIProvider { } VaadinServiceSession.getCurrent().setAttribute( LegacyApplication.class, application); - application.doInit(); + + URL applicationUrl; + try { + applicationUrl = VaadinService.getCurrent().getApplicationUrl( + VaadinService.getCurrentRequest()); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + application.doInit(applicationUrl); } if (application != null && !application.isRunning()) { diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index d9c8b83ea4..9d78d4c107 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -35,7 +35,6 @@ import javax.servlet.ServletException; import com.vaadin.LegacyApplication; import com.vaadin.annotations.PreserveOnRefresh; import com.vaadin.event.EventRouter; -import com.vaadin.server.VaadinServiceSession.SessionStartEvent; import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.ui.UI; import com.vaadin.util.CurrentInstance; @@ -426,19 +425,11 @@ public abstract class VaadinService implements Serializable { session.storeInSession(this, request.getWrappedSession()); - URL applicationUrl; - try { - applicationUrl = getApplicationUrl(request); - } catch (MalformedURLException e) { - throw new ServiceException(e); - } - // Initial locale comes from the request Locale locale = request.getLocale(); session.setLocale(locale); - session.start(new SessionStartEvent(applicationUrl, - getDeploymentConfiguration(), - createCommunicationManager(session))); + session.setConfiguration(getDeploymentConfiguration()); + session.setCommunicationManager(createCommunicationManager(session)); ServletPortletHelper.initDefaultUIProvider(session, this); onVaadinSessionStarted(request, session); diff --git a/server/src/com/vaadin/server/VaadinServiceSession.java b/server/src/com/vaadin/server/VaadinServiceSession.java index 67b224df70..62a11c710a 100644 --- a/server/src/com/vaadin/server/VaadinServiceSession.java +++ b/server/src/com/vaadin/server/VaadinServiceSession.java @@ -18,7 +18,6 @@ package com.vaadin.server; import java.io.Serializable; import java.lang.reflect.Method; -import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -81,79 +80,11 @@ public class VaadinServiceSession implements HttpSessionBindingListener, private final Lock lock = new ReentrantLock(); - /** - * An event sent to {@link #start(SessionStartEvent)} when a new Application - * is being started. - * - * @deprecated might be refactored or removed before 7.0.0 - */ - @Deprecated - public static class SessionStartEvent implements Serializable { - private final URL applicationUrl; - - private final DeploymentConfiguration configuration; - - private final AbstractCommunicationManager communicationManager; - - /** - * @param applicationUrl - * the URL the application should respond to. - * @param configuration - * the deployment configuration for the session. - * @param communicationManager - * the communication manager for the session. - */ - public SessionStartEvent(URL applicationUrl, - DeploymentConfiguration configuration, - AbstractCommunicationManager communicationManager) { - this.applicationUrl = applicationUrl; - this.configuration = configuration; - this.communicationManager = communicationManager; - } - - /** - * Gets the URL the application should respond to. - * - * @return the URL the application should respond to or - * null if the URL is not defined. - * - * @see VaadinServiceSession#getURL() - */ - public URL getApplicationUrl() { - return applicationUrl; - } - - /** - * Returns the deployment configuration used by this session. - * - * @return the deployment configuration. - */ - public DeploymentConfiguration getConfiguration() { - return configuration; - } - - /** - * Gets the communication manager for this application. - * - * @return the communication manager for this application. - * - * @see VaadinServiceSession#getCommunicationManager - */ - public AbstractCommunicationManager getCommunicationManager() { - return communicationManager; - } - } - /** * Configuration for the session. */ private DeploymentConfiguration configuration; - /** - * The application's URL. - */ - private URL applicationUrl; - /** * Default locale of the session. */ @@ -285,26 +216,6 @@ public class VaadinServiceSession implements HttpSessionBindingListener, return communicationManager; } - /** - * Gets the URL of the application. - * - *

- * This is the URL what can be entered to a browser window to start the - * application. Navigating to the application URL shows the main window ( - * {@link #getMainWindow()}) of the application. Note that the main window - * can also be shown by navigating to the window url ( - * {@link com.vaadin.ui.Window#getURL()}). - *

- * - * @return the application's URL. - * - * @deprecated might be refactored or removed before 7.0.0 - */ - @Deprecated - public URL getURL() { - return applicationUrl; - } - /** * @param service * TODO @@ -354,34 +265,21 @@ public class VaadinServiceSession implements HttpSessionBindingListener, this.session = session; } - /** - * Starts the application on the given URL. - * - *

- * This method is called by Vaadin framework when a user navigates to the - * application. After this call the application corresponds to the given URL - * and it will return windows when asked for them. There is no need to call - * this method directly. - *

- * - *

- * Application properties are defined by servlet configuration object - * {@link javax.servlet.ServletConfig} and they are overridden by - * context-wide initialization parameters - * {@link javax.servlet.ServletContext}. - *

- * - * @param event - * the application start event containing details required for - * starting the application. - * - * @deprecated might be refactored or removed before 7.0.0 - */ - @Deprecated - public void start(SessionStartEvent event) { - applicationUrl = event.getApplicationUrl(); - configuration = event.getConfiguration(); - communicationManager = event.getCommunicationManager(); + public void setCommunicationManager( + AbstractCommunicationManager communicationManager) { + if (communicationManager == null) { + throw new IllegalArgumentException("Can not set to null"); + } + assert this.communicationManager == null : "Communication manager can only be set once"; + this.communicationManager = communicationManager; + } + + public void setConfiguration(DeploymentConfiguration configuration) { + if (configuration == null) { + throw new IllegalArgumentException("Can not set to null"); + } + assert this.configuration == null : "Configuration can only be set once"; + this.configuration = configuration; } /** diff --git a/server/src/com/vaadin/ui/LoginForm.java b/server/src/com/vaadin/ui/LoginForm.java index 76cec66f27..b1e4741450 100644 --- a/server/src/com/vaadin/ui/LoginForm.java +++ b/server/src/com/vaadin/ui/LoginForm.java @@ -15,21 +15,18 @@ */ package com.vaadin.ui; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; -import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import com.vaadin.server.ConnectorResource; -import com.vaadin.server.DownloadStream; -import com.vaadin.server.RequestHandler; +import com.vaadin.server.DynamicConnectorResource; import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinResponse; -import com.vaadin.server.VaadinServiceSession; +import com.vaadin.server.VaadinService; +import com.vaadin.server.VaadinServletService; import com.vaadin.shared.ApplicationConstants; /** @@ -62,67 +59,56 @@ public class LoginForm extends CustomComponent { private Embedded iframe = new Embedded(); - private ConnectorResource loginPage = new ConnectorResource() { - @Override - public String getFilename() { - return "login"; + @Override + public boolean handleConnectorRequest(VaadinRequest request, + VaadinResponse response, String path) throws IOException { + String method = VaadinServletService.getCurrentServletRequest() + .getMethod(); + if (!path.equals("login")) { + return super.handleConnectorRequest(request, response, path); } - - @Override - public DownloadStream getStream() { - byte[] loginHTML = getLoginHTML(); - DownloadStream downloadStream = new DownloadStream( - new ByteArrayInputStream(loginHTML), getMIMEType(), - getFilename()); - downloadStream.setBufferSize(loginHTML.length); - downloadStream.setCacheTime(-1); - return downloadStream; + String responseString = null; + if (method.equalsIgnoreCase("post")) { + responseString = handleLogin(request); + } else { + responseString = getLoginHTML(); } - @Override - public String getMIMEType() { - return "text/html; charset=utf-8"; - } - }; - - private final RequestHandler requestHandler = new RequestHandler() { - @Override - public boolean handleRequest(VaadinServiceSession session, - VaadinRequest request, VaadinResponse response) - throws IOException { - String requestPathInfo = request.getRequestPathInfo(); - if ("/loginHandler".equals(requestPathInfo)) { - // Ensure UI.getCurrent() works in listeners - UI.setCurrent(getUI()); - - response.setCacheTime(-1); - response.setContentType("text/html; charset=utf-8"); - response.getWriter() - .write("Login form handled." - + ""); - - Map parameters = request.getParameterMap(); - - HashMap params = new HashMap(); - // expecting single params - for (Iterator it = parameters.keySet().iterator(); it - .hasNext();) { - String key = it.next(); - String value = (parameters.get(key))[0]; - params.put(key, value); - } - LoginEvent event = new LoginEvent(LoginForm.this, params); - fireEvent(event); - return true; - } + if (responseString != null) { + response.setContentType("text/html; charset=utf-8"); + response.setCacheTime(-1); + response.getWriter().write(responseString); + return true; + } else { return false; } - }; + } + + private String handleLogin(VaadinRequest request) { + // Ensure UI.getCurrent() works in listeners + + Map parameters = VaadinService.getCurrentRequest() + .getParameterMap(); + + HashMap params = new HashMap(); + // expecting single params + for (Iterator it = parameters.keySet().iterator(); it.hasNext();) { + String key = it.next(); + String value = (parameters.get(key))[0]; + params.put(key, value); + } + LoginEvent event = new LoginEvent(LoginForm.this, params); + fireEvent(event); + + return "Login form handled." + + ""; + } public LoginForm() { iframe.setType(Embedded.TYPE_BROWSER); iframe.setSizeFull(); + iframe.setSource(new DynamicConnectorResource(this, "login")); setSizeFull(); setCompositionRoot(iframe); addStyleName("v-loginform"); @@ -135,67 +121,46 @@ public class LoginForm extends CustomComponent { * * @return byte array containing login page html */ - protected byte[] getLoginHTML() { - String appUri = getSession().getURL().toString(); - - try { - return ("\n" + "" - + "" - + "" - + "
" - + "" - + "
" - + "
" - + usernameCaption - + "
" - + "
" - + "
" - + passwordCaption - + "
" - + "
" - + "
" - + loginButtonCaption - + "
" + "") - .getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 encoding not avalable", e); - } - } - - @Override - public void attach() { - super.attach(); - getSession().addRequestHandler(requestHandler); - iframe.setSource(loginPage); - } - - @Override - public void detach() { - getSession().removeRequestHandler(requestHandler); - - super.detach(); + protected String getLoginHTML() { + return "\n" + + "" + + "" + + "" + + "
" + + "" + + "
" + + "
" + + usernameCaption + + "
" + + "
" + + "
" + + passwordCaption + + "
" + + "
" + + "
" + + loginButtonCaption + + "
" + ""; } /** diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java index 3cacf5c497..0954bbec13 100644 --- a/server/src/com/vaadin/ui/UI.java +++ b/server/src/com/vaadin/ui/UI.java @@ -149,7 +149,7 @@ public abstract class UI extends AbstractComponentContainer implements * to a window. All windows can be accessed through * {@code http://host:port/app/win} where {@code http://host:port/app} * is the application URL (as returned by - * {@link VaadinServiceSession#getURL()} and {@code win} is the window + * {@link LegacyApplication#getURL()} and {@code win} is the window * name. *

*

@@ -170,7 +170,7 @@ public abstract class UI extends AbstractComponentContainer implements * to a window. All windows can be accessed through * {@code http://host:port/app/win} where {@code http://host:port/app} * is the application URL (as returned by - * {@link VaadinServiceSession#getURL()} and {@code win} is the window + * {@link LegacyApplication#getURL()} and {@code win} is the window * name. *

*

@@ -208,13 +208,13 @@ public abstract class UI extends AbstractComponentContainer implements * to an application */ public URL getURL() { - VaadinServiceSession session = getSession(); - if (session == null) { + LegacyApplication application = getApplication(); + if (application == null) { return null; } try { - return new URL(session.getURL(), getName() + "/"); + return new URL(application.getURL(), getName() + "/"); } catch (MalformedURLException e) { throw new RuntimeException( "Internal problem getting window URL, please report"); diff --git a/server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java b/server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java index e071c0e484..405d3d1931 100644 --- a/server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java +++ b/server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java @@ -15,7 +15,6 @@ import com.vaadin.server.UIClassSelectionEvent; import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinService; import com.vaadin.server.VaadinServiceSession; -import com.vaadin.server.VaadinServiceSession.SessionStartEvent; import com.vaadin.ui.UI; public class CustomUIClassLoader extends TestCase { @@ -55,8 +54,7 @@ public class CustomUIClassLoader extends TestCase { */ public void testWithNullClassLoader() throws Exception { VaadinServiceSession application = createStubApplication(); - application.start(new SessionStartEvent(null, - createConfigurationMock(), null)); + application.setConfiguration(createConfigurationMock()); DefaultUIProvider uiProvider = new DefaultUIProvider(); Class uiClass = uiProvider @@ -76,12 +74,16 @@ public class CustomUIClassLoader extends TestCase { // Mock a VaadinService to give the passed classloader VaadinService configurationMock = EasyMock .createMock(VaadinService.class); + EasyMock.expect(configurationMock.getDeploymentConfiguration()) + .andReturn(createConfigurationMock()); EasyMock.expect(configurationMock.getClassLoader()).andReturn( classloader); // Mock a VaadinRequest to give the mocked vaadin service VaadinRequest requestMock = EasyMock.createMock(VaadinRequest.class); EasyMock.expect(requestMock.getService()).andReturn(configurationMock); + EasyMock.expect(requestMock.getService()).andReturn(configurationMock); + EasyMock.expect(requestMock.getService()).andReturn(configurationMock); EasyMock.replay(configurationMock, requestMock); return requestMock; @@ -97,13 +99,10 @@ public class CustomUIClassLoader extends TestCase { public void testWithClassLoader() throws Exception { LoggingClassLoader loggingClassLoader = new LoggingClassLoader(); - VaadinServiceSession application = createStubApplication(); - application.start(new SessionStartEvent(null, - createConfigurationMock(), null)); - DefaultUIProvider uiProvider = new DefaultUIProvider(); Class uiClass = uiProvider - .getUIClass(new UIClassSelectionEvent(createRequestMock(null))); + .getUIClass(new UIClassSelectionEvent( + createRequestMock(loggingClassLoader))); assertEquals(MyUI.class, uiClass); assertEquals(1, loggingClassLoader.requestedClasses.size()); diff --git a/uitest/src/com/vaadin/tests/TestBench.java b/uitest/src/com/vaadin/tests/TestBench.java index 91e3afd993..5a76a7259c 100644 --- a/uitest/src/com/vaadin/tests/TestBench.java +++ b/uitest/src/com/vaadin/tests/TestBench.java @@ -224,7 +224,7 @@ public class TestBench extends com.vaadin.LegacyApplication implements private Component createTestable(Class c) { try { final LegacyApplication app = (LegacyApplication) c.newInstance(); - app.doInit(); + app.doInit(null); Layout lo = (Layout) app.getMainWindow().getContent(); lo.setParent(null); return lo; diff --git a/uitest/src/com/vaadin/tests/components/ui/LazyInitUIs.java b/uitest/src/com/vaadin/tests/components/ui/LazyInitUIs.java index 4648529db7..e1fae91a3c 100644 --- a/uitest/src/com/vaadin/tests/components/ui/LazyInitUIs.java +++ b/uitest/src/com/vaadin/tests/components/ui/LazyInitUIs.java @@ -5,7 +5,6 @@ import com.vaadin.server.UIClassSelectionEvent; import com.vaadin.server.UICreateEvent; import com.vaadin.server.UIProviderEvent; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.VaadinServiceSession; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.tests.components.AbstractTestUIProvider; import com.vaadin.ui.Label; @@ -53,17 +52,16 @@ public class LazyInitUIs extends AbstractTestUIProvider { protected void init(VaadinRequest request) { addComponent(getRequestInfo("NormalUI", request)); + String location = getPage().getLocation().toString(); Link lazyCreateLink = new Link("Open lazyCreate UI", - new ExternalResource(VaadinServiceSession - .getCurrent().getURL() - + "?lazyCreate#lazyCreate")); + new ExternalResource(location.replaceFirst( + "(\\?|#|$).*", "?lazyCreate#lazyCreate"))); lazyCreateLink.setTargetName("_blank"); addComponent(lazyCreateLink); Link lazyInitLink = new Link("Open eagerInit UI", - new ExternalResource(VaadinServiceSession - .getCurrent().getURL() - + "?eagerInit#eagerInit")); + new ExternalResource(location.replaceFirst( + "(\\?|#|$).*", "?eagerInit#eagerInit"))); lazyInitLink.setTargetName("_blank"); addComponent(lazyInitLink); } diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7a1/DynamicImageUI.java b/uitest/src/com/vaadin/tests/minitutorials/v7a1/DynamicImageUI.java index 4c9ba80677..b5057dd9c2 100644 --- a/uitest/src/com/vaadin/tests/minitutorials/v7a1/DynamicImageUI.java +++ b/uitest/src/com/vaadin/tests/minitutorials/v7a1/DynamicImageUI.java @@ -2,8 +2,6 @@ package com.vaadin.tests.minitutorials.v7a1; import java.awt.image.BufferedImage; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; import javax.imageio.ImageIO; @@ -23,14 +21,8 @@ public class DynamicImageUI extends AbstractTestUI { getSession().addRequestHandler(new DynamicImageRequestHandler()); // Create a URL that we can handle in DynamicImageRequestHandler - URL imageUrl; - try { - imageUrl = new URL(getSession().getURL(), - DynamicImageRequestHandler.IMAGE_URL + "?text=Hello!"); - } catch (MalformedURLException e) { - // This should never happen - throw new RuntimeException(e); - } + String imageUrl = "app://" + DynamicImageRequestHandler.IMAGE_URL + + "?text=Hello!"; // Add an embedded using the created URL Embedded embedded = new Embedded("A dynamically generated image", -- 2.39.5