You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rc...@apache.org on 2020/04/10 02:02:20 UTC

[james-project] 12/15: JAMES-3092 Add a builder for JMAPRoute

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 9af645e1352656f78033a47c7410fbcdde60ea50
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Mon Mar 30 16:27:38 2020 +0700

    JAMES-3092 Add a builder for JMAPRoute
---
 .../james/jmap/http/AuthenticationRoutes.java      | 20 ++++++++---
 .../org/apache/james/jmap/http/DownloadRoutes.java | 30 ++++++++++++----
 .../org/apache/james/jmap/http/JMAPApiRoutes.java  | 10 ++++--
 .../org/apache/james/jmap/http/UploadRoutes.java   | 10 ++++--
 .../main/java/org/apache/james/jmap/JMAPRoute.java | 40 +++++++++++++++++++++-
 .../java/org/apache/james/jmap/JMAPServerTest.java |  5 ++-
 6 files changed, 99 insertions(+), 16 deletions(-)

diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/AuthenticationRoutes.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/AuthenticationRoutes.java
index f9f9eb1..70b0490 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/AuthenticationRoutes.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/AuthenticationRoutes.java
@@ -107,10 +107,22 @@ public class AuthenticationRoutes implements JMAPRoutes {
     @Override
     public Stream<JMAPRoute> routes() {
         return Stream.of(
-            new JMAPRoute(new Endpoint(HttpMethod.POST, AUTHENTICATION), JMAPRoutes.corsHeaders(this::post)),
-            new JMAPRoute(new Endpoint(HttpMethod.GET, AUTHENTICATION), JMAPRoutes.corsHeaders(this::returnEndPointsResponse)),
-            new JMAPRoute(new Endpoint(HttpMethod.DELETE, AUTHENTICATION), JMAPRoutes.corsHeaders(this::delete)),
-            new JMAPRoute(new Endpoint(HttpMethod.OPTIONS, AUTHENTICATION), CORS_CONTROL)
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.POST, AUTHENTICATION))
+                .action(this::post)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.GET, AUTHENTICATION))
+                .action(this::returnEndPointsResponse)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.DELETE, AUTHENTICATION))
+                .action(this::delete)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.OPTIONS, AUTHENTICATION))
+                .action(CORS_CONTROL)
+                .noCorsHeaders()
         );
     }
 
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/DownloadRoutes.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/DownloadRoutes.java
index e0cc639..8ac0bc3 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/DownloadRoutes.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/DownloadRoutes.java
@@ -100,12 +100,30 @@ public class DownloadRoutes implements JMAPRoutes {
     @Override
     public Stream<JMAPRoute> routes() {
         return Stream.of(
-            new JMAPRoute(new Endpoint(HttpMethod.POST, DOWNLOAD_FROM_ID), JMAPRoutes.corsHeaders(this::postFromId)),
-            new JMAPRoute(new Endpoint(HttpMethod.GET, DOWNLOAD_FROM_ID), JMAPRoutes.corsHeaders(this::getFromId)),
-            new JMAPRoute(new Endpoint(HttpMethod.POST, DOWNLOAD_FROM_ID_AND_NAME), JMAPRoutes.corsHeaders(this::postFromIdAndName)),
-            new JMAPRoute(new Endpoint(HttpMethod.GET, DOWNLOAD_FROM_ID_AND_NAME), JMAPRoutes.corsHeaders(this::getFromIdAndName)),
-            new JMAPRoute(new Endpoint(HttpMethod.OPTIONS, DOWNLOAD_FROM_ID), CORS_CONTROL),
-            new JMAPRoute(new Endpoint(HttpMethod.OPTIONS, DOWNLOAD_FROM_ID_AND_NAME), CORS_CONTROL)
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.POST, DOWNLOAD_FROM_ID))
+                .action(this::postFromId)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.GET, DOWNLOAD_FROM_ID))
+                .action(this::getFromId)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.POST, DOWNLOAD_FROM_ID_AND_NAME))
+                .action(this::postFromIdAndName)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.GET, DOWNLOAD_FROM_ID_AND_NAME))
+                .action(this::getFromIdAndName)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.OPTIONS, DOWNLOAD_FROM_ID))
+                .action(CORS_CONTROL)
+                .noCorsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.OPTIONS, DOWNLOAD_FROM_ID_AND_NAME))
+                .action(CORS_CONTROL)
+                .noCorsHeaders()
         );
     }
 
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/JMAPApiRoutes.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/JMAPApiRoutes.java
index 6530171..7454afe 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/JMAPApiRoutes.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/JMAPApiRoutes.java
@@ -87,8 +87,14 @@ public class JMAPApiRoutes implements JMAPRoutes {
     @Override
     public Stream<JMAPRoute> routes() {
         return Stream.of(
-            new JMAPRoute(new Endpoint(HttpMethod.POST, JMAP), JMAPRoutes.corsHeaders(this::post)),
-            new JMAPRoute(new Endpoint(HttpMethod.OPTIONS, JMAP), CORS_CONTROL)
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.POST, JMAP))
+                .action(this::post)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.OPTIONS, JMAP))
+                .action(CORS_CONTROL)
+                .noCorsHeaders()
         );
     }
 
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/UploadRoutes.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/UploadRoutes.java
index b0f22ba..6788c69 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/UploadRoutes.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/http/UploadRoutes.java
@@ -89,8 +89,14 @@ public class UploadRoutes implements JMAPRoutes {
     @Override
     public Stream<JMAPRoute> routes() {
         return Stream.of(
-            new JMAPRoute(new Endpoint(HttpMethod.POST, UPLOAD), JMAPRoutes.corsHeaders(this::post)),
-            new JMAPRoute(new Endpoint(HttpMethod.OPTIONS, UPLOAD), CORS_CONTROL)
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.POST, UPLOAD))
+                .action(this::post)
+                .corsHeaders(),
+            JMAPRoute.builder()
+                .endpoint(new Endpoint(HttpMethod.OPTIONS, UPLOAD))
+                .action(CORS_CONTROL)
+                .noCorsHeaders()
         );
     }
 
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoute.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoute.java
index c28cc73..8ad8ecf 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoute.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPRoute.java
@@ -29,10 +29,48 @@ public class JMAPRoute {
         Publisher<Void> handleRequest(HttpServerRequest request, HttpServerResponse response);
     }
 
+    public static class Builder {
+        @FunctionalInterface
+        public interface RequireEndpoint {
+            RequireAction endpoint(Endpoint endpoint);
+        }
+
+        @FunctionalInterface
+        public interface RequireAction {
+            ReadyToBuild action(Action action);
+        }
+
+        public static class ReadyToBuild {
+            private final Endpoint endpoint;
+            private final Action action;
+
+            ReadyToBuild(Endpoint endpoint, Action action) {
+                this.endpoint = endpoint;
+                this.action = action;
+            }
+
+            public JMAPRoute corsHeaders() {
+                return build(JMAPRoutes.corsHeaders(action));
+            }
+
+            public JMAPRoute noCorsHeaders() {
+                return build(action);
+            }
+
+            private JMAPRoute build(Action action) {
+                return new JMAPRoute(endpoint, action);
+            }
+        }
+    }
+
+    public static Builder.RequireEndpoint builder() {
+        return endpoint -> action -> new Builder.ReadyToBuild(endpoint, action);
+    }
+
     private final Endpoint endpoint;
     private final Action action;
 
-    public JMAPRoute(Endpoint endpoint, Action action) {
+    private JMAPRoute(Endpoint endpoint, Action action) {
         this.endpoint = endpoint;
         this.action = action;
     }
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServerTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServerTest.java
index fe2e2f9..759bbb3 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServerTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServerTest.java
@@ -226,7 +226,10 @@ class JMAPServerTest {
         @Override
         public Stream<JMAPRoute> routes() {
             return endpoints.stream()
-                .map(endpoint -> new JMAPRoute(endpoint, (request, response) -> sendVersionResponse(response)));
+                .map(endpoint -> JMAPRoute.builder()
+                    .endpoint(endpoint)
+                    .action((request, response) -> sendVersionResponse(response))
+                    .noCorsHeaders());
         }
 
         @Override


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