diff options
author | Thomas Watson | 2015-04-14 18:16:55 +0000 |
---|---|---|
committer | Thomas Watson | 2015-04-14 18:16:55 +0000 |
commit | de0057cca3989bae3569abf780acc18c8d976831 (patch) | |
tree | 4dede9cc7b4207eae0781bb6e34dc09386263f63 | |
parent | 502c990a599ffbbf10061e9c7df66b39d4a1d97d (diff) | |
download | rt.equinox.bundles-de0057cca3989bae3569abf780acc18c8d976831.tar.gz rt.equinox.bundles-de0057cca3989bae3569abf780acc18c8d976831.tar.xz rt.equinox.bundles-de0057cca3989bae3569abf780acc18c8d976831.zip |
Bug 464518 - [http whiteboard] support 4xx and 5xx errorPage values
3 files changed, 115 insertions, 28 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 bd1b287b8..05b68cad5 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 @@ -13,6 +13,7 @@ package org.eclipse.equinox.http.servlet.tests; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.CookieHandler; @@ -36,6 +37,7 @@ import java.util.concurrent.atomic.AtomicReference; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; +import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; @@ -113,10 +115,14 @@ public class ServletTest extends TestCase { @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 { @@ -204,6 +210,27 @@ public class ServletTest extends TestCase { "Expected <" + expected + "*> but got <" + actual + ">", actual.startsWith(expected)); } + public void test_ErrorPage5() throws Exception { + Dictionary<String, Object> errorProps = new Hashtable<String, Object>(); + errorProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E5.4xx"); + errorProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, "4xx"); + registrations.add(getBundleContext().registerService(Servlet.class, new ErrorServlet("4xx"), errorProps)); + errorProps = new Hashtable<String, Object>(); + errorProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E5.5xx"); + errorProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, "5xx"); + registrations.add(getBundleContext().registerService(Servlet.class, new ErrorServlet("5xx"), errorProps)); + for(String expectedCode: Arrays.asList("400", "450", "499", "500", "550", "599")) { + Map<String, List<String>> response = doRequestGetResponse(ERROR, Collections.singletonMap(TEST_ERROR_CODE, expectedCode)); + String expectedResponse = expectedCode.charAt(0) + "xx : " + expectedCode + " : ERROR"; + String actualCode = response.get("responseCode").get(0); + String actualResponse = response.get("responseBody").get(0); + + Assert.assertEquals(expectedCode, actualCode); + Assert.assertTrue( + "Expected <" + expectedResponse + "*> but got <" + actualResponse + ">", actualResponse.startsWith(expectedResponse)); + } + } + public void test_Filter1() throws Exception { String expected = "bab"; String actual; @@ -503,7 +530,6 @@ public class ServletTest extends TestCase { } public void test_Filter21() throws Exception { - Collection<ServiceRegistration<? extends Object>> registrations = new ArrayList<ServiceRegistration<? extends Object>>(); // Make sure exact path matching is honored by filters registrations String expected = "a"; TestFilter testFilter1 = new TestFilter(); @@ -524,23 +550,17 @@ public class ServletTest extends TestCase { props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/hello/*"); registrations.add(getBundleContext().registerService(Servlet.class, testServlet, props)); - try { - String actual = requestAdvisor.request("hello"); - Assert.assertEquals(expected, actual); - Assert.assertTrue("testFilter1 did not get called.", testFilter1.getCalled()); - Assert.assertTrue("testFilter2 did not get called.", testFilter2.getCalled()); + String actual = requestAdvisor.request("hello"); + Assert.assertEquals(expected, actual); + Assert.assertTrue("testFilter1 did not get called.", testFilter1.getCalled()); + Assert.assertTrue("testFilter2 did not get called.", testFilter2.getCalled()); - testFilter1.clear(); - testFilter2.clear(); - actual = requestAdvisor.request("hello/test"); - Assert.assertEquals(expected, actual); - Assert.assertFalse("testFilter1 got called.", testFilter1.getCalled()); - Assert.assertTrue("testFilter2 did not get called.", testFilter2.getCalled()); - } finally { - for (ServiceRegistration<? extends Object> serviceRegistration : registrations) { - serviceRegistration.unregister(); - } - } + testFilter1.clear(); + testFilter2.clear(); + actual = requestAdvisor.request("hello/test"); + Assert.assertEquals(expected, actual); + Assert.assertFalse("testFilter1 got called.", testFilter1.getCalled()); + Assert.assertTrue("testFilter2 did not get called.", testFilter2.getCalled()); } public void test_Registration1() throws Exception { String expected = "Alias cannot be null"; @@ -1906,9 +1926,11 @@ 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"; public void testWBServletChangeInitParams() throws Exception{ String actual; @@ -2107,6 +2129,10 @@ public class ServletTest extends TestCase { } 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(PROTOTYPE); requestInfo.append(action); if (!params.isEmpty()) { @@ -2123,7 +2149,7 @@ public class ServletTest extends TestCase { requestInfo.append(param.getValue()); } } - return requestAdvisor.request(requestInfo.toString()); + return requestAdvisor.request(requestInfo.toString(), null); } private BundleContext getBundleContext() { @@ -2206,6 +2232,7 @@ public class ServletTest extends TestCase { 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 { @@ -2258,4 +2285,34 @@ public class ServletTest extends TestCase { } } + + 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); + } + + }; } diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/util/TestServletPrototype.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/util/TestServletPrototype.java index f3c5188ef..d4c284037 100644 --- a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/util/TestServletPrototype.java +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/util/TestServletPrototype.java @@ -1,3 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2015 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Raymond Augé <raymond.auge@liferay.com> - Bug 436698 + *******************************************************************************/ package org.eclipse.equinox.http.servlet.tests.util; import java.io.IOException; @@ -15,7 +26,6 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.eclipse.equinox.http.servlet.tests.tb.AbstractTestServlet; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; @@ -41,10 +51,10 @@ public class TestServletPrototype extends HttpServlet { PrintWriter writer = response.getWriter(); try { - handleDoGet(request, writer); + handleDoGet(request, response, writer); } finally { - writer.close(); + //writer.close(); } } @@ -84,7 +94,7 @@ public class TestServletPrototype extends HttpServlet { } - protected void handleDoGet(HttpServletRequest request, PrintWriter writer) throws ServletException, IOException { + protected void handleDoGet(HttpServletRequest request, HttpServletResponse response, PrintWriter writer) throws ServletException, IOException { String pathInfo = request.getPathInfo(); if ("/lastGet".equals(pathInfo)) { writer.print(lastGetName.getAndSet(null)); @@ -101,6 +111,11 @@ public class TestServletPrototype extends HttpServlet { Factory factory = factories.remove(prototypeName); factory.unregister(); writer.print(prototypeName); + } else if ("/error".equals(pathInfo)) { + String errorCode = request.getParameter("test.error.code"); + if (errorCode != null) { + response.sendError(Integer.parseInt(errorCode)); + } } } 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 80786cef4..28c1d1890 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 @@ -507,22 +507,37 @@ public class ContextController { errorPageDTO = new ErrorPageDTO(); errorPageDTO.asyncSupported = asyncSupported; - long[] errorCodes = new long[0]; List<String> exceptions = new ArrayList<String>(); + // Not sure if it is important to maintain order of insertion or natural ordering here. + // Using insertion ordering with linked hash set. + Set<Long> errorCodeSet = new LinkedHashSet<Long>(); for(String errorPage : errorPages) { try { - long longValue = Long.parseLong(errorPage); - - errorCodes = Arrays.copyOf(errorCodes, errorCodes.length + 1); - - errorCodes[errorCodes.length - 1] = longValue; + if ("4xx".equals(errorPage)) { //$NON-NLS-1$ + for (long code = 400; code < 500; code++) { + errorCodeSet.add(code); + } + } else if ("5xx".equals(errorPage)) { //$NON-NLS-1$ + for (long code = 500; code < 600; code++) { + errorCodeSet.add(code); + } + } else { + long code = Long.parseLong(errorPage); + errorCodeSet.add(code); + } } catch (NumberFormatException nfe) { exceptions.add(errorPage); } } + long[] errorCodes = new long[errorCodeSet.size()]; + int i = 0; + for(Long code : errorCodeSet) { + errorCodes[i] = code; + i++; + } errorPageDTO.errorCodes = errorCodes; errorPageDTO.exceptions = exceptions.toArray(new String[exceptions.size()]); errorPageDTO.initParams = servletInitParams; |