You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2020/09/14 02:53:51 UTC

[james-project] 09/23: JAMES-3369 Email/get to property

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 01669d3159d22b1a187f06828305bad5da03c5dc
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Sep 9 17:44:44 2020 +0700

    JAMES-3369 Email/get to property
---
 .../rfc8621/contract/EmailGetMethodContract.scala  | 100 +++++++++++++++++++++
 .../org/apache/james/jmap/json/Serializer.scala    |   5 +-
 .../scala/org/apache/james/jmap/mail/Email.scala   |   5 +-
 .../org/apache/james/jmap/mail/EmailAddress.scala  |  42 +++++++++
 .../org/apache/james/jmap/mail/EmailGet.scala      |   3 +-
 5 files changed, 151 insertions(+), 4 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailGetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailGetMethodContract.scala
index 23d0d81..6be5b7c 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailGetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailGetMethodContract.scala
@@ -33,6 +33,7 @@ import org.apache.james.mailbox.MessageManager.AppendCommand
 import org.apache.james.mailbox.model.MailboxACL.Right
 import org.apache.james.mailbox.model.{MailboxACL, MailboxPath, MessageId}
 import org.apache.james.mime4j.dom.Message
+import org.apache.james.mime4j.stream.RawField
 import org.apache.james.modules.{ACLProbeImpl, MailboxProbeImpl}
 import org.apache.james.utils.DataProbeImpl
 import org.junit.jupiter.api.{BeforeEach, Test}
@@ -402,6 +403,105 @@ trait EmailGetMethodContract {
   }
 
   @Test
+  def toPropertyShouldBeSupported(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("To",
+        "\"user1\" <us...@domain.tld>, user2@domain.tld"))
+      .setSubject("test")
+      .setBody("testmail", StandardCharsets.UTF_8)
+      .build
+    server.getProbe(classOf[MailboxProbeImpl]).createMailbox(MailboxPath.inbox(BOB))
+    val messageId: MessageId = server.getProbe(classOf[MailboxProbeImpl])
+      .appendMessage(BOB.asString, MailboxPath.inbox(BOB), AppendCommand.from(message))
+      .getMessageId
+
+    val request =
+      s"""{
+         |  "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
+         |  "methodCalls": [[
+         |    "Email/get",
+         |    {
+         |      "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+         |      "ids": ["${messageId.serialize()}"],
+         |      "properties": ["to"]
+         |    },
+         |    "c1"]]
+         |}""".stripMargin
+    val response = `given`
+      .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+      .body(request)
+    .when
+      .post
+    .`then`
+      .statusCode(SC_OK)
+      .contentType(JSON)
+      .extract
+      .body
+      .asString
+
+    assertThatJson(response)
+      .inPath("methodResponses[0][1].list[0]")
+      .isEqualTo(
+      s"""{
+         |    "id": "${messageId.serialize()}",
+         |    "to": [
+         |         {
+         |             "name": "user1",
+         |             "email": "user1@domain.tld"
+         |          },
+         |          {
+         |             "email": "user2@domain.tld"
+         |          }
+         |    ]
+         |}""".stripMargin)
+  }
+
+  @Test
+  def toPropertyShouldBeNullWhenMissing(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .setSubject("test")
+      .setBody("testmail", StandardCharsets.UTF_8)
+      .build
+    server.getProbe(classOf[MailboxProbeImpl]).createMailbox(MailboxPath.inbox(BOB))
+    val messageId: MessageId = server.getProbe(classOf[MailboxProbeImpl])
+      .appendMessage(BOB.asString, MailboxPath.inbox(BOB), AppendCommand.from(message))
+      .getMessageId
+
+    val request =
+      s"""{
+         |  "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
+         |  "methodCalls": [[
+         |    "Email/get",
+         |    {
+         |      "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+         |      "ids": ["${messageId.serialize()}"],
+         |      "properties": ["to"]
+         |    },
+         |    "c1"]]
+         |}""".stripMargin
+    val response = `given`
+      .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+      .body(request)
+    .when
+      .post
+    .`then`
+      .statusCode(SC_OK)
+      .contentType(JSON)
+      .extract
+      .body
+      .asString
+
+    assertThatJson(response)
+      .inPath("methodResponses[0][1].list[0]")
+      .isEqualTo(
+      s"""{
+         |    "id": "${messageId.serialize()}"
+         |}""".stripMargin)
+  }
+
+  @Test
   def messageIdShouldReturnNullWhenNone(server: GuiceJamesServer): Unit = {
     val path = MailboxPath.inbox(BOB)
     server.getProbe(classOf[MailboxProbeImpl]).createMailbox(path)
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 a89b953..ee49b13 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
@@ -30,7 +30,7 @@ import javax.inject.Inject
 import org.apache.james.core.{Domain, Username}
 import org.apache.james.jmap.mail.MailboxSetRequest.{MailboxCreationId, UnparsedMailboxId, UnparsedMailboxIdConstraint}
 import org.apache.james.jmap.mail.VacationResponse.{UnparsedVacationResponseId, VACATION_RESPONSE_ID}
-import org.apache.james.jmap.mail.{BlobId, Charset, DelegatedNamespace, Disposition, Email, EmailBodyPart, EmailBodyValue, EmailGetRequest, EmailGetResponse, EmailHeader, EmailHeaderName, EmailHeaderValue, EmailIds, EmailNotFound, EmailQueryRequest, EmailQueryResponse, FetchAllBodyValues, FetchHTMLBodyValues, FetchTextBodyValues, FromDate, HeaderMessageId, HtmlBody, Ids, IsEnabled, IsEncodingProblem, IsSubscribed, IsTruncated, Language, Limit, Location, Mailbox, MailboxCreationRequest, M [...]
+import org.apache.james.jmap.mail.{Address, BlobId, Charset, DelegatedNamespace, Disposition, Email, EmailAddress, EmailBodyPart, EmailBodyValue, EmailGetRequest, EmailGetResponse, EmailHeader, EmailHeaderName, EmailHeaderValue, EmailIds, EmailNotFound, EmailQueryRequest, EmailQueryResponse, EmailerName, FetchAllBodyValues, FetchHTMLBodyValues, FetchTextBodyValues, FromDate, HeaderMessageId, HtmlBody, Ids, IsEnabled, IsEncodingProblem, IsSubscribed, IsTruncated, Language, Limit, Location [...]
 import org.apache.james.jmap.model
 import org.apache.james.jmap.model.CapabilityIdentifier.CapabilityIdentifier
 import org.apache.james.jmap.model.Invocation.{Arguments, MethodCallId, MethodName}
@@ -256,6 +256,9 @@ class Serializer @Inject() (mailboxIdFactory: MailboxId.Factory) {
   private implicit val dispositionWrites: Writes[Disposition] = Json.valueWrites[Disposition]
   private implicit val languageWrites: Writes[Language] = Json.valueWrites[Language]
   private implicit val locationWrites: Writes[Location] = Json.valueWrites[Location]
+  private implicit val emailerNameWrites: Writes[EmailerName] = Json.valueWrites[EmailerName]
+  private implicit val addressWrites: Writes[Address] = Json.valueWrites[Address]
+  private implicit val emailAddressWrites: Writes[EmailAddress] = Json.writes[EmailAddress]
   private implicit val headerMessageIdWrites: Writes[HeaderMessageId] = Json.valueWrites[HeaderMessageId]
   private implicit val isEncodingProblemWrites: Writes[IsEncodingProblem] = Json.valueWrites[IsEncodingProblem]
   private implicit val isTruncatedWrites: Writes[IsTruncated] = Json.valueWrites[IsTruncated]
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/Email.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/Email.scala
index 46c3c4e..9b36cde 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/Email.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/Email.scala
@@ -40,7 +40,7 @@ object Email {
 
   val defaultProperties: Properties = Properties("id", "size")
   val allowedProperties: Properties = Properties("id", "size", "bodyStructure", "textBody", "htmlBody",
-    "attachments", "headers", "bodyValues", "messageId", "inReplyTo", "references")
+    "attachments", "headers", "bodyValues", "messageId", "inReplyTo", "references", "to")
   val idProperty: Properties = Properties("id")
 
   def asUnparsed(messageId: MessageId): Try[UnparsedEmailId] =
@@ -84,4 +84,5 @@ case class Email(id: MessageId,
                  bodyValues: Map[PartId, EmailBodyValue],
                  messageId: Option[List[HeaderMessageId]],
                  inReplyTo: Option[List[HeaderMessageId]],
-                 references: Option[List[HeaderMessageId]])
+                 references: Option[List[HeaderMessageId]],
+                 to: Option[List[EmailAddress]])
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailAddress.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailAddress.scala
new file mode 100644
index 0000000..967fab3
--- /dev/null
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailAddress.scala
@@ -0,0 +1,42 @@
+/****************************************************************
+ * 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.mail
+
+import org.apache.james.mime4j.dom.address.{AddressList, Mailbox => Mime4jMailbox}
+
+import scala.jdk.CollectionConverters._
+
+case class EmailerName(value: String) extends AnyVal
+case class Address(value: String) extends AnyVal
+
+object EmailAddress {
+  def from(addressList: AddressList): List[EmailAddress] =
+    addressList.flatten()
+      .asScala
+      .toList
+      .map(from)
+
+  def from(mailbox: Mime4jMailbox): EmailAddress =
+    EmailAddress(
+      name = Option(mailbox.getName).map(EmailerName),
+      email = Address(mailbox.getAddress))
+}
+
+case class EmailAddress(name: Option[EmailerName], email: Address)
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailGet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailGet.scala
index 3c2794f..cc4ecbf 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailGet.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailGet.scala
@@ -84,7 +84,8 @@ case class EmailGetRequest(accountId: AccountId,
         bodyValues = bodyValues,
         messageId = extractMessageId(mime4JMessage, "Message-Id"),
         inReplyTo = extractMessageId(mime4JMessage, "In-Reply-To"),
-        references = extractMessageId(mime4JMessage, "References"))
+        references = extractMessageId(mime4JMessage, "References"),
+        to = Option(mime4JMessage.getTo).map(EmailAddress.from))
     }
   }
 


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