james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1719360 - in /james/project/trunk/server: container/cassandra-guice/src/main/java/org/apache/james/jmap/ protocols/jmap/ protocols/jmap/src/main/java/org/apache/james/jmap/ protocols/jmap/src/main/java/org/apache/james/jmap/methods/ protoc...
Date Fri, 11 Dec 2015 12:29:10 GMT
Author: matthieu
Date: Fri Dec 11 12:29:09 2015
New Revision: 1719360

URL: http://svn.apache.org/viewvc?rev=1719360&view=rev
Log:
JAMES-1644 Process JMAP request to dedicated method

Added:
    james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/MethodsModule.java
      - copied, changed from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequest.java
      - copied, changed from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParser.java
      - copied, changed from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParserImpl.java
      - copied, changed from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java
      - copied, changed from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriter.java
      - copied, changed from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriterImpl.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/Method.java
      - copied, changed from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapRequestParserImplTest.java
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapResponseWriterImplTest.java
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/RequestHandlerTest.java
Modified:
    james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/JMAPModule.java
    james/project/trunk/server/protocols/jmap/pom.xml
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServlet.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServletTest.java

Modified: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/JMAPModule.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/JMAPModule.java?rev=1719360&r1=1719359&r2=1719360&view=diff
==============================================================================
--- james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/JMAPModule.java
(original)
+++ james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/JMAPModule.java
Fri Dec 11 12:29:09 2015
@@ -20,17 +20,14 @@ package org.apache.james.jmap;
 
 import java.io.FileNotFoundException;
 
-import javax.inject.Singleton;
-
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.james.filesystem.api.FileSystem;
 import org.apache.james.jmap.methods.RequestHandler;
-import org.apache.james.jmap.model.ProtocolRequest;
-import org.apache.james.jmap.model.ProtocolResponse;
 
 import com.google.inject.AbstractModule;
 import com.google.inject.Provides;
+import com.google.inject.Singleton;
 import com.google.inject.name.Names;
 
 public class JMAPModule extends AbstractModule {
@@ -40,16 +37,8 @@ public class JMAPModule extends Abstract
     @Override
     protected void configure() {
         install(new JMAPCommonModule());
-        bind(AuthenticationFilter.class);
-        bind(RequestHandler.class).toInstance(new RequestHandler() {
-
-            @Override
-            public ProtocolResponse process(ProtocolRequest request) {
-                // TODO Auto-generated method stub
-                return null;
-            }
-            
-        });
+        install(new MethodsModule());
+        bind(RequestHandler.class).in(Singleton.class);
 
         bindConstant().annotatedWith(Names.named(JMAPServer.DEFAULT_JMAP_PORT)).to(DEFAULT_PORT);
     }

Copied: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/MethodsModule.java
(from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java)
URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/MethodsModule.java?p2=james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/MethodsModule.java&p1=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java&r1=1719326&r2=1719360&rev=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/jmap/MethodsModule.java
Fri Dec 11 12:29:09 2015
@@ -17,12 +17,30 @@
  * under the License.                                           *
  ****************************************************************/
 
-package org.apache.james.jmap.methods;
+package org.apache.james.jmap;
 
-import org.apache.james.jmap.model.ProtocolRequest;
-import org.apache.james.jmap.model.ProtocolResponse;
+import org.apache.james.jmap.methods.JmapRequestParser;
+import org.apache.james.jmap.methods.JmapRequestParserImpl;
+import org.apache.james.jmap.methods.JmapResponseWriter;
+import org.apache.james.jmap.methods.JmapResponseWriterImpl;
+import org.apache.james.jmap.methods.Method;
 
-public interface RequestHandler {
+import com.fasterxml.jackson.databind.Module;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.google.inject.AbstractModule;
+import com.google.inject.Singleton;
+import com.google.inject.multibindings.Multibinder;
+
+public class MethodsModule extends AbstractModule {
+
+    @Override
+    protected void configure() {
+        Multibinder<Module> jacksonModules = Multibinder.newSetBinder(binder(), Module.class);
+        jacksonModules.addBinding().to(Jdk8Module.class);
+        bind(JmapRequestParser.class).to(JmapRequestParserImpl.class).in(Singleton.class);
+        bind(JmapResponseWriter.class).to(JmapResponseWriterImpl.class).in(Singleton.class);
+
+        Multibinder<Method> methods = Multibinder.newSetBinder(binder(), Method.class);
+    }
 
-    ProtocolResponse process(ProtocolRequest request);
 }

Modified: james/project/trunk/server/protocols/jmap/pom.xml
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/pom.xml?rev=1719360&r1=1719359&r2=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/pom.xml (original)
+++ james/project/trunk/server/protocols/jmap/pom.xml Fri Dec 11 12:29:09 2015
@@ -180,7 +180,12 @@
                 <dependency>
                     <groupId>com.fasterxml.jackson.core</groupId>
                     <artifactId>jackson-databind</artifactId>
-                    <version>2.3.3</version>
+                    <version>2.6.3</version>
+                </dependency>
+                <dependency>
+                    <groupId>com.fasterxml.jackson.datatype</groupId>
+                    <artifactId>jackson-datatype-jdk8</artifactId>
+                    <version>2.6.3</version>
                 </dependency>
                 <dependency>
                     <groupId>com.jayway.restassured</groupId>

Modified: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServlet.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServlet.java?rev=1719360&r1=1719359&r2=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServlet.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServlet.java
Fri Dec 11 12:29:09 2015
@@ -46,22 +46,22 @@ public class JMAPServlet extends HttpSer
     public static final String JSON_CONTENT_TYPE = "application/json";
     public static final String JSON_CONTENT_TYPE_UTF8 = "application/json; charset=UTF-8";
 
-    private final RequestHandler requestHandler;
     private final ObjectMapper objectMapper;
+    private final RequestHandler requestHandler;
 
     @Inject
     @VisibleForTesting JMAPServlet(RequestHandler requestHandler) {
         this.requestHandler = requestHandler;
         this.objectMapper = new ObjectMapper();
     }
-    
+
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException
{
         try {
             List<Object[]> responses = 
                 requestAsJsonStream(req)
                 .map(ProtocolRequest::deserialize)
-                .map(requestHandler::process)
+                .map(requestHandler::handle)
                 .map(protocolResponse -> protocolResponse.asProtocolSpecification())
                 .collect(Collectors.toList());
 
@@ -75,5 +75,4 @@ public class JMAPServlet extends HttpSer
         return Arrays.stream(
                 objectMapper.readValue(req.getInputStream(), JsonNode[][].class));
     }
-
 }

Copied: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequest.java
(from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java)
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequest.java?p2=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequest.java&p1=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java&r1=1719326&r2=1719360&rev=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequest.java
Fri Dec 11 12:29:09 2015
@@ -19,10 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import org.apache.james.jmap.model.ProtocolRequest;
-import org.apache.james.jmap.model.ProtocolResponse;
+public interface JmapRequest {
 
-public interface RequestHandler {
-
-    ProtocolResponse process(ProtocolRequest request);
 }

Copied: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParser.java
(from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java)
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParser.java?p2=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParser.java&p1=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java&r1=1719326&r2=1719360&rev=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParser.java
Fri Dec 11 12:29:09 2015
@@ -19,10 +19,15 @@
 
 package org.apache.james.jmap.methods;
 
+import java.io.IOException;
+
 import org.apache.james.jmap.model.ProtocolRequest;
-import org.apache.james.jmap.model.ProtocolResponse;
 
-public interface RequestHandler {
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+
+public interface JmapRequestParser {
 
-    ProtocolResponse process(ProtocolRequest request);
+    <T extends JmapRequest> T extractJmapRequest(ProtocolRequest request, Class<T>
requestClass) 
+            throws IOException, JsonParseException, JsonMappingException;
 }

Copied: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParserImpl.java
(from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java)
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParserImpl.java?p2=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParserImpl.java&p1=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java&r1=1719326&r2=1719360&rev=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapRequestParserImpl.java
Fri Dec 11 12:29:09 2015
@@ -19,10 +19,33 @@
 
 package org.apache.james.jmap.methods;
 
+import java.io.IOException;
+import java.util.Set;
+
+import javax.inject.Inject;
+
 import org.apache.james.jmap.model.ProtocolRequest;
-import org.apache.james.jmap.model.ProtocolResponse;
 
-public interface RequestHandler {
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.Module;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class JmapRequestParserImpl implements JmapRequestParser {
+
+    private final ObjectMapper objectMapper;
+
+    @Inject
+    public JmapRequestParserImpl(Set<Module> jacksonModules) {
+        this.objectMapper = new ObjectMapper()
+                .registerModules(jacksonModules)
+                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);;
+    }
 
-    ProtocolResponse process(ProtocolRequest request);
+    @Override
+    public <T extends JmapRequest> T extractJmapRequest(ProtocolRequest request, Class<T>
requestClass) 
+            throws IOException, JsonParseException, JsonMappingException {
+        return objectMapper.readValue(request.getParameters().toString(), requestClass);
+    }
 }

Copied: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java
(from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java)
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java?p2=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java&p1=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java&r1=1719326&r2=1719360&rev=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java
Fri Dec 11 12:29:09 2015
@@ -19,10 +19,6 @@
 
 package org.apache.james.jmap.methods;
 
-import org.apache.james.jmap.model.ProtocolRequest;
-import org.apache.james.jmap.model.ProtocolResponse;
+public interface JmapResponse {
 
-public interface RequestHandler {
-
-    ProtocolResponse process(ProtocolRequest request);
 }

Copied: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriter.java
(from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java)
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriter.java?p2=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriter.java&p1=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java&r1=1719326&r2=1719360&rev=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriter.java
Fri Dec 11 12:29:09 2015
@@ -22,7 +22,11 @@ package org.apache.james.jmap.methods;
 import org.apache.james.jmap.model.ProtocolRequest;
 import org.apache.james.jmap.model.ProtocolResponse;
 
-public interface RequestHandler {
+public interface JmapResponseWriter {
 
-    ProtocolResponse process(ProtocolRequest request);
-}
+    ProtocolResponse formatMethodResponse(ProtocolRequest request, JmapResponse jmapResponse);
+
+    ProtocolResponse formatErrorResponse(ProtocolRequest request);
+
+    ProtocolResponse formatErrorResponse(ProtocolRequest request, String error);
+}
\ No newline at end of file

Added: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriterImpl.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriterImpl.java?rev=1719360&view=auto
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriterImpl.java
(added)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponseWriterImpl.java
Fri Dec 11 12:29:09 2015
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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.jmap.methods;
+
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.jmap.model.ProtocolRequest;
+import org.apache.james.jmap.model.ProtocolResponse;
+
+import com.fasterxml.jackson.databind.Module;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.annotations.VisibleForTesting;
+
+public class JmapResponseWriterImpl implements JmapResponseWriter {
+
+    @VisibleForTesting static final String DEFAULT_ERROR_MESSAGE = "Error while processing";
+    @VisibleForTesting static final String ERROR_METHOD = "error";
+
+    private final ObjectMapper objectMapper;
+
+    @Inject
+    public JmapResponseWriterImpl(Set<Module> jacksonModules) {
+        this.objectMapper = new ObjectMapper().registerModules(jacksonModules);
+    }
+
+    @Override
+    public ProtocolResponse formatMethodResponse(ProtocolRequest request, JmapResponse jmapResponse)
{
+        ObjectNode objectNode = objectMapper.valueToTree(jmapResponse);
+        return new ProtocolResponse(request.getMethod(), objectNode, request.getClientId());
+    }
+
+    @Override
+    public ProtocolResponse formatErrorResponse(ProtocolRequest request) {
+        return formatErrorResponse(request, DEFAULT_ERROR_MESSAGE);
+    }
+
+    @Override
+    public ProtocolResponse formatErrorResponse(ProtocolRequest request, String error) {
+        ObjectNode errorObjectNode = new ObjectNode(new JsonNodeFactory(false)).put("type",
error);
+        return new ProtocolResponse(ERROR_METHOD, errorObjectNode, request.getClientId());
+    }
+}

Copied: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/Method.java
(from r1719326, james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java)
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/Method.java?p2=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/Method.java&p1=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java&r1=1719326&r2=1719360&rev=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/Method.java
Fri Dec 11 12:29:09 2015
@@ -22,7 +22,10 @@ package org.apache.james.jmap.methods;
 import org.apache.james.jmap.model.ProtocolRequest;
 import org.apache.james.jmap.model.ProtocolResponse;
 
-public interface RequestHandler {
+public interface Method {
+
+    String methodName();
 
     ProtocolResponse process(ProtocolRequest request);
+
 }

Modified: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java?rev=1719360&r1=1719359&r2=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/RequestHandler.java
Fri Dec 11 12:29:09 2015
@@ -19,10 +19,29 @@
 
 package org.apache.james.jmap.methods;
 
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import javax.inject.Inject;
+
 import org.apache.james.jmap.model.ProtocolRequest;
 import org.apache.james.jmap.model.ProtocolResponse;
 
-public interface RequestHandler {
+public class RequestHandler {
+
+    private final Map<String, Method> methods;
+
+    @Inject
+    public RequestHandler(Set<Method> methods) {
+        this.methods = methods.stream()
+                .collect(Collectors.toMap(Method::methodName, method -> method));
+    }
 
-    ProtocolResponse process(ProtocolRequest request);
+    public ProtocolResponse handle(ProtocolRequest request) {
+        return Optional.ofNullable(methods.get(request.getMethod()))
+            .map(method -> method.process(request))
+            .orElseThrow(() -> new IllegalStateException("unknown method"));
+    }
 }

Modified: james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServletTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServletTest.java?rev=1719360&r1=1719359&r2=1719360&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServletTest.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JMAPServletTest.java
Fri Dec 11 12:29:09 2015
@@ -58,7 +58,6 @@ public class JMAPServletTest {
                 .randomPort()
                 .build());
 
-
         server.start();
 
         RestAssured.port = server.getPort();
@@ -90,7 +89,7 @@ public class JMAPServletTest {
         ObjectNode json = new ObjectNode(new JsonNodeFactory(false));
         json.put("type", "invalidArgument");
 
-        when(requestHandler.process(any()))
+        when(requestHandler.handle(any()))
             .thenReturn(new ProtocolResponse("error", json, "#0"));
 
         given()
@@ -114,7 +113,7 @@ public class JMAPServletTest {
         list.put("name", "roger@barcamp");
         arrayNode.add(list);
 
-        when(requestHandler.process(any()))
+        when(requestHandler.handle(any()))
             .thenReturn(new ProtocolResponse("accounts", json, "#0"));
 
         given()

Added: james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapRequestParserImplTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapRequestParserImplTest.java?rev=1719360&view=auto
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapRequestParserImplTest.java
(added)
+++ james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapRequestParserImplTest.java
Fri Dec 11 12:29:09 2015
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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.jmap.methods;
+
+import org.apache.james.jmap.model.ProtocolRequest;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.google.common.collect.ImmutableSet;
+
+public class JmapRequestParserImplTest {
+
+    @Test(expected=IllegalArgumentException.class)
+    public void extractJmapRequestShouldThrowWhenNullRequestClass() throws Exception {
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode("unknwonMethod"),
+                new ObjectNode(new JsonNodeFactory(false)).putObject("{\"id\": \"id\"}"),
+                new ObjectNode(new JsonNodeFactory(false)).textNode("#1")} ;
+
+        JmapRequestParserImpl jmapRequestParserImpl = new JmapRequestParserImpl(ImmutableSet.of(new
Jdk8Module()));
+        jmapRequestParserImpl.extractJmapRequest(ProtocolRequest.deserialize(nodes), null);
+    }
+
+    @Test
+    public void extractJmapRequestShouldNotThrowWhenJsonContainsUnknownProperty() throws
Exception {
+        ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false));
+        parameters.put("id", "myId");
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode("unknwonMethod"),
+                parameters,
+                new ObjectNode(new JsonNodeFactory(false)).textNode("#1")} ;
+
+        JmapRequestParserImpl jmapRequestParserImpl = new JmapRequestParserImpl(ImmutableSet.of(new
Jdk8Module()));
+        jmapRequestParserImpl.extractJmapRequest(ProtocolRequest.deserialize(nodes), RequestClass.class);
+    }
+
+    @Test
+    public void extractJmapRequestShouldNotThrowWhenPropertyMissingInJson() throws Exception
{
+        ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false));
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode("unknwonMethod"),
+                parameters,
+                new ObjectNode(new JsonNodeFactory(false)).textNode("#1")} ;
+
+        JmapRequestParserImpl jmapRequestParserImpl = new JmapRequestParserImpl(ImmutableSet.of(new
Jdk8Module()));
+        jmapRequestParserImpl.extractJmapRequest(ProtocolRequest.deserialize(nodes), RequestClass.class);
+    }
+
+    private static class RequestClass implements JmapRequest {
+
+        @SuppressWarnings("unused")
+        public String parameter;
+    }
+}

Added: james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapResponseWriterImplTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapResponseWriterImplTest.java?rev=1719360&view=auto
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapResponseWriterImplTest.java
(added)
+++ james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/JmapResponseWriterImplTest.java
Fri Dec 11 12:29:09 2015
@@ -0,0 +1,103 @@
+/****************************************************************
+ * 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.jmap.methods;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.jmap.model.ProtocolRequest;
+import org.apache.james.jmap.model.ProtocolResponse;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.google.common.collect.ImmutableSet;
+
+public class JmapResponseWriterImplTest {
+
+    @Test(expected=IllegalStateException.class)
+    public void formatMethodResponseShouldWorkWhenNullJmapResponse() {
+        String expectedMethod = "unknwonMethod";
+        String expectedClientId = "#1";
+        String expectedId = "myId";
+
+        ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false));
+        parameters.put("id", expectedId);
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode(expectedMethod),
+                parameters,
+                new ObjectNode(new JsonNodeFactory(false)).textNode(expectedClientId)} ;
+
+        JmapResponseWriterImpl jmapResponseWriterImpl = new JmapResponseWriterImpl(ImmutableSet.of(new
Jdk8Module()));
+        ProtocolResponse response = jmapResponseWriterImpl.formatMethodResponse(ProtocolRequest.deserialize(nodes),
null);
+
+        assertThat(response.getMethod()).isEqualTo(expectedMethod);
+        assertThat(response.getResults().findValue("id").asText()).isEqualTo(expectedId);
+        assertThat(response.getClientId()).isEqualTo(expectedClientId);
+    }
+
+    @Test
+    public void formatMethodResponseShouldWork() {
+        String expectedMethod = "unknwonMethod";
+        String expectedClientId = "#1";
+        String expectedId = "myId";
+
+        ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false));
+        parameters.put("id", expectedId);
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode(expectedMethod),
+                parameters,
+                new ObjectNode(new JsonNodeFactory(false)).textNode(expectedClientId)} ;
+
+        ResponseClass responseClass = new ResponseClass();
+        responseClass.id = expectedId;
+
+        JmapResponseWriterImpl jmapResponseWriterImpl = new JmapResponseWriterImpl(ImmutableSet.of(new
Jdk8Module()));
+        ProtocolResponse response = jmapResponseWriterImpl.formatMethodResponse(ProtocolRequest.deserialize(nodes),
responseClass);
+
+        assertThat(response.getMethod()).isEqualTo(expectedMethod);
+        assertThat(response.getResults().findValue("id").asText()).isEqualTo(expectedId);
+        assertThat(response.getClientId()).isEqualTo(expectedClientId);
+    }
+
+    private static class ResponseClass implements JmapResponse {
+
+        @SuppressWarnings("unused")
+        public String id;
+        
+    }
+
+    @Test
+    public void formatErrorResponseShouldWork() {
+        String expectedClientId = "#1";
+
+        ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false));
+        parameters.put("id", "myId");
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode("unknwonMethod"),
+                parameters,
+                new ObjectNode(new JsonNodeFactory(false)).textNode(expectedClientId)} ;
+
+        JmapResponseWriterImpl jmapResponseWriterImpl = new JmapResponseWriterImpl(ImmutableSet.of(new
Jdk8Module()));
+        ProtocolResponse response = jmapResponseWriterImpl.formatErrorResponse(ProtocolRequest.deserialize(nodes));
+
+        assertThat(response.getMethod()).isEqualTo(JmapResponseWriterImpl.ERROR_METHOD);
+        assertThat(response.getResults().findValue("type").asText()).isEqualTo(JmapResponseWriterImpl.DEFAULT_ERROR_MESSAGE);
+        assertThat(response.getClientId()).isEqualTo(expectedClientId);
+    }
+}

Added: james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/RequestHandlerTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/RequestHandlerTest.java?rev=1719360&view=auto
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/RequestHandlerTest.java
(added)
+++ james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/RequestHandlerTest.java
Fri Dec 11 12:29:09 2015
@@ -0,0 +1,182 @@
+/****************************************************************
+ * 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.jmap.methods;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+
+import javax.inject.Inject;
+
+import org.apache.james.jmap.model.ProtocolRequest;
+import org.apache.james.jmap.model.ProtocolResponse;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableSet;
+
+public class RequestHandlerTest {
+
+    public static class TestJmapRequest implements JmapRequest {
+
+        public String id;
+        public String name;
+
+        public String getId() {
+            return id;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+
+    public static class TestJmapResponse implements JmapResponse {
+
+        private final String id;
+        private final String name;
+        private final String message;
+
+        public TestJmapResponse(String id, String name, String message) {
+            this.id = id;
+            this.name = name;
+            this.message = message;
+        }
+
+        public String getId() {
+            return id;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public String getMessage() {
+            return message;
+        }
+    }
+
+    public static class TestMethod implements Method {
+
+        private final JmapRequestParser jmapRequestParser;
+        private final JmapResponseWriter jmapResponseWriter;
+
+        @Inject
+        @VisibleForTesting TestMethod(JmapRequestParser jmapRequestParser, JmapResponseWriter
jmapResponseWriter) {
+            this.jmapRequestParser = jmapRequestParser;
+            this.jmapResponseWriter = jmapResponseWriter;
+        }
+
+        @Override
+        public String methodName() {
+            return "testMethod";
+        }
+
+        @Override
+        public ProtocolResponse process(ProtocolRequest request) {
+            try {
+                TestJmapRequest typedRequest = jmapRequestParser.extractJmapRequest(request,
TestJmapRequest.class);
+                return jmapResponseWriter.formatMethodResponse(request, 
+                        new TestJmapResponse(typedRequest.getId(), typedRequest.getName(),
"works"));
+            } catch (IOException e) {
+                return jmapResponseWriter.formatErrorResponse(request);
+            }
+        }
+    }
+
+    private RequestHandler testee;
+    private JmapRequestParser jmapRequestParser;
+    private JmapResponseWriter jmapResponseWriter;
+
+    @Before
+    public void setup() {
+        jmapRequestParser = new JmapRequestParserImpl(ImmutableSet.of(new Jdk8Module()));
+        jmapResponseWriter = new JmapResponseWriterImpl(ImmutableSet.of(new Jdk8Module()));
+        testee = new RequestHandler(ImmutableSet.of(new TestMethod(jmapRequestParser, jmapResponseWriter)));
+    }
+
+
+    @Test(expected=IllegalStateException.class)
+    public void processShouldThrowWhenUnknownMethod() {
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode("unknwonMethod"),
+                new ObjectNode(new JsonNodeFactory(false)).putObject("{\"id\": \"id\"}"),
+                new ObjectNode(new JsonNodeFactory(false)).textNode("#1")} ;
+
+        RequestHandler requestHandlerImpl = new RequestHandler(ImmutableSet.of());
+        requestHandlerImpl.handle(ProtocolRequest.deserialize(nodes));
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void requestHandlerShouldThrowWhenAMethodIsRecordedTwice() {
+        new RequestHandler(ImmutableSet.of(new TestMethod(jmapRequestParser, jmapResponseWriter),
new TestMethod(jmapRequestParser, jmapResponseWriter)));
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void requestHandlerShouldThrowWhenTwoMethodsWithSameName() {
+        new RequestHandler(ImmutableSet.of(new NamedMethod("name"), new NamedMethod("name")));
+    }
+
+    @Test
+    public void requestHandlerMayBeCreatedWhenTwoMethodsWithDifferentName() {
+        new RequestHandler(ImmutableSet.of(new NamedMethod("name"), new NamedMethod("name2")));
+    }
+
+    private class NamedMethod implements Method {
+
+        private final String methodName;
+
+        public NamedMethod(String methodName) {
+            this.methodName = methodName;
+            
+        }
+
+        @Override
+        public String methodName() {
+            return methodName;
+        }
+
+        @Override
+        public ProtocolResponse process(ProtocolRequest request) {
+            return null;
+        }
+    }
+
+    @Test
+    public void processShouldWorkWhenKnownMethod() {
+        ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false));
+        parameters.put("id", "testId");
+        parameters.put("name", "testName");
+        
+        JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode("testMethod"),
+                parameters,
+                new ObjectNode(new JsonNodeFactory(false)).textNode("#1")} ;
+
+        ProtocolResponse response = testee.handle(ProtocolRequest.deserialize(nodes));
+
+        assertThat(response.getResults().findValue("id").asText()).isEqualTo("testId");
+        assertThat(response.getResults().findValue("name").asText()).isEqualTo("testName");
+        assertThat(response.getResults().findValue("message").asText()).isEqualTo("works");
+    }
+}




---------------------------------------------------------------------
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