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 bt...@apache.org on 2020/04/13 02:53:35 UTC

[james-project] 08/13: JAMES-2888: implement Echo method

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c6979ccad9e77550742d0fc1a59c03c296796fe3
Author: ducnv <du...@gmail.com>
AuthorDate: Mon Mar 23 15:42:38 2020 +0700

    JAMES-2888: implement Echo method
---
 .../james/jmap/rfc/api/method/EchoMethod.scala     | 48 +++++++++++++++++++++
 .../jmap/rfc/api/parser/RequestObjectParser.scala  | 16 +++++++
 .../james/jmap/rfc/api/routes/JMAPAPIRoute.scala   | 13 ++++++
 .../james/jmap/rfc/api/routes/JMAPApiRoutes.scala  | 50 ++++++++++++++++++++++
 .../james/jmap/rfc/model/ResponseObject.scala      | 38 ++++++++++++++++
 .../james/jmap/rfc/api/method/EchoMethodTest.scala | 26 +++++++++++
 6 files changed, 191 insertions(+)

diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/method/EchoMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/method/EchoMethod.scala
new file mode 100644
index 0000000..fabc9cc
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/method/EchoMethod.scala
@@ -0,0 +1,48 @@
+/** **************************************************************
+ * 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.rfc.api.method
+
+import eu.timepit.refined.auto._
+import org.apache.james.jmap.rfc.api.parser.RequestObjectParser
+import org.apache.james.jmap.rfc.api.routes.{JMAPAPIRoute, MethodName}
+import org.apache.james.jmap.rfc.model.{RequestObject, ResponseObject}
+import org.reactivestreams.Publisher
+import reactor.core.publisher.Mono
+import reactor.netty.http.server.HttpServerRequest
+
+class EchoMethod extends JMAPAPIRoute[ResponseObject] {
+
+  private val requestObjectParser: RequestObjectParser = new RequestObjectParser()
+
+  def toResponseObject(requestObject: RequestObject): ResponseObject = {
+    ResponseObject(ResponseObject.SESSION_STATE, requestObject.methodCalls)
+  }
+
+  override var methodName = MethodName("echoMethod")
+
+  override def process(httpServerRequest: HttpServerRequest): Publisher[ResponseObject] = {
+    httpServerRequest
+      .receive()
+      .asInputStream()
+      .flatMap(requestObjectParser.toRequestObject)
+      .flatMap((requestObject: RequestObject) => {
+        return Mono.just(new ResponseObject(ResponseObject.SESSION_STATE, requestObject.methodCalls))
+      })
+  }
+}
\ No newline at end of file
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/parser/RequestObjectParser.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/parser/RequestObjectParser.scala
new file mode 100644
index 0000000..154d6da
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/parser/RequestObjectParser.scala
@@ -0,0 +1,16 @@
+package org.apache.james.jmap.rfc.api.parser
+
+import java.io.InputStream
+
+import org.apache.james.jmap.rfc.model.RequestObject
+import play.api.libs.json.{JsError, JsSuccess, Json}
+import reactor.core.publisher.Mono
+
+class RequestObjectParser {
+  def toRequestObject(inputStream: InputStream): Mono[RequestObject] = {
+    Json.fromJson[RequestObject](Json.parse(inputStream)) match {
+      case JsSuccess(requestObject, _) => Mono.just(requestObject)
+      case JsError(errors) => Mono.error(new RuntimeException(errors.toString()))
+    }
+  }
+}
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPAPIRoute.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPAPIRoute.scala
new file mode 100644
index 0000000..d505795
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPAPIRoute.scala
@@ -0,0 +1,13 @@
+package org.apache.james.jmap.rfc.api.routes
+
+import eu.timepit.refined.api.Refined
+import eu.timepit.refined.collection.NonEmpty
+import org.reactivestreams.Publisher
+import reactor.netty.http.server.HttpServerRequest
+
+case class MethodName(value: String Refined NonEmpty)
+trait JMAPAPIRoute[T] {
+  var methodName: MethodName
+  def process(httpServerRequest: HttpServerRequest):Publisher[T]
+}
+
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPApiRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPApiRoutes.scala
new file mode 100644
index 0000000..973ca77
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/api/routes/JMAPApiRoutes.scala
@@ -0,0 +1,50 @@
+/** **************************************************************
+ * 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.rfc.api.routes
+
+import io.netty.handler.codec.http.HttpResponseStatus
+import org.apache.james.jmap.rfc.api.method.EchoMethod
+import reactor.core.publisher.Mono
+import reactor.netty.http.server.{HttpServerRequest, HttpServerResponse}
+
+object JMAPApiRoutes {
+  private val ECHO_METHOD = new EchoMethod();
+  private val METHOD_NAME_PARAMETER = "method-name"
+}
+
+class JMAPApiRoutes {
+  def post(httpServerRequest: HttpServerRequest, httpServerResponse: HttpServerResponse): Mono[Void] = {
+    Mono.just(httpServerRequest)
+      .flatMap(httpRequest => this.process(httpRequest, httpServerResponse))
+      .`then`()
+  }
+
+  def process(httpRequest: HttpServerRequest, httpServerResponse: HttpServerResponse) = {
+    httpRequest.param(JMAPApiRoutes.METHOD_NAME_PARAMETER) match {
+      case JMAPApiRoutes.ECHO_METHOD.methodName.value.value => Mono.just(httpServerResponse
+        .status(HttpResponseStatus.OK)
+        .sendObject(JMAPApiRoutes.ECHO_METHOD.process(httpRequest))).`then`()
+
+      case _ => Mono.just(httpServerResponse
+        .status(HttpResponseStatus.NOT_IMPLEMENTED)
+        .sendObject("Api not implemented")).`then`()
+    }
+  }
+}
+
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/model/ResponseObject.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/model/ResponseObject.scala
new file mode 100644
index 0000000..90ee590
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/rfc/model/ResponseObject.scala
@@ -0,0 +1,38 @@
+/** **************************************************************
+ * 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.rfc.model
+
+import eu.timepit.refined.types.string.NonEmptyString
+import org.apache.james.jmap.rfc.model.ResponseObject.SessionState
+import play.api.libs.json.{JsResult, Json}
+
+case class ResponseObject(sessionState: SessionState, methodResponses: Seq[Invocation])
+
+object ResponseObject {
+
+  case class SessionState(value: NonEmptyString)
+
+  implicit val sessionStateFormat = Json.valueFormat[SessionState]
+  implicit val responseObjectFormat = Json.format[ResponseObject]
+
+  def deserialize(input: String): JsResult[ResponseObject] = {
+    Json.parse(input).validate[ResponseObject]
+  }
+}
diff --git a/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/rfc/api/method/EchoMethodTest.scala b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/rfc/api/method/EchoMethodTest.scala
new file mode 100644
index 0000000..564745f
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/rfc/api/method/EchoMethodTest.scala
@@ -0,0 +1,26 @@
+/** **************************************************************
+ * 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.rfc.api.method
+
+import org.scalatestplus.play.PlaySpec
+
+class EchoMethodTest extends PlaySpec {
+  "EchoMethod succeed" must {
+  }
+}


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