From f4d93126ca75631702a2086d770d068d8c13d547 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 4 Sep 2012 10:10:21 +0300 Subject: [PATCH] Make DeploymentConfiguration configurable (#9402) --- .../server/DeploymentConfiguration.java | 11 +- .../src/com/vaadin/server/VaadinPortlet.java | 198 ++++++++-------- .../src/com/vaadin/server/VaadinServlet.java | 219 ++++++++---------- .../launcher/ApplicationRunnerServlet.java | 24 +- 4 files changed, 220 insertions(+), 232 deletions(-) diff --git a/server/src/com/vaadin/server/DeploymentConfiguration.java b/server/src/com/vaadin/server/DeploymentConfiguration.java index acfba405e6..6150edbafb 100644 --- a/server/src/com/vaadin/server/DeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DeploymentConfiguration.java @@ -32,14 +32,19 @@ import javax.servlet.ServletContext; * @since 7.0 */ public interface DeploymentConfiguration extends Serializable { - /** - * Gets the base URL of the location of Vaadin's static files. + * Return the URL from where static files, e.g. the widgetset and the theme, + * are served. In a standard configuration the VAADIN folder inside the + * returned folder is what is used for widgetsets and themes. + * + * The returned folder is usually the same as the context path and + * independent of the application. * * @param request * the request for which the location should be determined * - * @return a string with the base URL for static files + * @return The location of static resources (should contain the VAADIN + * directory). Never ends with a slash (/). */ public String getStaticFileLocation(WrappedRequest request); diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 25512e9237..4d6d7b84f0 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -66,11 +66,94 @@ import com.vaadin.ui.UI; * * @author peholmst */ -public class VaadinPortlet extends GenericPortlet implements - Constants { +public class VaadinPortlet extends GenericPortlet implements Constants { public static final String RESOURCE_URL_ID = "APP"; + public static class PortletDeploymentConfiguration extends + AbstractDeploymentConfiguration { + private final VaadinPortlet portlet; + + public PortletDeploymentConfiguration(VaadinPortlet portlet, + Properties applicationProperties) { + super(portlet.getClass(), applicationProperties); + this.portlet = portlet; + } + + protected VaadinPortlet getPortlet() { + return portlet; + } + + @Override + public String getConfiguredWidgetset(WrappedRequest request) { + + String widgetset = getApplicationOrSystemProperty( + PARAMETER_WIDGETSET, null); + + if (widgetset == null) { + // If no widgetset defined for the application, check the + // portal property + widgetset = WrappedPortletRequest.cast(request) + .getPortalProperty(PORTAL_PARAMETER_VAADIN_WIDGETSET); + } + + if (widgetset == null) { + // If no widgetset defined for the portal, use the default + widgetset = DEFAULT_WIDGETSET; + } + + return widgetset; + } + + @Override + public String getConfiguredTheme(WrappedRequest request) { + + // is the default theme defined by the portal? + String themeName = WrappedPortletRequest.cast(request) + .getPortalProperty(Constants.PORTAL_PARAMETER_VAADIN_THEME); + + if (themeName == null) { + // no, using the default theme defined by Vaadin + themeName = DEFAULT_THEME_NAME; + } + + return themeName; + } + + @Override + public boolean isStandalone(WrappedRequest request) { + return false; + } + + @Override + public String getStaticFileLocation(WrappedRequest request) { + String staticFileLocation = WrappedPortletRequest.cast(request) + .getPortalProperty( + Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH); + if (staticFileLocation != null) { + // remove trailing slash if any + while (staticFileLocation.endsWith(".")) { + staticFileLocation = staticFileLocation.substring(0, + staticFileLocation.length() - 1); + } + return staticFileLocation; + } else { + // default for Liferay + return "/html"; + } + } + + @Override + public String getMimeType(String resourceName) { + return getPortlet().getPortletContext().getMimeType(resourceName); + } + + @Override + public SystemMessages getSystemMessages() { + return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES; + } + } + public static class WrappedHttpAndPortletRequest extends WrappedPortletRequest { @@ -177,8 +260,7 @@ public class VaadinPortlet extends GenericPortlet implements private final VaadinPortlet portlet; - public AbstractApplicationPortletWrapper( - VaadinPortlet portlet) { + public AbstractApplicationPortletWrapper(VaadinPortlet portlet) { this.portlet = portlet; } @@ -236,101 +318,17 @@ public class VaadinPortlet extends GenericPortlet implements config.getInitParameter(name)); } - deploymentConfiguration = new AbstractDeploymentConfiguration( - getClass(), applicationProperties) { - @Override - public String getConfiguredWidgetset(WrappedRequest request) { - - String widgetset = getApplicationOrSystemProperty( - PARAMETER_WIDGETSET, null); - - if (widgetset == null) { - // If no widgetset defined for the application, check the - // portal property - widgetset = WrappedPortletRequest.cast(request) - .getPortalProperty( - PORTAL_PARAMETER_VAADIN_WIDGETSET); - } - - if (widgetset == null) { - // If no widgetset defined for the portal, use the default - widgetset = DEFAULT_WIDGETSET; - } - - return widgetset; - } - - @Override - public String getConfiguredTheme(WrappedRequest request) { - - // is the default theme defined by the portal? - String themeName = WrappedPortletRequest.cast(request) - .getPortalProperty( - Constants.PORTAL_PARAMETER_VAADIN_THEME); - - if (themeName == null) { - // no, using the default theme defined by Vaadin - themeName = DEFAULT_THEME_NAME; - } - - return themeName; - } - - @Override - public boolean isStandalone(WrappedRequest request) { - return false; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.terminal.DeploymentConfiguration#getStaticFileLocation - * (com.vaadin.terminal.WrappedRequest) - * - * Return the URL from where static files, e.g. the widgetset and - * the theme, are served. In a standard configuration the VAADIN - * folder inside the returned folder is what is used for widgetsets - * and themes. - * - * @return The location of static resources (inside which there - * should be a VAADIN directory). Does not end with a slash (/). - */ - - @Override - public String getStaticFileLocation(WrappedRequest request) { - String staticFileLocation = WrappedPortletRequest - .cast(request) - .getPortalProperty( - Constants.PORTAL_PARAMETER_VAADIN_RESOURCE_PATH); - if (staticFileLocation != null) { - // remove trailing slash if any - while (staticFileLocation.endsWith(".")) { - staticFileLocation = staticFileLocation.substring(0, - staticFileLocation.length() - 1); - } - return staticFileLocation; - } else { - // default for Liferay - return "/html"; - } - } - - @Override - public String getMimeType(String resourceName) { - return getPortletContext().getMimeType(resourceName); - } - - @Override - public SystemMessages getSystemMessages() { - return VaadinPortlet.this.getSystemMessages(); - } - }; + deploymentConfiguration = createDeploymentConfiguration(applicationProperties); addonContext = new AddonContext(deploymentConfiguration); addonContext.init(); } + protected DeploymentConfiguration createDeploymentConfiguration( + Properties applicationProperties) { + return new PortletDeploymentConfiguration(this, applicationProperties); + } + @Override public void destroy() { super.destroy(); @@ -913,15 +911,6 @@ public class VaadinPortlet extends GenericPortlet implements } } - /** - * Get system messages from the current application class - * - * @return - */ - protected SystemMessages getSystemMessages() { - return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES; - } - private void handleServiceException(WrappedPortletRequest request, WrappedPortletResponse response, Application application, Throwable e) throws IOException, PortletException { @@ -930,7 +919,8 @@ public class VaadinPortlet extends GenericPortlet implements // if this was an UIDL request, response UIDL back to client if (getRequestType(request) == RequestType.UIDL) { - SystemMessages ci = getSystemMessages(); + SystemMessages ci = getDeploymentConfiguration() + .getSystemMessages(); criticalNotification(request, response, ci.getInternalErrorCaption(), ci.getInternalErrorMessage(), null, ci.getInternalErrorURL()); diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index 0766d46e93..a10ad965c5 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -54,6 +54,90 @@ import com.vaadin.ui.UI; @SuppressWarnings("serial") public class VaadinServlet extends HttpServlet implements Constants { + public static class ServletDeploymentConfiguration extends + AbstractDeploymentConfiguration { + private final VaadinServlet servlet; + + public ServletDeploymentConfiguration(VaadinServlet servlet, + Properties applicationProperties) { + super(servlet.getClass(), applicationProperties); + this.servlet = servlet; + } + + protected VaadinServlet getServlet() { + return servlet; + } + + @Override + public String getStaticFileLocation(WrappedRequest request) { + HttpServletRequest servletRequest = WrappedHttpServletRequest + .cast(request); + String staticFileLocation; + // if property is defined in configurations, use that + staticFileLocation = getApplicationOrSystemProperty( + PARAMETER_VAADIN_RESOURCES, null); + if (staticFileLocation != null) { + return staticFileLocation; + } + + // the last (but most common) option is to generate default location + // from request + + // if context is specified add it to widgetsetUrl + String ctxPath = servletRequest.getContextPath(); + + // FIXME: ctxPath.length() == 0 condition is probably unnecessary + // and + // might even be wrong. + + if (ctxPath.length() == 0 + && request + .getAttribute("javax.servlet.include.context_path") != null) { + // include request (e.g portlet), get context path from + // attribute + ctxPath = (String) request + .getAttribute("javax.servlet.include.context_path"); + } + + // Remove heading and trailing slashes from the context path + ctxPath = removeHeadingOrTrailing(ctxPath, "/"); + + if (ctxPath.equals("")) { + return ""; + } else { + return "/" + ctxPath; + } + } + + @Override + public String getConfiguredWidgetset(WrappedRequest request) { + return getApplicationOrSystemProperty( + VaadinServlet.PARAMETER_WIDGETSET, + VaadinServlet.DEFAULT_WIDGETSET); + } + + @Override + public String getConfiguredTheme(WrappedRequest request) { + // Use the default + return VaadinServlet.getDefaultTheme(); + } + + @Override + public boolean isStandalone(WrappedRequest request) { + return true; + } + + @Override + public String getMimeType(String resourceName) { + return getServlet().getServletContext().getMimeType(resourceName); + } + + @Override + public SystemMessages getSystemMessages() { + return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES; + } + } + private static class AbstractApplicationServletWrapper implements Callback { private final VaadinServlet servlet; @@ -116,50 +200,17 @@ public class VaadinServlet extends HttpServlet implements Constants { servletConfig.getInitParameter(name)); } - deploymentConfiguration = new AbstractDeploymentConfiguration( - getClass(), applicationProperties) { - - @Override - public String getStaticFileLocation(WrappedRequest request) { - HttpServletRequest servletRequest = WrappedHttpServletRequest - .cast(request); - return VaadinServlet.this - .getStaticFilesLocation(servletRequest); - } - - @Override - public String getConfiguredWidgetset(WrappedRequest request) { - return getApplicationOrSystemProperty( - VaadinServlet.PARAMETER_WIDGETSET, - VaadinServlet.DEFAULT_WIDGETSET); - } - - @Override - public String getConfiguredTheme(WrappedRequest request) { - // Use the default - return VaadinServlet.getDefaultTheme(); - } - - @Override - public boolean isStandalone(WrappedRequest request) { - return true; - } - - @Override - public String getMimeType(String resourceName) { - return getServletContext().getMimeType(resourceName); - } - - @Override - public SystemMessages getSystemMessages() { - return VaadinServlet.this.getSystemMessages(); - } - }; + deploymentConfiguration = createDeploymentConfiguration(applicationProperties); addonContext = new AddonContext(deploymentConfiguration); addonContext.init(); } + protected ServletDeploymentConfiguration createDeploymentConfiguration( + Properties applicationProperties) { + return new ServletDeploymentConfiguration(this, applicationProperties); + } + @Override public void destroy() { super.destroy(); @@ -430,10 +481,12 @@ public class VaadinServlet extends HttpServlet implements Constants { // This can be removed if cookieless mode (#3228) is supported if (request.getRequestedSessionId() == null) { // User has cookies disabled - criticalNotification(request, response, getSystemMessages() - .getCookiesDisabledCaption(), getSystemMessages() - .getCookiesDisabledMessage(), null, getSystemMessages() - .getCookiesDisabledURL()); + SystemMessages systemMessages = getDeploymentConfiguration() + .getSystemMessages(); + criticalNotification(request, response, + systemMessages.getCookiesDisabledCaption(), + systemMessages.getCookiesDisabledMessage(), null, + systemMessages.getCookiesDisabledURL()); return false; } } @@ -695,7 +748,8 @@ public class VaadinServlet extends HttpServlet implements Constants { Throwable e) throws IOException, ServletException { // if this was an UIDL request, response UIDL back to client if (getRequestType(request) == RequestType.UIDL) { - SystemMessages ci = getSystemMessages(); + SystemMessages ci = getDeploymentConfiguration() + .getSystemMessages(); criticalNotification(request, response, ci.getInternalErrorCaption(), ci.getInternalErrorMessage(), null, ci.getInternalErrorURL()); @@ -758,7 +812,8 @@ public class VaadinServlet extends HttpServlet implements Constants { } try { - SystemMessages ci = getSystemMessages(); + SystemMessages ci = getDeploymentConfiguration() + .getSystemMessages(); if (getRequestType(request) != RequestType.UIDL) { // 'plain' http req - e.g. browser reload; // just go ahead redirect the browser @@ -800,7 +855,8 @@ public class VaadinServlet extends HttpServlet implements Constants { } try { - SystemMessages ci = getSystemMessages(); + SystemMessages ci = getDeploymentConfiguration() + .getSystemMessages(); if (getRequestType(request) != RequestType.UIDL) { // 'plain' http req - e.g. browser reload; // just go ahead redirect the browser @@ -1184,77 +1240,6 @@ public class VaadinServlet extends HttpServlet implements Constants { return request.getParameter(ApplicationConstants.PARAM_UNLOADBURST) != null; } - /** - * Get system messages - * - * @return - */ - protected SystemMessages getSystemMessages() { - return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES; - } - - /** - * Return the URL from where static files, e.g. the widgetset and the theme, - * are served. In a standard configuration the VAADIN folder inside the - * returned folder is what is used for widgetsets and themes. - * - * The returned folder is usually the same as the context path and - * independent of the application. - * - * @param request - * @return The location of static resources (should contain the VAADIN - * directory). Never ends with a slash (/). - */ - protected String getStaticFilesLocation(HttpServletRequest request) { - - return getWebApplicationsStaticFileLocation(request); - } - - /** - * The default method to fetch static files location (URL). This method does - * not check for request attribute {@value #REQUEST_VAADIN_STATIC_FILE_PATH} - * - * @param request - * @return - */ - private String getWebApplicationsStaticFileLocation( - HttpServletRequest request) { - String staticFileLocation; - // if property is defined in configurations, use that - staticFileLocation = getDeploymentConfiguration() - .getApplicationOrSystemProperty(PARAMETER_VAADIN_RESOURCES, - null); - if (staticFileLocation != null) { - return staticFileLocation; - } - - // the last (but most common) option is to generate default location - // from request - - // if context is specified add it to widgetsetUrl - String ctxPath = request.getContextPath(); - - // FIXME: ctxPath.length() == 0 condition is probably unnecessary and - // might even be wrong. - - if (ctxPath.length() == 0 - && request.getAttribute("javax.servlet.include.context_path") != null) { - // include request (e.g portlet), get context path from - // attribute - ctxPath = (String) request - .getAttribute("javax.servlet.include.context_path"); - } - - // Remove heading and trailing slashes from the context path - ctxPath = removeHeadingOrTrailing(ctxPath, "/"); - - if (ctxPath.equals("")) { - return ""; - } else { - return "/" + ctxPath; - } - } - /** * Remove any heading or trailing "what" from the "string". * diff --git a/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java b/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java index bbe6e061fb..11685033a9 100644 --- a/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java +++ b/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java @@ -21,6 +21,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; import java.util.LinkedHashSet; +import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -30,8 +31,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.vaadin.Application; -import com.vaadin.server.VaadinServlet; import com.vaadin.server.AbstractUIProvider; +import com.vaadin.server.VaadinServlet; import com.vaadin.server.WrappedHttpServletRequest; import com.vaadin.server.WrappedRequest; import com.vaadin.tests.components.TestBase; @@ -257,14 +258,21 @@ public class ApplicationRunnerServlet extends VaadinServlet { } @Override - protected String getStaticFilesLocation(HttpServletRequest request) { - URIS uris = getApplicationRunnerURIs(request); - String staticFilesPath = uris.staticFilesPath; - if (staticFilesPath.equals("/")) { - staticFilesPath = ""; - } + protected ServletDeploymentConfiguration createDeploymentConfiguration( + Properties applicationProperties) { + return new ServletDeploymentConfiguration(this, applicationProperties) { + @Override + public String getStaticFileLocation(WrappedRequest request) { + URIS uris = getApplicationRunnerURIs(WrappedHttpServletRequest + .cast(request)); + String staticFilesPath = uris.staticFilesPath; + if (staticFilesPath.equals("/")) { + staticFilesPath = ""; + } - return staticFilesPath; + return staticFilesPath; + } + }; } @Override -- 2.39.5