You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2020/09/07 03:34:47 UTC

[james-project] 01/16: JAMES-3366 VacationResponse/set request and response serialization

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 70ae2f3072b332c0e13ec0dcbee4ab8bbda9dfd9
Author: LanKhuat <dl...@linagora.com>
AuthorDate: Wed Aug 26 14:57:36 2020 +0700

    JAMES-3366 VacationResponse/set request and response serialization
---
 .../java/org/apache/james/util/ValuePatch.java     |  12 +++
 .../james/jmap/api/vacation/VacationPatch.java     |  10 ++
 .../org/apache/james/jmap/json/Serializer.scala    |  33 ++++++
 .../james/jmap/vacation/VacationResponseSet.scala  | 113 +++++++++++++++++++++
 4 files changed, 168 insertions(+)

diff --git a/server/container/util/src/main/java/org/apache/james/util/ValuePatch.java b/server/container/util/src/main/java/org/apache/james/util/ValuePatch.java
index 5c3d01a..dc70c6b 100644
--- a/server/container/util/src/main/java/org/apache/james/util/ValuePatch.java
+++ b/server/container/util/src/main/java/org/apache/james/util/ValuePatch.java
@@ -107,6 +107,18 @@ public class ValuePatch<T> {
         return toOptional().orElse(replacement);
     }
 
+    public ValuePatch<T> merge(ValuePatch<T> other) {
+        if (isKept()) {
+            return other;
+        }
+
+        if (other.isKept()) {
+            return this;
+        }
+
+        throw new IllegalStateException("the two merged ValuePatch represent a mutation");
+    }
+
     @Override
     public boolean equals(Object o) {
         if (o instanceof ValuePatch) {
diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationPatch.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationPatch.java
index 6a2cee1..e28dc8c 100644
--- a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationPatch.java
+++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationPatch.java
@@ -117,6 +117,16 @@ public class VacationPatch {
             return this;
         }
 
+        public Builder addAll(Builder other) {
+            return builder()
+                .isEnabled(this.isEnabled.merge(other.isEnabled))
+                .fromDate(this.fromDate.merge(other.fromDate))
+                .toDate(this.toDate.merge(other.toDate))
+                .subject(this.subject.merge(other.subject))
+                .htmlBody(this.htmlBody.merge(other.htmlBody))
+                .textBody(this.textBody.merge(other.textBody));
+        }
+
         public VacationPatch build() {
             return new VacationPatch(subject, textBody, htmlBody, toDate, fromDate, isEnabled);
         }
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala
index 0038321..e435f8e 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/Serializer.scala
@@ -21,6 +21,7 @@ package org.apache.james.jmap.json
 
 import java.io.InputStream
 import java.net.URL
+import java.time.ZonedDateTime
 import java.time.format.DateTimeFormatter
 
 import eu.timepit.refined._
@@ -36,6 +37,7 @@ import org.apache.james.jmap.model.Invocation.{Arguments, MethodCallId, MethodNa
 import org.apache.james.jmap.model.SetError.SetErrorDescription
 import org.apache.james.jmap.model.{Account, Invocation, Session, _}
 import org.apache.james.jmap.routes.{BackReference, JsonPath}
+import org.apache.james.jmap.vacation.{FromDate, IsEnabled, ToDate, UTCDate, VacationResponseId, VacationResponsePatchObject, VacationResponseSetError, VacationResponseSetRequest, VacationResponseSetResponse, VacationResponseUpdateResponse}
 import org.apache.james.mailbox.Role
 import org.apache.james.mailbox.model.MailboxACL.{Right => JavaRight}
 import org.apache.james.mailbox.model.{MailboxACL, MailboxId}
@@ -271,6 +273,28 @@ class Serializer @Inject() (mailboxIdFactory: MailboxId.Factory) {
 
   private implicit val mailboxSetRequestReads: Reads[MailboxSetRequest] = Json.reads[MailboxSetRequest]
 
+  private implicit val vacationResponseIdReads: Reads[VacationResponseId] = Json.valueReads[VacationResponseId]
+
+  private implicit val UTCDateReads: Reads[UTCDate] = {
+    case JsString(value) =>
+      try {
+        JsSuccess(UTCDate(ZonedDateTime.parse(value, DateTimeFormatter.ISO_DATE_TIME)))
+      } catch {
+        case e: Throwable => JsError(e.getMessage)
+      }
+    case _ => JsError("Expecting js string to represent UTC Date")
+  }
+
+  private implicit val UTCDateWrites: Writes[UTCDate] = date => JsString(DateTimeFormatter.ISO_DATE_TIME.format(date.value))
+  private implicit val fromDateFormat: Format[FromDate] = Json.valueFormat[FromDate]
+  private implicit val toDateFormat: Format[ToDate] = Json.valueFormat[ToDate]
+  private implicit val isEnabledFormat: Format[IsEnabled] = Json.valueFormat[IsEnabled]
+  private implicit val vacationResponsePatchObjectReads: Reads[VacationResponsePatchObject] = {
+    case jsObject: JsObject => JsSuccess(VacationResponsePatchObject(jsObject))
+    case _ => JsError("VacationResponsePatchObject needs to be represented by a JsObject")
+  }
+  private implicit val vacationResponseSetRequestReads: Reads[VacationResponseSetRequest] = Json.reads[VacationResponseSetRequest]
+
   private implicit def notFoundWrites(implicit mailboxIdWrites: Writes[UnparsedMailboxId]): Writes[NotFound] =
     notFound => JsArray(notFound.value.toList.map(mailboxIdWrites.writes))
 
@@ -286,6 +310,12 @@ class Serializer @Inject() (mailboxIdFactory: MailboxId.Factory) {
 
   private implicit val mailboxSetErrorWrites: Writes[SetError] = Json.writes[SetError]
 
+  private implicit val vacationResponseSetUpdateResponseWrites: Writes[VacationResponseUpdateResponse] = Json.valueWrites[VacationResponseUpdateResponse]
+
+  private implicit val vacationResponseSetErrorWrites: Writes[VacationResponseSetError] = Json.writes[VacationResponseSetError]
+
+  private implicit val vacationResponseSetResponseWrites: Writes[VacationResponseSetResponse] = Json.writes[VacationResponseSetResponse]
+
   private implicit def mailboxMapSetErrorForCreationWrites: Writes[Map[MailboxCreationId, SetError]] =
     mapWrites[MailboxCreationId, SetError](_.value, mailboxSetErrorWrites)
   private implicit def mailboxMapSetErrorWrites: Writes[Map[MailboxId, SetError]] =
@@ -384,6 +414,8 @@ class Serializer @Inject() (mailboxIdFactory: MailboxId.Factory) {
   def serialize(vacationResponseGetResponse: VacationResponseGetResponse, properties: Properties): JsValue =
     serialize(vacationResponseGetResponse)(vacationResponseWritesWithFilteredProperties(properties))
 
+  def serialize(vacationResponseSetResponse: VacationResponseSetResponse): JsValue = Json.toJson(vacationResponseSetResponse)
+
   def deserializeRequestObject(input: String): JsResult[RequestObject] = Json.parse(input).validate[RequestObject]
 
   def deserializeRequestObject(input: InputStream): JsResult[RequestObject] = Json.parse(input).validate[RequestObject]
@@ -406,4 +438,5 @@ class Serializer @Inject() (mailboxIdFactory: MailboxId.Factory) {
 
   def deserializeBackReference(input: JsValue): JsResult[BackReference] = Json.fromJson[BackReference](input)
 
+  def deserializeVacationResponseSetRequest(input: JsValue): JsResult[VacationResponseSetRequest] = Json.fromJson[VacationResponseSetRequest](input)
 }
\ No newline at end of file
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/vacation/VacationResponseSet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/vacation/VacationResponseSet.scala
new file mode 100644
index 0000000..d915440
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/vacation/VacationResponseSet.scala
@@ -0,0 +1,113 @@
+/****************************************************************
+ * 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.vacation
+
+import java.time.ZonedDateTime
+import java.time.format.DateTimeFormatter
+
+import org.apache.james.jmap.api.vacation.VacationPatch
+import org.apache.james.jmap.mail.MailboxSetError.{invalidArgumentValue, serverFailValue}
+import org.apache.james.jmap.mail.{SetErrorDescription, SetErrorType}
+import org.apache.james.jmap.model.AccountId
+import org.apache.james.jmap.model.State.State
+import org.apache.james.util.ValuePatch
+import play.api.libs.json.{JsBoolean, JsNull, JsObject, JsString, JsValue}
+
+case class VacationResponseSetRequest(accountId: AccountId,
+                               update: Map[String, VacationResponsePatchObject]) {
+  def parsePatch(): Either[IllegalArgumentException, VacationResponsePatchObject] = {
+    update.map({
+      case (id, patch) => id match {
+        case "singleton" => Right(patch)
+        case _ => Left(new IllegalArgumentException("id must be singleton"))
+      }
+    }).head
+  }
+}
+
+case class VacationResponseId(value: String)
+
+case class IsEnabled(value: Boolean) extends AnyVal
+
+case class UTCDate(value: ZonedDateTime)
+
+case class FromDate(value: UTCDate)
+
+case class ToDate(value: UTCDate)
+
+case class VacationResponsePatchObject(jsObject: JsObject) {
+  def asVacationPatch: Either[IllegalArgumentException, VacationPatch] = {
+    val parsedEntries: Seq[Either[IllegalArgumentException, VacationPatch.Builder]] = jsObject.fields
+      .map(asVacationPatch)
+      .toSeq
+
+    val maybeError: Option[IllegalArgumentException] = parsedEntries.flatMap({
+      case Left(e) => Some(e)
+      case Right(_) => None
+    }).headOption
+
+    val patch: VacationPatch = parsedEntries.flatMap({
+      case Right(builder) => Some(builder)
+      case _ => None
+    })
+      .foldLeft(VacationPatch.builder()) { case (a, b) => a.addAll(b)}
+      .build()
+
+    maybeError.map(e => Left(e))
+      .getOrElse(Right(patch))
+  }
+
+  private def asVacationPatch(entry: (String, JsValue)): Either[IllegalArgumentException, VacationPatch.Builder] = entry match {
+    case ("isEnabled", JsBoolean(bool)) => Right(VacationPatch.builder().isEnabled(bool))
+    case ("isEnabled", JsNull) => Right(VacationPatch.builder().isEnabled(ValuePatch.remove[java.lang.Boolean]()))
+    case ("fromDate", JsString(value)) => parseDate(value).map(date => VacationPatch.builder().fromDate(date))
+    case ("fromDate", JsNull) => Right(VacationPatch.builder().fromDate(ValuePatch.remove[ZonedDateTime]()))
+    case ("toDate", JsString(value)) => parseDate(value).map(date => VacationPatch.builder().toDate(date))
+    case ("toDate", JsNull) => Right(VacationPatch.builder().toDate(ValuePatch.remove[ZonedDateTime]()))
+    case ("subject", JsString(value)) => Right(VacationPatch.builder().subject(value))
+    case ("subject", JsNull) => Right(VacationPatch.builder().subject(ValuePatch.remove[String]()))
+    case ("textBody", JsString(value)) => Right(VacationPatch.builder().textBody(value))
+    case ("textBody", JsNull) => Right(VacationPatch.builder().textBody(ValuePatch.remove[String]()))
+    case ("htmlBody", JsString(value)) => Right(VacationPatch.builder().htmlBody(value))
+    case ("htmlBody", JsNull) => Right(VacationPatch.builder().htmlBody(ValuePatch.remove[String]()))
+    case ("id", _) => Left(new IllegalArgumentException("id is server-set thus cannot be changed"))
+    case (unknownProperty, _) => Left(new IllegalArgumentException(s"$unknownProperty is an unknown property"))
+  }
+
+  private def parseDate(string: String): Either[IllegalArgumentException, ZonedDateTime] = try {
+    Right(ZonedDateTime.parse(string, DateTimeFormatter.ISO_DATE_TIME))
+  } catch {
+    case e: Throwable => Left(new IllegalArgumentException(e))
+  }
+}
+
+case class VacationResponseSetResponse(accountId: AccountId,
+                                       newState: State,
+                                       updated: Option[Map[String, VacationResponseUpdateResponse]],
+                                       notUpdated: Option[Map[String, VacationResponseSetError]])
+
+object VacationResponseSetError {
+  def invalidArgument(description: Option[SetErrorDescription]) = VacationResponseSetError(invalidArgumentValue, description)
+  def serverFail(description: Option[SetErrorDescription]) = VacationResponseSetError(serverFailValue, description)
+}
+
+case class VacationResponseUpdateResponse(value: JsObject)
+
+case class VacationResponseSetError(`type`: SetErrorType, description: Option[SetErrorDescription])


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