summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Signell <artur.signell@itmill.com>2009-06-16 11:17:16 +0000
committerArtur Signell <artur.signell@itmill.com>2009-06-16 11:17:16 +0000
commit2d1aba041d90e8c892153eff64c519be2f215a7d (patch)
tree2ad1a310baeda4b4f878a9ad941d44a94afd9470
parent93fc4ab9f3480730df25ce278753adb7e1f7fe30 (diff)
downloadvaadin-framework-2d1aba041d90e8c892153eff64c519be2f215a7d.tar.gz
vaadin-framework-2d1aba041d90e8c892153eff64c519be2f215a7d.zip
Test case and fix for #3055 - Widgetset is loaded from wrong url when contextpath contains slashes
svn changeset:8203/svn branch:6.0
-rw-r--r--src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java57
-rw-r--r--src/com/vaadin/terminal/gwt/server/ApplicationRunnerServlet.java16
-rw-r--r--src/com/vaadin/tests/applicationservlet/TestStaticFilesLocation.java155
3 files changed, 203 insertions, 25 deletions
diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
index f613a2b6fe..ea75ae7283 100644
--- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
+++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
@@ -1029,21 +1029,25 @@ public abstract class AbstractApplicationServlet extends HttpServlet {
throws ClassNotFoundException;
/**
- * Resolve widgetset URL. Widgetset is not application specific.
+ * 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
+ * @return The location of static resources (should contain the VAADIN
+ * directory). Never ends with a slash (/).
* @throws MalformedURLException
*/
- String getWidgetsetLocation(HttpServletRequest request)
- throws MalformedURLException {
- URL applicationURL = getApplicationUrl(request);
-
- String applicationPath = applicationURL.getPath();
- String pathParts[] = applicationPath.split("\\/");
-
+ String getStaticFilesLocation(HttpServletRequest 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
@@ -1052,14 +1056,33 @@ public abstract class AbstractApplicationServlet extends HttpServlet {
.getAttribute("javax.servlet.include.context_path");
}
- String widgetsetPath = "";
- if (pathParts.length > 1
- && pathParts[1].equals(ctxPath.replaceAll("\\/", ""))) {
- widgetsetPath = "/" + pathParts[1];
+ // 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".
+ *
+ * @param string
+ * @param what
+ * @return
+ */
+ private static String removeHeadingOrTrailing(String string, String what) {
+ while (string.startsWith(what)) {
+ string = string.substring(1);
}
- return widgetsetPath;
+ while (string.endsWith(what)) {
+ string = string.substring(0, string.length() - 1);
+ }
+ return string;
}
/**
@@ -1129,10 +1152,10 @@ public abstract class AbstractApplicationServlet extends HttpServlet {
appUrl = appUrl.substring(0, appUrl.length() - 1);
}
- final String widgetsetPath = getWidgetsetLocation(request);
+ final String staticFilesLocation = getStaticFilesLocation(request);
final String staticFilePath = getApplicationOrSystemProperty(
- PARAMETER_VAADIN_RESOURCES, widgetsetPath);
+ PARAMETER_VAADIN_RESOURCES, staticFilesLocation);
// Default theme does not use theme URI
String themeUri = null;
@@ -1539,7 +1562,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet {
|| servletPath.charAt(servletPath.length() - 1) != '/') {
servletPath = servletPath + "/";
}
-
+ System.out.println(request.getPathInfo());
URL u = new URL(reqURL, servletPath);
return u;
}
diff --git a/src/com/vaadin/terminal/gwt/server/ApplicationRunnerServlet.java b/src/com/vaadin/terminal/gwt/server/ApplicationRunnerServlet.java
index 8d0f195c15..c9c0f314fb 100644
--- a/src/com/vaadin/terminal/gwt/server/ApplicationRunnerServlet.java
+++ b/src/com/vaadin/terminal/gwt/server/ApplicationRunnerServlet.java
@@ -77,7 +77,7 @@ public class ApplicationRunnerServlet extends AbstractApplicationServlet {
}
private static class URIS {
- String widgetsetPath;
+ String staticFilesPath;
String applicationURI;
String context;
String runner;
@@ -117,7 +117,7 @@ public class ApplicationRunnerServlet extends AbstractApplicationServlet {
}
applicationClassname = urlParts[3];
- uris.widgetsetPath = "/" + context;
+ uris.staticFilesPath = "/" + context;
uris.applicationURI = "/" + context + "/" + runner + "/"
+ applicationClassname;
uris.context = context;
@@ -132,7 +132,7 @@ public class ApplicationRunnerServlet extends AbstractApplicationServlet {
}
applicationClassname = urlParts[2];
- uris.widgetsetPath = "/";
+ uris.staticFilesPath = "/";
uris.applicationURI = "/" + runner + "/" + applicationClassname;
uris.context = context;
uris.runner = runner;
@@ -160,14 +160,14 @@ public class ApplicationRunnerServlet extends AbstractApplicationServlet {
}
@Override
- String getWidgetsetLocation(HttpServletRequest request) {
+ String getStaticFilesLocation(HttpServletRequest request) {
URIS uris = getApplicationRunnerURIs(request);
- String widgetsetPath = uris.widgetsetPath;
- if (widgetsetPath.equals("/")) {
- widgetsetPath = "";
+ String staticFilesPath = uris.staticFilesPath;
+ if (staticFilesPath.equals("/")) {
+ staticFilesPath = "";
}
- return widgetsetPath;
+ return staticFilesPath;
}
}
diff --git a/src/com/vaadin/tests/applicationservlet/TestStaticFilesLocation.java b/src/com/vaadin/tests/applicationservlet/TestStaticFilesLocation.java
new file mode 100644
index 0000000000..bf991d01cf
--- /dev/null
+++ b/src/com/vaadin/tests/applicationservlet/TestStaticFilesLocation.java
@@ -0,0 +1,155 @@
+package com.vaadin.tests.applicationservlet;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.servlet.http.HttpServletRequest;
+
+import junit.framework.TestCase;
+
+import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
+import com.vaadin.terminal.gwt.server.ApplicationServlet;
+
+public class TestStaticFilesLocation extends TestCase {
+
+ ApplicationServlet servlet;
+
+ private Method getStaticFilesLocationMethod;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ servlet = new ApplicationServlet();
+
+ getStaticFilesLocationMethod = AbstractApplicationServlet.class
+ .getDeclaredMethod(
+ "getStaticFilesLocation",
+ new Class[] { javax.servlet.http.HttpServletRequest.class });
+ getStaticFilesLocationMethod.setAccessible(true);
+
+ }
+
+ public void testWidgetSetLocation() throws Exception {
+ String location;
+
+ /* SERVLETS */
+ // http://dummy.host:8080/contextpath/servlet
+ // should return /contextpath
+ location = testLocation("http://dummy.host:8080", "/contextpath",
+ "/servlet", "");
+ assertEquals("/contextpath", location);
+
+ // http://dummy.host:8080/servlet
+ // should return ""
+ location = testLocation("http://dummy.host:8080", "", "/servlet", "");
+ assertEquals("", location);
+
+ // http://dummy.host/contextpath/servlet/extra/stuff
+ // should return /contextpath
+ location = testLocation("http://dummy.host", "/contextpath",
+ "/servlet", "/extra/stuff");
+ assertEquals("/contextpath", location);
+
+ // http://dummy.host/context/path/servlet/extra/stuff
+ // should return /context/path
+ location = testLocation("http://dummy.host", "/context/path",
+ "/servlet", "/extra/stuff");
+ assertEquals("/context/path", location);
+
+ /* Include requests */
+ location = testIncludedLocation("http://my.portlet.server", "/user",
+ "/tmpservletlocation1", "");
+ assertEquals("/user", location);
+
+ }
+
+ private String testLocation(String base, String contextPath,
+ String servletPath, String pathInfo) throws Exception {
+
+ HttpServletRequest request = createNonIncludeRequest(base, contextPath,
+ servletPath, pathInfo);
+ // Set request into replay mode
+ replay(request);
+
+ String location = (String) getStaticFilesLocationMethod.invoke(servlet,
+ request);
+ return location;
+ }
+
+ private String testIncludedLocation(String base, String portletContextPath,
+ String servletPath, String pathInfo) throws Exception {
+
+ HttpServletRequest request = createIncludeRequest(base,
+ portletContextPath, servletPath, pathInfo);
+ // Set request into replay mode
+ replay(request);
+
+ String location = (String) getStaticFilesLocationMethod.invoke(servlet,
+ request);
+ return location;
+ }
+
+ private HttpServletRequest createIncludeRequest(String base,
+ String realContextPath, String realServletPath, String pathInfo)
+ throws Exception {
+ HttpServletRequest request = createRequest(base, "", "", pathInfo);
+ expect(request.getAttribute("javax.servlet.include.context_path"))
+ .andReturn(realContextPath).anyTimes();
+ expect(request.getAttribute("javax.servlet.include.servlet_path"))
+ .andReturn(realServletPath).anyTimes();
+
+ return request;
+ }
+
+ private HttpServletRequest createNonIncludeRequest(String base,
+ String realContextPath, String realServletPath, String pathInfo)
+ throws Exception {
+ HttpServletRequest request = createRequest(base, realContextPath,
+ realServletPath, pathInfo);
+ expect(request.getAttribute("javax.servlet.include.context_path"))
+ .andReturn(null).anyTimes();
+ expect(request.getAttribute("javax.servlet.include.servlet_path"))
+ .andReturn(null).anyTimes();
+
+ return request;
+ }
+
+ /**
+ * Creates a HttpServletRequest mock using the supplied parameters.
+ *
+ * @param base
+ * The base url, e.g. http://localhost:8080
+ * @param contextPath
+ * The context path where the application is deployed, e.g.
+ * /mycontext
+ * @param servletPath
+ * The servlet path to the servlet we are testing, e.g. /myapp
+ * @param pathInfo
+ * Any text following the servlet path in the request, not
+ * including query parameters, e.g. /UIDL/
+ * @return A mock HttpServletRequest object useful for testing
+ * @throws MalformedURLException
+ */
+ private HttpServletRequest createRequest(String base, String contextPath,
+ String servletPath, String pathInfo) throws MalformedURLException {
+ URL url = new URL(base + contextPath + pathInfo);
+ HttpServletRequest request = createMock(HttpServletRequest.class);
+ expect(request.isSecure()).andReturn(
+ url.getProtocol().equalsIgnoreCase("https")).anyTimes();
+ expect(request.getServerName()).andReturn(url.getHost()).anyTimes();
+ expect(request.getServerPort()).andReturn(url.getPort()).anyTimes();
+ expect(request.getRequestURI()).andReturn(url.getPath()).anyTimes();
+ expect(request.getContextPath()).andReturn(contextPath).anyTimes();
+ expect(request.getPathInfo()).andReturn(pathInfo).anyTimes();
+ expect(request.getServletPath()).andReturn(servletPath).anyTimes();
+
+ return request;
+ }
+
+}