This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 0983c48fb83fc59e0b55ac4d8360c422b23c1cff Author: Rene Cordier <rcord...@linagora.com> AuthorDate: Wed Mar 11 13:58:37 2020 +0700 JAMES-3078 Simplify JettyHttpServer --- .../org/apache/james/http/jetty/Configuration.java | 47 ----------- .../apache/james/http/jetty/ConfigurationTest.java | 91 +--------------------- .../org/apache/james/http/jetty/CoolFilter.java | 45 ----------- .../james/http/jetty/JettyHttpServerTest.java | 68 +--------------- .../org/apache/james/http/jetty/LambdaFilter.java | 35 --------- .../org/apache/james/http/jetty/SpyFilter.java | 49 ------------ .../jetty/src/test/resources/conflictingport.xml | 11 --- .../jetty/src/test/resources/emptyfiltername.xml | 11 --- .../test/resources/emptymappingconfiguration.xml | 5 -- .../jetty/src/test/resources/emptyservletname.xml | 11 --- .../jetty/src/test/resources/httpserver.xml | 30 ------- .../src/test/resources/unavailablefiltername.xml | 11 --- .../src/test/resources/unavailableservletname.xml | 11 --- 13 files changed, 3 insertions(+), 422 deletions(-) diff --git a/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java b/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java index 13b43af..f975a62 100644 --- a/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java +++ b/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java @@ -21,7 +21,6 @@ package org.apache.james.http.jetty; import java.util.Objects; import java.util.Optional; -import javax.servlet.Filter; import javax.servlet.Servlet; import org.apache.james.util.Port; @@ -43,8 +42,6 @@ public class Configuration { public static class Builder { - private static final String TEMPLATE_LEVEL1 = "/*"; - private final ImmutableMap.Builder<String, Object> mappings; private final ImmutableListMultimap.Builder<String, Object> filters; private Optional<Integer> port; @@ -69,35 +66,6 @@ public class Configuration { } } - public class FilterBinder { - private final String filterUrl; - - private FilterBinder(String filterUrl) { - this.filterUrl = filterUrl; - } - - public FilterBinder with(Filter filter) { - Preconditions.checkNotNull(filter); - filters.put(filterUrl, filter); - return this; - } - - public FilterBinder and(Filter filter) { - return with(filter); - } - - public FilterBinder with(Class<? extends Filter> filterClass) { - Preconditions.checkNotNull(filterClass); - filters.put(filterUrl, filterClass); - return this; - } - - public Configuration.Builder only() { - return Builder.this; - } - - } - private Builder() { mappings = ImmutableMap.builder(); filters = ImmutableListMultimap.builder(); @@ -108,21 +76,6 @@ public class Configuration { urlPreconditions(mappingUrl); return new ServletBinder(mappingUrl); } - - public ServletBinder serveAsOneLevelTemplate(String mappingUrl) { - urlPreconditions(mappingUrl); - return new ServletBinder(mappingUrl + TEMPLATE_LEVEL1); - } - - public FilterBinder filter(String mappingUrl) { - urlPreconditions(mappingUrl); - return new FilterBinder(mappingUrl); - } - - public FilterBinder filterAsOneLevelTemplate(String mappingUrl) { - urlPreconditions(mappingUrl); - return new FilterBinder(mappingUrl + TEMPLATE_LEVEL1); - } private void urlPreconditions(String mappingUrl) { Preconditions.checkNotNull(mappingUrl); diff --git a/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java b/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java index 3311873..b2ee526 100644 --- a/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java +++ b/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java @@ -21,13 +21,10 @@ package org.apache.james.http.jetty; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import javax.servlet.Filter; import javax.servlet.Servlet; import org.junit.Test; -import com.google.common.collect.ImmutableList; - public class ConfigurationTest { @Test @@ -40,8 +37,6 @@ public class ConfigurationTest { @Test public void shouldAllowWorkingDefinition() { Bad400 bad400 = new Bad400(); - SpyFilter spyFilter = new SpyFilter(); - LambdaFilter anotherFilter = (req, resp, chain) -> chain.doFilter(req, resp); Configuration testee = Configuration .builder() .port(2000) @@ -49,24 +44,12 @@ public class ConfigurationTest { .with(Ok200.class) .serve("/def") .with(bad400) - .filter("/123") - .with(CoolFilter.class) - .and(anotherFilter).only() - .filter("/456") - .with(spyFilter).only() - .serveAsOneLevelTemplate("/level") - .with(Ok200.class) .build(); assertThat(testee.getPort()).isPresent().contains(2000); assertThat(testee.getMappings()) - .hasSize(3) - .containsEntry("/abc", Ok200.class) - .containsEntry("/def", bad400) - .containsEntry("/level/*", Ok200.class); - assertThat(testee.getFilters().asMap()) .hasSize(2) - .containsEntry("/123", ImmutableList.of(CoolFilter.class, anotherFilter)) - .containsEntry("/456", ImmutableList.of(spyFilter)); + .containsEntry("/abc", Ok200.class) + .containsEntry("/def", bad400); } @Test @@ -123,74 +106,4 @@ public class ConfigurationTest { public void shouldNotAllowNullServletClassname() { assertThatThrownBy(() -> Configuration.builder().serve("/").with((Class<? extends Servlet>)null)).isInstanceOf(NullPointerException.class); } - - @Test - public void shouldNotAllowNullServletAsOneLevelTemplateMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().serveAsOneLevelTemplate(null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void shouldNotAllowEmptyServletAsOneLevelTemplateMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().serveAsOneLevelTemplate("")).isInstanceOf(IllegalArgumentException.class); - } - - - @Test - public void shouldNotAllowWhitespaceOnlyServletAsOneLevelTemplateMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().serveAsOneLevelTemplate(" ")).isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void shouldNotAllowNullFilterMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().filter(null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void shouldNotAllowEmptyFilterMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().filter("")).isInstanceOf(IllegalArgumentException.class); - } - - - @Test - public void shouldNotAllowWhitespaceOnlyFilterMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().filter(" ")).isInstanceOf(IllegalArgumentException.class); - } - - - @Test - public void shouldNotAllowNullFilter() { - assertThatThrownBy(() -> Configuration.builder().filter("/").with((Filter)null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void shouldNotAllowNullFilterClassname() { - assertThatThrownBy(() -> Configuration.builder().filter("/").with((Class<? extends Filter>)null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void shouldNotAllowNullFilterAsOneLevelTemplateMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().filterAsOneLevelTemplate(null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void shouldNotAllowEmptyFilterAsOneLevelTemplateMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().filterAsOneLevelTemplate("")).isInstanceOf(IllegalArgumentException.class); - } - - - @Test - public void shouldNotAllowWhitespaceOnlyFilterAsOneLevelTemplateMappingUrl() { - assertThatThrownBy(() -> Configuration.builder().filterAsOneLevelTemplate(" ")).isInstanceOf(IllegalArgumentException.class); - } - - - @Test - public void shouldNotAllowNullFilterAsOneLevelTemplate() { - assertThatThrownBy(() -> Configuration.builder().filterAsOneLevelTemplate("/").with((Filter)null)).isInstanceOf(NullPointerException.class); - } - - @Test - public void shouldNotAllowNullFilterClassnameAsOneLevelTemplate() { - assertThatThrownBy(() -> Configuration.builder().filterAsOneLevelTemplate("/").with((Class<? extends Filter>)null)).isInstanceOf(NullPointerException.class); - } } diff --git a/server/container/jetty/src/test/java/org/apache/james/http/jetty/CoolFilter.java b/server/container/jetty/src/test/java/org/apache/james/http/jetty/CoolFilter.java deleted file mode 100644 index 9f0b578..0000000 --- a/server/container/jetty/src/test/java/org/apache/james/http/jetty/CoolFilter.java +++ /dev/null @@ -1,45 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.http.jetty; - -import java.io.IOException; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -public class CoolFilter implements Filter { - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - chain.doFilter(request, response); - } - - @Override - public void destroy() { - } -} diff --git a/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java b/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java index 7f31411..d9a12bf 100644 --- a/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java +++ b/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java @@ -24,12 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -41,6 +36,7 @@ import org.junit.Before; import org.junit.Test; import com.google.common.io.Closeables; + import io.restassured.RestAssured; public class JettyHttpServerTest { @@ -60,25 +56,6 @@ public class JettyHttpServerTest { }; } - public static class OverrideFilter implements Filter { - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - response.getWriter().print("overriden by filter"); - response.flushBuffer(); - } - - @Override - public void destroy() { - } - - } - private JettyHttpServer testee; private Configuration.Builder configurationBuilder; @@ -179,47 +156,4 @@ public class JettyHttpServerTest { .body(Matchers.equalTo("Ok")); } - @Test - public void shouldCallFilterWhenConfiguredByClass() throws Exception { - testee = JettyHttpServer.create(configurationBuilder - .serve("/foo") - .with(Ok200.class) - .filter("/foo") - .with(OverrideFilter.class).only() - .build()) - .start(); - - RestAssured.port = testee.getPort(); - - when() - .get("/foo") - .then() - .assertThat() - .statusCode(200) - .body(Matchers.equalTo("overriden by filter")); - } - - @Test - public void shouldLetConfiguredServletHandleIncomingRequestAfterFilterHandling() throws Exception { - - SpyFilter spyFilter = new SpyFilter(); - testee = JettyHttpServer.create(configurationBuilder - .serve("/foo") - .with(Ok200.class) - .filter("/foo") - .with(spyFilter).only() - .build()) - .start(); - - RestAssured.port = testee.getPort(); - - when() - .get("/foo") - .then() - .assertThat() - .statusCode(200) - .body(Matchers.equalTo("Ok")); - assertThat(spyFilter.filtered).isTrue(); - } - } diff --git a/server/container/jetty/src/test/java/org/apache/james/http/jetty/LambdaFilter.java b/server/container/jetty/src/test/java/org/apache/james/http/jetty/LambdaFilter.java deleted file mode 100644 index d10fd7f..0000000 --- a/server/container/jetty/src/test/java/org/apache/james/http/jetty/LambdaFilter.java +++ /dev/null @@ -1,35 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.http.jetty; - -import javax.servlet.Filter; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; - -@FunctionalInterface -public interface LambdaFilter extends Filter { - - @Override - default void init(FilterConfig filterConfig) throws ServletException { - } - - @Override - default void destroy() { - } -} diff --git a/server/container/jetty/src/test/java/org/apache/james/http/jetty/SpyFilter.java b/server/container/jetty/src/test/java/org/apache/james/http/jetty/SpyFilter.java deleted file mode 100644 index a504061..0000000 --- a/server/container/jetty/src/test/java/org/apache/james/http/jetty/SpyFilter.java +++ /dev/null @@ -1,49 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.http.jetty; - -import java.io.IOException; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -public class SpyFilter implements Filter { - - boolean filtered = false; - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - filtered = true; - chain.doFilter(request, response); - } - - @Override - public void destroy() { - } - -} diff --git a/server/container/jetty/src/test/resources/conflictingport.xml b/server/container/jetty/src/test/resources/conflictingport.xml deleted file mode 100644 index c3c50c4..0000000 --- a/server/container/jetty/src/test/resources/conflictingport.xml +++ /dev/null @@ -1,11 +0,0 @@ -<httpservers> - <httpserver> - <port fixed="5000" random="true"/> - <mappings> - <mapping> - <path>/foo</path> - <servlet>org.apache.james.http.jetty.Ok200</servlet> - </mapping> - </mappings> - </httpserver> -</httpservers> \ No newline at end of file diff --git a/server/container/jetty/src/test/resources/emptyfiltername.xml b/server/container/jetty/src/test/resources/emptyfiltername.xml deleted file mode 100644 index e7fd468..0000000 --- a/server/container/jetty/src/test/resources/emptyfiltername.xml +++ /dev/null @@ -1,11 +0,0 @@ -<httpservers> - <httpserver> - <port fixed="5000"/> - <filters> - <mapping> - <path>/foo</path> - <filter></filter> - </mapping> - </filters> - </httpserver> -</httpservers> \ No newline at end of file diff --git a/server/container/jetty/src/test/resources/emptymappingconfiguration.xml b/server/container/jetty/src/test/resources/emptymappingconfiguration.xml deleted file mode 100644 index 14e368d..0000000 --- a/server/container/jetty/src/test/resources/emptymappingconfiguration.xml +++ /dev/null @@ -1,5 +0,0 @@ -<httpservers> - <httpserver> - <port fixed="5000"/> - </httpserver> -</httpservers> \ No newline at end of file diff --git a/server/container/jetty/src/test/resources/emptyservletname.xml b/server/container/jetty/src/test/resources/emptyservletname.xml deleted file mode 100644 index f698c19..0000000 --- a/server/container/jetty/src/test/resources/emptyservletname.xml +++ /dev/null @@ -1,11 +0,0 @@ -<httpservers> - <httpserver> - <port fixed="5000"/> - <mappings> - <mapping> - <path>/foo</path> - <servlet></servlet> - </mapping> - </mappings> - </httpserver> -</httpservers> \ No newline at end of file diff --git a/server/container/jetty/src/test/resources/httpserver.xml b/server/container/jetty/src/test/resources/httpserver.xml deleted file mode 100644 index feb267a..0000000 --- a/server/container/jetty/src/test/resources/httpserver.xml +++ /dev/null @@ -1,30 +0,0 @@ -<httpservers> - <httpserver> - <port fixed="5000"/> - <mappings> - <mapping> - <path>/foo</path> - <servlet>org.apache.james.http.jetty.Ok200</servlet> - </mapping> - <mapping> - <path>/bar</path> - <servlet>org.apache.james.http.jetty.Bad400</servlet> - </mapping> - </mappings> - </httpserver> - <httpserver> - <port random="true"/> - <mappings> - <mapping> - <path>/foo</path> - <servlet>org.apache.james.http.jetty.Ok200</servlet> - </mapping> - </mappings> - <filters> - <mapping> - <path>/*</path> - <filter>org.apache.james.http.jetty.SpyFilter</filter> - </mapping> - </filters> - </httpserver> -</httpservers> \ No newline at end of file diff --git a/server/container/jetty/src/test/resources/unavailablefiltername.xml b/server/container/jetty/src/test/resources/unavailablefiltername.xml deleted file mode 100644 index 1244fc5..0000000 --- a/server/container/jetty/src/test/resources/unavailablefiltername.xml +++ /dev/null @@ -1,11 +0,0 @@ -<httpservers> - <httpserver> - <port fixed="5000"/> - <filters> - <mapping> - <path>/foo</path> - <filter>com.google.is.not.Evil</filter> - </mapping> - </filters> - </httpserver> -</httpservers> \ No newline at end of file diff --git a/server/container/jetty/src/test/resources/unavailableservletname.xml b/server/container/jetty/src/test/resources/unavailableservletname.xml deleted file mode 100644 index 6cf4ea0..0000000 --- a/server/container/jetty/src/test/resources/unavailableservletname.xml +++ /dev/null @@ -1,11 +0,0 @@ -<httpservers> - <httpserver> - <port fixed="5000"/> - <mappings> - <mapping> - <path>/foo</path> - <servlet>com.apple.FreeServlet</servlet> - </mapping> - </mappings> - </httpserver> -</httpservers> \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org