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


The following commit(s) were added to refs/heads/master by this push:
     new 0156583  JAMES-2997 MIMEMessageConverter should override blob 
contentType with attachment contentType
0156583 is described below

commit 0156583f44e7b68609bdab8bc97cd84cd40450b0
Author: Benoit Tellier <[email protected]>
AuthorDate: Mon May 18 11:29:11 2020 +0700

    JAMES-2997 MIMEMessageConverter should override blob contentType with 
attachment contentType
---
 .../jmap/draft/methods/MIMEMessageConverter.java   |  14 ++-
 .../draft/methods/MIMEMessageConverterTest.java    | 119 +++++++++++++++++++++
 2 files changed, 131 insertions(+), 2 deletions(-)

diff --git 
a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MIMEMessageConverter.java
 
b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MIMEMessageConverter.java
index 64f9dd3..3396256 100644
--- 
a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MIMEMessageConverter.java
+++ 
b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MIMEMessageConverter.java
@@ -25,6 +25,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.Date;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Optional;
 import java.util.TimeZone;
 import java.util.function.Consumer;
@@ -327,18 +328,27 @@ public class MIMEMessageConverter {
         }
     }
 
-    private ContentTypeField contentTypeField(MessageAttachmentMetadata att) {
+    @VisibleForTesting
+    ContentTypeField contentTypeField(MessageAttachmentMetadata att) {
         ContentTypeField typeAsField =  
att.getAttachment().getType().asMime4J();
         if (att.getName().isPresent()) {
             return Fields.contentType(typeAsField.getMimeType(),
                 ImmutableMap.<String, String>builder()
-                    .putAll(typeAsField.getParameters())
+                    .putAll(parametersWithoutName(typeAsField))
                     .put("name", encode(att.getName().get()))
                     .build());
         }
         return typeAsField;
     }
 
+    private ImmutableMap<String, String> 
parametersWithoutName(ContentTypeField typeAsField) {
+        return typeAsField.getParameters()
+            .entrySet()
+            .stream()
+            .filter(entry -> !entry.getKey().equals("name"))
+            .collect(Guavate.toImmutableMap(Map.Entry::getKey, 
Map.Entry::getValue));
+    }
+
     private String encode(String name) {
         return EncoderUtil.encodeEncodedWord(name, Usage.TEXT_TOKEN);
     }
diff --git 
a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/methods/MIMEMessageConverterTest.java
 
b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/methods/MIMEMessageConverterTest.java
index 6cff89c..5f297b0 100644
--- 
a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/methods/MIMEMessageConverterTest.java
+++ 
b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/methods/MIMEMessageConverterTest.java
@@ -20,6 +20,7 @@
 package org.apache.james.jmap.draft.methods;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -33,6 +34,7 @@ import java.time.ZonedDateTime;
 
 import org.apache.james.core.Username;
 import org.apache.james.jmap.draft.methods.ValueWithId.MessageWithId;
+import org.apache.james.jmap.draft.model.Attachment;
 import org.apache.james.jmap.draft.model.CreationMessage;
 import org.apache.james.jmap.draft.model.CreationMessage.DraftEmailer;
 import org.apache.james.jmap.draft.model.CreationMessageId;
@@ -51,6 +53,7 @@ import org.apache.james.mime4j.dom.Multipart;
 import org.apache.james.mime4j.dom.TextBody;
 import org.apache.james.mime4j.dom.address.Mailbox;
 import org.apache.james.mime4j.dom.field.ContentTypeField;
+import org.apache.james.mime4j.field.Fields;
 import org.apache.james.mime4j.message.BasicBodyFactory;
 import org.apache.james.mime4j.stream.Field;
 import org.assertj.core.data.Index;
@@ -920,6 +923,122 @@ class MIMEMessageConverterTest {
         }
 
         @Test
+        void 
convertToMimeShouldNotThrowWhenNameInContentTypeFieldAndAttachmentMetadata() 
throws Exception {
+            MIMEMessageConverter sut = new 
MIMEMessageConverter(attachmentContentLoader);
+
+            CreationMessage testMessage = CreationMessage.builder()
+                .mailboxIds(ImmutableList.of("dead-bada55"))
+                .subject("subject")
+                .from(DraftEmailer.builder().name("sender").build())
+                .htmlBody("Hello <b>all<b>!")
+                .build();
+
+            String text = "123456";
+            MessageAttachmentMetadata attachment = 
MessageAttachmentMetadata.builder()
+                .name("fgh.png")
+                .attachment(AttachmentMetadata.builder()
+                    .attachmentId(AttachmentId.from("blodId"))
+                    .size(text.getBytes().length)
+                    .type("image/png; name=abc.png")
+                    .build())
+                .cid(Cid.from("cid"))
+                .isInline(false)
+                .build();
+            when(attachmentContentLoader.load(attachment.getAttachment(), 
session))
+                .thenReturn(new ByteArrayInputStream(text.getBytes()));
+
+            assertThatCode(() -> sut.convertToMime(new 
ValueWithId.CreationMessageEntry(
+                    CreationMessageId.of("user|mailbox|1"), testMessage), 
ImmutableList.of(attachment), session))
+                .doesNotThrowAnyException();
+        }
+
+        @Test
+        void attachmentNameShouldBeOverriddenWhenSpecified() throws Exception {
+            MIMEMessageConverter sut = new 
MIMEMessageConverter(attachmentContentLoader);
+
+            String text = "123456";
+            MessageAttachmentMetadata attachment = 
MessageAttachmentMetadata.builder()
+                .name("fgh.png")
+                .attachment(AttachmentMetadata.builder()
+                    .attachmentId(AttachmentId.from("blodId"))
+                    .size(text.getBytes().length)
+                    .type("image/png; name=abc.png; charset=\"iso-8859-1\"")
+                    .build())
+                .cid(Cid.from("cid"))
+                .isInline(false)
+                .build();
+            when(attachmentContentLoader.load(attachment.getAttachment(), 
session))
+                .thenReturn(new ByteArrayInputStream(text.getBytes()));
+
+            assertThat(sut.contentTypeField(attachment).getBody())
+                .isEqualTo("image/png; charset=iso-8859-1; 
name=\"=?US-ASCII?Q?fgh.png?=\"");
+        }
+
+        @Test
+        void nameShouldBeAddedToContentTypeWhenSpecified() throws Exception {
+            MIMEMessageConverter sut = new 
MIMEMessageConverter(attachmentContentLoader);
+
+            String text = "123456";
+            MessageAttachmentMetadata attachment = 
MessageAttachmentMetadata.builder()
+                .name("fgh.png")
+                .attachment(AttachmentMetadata.builder()
+                    .attachmentId(AttachmentId.from("blodId"))
+                    .size(text.getBytes().length)
+                    .type("image/png")
+                    .build())
+                .cid(Cid.from("cid"))
+                .isInline(false)
+                .build();
+            when(attachmentContentLoader.load(attachment.getAttachment(), 
session))
+                .thenReturn(new ByteArrayInputStream(text.getBytes()));
+
+            assertThat(sut.contentTypeField(attachment).getBody())
+                .isEqualTo("image/png; name=\"=?US-ASCII?Q?fgh.png?=\"");
+        }
+
+        @Test
+        void attachmentNameShouldBePreservedWhenNameNotSpecified() throws 
Exception {
+            MIMEMessageConverter sut = new 
MIMEMessageConverter(attachmentContentLoader);
+
+            String text = "123456";
+            MessageAttachmentMetadata attachment = 
MessageAttachmentMetadata.builder()
+                .attachment(AttachmentMetadata.builder()
+                    .attachmentId(AttachmentId.from("blodId"))
+                    .size(text.getBytes().length)
+                    .type("image/png; name=abc.png")
+                    .build())
+                .cid(Cid.from("cid"))
+                .isInline(false)
+                .build();
+            when(attachmentContentLoader.load(attachment.getAttachment(), 
session))
+                .thenReturn(new ByteArrayInputStream(text.getBytes()));
+
+            assertThat(sut.contentTypeField(attachment).getBody())
+                .isEqualTo("image/png; name=abc.png");
+        }
+
+        @Test
+        void attachmentNameShouldBeUnspecifiedWhenNone() throws Exception {
+            MIMEMessageConverter sut = new 
MIMEMessageConverter(attachmentContentLoader);
+
+            String text = "123456";
+            MessageAttachmentMetadata attachment = 
MessageAttachmentMetadata.builder()
+                .attachment(AttachmentMetadata.builder()
+                    .attachmentId(AttachmentId.from("blodId"))
+                    .size(text.getBytes().length)
+                    .type("image/png")
+                    .build())
+                .cid(Cid.from("cid"))
+                .isInline(false)
+                .build();
+            when(attachmentContentLoader.load(attachment.getAttachment(), 
session))
+                .thenReturn(new ByteArrayInputStream(text.getBytes()));
+
+            assertThat(sut.contentTypeField(attachment).getBody())
+                .isEqualTo("image/png");
+        }
+
+        @Test
         void convertToMimeShouldHaveChildMultipartWhenOnlyInline() throws 
Exception {
             MIMEMessageConverter sut = new 
MIMEMessageConverter(attachmentContentLoader);
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to