diff options
author | Leif Åstrand <leif@vaadin.com> | 2012-10-08 13:17:07 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2012-10-08 10:43:19 +0000 |
commit | 48c19777e566dd8cd5e496ea364de8aea447abbf (patch) | |
tree | e6f5ff65c2d4d249230e92e62a52db537e6042a7 /server/src/com | |
parent | 13d5b3e98954c2ade382305f8d044b2b49fdbd0b (diff) | |
download | vaadin-framework-48c19777e566dd8cd5e496ea364de8aea447abbf.tar.gz vaadin-framework-48c19777e566dd8cd5e496ea364de8aea447abbf.zip |
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
Diffstat (limited to 'server/src/com')
-rw-r--r-- | server/src/com/vaadin/LegacyApplication.java | 6 | ||||
-rw-r--r-- | server/src/com/vaadin/server/LegacyApplicationUIProvider.java | 12 | ||||
-rw-r--r-- | server/src/com/vaadin/server/VaadinService.java | 13 | ||||
-rw-r--r-- | server/src/com/vaadin/server/VaadinServiceSession.java | 132 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/LoginForm.java | 203 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/UI.java | 10 |
6 files changed, 121 insertions, 255 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; @@ -82,79 +81,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 - * <code>null</code> 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. */ private Locale locale; @@ -286,26 +217,6 @@ public class VaadinServiceSession implements HttpSessionBindingListener, } /** - * Gets the URL of the application. - * - * <p> - * 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()}). - * </p> - * - * @return the application's URL. - * - * @deprecated might be refactored or removed before 7.0.0 - */ - @Deprecated - public URL getURL() { - return applicationUrl; - } - - /** * @param service * TODO * @param underlyingSession @@ -354,34 +265,21 @@ public class VaadinServiceSession implements HttpSessionBindingListener, this.session = session; } - /** - * Starts the application on the given URL. - * - * <p> - * 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. - * </p> - * - * <p> - * 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}. - * </p> - * - * @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("<html><body>Login form handled." - + "<script type='text/javascript'>parent.parent.vaadin.forceSync();" - + "</script></body></html>"); - - Map<String, String[]> parameters = request.getParameterMap(); - - HashMap<String, String> params = new HashMap<String, String>(); - // expecting single params - for (Iterator<String> 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<String, String[]> parameters = VaadinService.getCurrentRequest() + .getParameterMap(); + + HashMap<String, String> params = new HashMap<String, String>(); + // expecting single params + for (Iterator<String> 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 "<html><body>Login form handled." + + "<script type='text/javascript'>parent.parent.vaadin.forceSync();" + + "</script></body></html>"; + } 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 ("<!DOCTYPE html>\n" + "<html>" - + "<head><script type='text/javascript'>" - + "var setTarget = function() {" + "var uri = '" - + appUri - + "loginHandler" - + "'; var f = document.getElementById('loginf');" - + "document.forms[0].action = uri;document.forms[0].username.focus();};" - + "" - + "var styles = window.parent.document.styleSheets;" - + "for(var j = 0; j < styles.length; j++) {\n" - + "if(styles[j].href) {" - + "var stylesheet = document.createElement('link');\n" - + "stylesheet.setAttribute('rel', 'stylesheet');\n" - + "stylesheet.setAttribute('type', 'text/css');\n" - + "stylesheet.setAttribute('href', styles[j].href);\n" - + "document.getElementsByTagName('head')[0].appendChild(stylesheet);\n" - + "}" - + "}\n" - + "function submitOnEnter(e) { var keycode = e.keyCode || e.which;" - + " if (keycode == 13) {document.forms[0].submit();} } \n" - + "</script>" - + "</head><body onload='setTarget();' style='margin:0;padding:0; background:transparent;' class=\"" - + ApplicationConstants.GENERATED_BODY_CLASSNAME - + "\">" - + "<div class='v-app v-app-loginpage' style=\"background:transparent;\">" - + "<iframe name='logintarget' style='width:0;height:0;" - + "border:0;margin:0;padding:0;display:block'></iframe>" - + "<form id='loginf' target='logintarget' onkeypress=\"submitOnEnter(event)\" method=\"post\">" - + "<div>" - + usernameCaption - + "</div><div >" - + "<input class='v-textfield v-widget' style='display:block;' type='text' name='username'></div>" - + "<div>" - + passwordCaption - + "</div>" - + "<div><input class='v-textfield v-widget' style='display:block;' type='password' name='password'></div>" - + "<div><div onclick=\"document.forms[0].submit();\" tabindex=\"0\" class=\"v-button\" role=\"button\" ><span class=\"v-button-wrap\"><span class=\"v-button-caption\">" - + loginButtonCaption - + "</span></span></div></div></form></div>" + "</body></html>") - .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 "<!DOCTYPE html>\n" + + "<html>" + + "<head><script type='text/javascript'>" + + "var setTarget = function() {" + + "var uri = window.location;" + + "var f = document.getElementById('loginf');" + + "document.forms[0].action = uri;document.forms[0].username.focus();};" + + "" + + "var styles = window.parent.document.styleSheets;" + + "for(var j = 0; j < styles.length; j++) {\n" + + "if(styles[j].href) {" + + "var stylesheet = document.createElement('link');\n" + + "stylesheet.setAttribute('rel', 'stylesheet');\n" + + "stylesheet.setAttribute('type', 'text/css');\n" + + "stylesheet.setAttribute('href', styles[j].href);\n" + + "document.getElementsByTagName('head')[0].appendChild(stylesheet);\n" + + "}" + + "}\n" + + "function submitOnEnter(e) { var keycode = e.keyCode || e.which;" + + " if (keycode == 13) {document.forms[0].submit();} } \n" + + "</script>" + + "</head><body onload='setTarget();' style='margin:0;padding:0; background:transparent;' class=\"" + + ApplicationConstants.GENERATED_BODY_CLASSNAME + + "\">" + + "<div class='v-app v-app-loginpage' style=\"background:transparent;\">" + + "<iframe name='logintarget' style='width:0;height:0;" + + "border:0;margin:0;padding:0;display:block'></iframe>" + + "<form id='loginf' target='logintarget' onkeypress=\"submitOnEnter(event)\" method=\"post\">" + + "<div>" + + usernameCaption + + "</div><div >" + + "<input class='v-textfield v-widget' style='display:block;' type='text' name='username'></div>" + + "<div>" + + passwordCaption + + "</div>" + + "<div><input class='v-textfield v-widget' style='display:block;' type='password' name='password'></div>" + + "<div><div onclick=\"document.forms[0].submit();\" tabindex=\"0\" class=\"v-button\" role=\"button\" ><span class=\"v-button-wrap\"><span class=\"v-button-caption\">" + + loginButtonCaption + + "</span></span></div></div></form></div>" + "</body></html>"; } /** 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. * </p> * <p> @@ -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. * </p> * <p> @@ -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"); |