diff options
author | Raymond Auge | 2016-03-25 17:09:56 +0000 |
---|---|---|
committer | Raymond Auge | 2016-03-25 17:09:56 +0000 |
commit | c26880e0f7e52118102909924cb3217105ebf3eb (patch) | |
tree | 811409c89ab6a30b0bae2167fed78c29aceaa663 | |
parent | cf1b7483d4e05df18a8ac4ca5fc6b20db0a22c35 (diff) | |
download | rt.equinox.bundles-c26880e0f7e52118102909924cb3217105ebf3eb.tar.gz rt.equinox.bundles-c26880e0f7e52118102909924cb3217105ebf3eb.tar.xz rt.equinox.bundles-c26880e0f7e52118102909924cb3217105ebf3eb.zip |
Bug 476069 - [http servlet] just tests
3 files changed, 966 insertions, 151 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/DispatchingTest.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/DispatchingTest.java new file mode 100644 index 000000000..cbd13c197 --- /dev/null +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/DispatchingTest.java @@ -0,0 +1,880 @@ +/******************************************************************************* + * Copyright (c) 2016 Raymond Augé and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Raymond Augé <raymond.auge@liferay.com> - initial implementation + *******************************************************************************/ +package org.eclipse.equinox.http.servlet.tests; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; + +import javax.servlet.RequestDispatcher; +import javax.servlet.Servlet; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.equinox.http.servlet.context.ContextPathCustomizer; +import org.eclipse.equinox.http.servlet.tests.bundle.Activator; +import org.eclipse.equinox.http.servlet.tests.bundle.BundleAdvisor; +import org.eclipse.equinox.http.servlet.tests.bundle.BundleInstaller; +import org.eclipse.equinox.http.servlet.tests.util.BaseServlet; +import org.eclipse.equinox.http.servlet.tests.util.DispatchResultServlet; +import org.eclipse.equinox.http.servlet.tests.util.ServletRequestAdvisor; + +import org.junit.Assert; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; +import org.osgi.framework.ServiceFactory; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.http.HttpContext; +import org.osgi.service.http.HttpService; +import org.osgi.service.http.context.ServletContextHelper; +import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; + +import junit.framework.TestCase; + +public class DispatchingTest extends TestCase { + + @Override + public void setUp() throws Exception { + // Quiet logging for tests + System.setProperty("/.LEVEL", "OFF"); + System.setProperty("org.eclipse.jetty.server.LEVEL", "OFF"); + System.setProperty("org.eclipse.jetty.servlet.LEVEL", "OFF"); + + System.setProperty("org.osgi.service.http.port", "8090"); + BundleContext bundleContext = getBundleContext(); + installer = new BundleInstaller(ServletTest.TEST_BUNDLES_BINARY_DIRECTORY, bundleContext); + advisor = new BundleAdvisor(bundleContext); + String port = getPort(); + String contextPath = getContextPath(); + requestAdvisor = new ServletRequestAdvisor(port, contextPath); + startBundles(); + stopJetty(); + startJetty(); + } + + @Override + public void tearDown() throws Exception { + for (ServiceRegistration<? extends Object> serviceRegistration : registrations) { + serviceRegistration.unregister(); + } + stopJetty(); + stopBundles(); + requestAdvisor = null; + advisor = null; + registrations.clear(); + try { + installer.shutdown(); + } finally { + installer = null; + } + } + + public void test_forwardDepth1() throws Exception { + Servlet servlet1 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s2/i4?u=5").forward(request, response); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String response = requestAdvisor.request("a/s1/d?p=1"); + + Assert.assertEquals("/a|/i4|u=5|/a/s2/i4|/s2|/a|/d|p=1|/a/s1/d|/s1", response); + } + + public void test_forwardDepth2() throws Exception { + Servlet servlet1 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s2/i2?p2=2").forward(request, response); + } + }; + + Servlet servlet2 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s3/i3?p3=3").forward(request, response); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet2, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s3/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String response = requestAdvisor.request("c1/s1/i1?p1=1"); + + Assert.assertEquals("/c1|/i3|p3=3|/c1/s3/i3|/s3|/c1|/i1|p1=1|/c1/s1/i1|/s1", response); + } + + public void test_forwardDepth3() throws Exception { + Servlet servlet1 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s2/i2?p2=2").forward(request, response); + } + }; + + Servlet servlet2 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s3/i3?p3=3").forward(request, response); + } + }; + + Servlet servlet3 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s4/i4?p4=4").forward(request, response); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet2, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s3/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet3, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s4/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String response = requestAdvisor.request("c1/s1/i1?p1=1"); + + Assert.assertEquals("/c1|/i4|p4=4|/c1/s4/i4|/s4|/c1|/i1|p1=1|/c1/s1/i1|/s1", response); + } + + public void test_forwardNamedParameterAggregationAndPrecedence() throws Exception { + Servlet sA = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + ServletContext servletContext = getServletContext(); + + RequestDispatcher requestDispatcher = servletContext.getNamedDispatcher("s2"); + + requestDispatcher.forward(req, resp); + } + }; + Servlet sB = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + PrintWriter writer = resp.getWriter(); + writer.write(String.valueOf(req.getQueryString())); + writer.write("|"); + writer.write(String.valueOf(req.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING))); + writer.write("|"); + writer.write(req.getParameter("p")); + writer.write("|"); + writer.write(Arrays.toString(req.getParameterValues("p"))); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s2"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sB, props)); + + String result = requestAdvisor.request("c1/s1/a?p=1&p=2"); + + Assert.assertEquals("p=1&p=2|null|1|[1, 2]", result); + } + + public void test_forwardNamed() throws Exception { + Servlet sA = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + ServletContext servletContext = getServletContext(); + + RequestDispatcher requestDispatcher = servletContext.getNamedDispatcher("s2"); + + requestDispatcher.forward(req, resp); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s2"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String result = requestAdvisor.request("c1/s1/a?p=1&p=2"); + + Assert.assertEquals("/c1|/a|p=1&p=2|/c1/s1/a|/s1|null|null|null|null|null", result); + } + + public void test_forwardParameterAggregationAndPrecedence() throws Exception { + Servlet sA = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Servlet13B/a?p=3&p=4"); + + requestDispatcher.forward(req, resp); + } + }; + Servlet sB = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + PrintWriter writer = resp.getWriter(); + writer.write(req.getQueryString()); + writer.write("|"); + writer.write((String)req.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING)); + writer.write("|"); + writer.write(req.getParameter("p")); + writer.write("|"); + writer.write(Arrays.toString(req.getParameterValues("p"))); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13A"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13A/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13B"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13B/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sB, props)); + + String result = requestAdvisor.request("Servlet13A/a?p=1&p=2"); + + Assert.assertEquals("p=3&p=4|p=1&p=2|3|[3, 4, 1, 2]", result); + } + + public void test_includeBasic() throws Exception { + Servlet servlet8 = new HttpServlet() { + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + RequestDispatcher requestDispatcher = + request.getRequestDispatcher("/S8/target"); + + requestDispatcher.include(request, response); + } + + }; + + Servlet servlet8Target = new HttpServlet() { + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + response.getWriter().print("s8target"); + } + + }; + + HttpService httpService = getHttpService(); + + HttpContext httpContext = httpService.createDefaultHttpContext(); + + httpService.registerServlet("/S8", servlet8, null, httpContext); + httpService.registerServlet("/S8/target", servlet8Target, null, httpContext); + + Assert.assertEquals("s8target", requestAdvisor.request("S8")); + } + + public void test_includeDepth1() throws Exception { + Servlet servlet1 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s2/b?u=5").include(request, response); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String response = requestAdvisor.request("a/s1/d?p=1"); + + Assert.assertEquals("/a|/d|p=1|/a/s1/d|/s1|/a|/b|u=5|/a/s2/b|/s2", response); + } + + public void test_includeDepth2() throws Exception { + Servlet servlet1 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s2/i2?p2=2").include(request, response); + } + }; + + Servlet servlet2 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s3/i3?p3=3").include(request, response); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet2, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s3/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String response = requestAdvisor.request("c1/s1/i1?p1=1"); + + Assert.assertEquals("/c1|/i1|p1=1|/c1/s1/i1|/s1|/c1|/i3|p3=3|/c1/s3/i3|/s3", response); + } + + public void test_includeDepth3() throws Exception { + Servlet servlet1 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s2/i2?p2=2").include(request, response); + } + }; + + Servlet servlet2 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s3/i3?p3=3").include(request, response); + } + }; + + Servlet servlet3 = new BaseServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/s4/i4?p4=4").include(request, response); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet2, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s3/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, servlet3, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s4/*"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String response = requestAdvisor.request("c1/s1/i1?p1=1"); + + Assert.assertEquals("/c1|/i1|p1=1|/c1/s1/i1|/s1|/c1|/i4|p4=4|/c1/s4/i4|/s4", response); + } + + public void test_includeNamedParameterAggregationAndPrecedence() throws Exception { + Servlet sA = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + ServletContext servletContext = getServletContext(); + + RequestDispatcher requestDispatcher = servletContext.getNamedDispatcher("s2"); + + requestDispatcher.include(req, resp); + } + }; + Servlet sB = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + PrintWriter writer = resp.getWriter(); + writer.write(req.getQueryString()); + writer.write("|"); + writer.write(String.valueOf(req.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING))); + writer.write("|"); + writer.write(req.getParameter("p")); + writer.write("|"); + writer.write(Arrays.toString(req.getParameterValues("p"))); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s2"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sB, props)); + + String result = requestAdvisor.request("c1/s1/a?p=1&p=2"); + + Assert.assertEquals("p=1&p=2|null|1|[1, 2]", result); + } + + public void test_includeNamed() throws Exception { + Servlet sA = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + ServletContext servletContext = getServletContext(); + + RequestDispatcher requestDispatcher = servletContext.getNamedDispatcher("s2"); + + requestDispatcher.include(req, resp); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "c1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/c1"); + registrations.add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {}, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s1"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "s2"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=c1)"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s2/*"); + registrations.add(getBundleContext().registerService(Servlet.class, new DispatchResultServlet(), props)); + + String result = requestAdvisor.request("c1/s1/a?p=1&p=2"); + + Assert.assertEquals("/c1|/a|p=1&p=2|/c1/s1/a|/s1|null|null|null|null|null", result); + } + + public void test_includeParameterAggregationAndPrecedence() throws Exception { + Servlet sA = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Servlet13B/a?p=3&p=4"); + + requestDispatcher.include(req, resp); + } + }; + Servlet sB = new HttpServlet() { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + PrintWriter writer = resp.getWriter(); + writer.write(req.getQueryString()); + writer.write("|"); + writer.write((String)req.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING)); + writer.write("|"); + writer.write(req.getParameter("p")); + writer.write("|"); + writer.write(Arrays.toString(req.getParameterValues("p"))); + } + }; + + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13A"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13A/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); + + props = new Hashtable<String, Object>(); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13B"); + props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13B/*"); + registrations.add(getBundleContext().registerService(Servlet.class, sB, props)); + + String result = requestAdvisor.request("Servlet13A/a?p=1&p=2"); + + Assert.assertEquals("p=1&p=2|p=3&p=4|3|[3, 4, 1, 2]", result); + } + + private String doRequest(String action, Map<String, String> params) throws IOException { + return doRequestGetResponse(action, params).get("responseBody").get(0); + } + + private Map<String, List<String>> doRequestGetResponse(String action, Map<String, String> params) throws IOException { + StringBuilder requestInfo = new StringBuilder(ServletTest.PROTOTYPE); + requestInfo.append(action); + if (!params.isEmpty()) { + boolean firstParam = true; + for (Map.Entry<String, String> param : params.entrySet()) { + if (firstParam) { + requestInfo.append('?'); + firstParam = false; + } else { + requestInfo.append('&'); + } + requestInfo.append(param.getKey()); + requestInfo.append('='); + requestInfo.append(param.getValue()); + } + } + return requestAdvisor.request(requestInfo.toString(), null); + } + + private BundleContext getBundleContext() { + return Activator.getBundleContext(); + } + + private String getContextPath() { + return getJettyProperty("context.path", ""); + } + + private HttpService getHttpService() { + ServiceReference<HttpService> serviceReference = getBundleContext().getServiceReference(HttpService.class); + return getBundleContext().getService(serviceReference); + } + + private String getJettyProperty(String key, String defaultValue) { + String qualifiedKey = ServletTest.JETTY_PROPERTY_PREFIX + key; + String value = getProperty(qualifiedKey); + if (value == null) { + value = defaultValue; + } + return value; + } + + private String getPort() { + String defaultPort = getProperty(ServletTest.OSGI_HTTP_PORT_PROPERTY); + if (defaultPort == null) { + defaultPort = "80"; + } + return getJettyProperty("port", defaultPort); + } + + private String getProperty(String key) { + BundleContext bundleContext = getBundleContext(); + String value = bundleContext.getProperty(key); + return value; + } + + private Bundle installBundle(String bundle) throws BundleException { + return installer.installBundle(bundle); + } + + private void startBundles() throws BundleException { + for (String bundle : ServletTest.BUNDLES) { + advisor.startBundle(bundle); + } + } + + private void startJetty() throws BundleException { + advisor.startBundle(ServletTest.EQUINOX_JETTY_BUNDLE); + } + + private void stopBundles() throws BundleException { + for (int i = ServletTest.BUNDLES.length - 1; i >= 0; i--) { + String bundle = ServletTest.BUNDLES[i]; + advisor.stopBundle(bundle); + } + } + + private void stopJetty() throws BundleException { + advisor.stopBundle(ServletTest.EQUINOX_JETTY_BUNDLE); + } + + private void uninstallBundle(Bundle bundle) throws BundleException { + installer.uninstallBundle(bundle); + } + + private static final String EQUINOX_DS_BUNDLE = "org.eclipse.equinox.ds"; + private static final String EQUINOX_JETTY_BUNDLE = "org.eclipse.equinox.http.jetty"; + private static final String JETTY_PROPERTY_PREFIX = "org.eclipse.equinox.http.jetty."; + private static final String OSGI_HTTP_PORT_PROPERTY = "org.osgi.service.http.port"; + private static final String STATUS_OK = "OK"; + private static final String TEST_BUNDLES_BINARY_DIRECTORY = "/bundles_bin/"; + private static final String TEST_BUNDLE_1 = "tb1"; + + private static final String[] BUNDLES = new String[] { + ServletTest.EQUINOX_DS_BUNDLE + }; + + private BundleInstaller installer; + private BundleAdvisor advisor; + private ServletRequestAdvisor requestAdvisor; + private final Collection<ServiceRegistration<? extends Object>> registrations = new ArrayList<ServiceRegistration<? extends Object>>(); + + static class TestServletContextHelperFactory implements ServiceFactory<ServletContextHelper> { + static class TestServletContextHelper extends ServletContextHelper { + public TestServletContextHelper(Bundle bundle) { + super(bundle); + }}; + @Override + public ServletContextHelper getService(Bundle bundle, ServiceRegistration<ServletContextHelper> registration) { + return new TestServletContextHelper(bundle); + } + + @Override + public void ungetService(Bundle bundle, ServiceRegistration<ServletContextHelper> registration, + ServletContextHelper service) { + // nothing + } + + } + + static class TestContextPathAdaptor extends ContextPathCustomizer { + private final String defaultFilter; + private final String contextPrefix; + private final String testName; + + /** + * @param defaultFilter + * @param contextPrefix + */ + public TestContextPathAdaptor(String defaultFilter, String contextPrefix, String testName) { + super(); + this.defaultFilter = defaultFilter; + this.contextPrefix = contextPrefix; + this.testName = testName; + } + + @Override + public String getDefaultContextSelectFilter(ServiceReference<?> httpWhiteBoardService) { + if (testName.equals(httpWhiteBoardService.getProperty("servlet.init." + ServletTest.TEST_PATH_CUSTOMIZER_NAME))) { + return defaultFilter; + } + return null; + } + + @Override + public String getContextPathPrefix(ServiceReference<ServletContextHelper> helper) { + if (testName.equals(helper.getProperty(ServletTest.TEST_PATH_CUSTOMIZER_NAME))) { + return contextPrefix; + } + return null; + } + + } + + static class ErrorServlet extends HttpServlet{ + private static final long serialVersionUID = 1L; + private final String errorCode; + + public ErrorServlet(String errorCode) { + super(); + this.errorCode = errorCode; + } + + @Override + protected void service( + HttpServletRequest request, HttpServletResponse response) + throws ServletException ,IOException { + + if (response.isCommitted()) { + System.out.println("Problem?"); + + return; + } + + PrintWriter writer = response.getWriter(); + + String requestURI = (String)request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI); + Integer status = (Integer)request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); + + writer.print(errorCode + " : " + status + " : ERROR : " + requestURI); + } + + }; +}
\ No newline at end of file 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 a6421a236..46ed4f2f2 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 @@ -787,8 +787,6 @@ public class ServletTest extends TestCase { Assert.assertTrue("testFilter2 did not get called.", testFilter2.getCalled()); } - - public void basicFilterTest22( String servlet1Pattern, String servlet2Pattern, String filterPattern, String expected, String[] dispatchers ) throws Exception { final AtomicReference<HttpServletRequestWrapper> httpServletRequestWrapper = new AtomicReference<HttpServletRequestWrapper>(); final AtomicReference<HttpServletResponseWrapper> httpServletResponseWrapper = new AtomicReference<HttpServletResponseWrapper>(); @@ -1692,44 +1690,6 @@ public class ServletTest extends TestCase { Assert.assertEquals(expected, actual); } - public void test_Servlet8() throws Exception { - Servlet servlet8 = new HttpServlet() { - private static final long serialVersionUID = 1L; - - @Override - protected void service(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - RequestDispatcher requestDispatcher = - request.getRequestDispatcher("/S8/target"); - - requestDispatcher.include(request, response); - } - - }; - - Servlet servlet8Target = new HttpServlet() { - private static final long serialVersionUID = 1L; - - @Override - protected void service(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - response.getWriter().print("s8target"); - } - - }; - - HttpService httpService = getHttpService(); - - HttpContext httpContext = httpService.createDefaultHttpContext(); - - httpService.registerServlet("/S8", servlet8, null, httpContext); - httpService.registerServlet("/S8/target", servlet8Target, null, httpContext); - - Assert.assertEquals("s8target", requestAdvisor.request("S8")); - } - public void test_Servlet9() throws Exception { String expected = "Equinox Jetty-based Http Service"; String actual; @@ -1832,101 +1792,6 @@ public class ServletTest extends TestCase { Assert.assertEquals("p=1&p=2|1|[1, 2]", result); } - public void test_Servlet14() throws Exception { - Servlet sA = new HttpServlet() { - private static final long serialVersionUID = 1L; - - @Override - protected void doGet( - HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - - RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Servlet13B/a?p=3&p=4"); - - requestDispatcher.include(req, resp); - } - }; - Servlet sB = new HttpServlet() { - private static final long serialVersionUID = 1L; - - @Override - protected void doGet( - HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - - PrintWriter writer = resp.getWriter(); - writer.write(req.getQueryString()); - writer.write("|"); - writer.write((String)req.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING)); - writer.write("|"); - writer.write(req.getParameter("p")); - writer.write("|"); - writer.write(Arrays.toString(req.getParameterValues("p"))); - } - }; - - Dictionary<String, Object> props = new Hashtable<String, Object>(); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13A"); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13A/*"); - registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); - - props = new Hashtable<String, Object>(); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13B"); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13B/*"); - registrations.add(getBundleContext().registerService(Servlet.class, sB, props)); - - String result = requestAdvisor.request("Servlet13A/a?p=1&p=2"); - - Assert.assertEquals("p=3&p=4&p=1&p=2|p=3&p=4|3|[3, 4, 1, 2]", result); - } - - public void test_Servlet15() throws Exception { - Servlet sA = new HttpServlet() { - private static final long serialVersionUID = 1L; - - @Override - protected void doGet( - HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - - RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Servlet13B/a?p=3&p=4"); - - requestDispatcher.forward(req, resp); - } - }; - Servlet sB = new HttpServlet() { - private static final long serialVersionUID = 1L; - - @Override - protected void doGet( - HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - - PrintWriter writer = resp.getWriter(); - writer.write(req.getQueryString()); - writer.write("|"); - writer.write((String)req.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING)); - writer.write("|"); - writer.write(req.getParameter("p")); - writer.write("|"); - writer.write(Arrays.toString(req.getParameterValues("p"))); - } - }; - - Dictionary<String, Object> props = new Hashtable<String, Object>(); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13A"); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13A/*"); - registrations.add(getBundleContext().registerService(Servlet.class, sA, props)); - - props = new Hashtable<String, Object>(); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13B"); - props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13B/*"); - registrations.add(getBundleContext().registerService(Servlet.class, sB, props)); - - String result = requestAdvisor.request("Servlet13A/a?p=1&p=2"); - - Assert.assertEquals("p=3&p=4&p=1&p=2|p=1&p=2|3|[3, 4, 1, 2]", result); - } public void test_ServletExactMatchPrecidence() throws Exception { Servlet sA = new HttpServlet() { @@ -2983,14 +2848,14 @@ public class ServletTest extends TestCase { } } - private static final String PROTOTYPE = "prototype/"; - private static final String CONFIGURE = "configure"; - private static final String UNREGISTER = "unregister"; - private static final String ERROR = "error"; - private static final String STATUS_PARAM = "servlet.init.status"; - private static final String TEST_PROTOTYPE_NAME = "test.prototype.name"; - private static final String TEST_PATH_CUSTOMIZER_NAME = "test.path.customizer.name"; - private static final String TEST_ERROR_CODE = "test.error.code"; + protected static final String PROTOTYPE = "prototype/"; + protected static final String CONFIGURE = "configure"; + protected static final String UNREGISTER = "unregister"; + protected static final String ERROR = "error"; + protected static final String STATUS_PARAM = "servlet.init.status"; + protected static final String TEST_PROTOTYPE_NAME = "test.prototype.name"; + protected static final String TEST_PATH_CUSTOMIZER_NAME = "test.path.customizer.name"; + protected static final String TEST_ERROR_CODE = "test.error.code"; public void testWBServletChangeInitParams() throws Exception{ String actual; @@ -3316,15 +3181,15 @@ public class ServletTest extends TestCase { installer.uninstallBundle(bundle); } - private static final String EQUINOX_DS_BUNDLE = "org.eclipse.equinox.ds"; - private static final String EQUINOX_JETTY_BUNDLE = "org.eclipse.equinox.http.jetty"; - private static final String JETTY_PROPERTY_PREFIX = "org.eclipse.equinox.http.jetty."; - private static final String OSGI_HTTP_PORT_PROPERTY = "org.osgi.service.http.port"; - private static final String STATUS_OK = "OK"; - private static final String TEST_BUNDLES_BINARY_DIRECTORY = "/bundles_bin/"; - private static final String TEST_BUNDLE_1 = "tb1"; + protected static final String EQUINOX_DS_BUNDLE = "org.eclipse.equinox.ds"; + protected static final String EQUINOX_JETTY_BUNDLE = "org.eclipse.equinox.http.jetty"; + protected static final String JETTY_PROPERTY_PREFIX = "org.eclipse.equinox.http.jetty."; + protected static final String OSGI_HTTP_PORT_PROPERTY = "org.osgi.service.http.port"; + protected static final String STATUS_OK = "OK"; + protected static final String TEST_BUNDLES_BINARY_DIRECTORY = "/bundles_bin/"; + protected static final String TEST_BUNDLE_1 = "tb1"; - private static final String[] BUNDLES = new String[] { + protected static final String[] BUNDLES = new String[] { ServletTest.EQUINOX_DS_BUNDLE }; diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/util/DispatchResultServlet.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/util/DispatchResultServlet.java new file mode 100644 index 000000000..08333ca47 --- /dev/null +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/util/DispatchResultServlet.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2016 Raymond Augé and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Raymond Augé <raymond.auge@liferay.com> - initial implementation + *******************************************************************************/ +package org.eclipse.equinox.http.servlet.tests.util; + +import java.io.IOException; +import java.io.StringWriter; + +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class DispatchResultServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + @Override + protected void service( + HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + StringWriter writer = new StringWriter(); + + writer.write(request.getContextPath()); + writer.write("|"); + writer.write(request.getPathInfo()); + writer.write("|"); + writer.write(request.getQueryString()); + writer.write("|"); + writer.write(request.getRequestURI()); + writer.write("|"); + writer.write(request.getServletPath()); + writer.write("|"); + + if (request.getDispatcherType() == DispatcherType.INCLUDE) { + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH))); + } + else if (request.getDispatcherType() == DispatcherType.FORWARD) { + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.FORWARD_PATH_INFO))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI))); + writer.write("|"); + writer.write(String.valueOf(request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH))); + } + + response.getWriter().write(writer.toString()); + } + +}
\ No newline at end of file |