Browse Source

Make DeploymentConfiguration configurable (#9402)

tags/7.0.0.beta1
Leif Åstrand 11 years ago
parent
commit
f4d93126ca

+ 8
- 3
server/src/com/vaadin/server/DeploymentConfiguration.java View File

@@ -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);


+ 94
- 104
server/src/com/vaadin/server/VaadinPortlet.java View File

@@ -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());

+ 102
- 117
server/src/com/vaadin/server/VaadinServlet.java View File

@@ -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".
*

+ 16
- 8
uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java View File

@@ -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

Loading…
Cancel
Save