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 2024/03/05 04:28:21 UTC

(james-project) branch master updated: [IMPROVEMENT] Allow Email/set create to override attachment charset

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


The following commit(s) were added to refs/heads/master by this push:
     new d6218c20ec [IMPROVEMENT] Allow Email/set create to override attachment charset
d6218c20ec is described below

commit d6218c20eca1f6684c4ca25dbbf17b2b75eccaae
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Mon Mar 4 08:35:04 2024 +0100

    [IMPROVEMENT] Allow Email/set create to override attachment charset
---
 .../rfc8621/contract/EmailSetMethodContract.scala  | 127 +++++++++++++++++++++
 .../org/apache/james/jmap/mail/EmailSet.scala      |  24 ++--
 2 files changed, 139 insertions(+), 12 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/EmailSetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSetMethodContract.scala
index 25163a8e8c..248429a36c 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSetMethodContract.scala
@@ -2322,6 +2322,133 @@ trait EmailSetMethodContract {
       .hasSameContentAs(new ByteArrayInputStream(payload))
   }
 
+  @Test
+  def createShouldSupportCustomCharsetInAttachment(server: GuiceJamesServer): Unit = {
+    val bobPath = MailboxPath.inbox(BOB)
+    val mailboxId = server.getProbe(classOf[MailboxProbeImpl]).createMailbox(bobPath)
+    val payload = "123456789\r\n".getBytes(StandardCharsets.UTF_8)
+
+    val uploadResponse: String = `given`
+      .basePath("")
+      .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+      .contentType("text/plain")
+      .body(payload)
+    .when
+      .post(s"/upload/$ACCOUNT_ID")
+    .`then`
+      .statusCode(SC_CREATED)
+      .extract
+      .body
+      .asString
+
+    val blobId: String = Json.parse(uploadResponse).\("blobId").get.asInstanceOf[JsString].value
+
+    val request =
+      s"""{
+         |  "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
+         |  "methodCalls": [
+         |    ["Email/set", {
+         |      "accountId": "$ACCOUNT_ID",
+         |      "create": {
+         |        "aaaaaa": {
+         |          "mailboxIds": {
+         |             "${mailboxId.serialize}": true
+         |          },
+         |          "subject": "World domination",
+         |          "attachments": [
+         |            {
+         |              "blobId": "$blobId",
+         |              "type":"text/plain",
+         |              "charset":"ascii",
+         |              "disposition": "attachment"
+         |            }
+         |          ]
+         |        }
+         |      }
+         |    }, "c1"],
+         |    ["Email/get",
+         |      {
+         |        "accountId": "$ACCOUNT_ID",
+         |        "ids": ["#aaaaaa"],
+         |        "properties": ["mailboxIds", "subject", "attachments"],
+         |        "bodyProperties": ["partId", "blobId", "size", "name", "type", "charset", "disposition", "cid"]
+         |      },
+         |    "c2"]
+         |  ]
+         |}""".stripMargin
+
+    val response = `given`
+      .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+      .body(request)
+    .when
+      .post
+    .`then`
+      .statusCode(SC_OK)
+      .contentType(JSON)
+      .extract
+      .body
+      .asString
+
+    val responseAsJson = Json.parse(response)
+      .\("methodResponses")
+      .\(0).\(1)
+      .\("created")
+      .\("aaaaaa")
+
+    val messageId = responseAsJson
+      .\("id")
+      .get.asInstanceOf[JsString].value
+    val size = responseAsJson
+      .\("size")
+      .get.asInstanceOf[JsNumber].value
+
+    assertThatJson(response)
+      .inPath("methodResponses[0][1].created.aaaaaa")
+      .isEqualTo(
+        s"""{
+           | "id": "$messageId",
+           | "blobId": "$messageId",
+           | "threadId": "$messageId",
+           | "size": $size
+           |}""".stripMargin)
+
+    assertThatJson(response)
+      .whenIgnoringPaths("methodResponses[1][1].list[0].id")
+      .inPath(s"methodResponses[1][1].list")
+      .isEqualTo(
+        s"""[{
+           |  "mailboxIds": {
+           |    "${mailboxId.serialize}": true
+           |  },
+           |  "subject": "World domination",
+           |  "attachments": [
+           |    {
+           |      "partId": "4",
+           |      "blobId": "${messageId}_4",
+           |      "size": 11,
+           |      "type": "text/plain",
+           |      "charset": "ascii",
+           |      "disposition": "attachment"
+           |    }
+           |  ]
+           |}]""".stripMargin)
+
+    val downloadResponse = `given`
+      .basePath("")
+      .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+    .when
+      .get(s"/download/$accountId/${messageId}_4")
+    .`then`
+      .statusCode(SC_OK)
+      .contentType("text/plain")
+      .extract
+      .body
+      .asInputStream()
+
+    assertThat(downloadResponse)
+      .hasSameContentAs(new ByteArrayInputStream(payload))
+  }
+
   @Test
   def rejectAttahmentCreationRequestWithContentTransferEncoding(server: GuiceJamesServer): Unit = {
     val bobPath = MailboxPath.inbox(BOB)
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
index cb919180db..5a018ceccf 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
@@ -335,22 +335,22 @@ case class EmailCreationRequest(mailboxIds: MailboxIds,
 
   private def contentTypeField(attachment: Attachment, blob: Blob): ContentTypeField = {
     val typeAsField: ContentTypeField = blob.contentType.asMime4J
-    if (attachment.name.isDefined) {
-      Fields.contentType(typeAsField.getMimeType,
-        Map.newBuilder[String, String]
-          .addAll(parametersWithoutName(typeAsField))
-          .addOne("name", EncoderUtil.encodeEncodedWord(attachment.name.get.value, Usage.TEXT_TOKEN))
-          .result
-          .asJava)
-    } else {
-      typeAsField
-    }
+    val parameterBuilder = Map.newBuilder[String, String]
+      .addAll(parametersWithoutNameAndCharset(typeAsField))
+
+    attachment.name.foreach(name => parameterBuilder.addOne("name", EncoderUtil.encodeEncodedWord(name.value, Usage.TEXT_TOKEN)))
+    attachment.charset.map(c => c.value).orElse(Option(typeAsField.getCharset))
+      .foreach(charset => parameterBuilder.addOne("charset", charset))
+
+    Fields.contentType(typeAsField.getMimeType,
+      parameterBuilder.result.asJava)
   }
 
-  private def parametersWithoutName(typeAsField: ContentTypeField): Map[String, String] =
+  private def parametersWithoutNameAndCharset(typeAsField: ContentTypeField): Map[String, String] =
     typeAsField.getParameters
       .asScala
-      .filter(!_._1.equals("name"))
+      .filter(!_._1.equalsIgnoreCase("name"))
+      .filter(!_._1.equalsIgnoreCase("charset"))
       .toMap
 
   def validateHtmlBody: Either[IllegalArgumentException, Option[ClientBodyPart]] = htmlBody match {


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