james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1719297 - in /james/project/trunk/server/container/jetty: ./ src/main/java/org/apache/james/http/jetty/ src/test/java/org/apache/james/http/jetty/
Date Fri, 11 Dec 2015 10:04:00 GMT
Author: matthieu
Date: Fri Dec 11 10:04:00 2015
New Revision: 1719297

URL: http://svn.apache.org/viewvc?rev=1719297&view=rev
Log:
JAMES-1639 Provide a configuration builder to describe servlet mapping

Added:
    james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
Modified:
    james/project/trunk/server/container/jetty/pom.xml
    james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
    james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java

Modified: james/project/trunk/server/container/jetty/pom.xml
URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/pom.xml?rev=1719297&r1=1719296&r2=1719297&view=diff
==============================================================================
--- james/project/trunk/server/container/jetty/pom.xml (original)
+++ james/project/trunk/server/container/jetty/pom.xml Fri Dec 11 10:04:00 2015
@@ -129,6 +129,10 @@
             </activation>
             <dependencies>
                 <dependency>
+                    <groupId>com.google.guava</groupId>
+                    <artifactId>guava</artifactId>
+                </dependency>
+                <dependency>
                     <groupId>com.jayway.restassured</groupId>
                     <artifactId>rest-assured</artifactId>
                     <version>2.6.0</version>

Added: james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java?rev=1719297&view=auto
==============================================================================
--- james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
(added)
+++ james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
Fri Dec 11 10:04:00 2015
@@ -0,0 +1,75 @@
+/****************************************************************
+ * 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.Servlet;
+
+import com.google.common.collect.ImmutableMap;
+
+public class Configuration {
+
+    public static Configuration empty() {
+        return builder().build();
+    }
+
+    public static Configuration.Builder builder() {
+        return new Builder();
+    }
+    
+    public static class Builder {
+        
+        private ImmutableMap.Builder<String, Servlet> mappings;
+        
+        public class ServletBinder {
+            private String mappingUrl;
+
+            private ServletBinder(String mappingUrl) {
+                this.mappingUrl = mappingUrl;
+            }
+            
+            public Configuration.Builder with(Servlet servlet) {
+                Preconditions.checkNotNull(servlet);
+                mappings.put(mappingUrl, servlet);
+                return Builder.this;
+            }
+        }
+        
+        private Builder() {
+            mappings = ImmutableMap.builder();
+        }
+        
+        public ServletBinder serve(String mappingUrl) {
+            return new ServletBinder(mappingUrl);
+        }
+
+        public Configuration build() {
+            return new Configuration(mappings.build());
+        }
+    }
+
+    private final ImmutableMap<String, Servlet> mappings;
+
+    public Configuration(ImmutableMap<String, Servlet> mappings) {
+        this.mappings = mappings;
+    }
+    
+    public ImmutableMap<String, Servlet> getMappings() {
+        return mappings;
+    }
+}
\ No newline at end of file

Modified: james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java?rev=1719297&r1=1719296&r2=1719297&view=diff
==============================================================================
--- james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
(original)
+++ james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/JettyHttpServer.java
Fri Dec 11 10:04:00 2015
@@ -21,24 +21,30 @@ package org.apache.james.http.jetty;
 
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.servlet.ServletHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
 
 public class JettyHttpServer {
-
-    public static class Configuration {
-        
-    }
     
     public static JettyHttpServer start(Configuration configuration) throws Exception {
-        return new JettyHttpServer().start();
+        return new JettyHttpServer(configuration).start();
     }
 
     private Server server;
     private ServerConnector serverConnector;
 
-    private JettyHttpServer() {
+    private JettyHttpServer(Configuration configuration) {
         server = new Server();
         serverConnector = new ServerConnector(server);
         server.addConnector(serverConnector);
+        ServletHandler servletHandler = buildServletHandler(configuration);
+        server.setHandler(servletHandler);
+    }
+
+    private ServletHandler buildServletHandler(Configuration configuration) {
+        ServletHandler servletHandler = new ServletHandler();
+        configuration.getMappings().forEach((path, servlet) -> servletHandler.addServletWithMapping(new
ServletHolder(servlet), path));
+        return servletHandler;
     }
     
     private JettyHttpServer start() throws Exception {
@@ -46,7 +52,8 @@ public class JettyHttpServer {
         return this;
     }
     
-    public void stop() {
+    public void stop() throws Exception {
+        server.stop();
     }
 
     public int getPort() {

Modified: james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java?rev=1719297&r1=1719296&r2=1719297&view=diff
==============================================================================
--- james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
(original)
+++ james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
Fri Dec 11 10:04:00 2015
@@ -19,8 +19,16 @@
 
 package org.apache.james.http.jetty;
 
-import static com.jayway.restassured.RestAssured.*;
+import static com.jayway.restassured.RestAssured.when;
 
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.hamcrest.Matchers;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -29,6 +37,21 @@ import com.jayway.restassured.RestAssure
 
 public class JettyHttpServerTest {
 
+    @FunctionalInterface
+    private interface ServletMethod {
+        void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException;
+    }
+    
+    private static HttpServlet get(ServletMethod method) {
+        return new HttpServlet() {
+            @Override
+            protected void doGet(HttpServletRequest req,
+                    HttpServletResponse resp) throws ServletException, IOException {
+                method.handle(req, resp);
+            }
+        };
+    }
+    
     private JettyHttpServer testee;
     
     @Before
@@ -36,13 +59,13 @@ public class JettyHttpServerTest {
     }
     
     @After
-    public void teardown() {
+    public void teardown() throws Exception {
         testee.stop();
     }
     
     @Test
     public void shouldReturn404WhenNoServletConfigured() throws Exception {
-        testee = JettyHttpServer.start(new JettyHttpServer.Configuration());
+        testee = JettyHttpServer.start(Configuration.empty());
         RestAssured.port = testee.getPort();
         when()
             .get("/")
@@ -51,4 +74,46 @@ public class JettyHttpServerTest {
                 .statusCode(404);
     }
     
+    @Test
+    public void shouldLetConfiguredServletHandleIncomingRequestWhenServletConfigured() throws
Exception {
+        ServletMethod getHandler = (req, resp) -> resp.getWriter().append("served").close();
+        
+        testee = JettyHttpServer.start(Configuration
+                                        .builder()
+                                        .serve("/")
+                                        .with(get(getHandler)).build());
+        
+        RestAssured.port = testee.getPort();
+        
+        when()
+            .get("/")
+        .then()
+            .assertThat()
+                .statusCode(200)
+                .body(Matchers.equalTo("served"));
+    }
+    
+    @Test
+    public void shouldDispatchToRightServletWhenTwoServletConfigured() throws Exception {
+        ServletMethod fooGetHandler = (req, resp) -> resp.getWriter().append("served").close();
+        ServletMethod barGetMethod = (req, resp) -> resp.sendError(400, "should not be
called");
+        
+        testee = JettyHttpServer.start(Configuration
+                                        .builder()
+                                        .serve("/foo")
+                                        .with(get(fooGetHandler))
+                                        .serve("/bar")
+                                        .with(get(barGetMethod))
+                                        .build());
+        
+        RestAssured.port = testee.getPort();
+        
+        when()
+            .get("/foo")
+        .then()
+            .assertThat()
+                .statusCode(200)
+                .body(Matchers.equalTo("served"));
+    }
+    
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message