From 6d69e31439418caba33baf40a66ad450a2f1ae89 Mon Sep 17 00:00:00 2001 From: gregw Date: Mon, 5 Oct 2020 12:28:10 +0200 Subject: [PATCH] Extra tests for #5378 --- .../jetty/servlet/ServletContextHandler.java | 17 +++++- .../servlet/ServletContextHandlerTest.java | 56 ++++++++++++++++--- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java index 2793b5cc7712..5607ef044e3f 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java @@ -190,9 +190,20 @@ public void addEventListener(EventListener listener) @Override public void setHandler(Handler handler) { - if (handler != null) - LOG.warn("ServletContextHandler.setHandler should not be called directly. Use insertHandler or setSessionHandler etc."); - super.setHandler(handler); + if (handler instanceof SessionHandler) + setSessionHandler((SessionHandler)handler); + else if (handler instanceof SecurityHandler) + setSecurityHandler((SecurityHandler)handler); + else if (handler instanceof ServletHandler) + setServletHandler((ServletHandler)handler); + else if (handler instanceof GzipHandler) + setGzipHandler((GzipHandler)handler); + else + { + if (handler != null) + LOG.warn("ServletContextHandler.setHandler should not be called directly. Use insertHandler or setSessionHandler etc."); + super.setHandler(handler); + } } private void doSetHandler(HandlerWrapper wrapper, Handler handler) diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java index 63070746fa89..fe076310ae36 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java @@ -897,7 +897,7 @@ public void destroy() } @Test - public void testAddServletFromSCL() throws Exception + public void testAddFilterServletFromSCL() throws Exception { //A servlet can be added from a ServletContextListener ServletContextHandler context = new ServletContextHandler(); @@ -905,10 +905,11 @@ public void testAddServletFromSCL() throws Exception context.setContextPath("/"); context.addEventListener(new ServletContextListener() { - @Override public void contextInitialized(ServletContextEvent sce) { + sce.getServletContext().addFilter("filter", new MyFilter()) + .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); ServletRegistration rego = sce.getServletContext().addServlet("hello", HelloServlet.class); rego.addMapping("/hello/*"); } @@ -927,11 +928,13 @@ public void contextDestroyed(ServletContextEvent sce) request.append("\n"); String response = _connector.getResponse(request.toString()); - assertThat("Response", response, containsString("Hello World")); + assertThat(response, containsString("200 OK")); + assertThat(response, containsString("filter: filter")); + assertThat(response, containsString("Hello World")); } @Test - public void testAddServletFromSCI() throws Exception + public void testAddFilterServletFromSCI() throws Exception { //A servlet can be added from a ServletContainerInitializer ContextHandlerCollection contexts = new ContextHandlerCollection(); @@ -943,6 +946,8 @@ class ServletAddingSCI implements ServletContainerInitializer @Override public void onStartup(Set> c, ServletContext ctx) { + ctx.addFilter("filter", new MyFilter()) + .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); ServletRegistration rego = ctx.addServlet("hello", HelloServlet.class); rego.addMapping("/hello/*"); } @@ -957,7 +962,9 @@ public void onStartup(Set> c, ServletContext ctx) request.append("\n"); String response = _connector.getResponse(request.toString()); - assertThat("Response", response, containsString("Hello World")); + assertThat(response, containsString("200 OK")); + assertThat(response, containsString("filter: filter")); + assertThat(response, containsString("Hello World")); } @Test @@ -1430,7 +1437,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) public static class MyFilter implements Filter { - @Override public void init(FilterConfig filterConfig) throws ServletException { @@ -1440,6 +1446,7 @@ public void init(FilterConfig filterConfig) throws ServletException public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + ((HttpServletResponse)response).addHeader("filter", "filter"); request.getServletContext().setAttribute("filter", "filter"); chain.doFilter(request, response); } @@ -1636,4 +1643,39 @@ else if ("delete".equalsIgnoreCase(action)) } } } -} + + @Test + public void testProgrammaticFilterServlet() throws Exception + { + ServletContextHandler context = new ServletContextHandler(); + ServletHandler handler = new ServletHandler(); + _server.setHandler(context); + context.setHandler(handler); + handler.addServletWithMapping(new ServletHolder(new TestServlet()), "/"); + + _server.start(); + + + String request = + "GET /hello HTTP/1.0\n" + + "Host: localhost\n" + + "\n"; + String response = _connector.getResponse(request); + assertThat(response, containsString("200 OK")); + assertThat(response, containsString("Test")); + + handler.addFilterWithMapping(new FilterHolder(new MyFilter()), "/*", EnumSet.of(DispatcherType.REQUEST)); + handler.addServletWithMapping(new ServletHolder(new HelloServlet()), "/hello/*"); + + _server.dumpStdErr(); + + request = + "GET /hello HTTP/1.0\n" + + "Host: localhost\n" + + "\n"; + response = _connector.getResponse(request); + assertThat(response, containsString("200 OK")); + assertThat(response, containsString("filter: filter")); + assertThat(response, containsString("Hello World")); + } +} \ No newline at end of file