diff options
author | Raymond Auge | 2018-01-20 06:24:22 +0000 |
---|---|---|
committer | Raymond Auge | 2018-01-22 15:55:42 +0000 |
commit | 0537d998171c333d7450eaf0df7a5d2b999a328a (patch) | |
tree | cb4254d6b4159b7bca659e4783bdeff5d1a2e498 | |
parent | 8640d436cb326bf2a59c322d59d4d0a1ff0763e3 (diff) | |
download | rt.equinox.bundles-0537d998171c333d7450eaf0df7a5d2b999a328a.tar.gz rt.equinox.bundles-0537d998171c333d7450eaf0df7a5d2b999a328a.tar.xz rt.equinox.bundles-0537d998171c333d7450eaf0df7a5d2b999a328a.zip |
Bug 530069 - [http servlet] During dispatching, javax.servlet.include.context_path attribute value isn't equal to return value of getContextPath
Signed-off-by: Raymond Auge <raymond.auge@liferay.com>
Signed-off-by: Dante Wang <dante.wang@liferay.com>
Change-Id: I2a555deefe078a2b225985fb64411e46a9142c74
4 files changed, 155 insertions, 6 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java index bc3de7dbd..fd226000b 100644 --- a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java @@ -2953,6 +2953,146 @@ public class ServletTest extends BaseTest { } @Test + public void test_ServletContextHelper15_fullContextPath_include() throws Exception { + try { + stopJetty(); + System.setProperty("org.eclipse.equinox.http.jetty.context.path", "/foo"); + } + finally { + startJetty(); + } + + BundleContext bundleContext = getBundleContext(); + Bundle bundle = bundleContext.getBundle(); + + ServletContextHelper servletContextHelperA = new ServletContextHelper(bundle){}; + + Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); + try { + final AtomicReference<String> path = new AtomicReference<String>(); + + Servlet servletA = new HttpServlet() { + private static final long serialVersionUID = 1L; + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) + throws IOException, ServletException { + RequestDispatcher rd = req.getRequestDispatcher("/foo/a/SB"); + rd.include(req, resp); + } + }; + Servlet servletB = new HttpServlet() { + private static final long serialVersionUID = 1L; + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) + throws IOException, ServletException { + path.set((String)req.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH)); + } + }; + + Dictionary<String, String> contextProps = new Hashtable<String, String>(); + contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); + contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); + registrations.add(bundleContext.registerService(ServletContextHelper.class, servletContextHelperA, contextProps)); + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "SA"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/SA"); + registrations.add(getBundleContext().registerService(Servlet.class, servletA, props)); + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "SB"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/SB"); + registrations.add(getBundleContext().registerService(Servlet.class, servletB, props)); + + requestAdvisor.request("foo/a/SA"); + + Assert.assertEquals("/foo/a", path.get()); + } + finally { + for (ServiceRegistration<?> registration : registrations) { + registration.unregister(); + } + try { + stopJetty(); + System.setProperty("org.eclipse.equinox.http.jetty.context.path", ""); + } + finally { + startJetty(); + } + } + } + + @Test + public void test_ServletContextHelper15_fullContextPath_forward() throws Exception { + try { + stopJetty(); + System.setProperty("org.eclipse.equinox.http.jetty.context.path", "/foo"); + } + finally { + startJetty(); + } + + BundleContext bundleContext = getBundleContext(); + Bundle bundle = bundleContext.getBundle(); + + ServletContextHelper servletContextHelperA = new ServletContextHelper(bundle){}; + + Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); + try { + final AtomicReference<String> path = new AtomicReference<String>(); + + Servlet servletA = new HttpServlet() { + private static final long serialVersionUID = 1L; + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) + throws IOException, ServletException { + RequestDispatcher rd = req.getRequestDispatcher("/foo/a/SB"); + rd.forward(req, resp); + } + }; + Servlet servletB = new HttpServlet() { + private static final long serialVersionUID = 1L; + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) + throws IOException, ServletException { + path.set((String)req.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH)); + } + }; + + Dictionary<String, String> contextProps = new Hashtable<String, String>(); + contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); + contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); + registrations.add(bundleContext.registerService(ServletContextHelper.class, servletContextHelperA, contextProps)); + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "SA"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/SA"); + registrations.add(getBundleContext().registerService(Servlet.class, servletA, props)); + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "SB"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/SB"); + registrations.add(getBundleContext().registerService(Servlet.class, servletB, props)); + + requestAdvisor.request("foo/a/SA"); + + Assert.assertEquals("/foo/a", path.get()); + } + finally { + for (ServiceRegistration<?> registration : registrations) { + registration.unregister(); + } + try { + stopJetty(); + System.setProperty("org.eclipse.equinox.http.jetty.context.path", ""); + } + finally { + startJetty(); + } + } + } + + @Test public void test_Listener1() throws Exception { BaseServletContextListener scl1 = new BaseServletContextListener(); diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java index 90f50d520..e286bca8c 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java @@ -810,6 +810,14 @@ public class ContextController { } public String getFullContextPath() { + if (fullContextPath != null) { + return fullContextPath; + } + + return fullContextPath = getFullContextPath0(); + } + + private String getFullContextPath0() { List<String> endpoints = httpServiceRuntime.getHttpServiceEndpoints(); if (endpoints.isEmpty()) { @@ -818,7 +826,7 @@ public class ContextController { String defaultEndpoint = endpoints.get(0); - if ((defaultEndpoint.length() > 0) && defaultEndpoint.endsWith("/")) { + if ((defaultEndpoint.length() > 0) && defaultEndpoint.endsWith(Const.SLASH)) { defaultEndpoint = defaultEndpoint.substring( 0, defaultEndpoint.length() - 1); } @@ -1322,6 +1330,7 @@ public class ContextController { private final BundleContext consumingContext; private final String contextName; private final String contextPath; + private volatile String fullContextPath; private final long contextServiceId; private final Set<EndpointRegistration<?>> endpointRegistrations = new ConcurrentSkipListSet<EndpointRegistration<?>>(); private final EventListeners eventListeners = new EventListeners(); diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java index 6e30ca04a..7988a0802 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java @@ -85,7 +85,7 @@ public class DispatchTargets { RequestAttributeSetter setter = new RequestAttributeSetter(originalRequest); if (dispatcherType == DispatcherType.INCLUDE) { - setter.setAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH, contextController.getContextPath()); + setter.setAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH, contextController.getFullContextPath()); setter.setAttribute(RequestDispatcher.INCLUDE_PATH_INFO, getPathInfo()); setter.setAttribute(RequestDispatcher.INCLUDE_QUERY_STRING, getQueryString()); setter.setAttribute(RequestDispatcher.INCLUDE_REQUEST_URI, getRequestURI()); @@ -198,7 +198,7 @@ public class DispatchTargets { String value = string; if (value == null) { - value = SIMPLE_NAME + '[' + contextController.getFullContextPath() + requestURI + (queryString != null ? '?' + queryString : "") + ", " + endpointRegistration.toString() + ']'; //$NON-NLS-1$ + value = SIMPLE_NAME + '[' + contextController.getFullContextPath() + requestURI + (queryString != null ? '?' + queryString : "") + ", " + endpointRegistration.toString() + ']'; //$NON-NLS-1$ //$NON-NLS-2$ string = value; } diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java index dd8bb5524..cfb001682 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java @@ -214,7 +214,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { if (specialOverides.containsKey(RequestDispatcher.INCLUDE_CONTEXT_PATH)) { return specialOverides.get(RequestDispatcher.INCLUDE_CONTEXT_PATH); } - return current.getContextController().getContextPath(); + return current.getContextController().getFullContextPath(); } else if (attributeName.equals(RequestDispatcher.INCLUDE_PATH_INFO)) { if (hasServletName) { @@ -268,7 +268,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { if (specialOverides.containsKey(RequestDispatcher.FORWARD_CONTEXT_PATH)) { return specialOverides.get(RequestDispatcher.FORWARD_CONTEXT_PATH); } - return original.getContextController().getContextPath(); + return original.getContextController().getFullContextPath(); } else if (attributeName.equals(RequestDispatcher.FORWARD_PATH_INFO)) { if (specialOverides.containsKey(RequestDispatcher.FORWARD_PATH_INFO)) { @@ -333,7 +333,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { return req.getPathInfo(); } - + public static String getDispatchRequestURI(HttpServletRequest req) { if (req.getDispatcherType() == DispatcherType.INCLUDE) return (String) req.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI); |