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:54:04 UTC

[james-project] 22/23: JAMES-3369 Return last header value for convenience headers

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 9ba269f7d44a384ac121591548c0d5e08e638512
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Sep 11 08:49:27 2020 +0700

    JAMES-3369 Return last header value for convenience headers
---
 .../rfc8621/contract/EmailGetMethodContract.scala  | 368 ++++++++++++++++++++-
 .../org/apache/james/jmap/mail/EmailGet.scala      |  42 ++-
 2 files changed, 397 insertions(+), 13 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 f6a691e..3b5530e 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
@@ -460,7 +460,7 @@ trait EmailGetMethodContract {
   }
 
   @Test
-  def convenienceHeaderShouldOnlyTakeFirstHeaderIntoAccount(server: GuiceJamesServer): Unit = {
+  def toPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
     val message: Message = Message.Builder
       .of
       .addField(new RawField("To",
@@ -505,15 +505,326 @@ trait EmailGetMethodContract {
       s"""{
          |    "id": "${messageId.serialize()}",
          |    "to": [
-         |         {
-         |             "name": "user1",
-         |             "email": "user1@domain.tld"
+         |          {
+         |             "email": "user2@domain.tld"
          |          }
          |    ]
          |}""".stripMargin)
   }
 
   @Test
+  def fromPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("From",
+        "\"user1\" <us...@domain.tld>"))
+      .addField(new RawField("From",
+        "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": ["from"]
+         |    },
+         |    "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()}",
+         |    "from": [
+         |          {
+         |             "email": "user2@domain.tld"
+         |          }
+         |    ]
+         |}""".stripMargin)
+  }
+
+  @Test
+  def ccPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("Cc",
+        "\"user1\" <us...@domain.tld>"))
+      .addField(new RawField("Cc",
+        "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": ["cc"]
+         |    },
+         |    "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()}",
+         |    "cc": [
+         |          {
+         |             "email": "user2@domain.tld"
+         |          }
+         |    ]
+         |}""".stripMargin)
+  }
+
+  @Test
+  def bccPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("Bcc",
+        "\"user1\" <us...@domain.tld>"))
+      .addField(new RawField("Bcc",
+        "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": ["bcc"]
+         |    },
+         |    "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()}",
+         |    "bcc": [
+         |          {
+         |             "email": "user2@domain.tld"
+         |          }
+         |    ]
+         |}""".stripMargin)
+  }
+
+  @Test
+  def senderPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("Sender",
+        "\"user1\" <us...@domain.tld>"))
+      .addField(new RawField("Sender",
+        "user2@domain.tld"))
+      .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": ["sender"]
+         |    },
+         |    "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()}",
+         |    "sender": [
+         |          {
+         |             "email": "user2@domain.tld"
+         |          }
+         |    ]
+         |}""".stripMargin)
+  }
+
+  @Test
+  def replyToPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("Reply-To",
+        "\"user1\" <us...@domain.tld>"))
+      .addField(new RawField("Reply-To",
+        "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": ["replyTo"]
+         |    },
+         |    "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()}",
+         |    "replyTo": [
+         |          {
+         |             "email": "user2@domain.tld"
+         |          }
+         |    ]
+         |}""".stripMargin)
+  }
+
+  @Test
+  def subjectPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("Subject",
+        "Ga Bou"))
+      .addField(new RawField("Subject",
+        "Zo Meuh"))
+      .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": ["subject"]
+         |    },
+         |    "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()}",
+         |    "subject": "Zo Meuh"
+         |}""".stripMargin)
+  }
+
+  @Test
   def toPropertyShouldBeNullWhenMissing(server: GuiceJamesServer): Unit = {
     val message: Message = Message.Builder
       .of
@@ -1089,6 +1400,55 @@ trait EmailGetMethodContract {
   }
 
   @Test
+  def sentAtPropertyShouldReturnLastWhenMultipleFields(server: GuiceJamesServer): Unit = {
+    val message: Message = Message.Builder
+      .of
+      .addField(new RawField("Date",
+        "Wed, 9 Sep 2014 07:00:26 +0200"))
+      .addField(new RawField("Date",
+        "Wed, 9 Sep 2020 07:00:26 +0200"))
+      .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": ["sentAt"]
+         |    },
+         |    "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()}",
+         |    "sentAt": "2020-09-09T05:00:26Z"
+         |}""".stripMargin)
+  }
+
+  @Test
   def sentAtPropertyShouldBeNullWhenMissing(server: GuiceJamesServer): Unit = {
     val message: Message = Message.Builder
       .of
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 f24908a..6315b1f 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
@@ -21,6 +21,7 @@ package org.apache.james.jmap.mail
 
 import java.nio.charset.StandardCharsets.US_ASCII
 import java.time.ZoneId
+import java.util.Date
 
 import cats.implicits._
 import eu.timepit.refined.api.Refined
@@ -32,9 +33,10 @@ import org.apache.james.jmap.model.State.State
 import org.apache.james.jmap.model.{AccountId, Properties, UTCDate}
 import org.apache.james.mailbox.model.{MessageId, MessageResult}
 import org.apache.james.mime4j.codec.DecodeMonitor
+import org.apache.james.mime4j.dom.field.{AddressListField, DateTimeField, MailboxField, MailboxListField}
 import org.apache.james.mime4j.dom.{Header, Message}
 import org.apache.james.mime4j.message.DefaultMessageBuilder
-import org.apache.james.mime4j.stream.MimeConfig
+import org.apache.james.mime4j.stream.{Field, MimeConfig}
 
 import scala.jdk.CollectionConverters._
 import scala.util.{Failure, Success, Try}
@@ -90,14 +92,14 @@ case class EmailGetRequest(accountId: AccountId,
           messageId = extractMessageId(mime4JMessage, "Message-Id"),
           inReplyTo = extractMessageId(mime4JMessage, "In-Reply-To"),
           references = extractMessageId(mime4JMessage, "References"),
-          to = Option(mime4JMessage.getTo).map(EmailAddress.from),
-          cc = Option(mime4JMessage.getCc).map(EmailAddress.from),
-          bcc = Option(mime4JMessage.getBcc).map(EmailAddress.from),
-          from = Option(mime4JMessage.getFrom).map(EmailAddress.from),
-          replyTo = Option(mime4JMessage.getReplyTo).map(EmailAddress.from),
-          sender = Option(mime4JMessage.getSender).map(EmailAddress.from).map(List(_)),
-          subject = Option(mime4JMessage.getSubject).map(Subject),
-          sentAt = Option(mime4JMessage.getDate).map(date => UTCDate.from(date, zoneId))),
+          to = extractAddresses(mime4JMessage, "To"),
+          cc = extractAddresses(mime4JMessage, "Cc"),
+          bcc = extractAddresses(mime4JMessage, "Bcc"),
+          from = extractAddresses(mime4JMessage, "From"),
+          replyTo = extractAddresses(mime4JMessage, "Reply-To"),
+          sender = extractAddresses(mime4JMessage, "Sender"),
+          subject = extractLastField(mime4JMessage, "Subject").map(_.getBody).map(Subject),
+          sentAt = extractDate(mime4JMessage, "Date").map(date => UTCDate.from(date, zoneId))),
         body = EmailBody(
           bodyStructure = bodyStructure,
           textBody = bodyStructure.textBody,
@@ -115,6 +117,28 @@ case class EmailGetRequest(accountId: AccountId,
         .toList)
       .filter(_.nonEmpty)
 
+  private def extractAddresses(mime4JMessage: Message, fieldName: String): Option[List[EmailAddress]] =
+    extractLastField(mime4JMessage, fieldName)
+      .flatMap {
+        case f: AddressListField => Some(EmailAddress.from(f.getAddressList))
+        case f: MailboxListField => Some(EmailAddress.from(f.getMailboxList))
+        case f: MailboxField => Some(List(EmailAddress.from(f.getMailbox)))
+        case _ => None
+      }
+      .filter(_.nonEmpty)
+
+  private def extractDate(mime4JMessage: Message, fieldName: String): Option[Date] =
+    extractLastField(mime4JMessage, fieldName)
+      .flatMap {
+        case f: DateTimeField => Some(f.getDate)
+        case _ => None
+      }
+
+  private def extractLastField(mime4JMessage: Message, fieldName: String): Option[Field] =
+    Option(mime4JMessage.getHeader.getFields(fieldName))
+      .map(_.asScala)
+      .flatMap(fields => fields.reverse.headOption)
+
   def extractBodyValues(bodyStructure: EmailBodyPart): Try[Map[PartId, EmailBodyValue]] = for {
     textBodyValues <- extractBodyValues(bodyStructure.textBody, fetchTextBodyValues.exists(_.value))
     htmlBodyValues <- extractBodyValues(bodyStructure.htmlBody, fetchHTMLBodyValues.exists(_.value))


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