james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [2/5] james-project git commit: JAMES-1814 Encoding filename in created messages
Date Thu, 22 Sep 2016 07:47:32 GMT
JAMES-1814 Encoding filename in created messages


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/7443396f
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/7443396f
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/7443396f

Branch: refs/heads/master
Commit: 7443396fd824ce7448d2f7045fdecd8c9abe19c7
Parents: 3403ca1
Author: Laura Royet <lroyet@linagora.com>
Authored: Wed Sep 21 12:14:15 2016 +0200
Committer: Laura Royet <lroyet@linagora.com>
Committed: Wed Sep 21 12:14:15 2016 +0200

----------------------------------------------------------------------
 .../jmap/methods/MIMEMessageConverter.java      |  8 ++-
 .../jmap/methods/MIMEMessageConverterTest.java  | 52 +++++++++++++++++++-
 2 files changed, 58 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/7443396f/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/MIMEMessageConverter.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/MIMEMessageConverter.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/MIMEMessageConverter.java
index 886fd4e..4eb54db 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/MIMEMessageConverter.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/MIMEMessageConverter.java
@@ -33,6 +33,8 @@ import org.apache.james.mailbox.model.MessageAttachment;
 import org.apache.james.jmap.model.CreationMessageId;
 import org.apache.james.mime4j.Charsets;
 import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.codec.EncoderUtil;
+import org.apache.james.mime4j.codec.EncoderUtil.Usage;
 import org.apache.james.mime4j.dom.FieldParser;
 import org.apache.james.mime4j.dom.Message;
 import org.apache.james.mime4j.dom.Multipart;
@@ -259,7 +261,7 @@ public class MIMEMessageConverter {
     private ContentTypeField contentTypeField(MessageAttachment att) {
         Builder<String, String> parameters = ImmutableMap.<String, String> builder();
         if (att.getName().isPresent()) {
-            parameters.put("name", att.getName().get());
+            parameters.put("name", encode(att.getName().get()));
         }
         String type = att.getAttachment().getType();
         if (type.contains(FIELD_PARAMETERS_SEPARATOR)) {
@@ -268,6 +270,10 @@ public class MIMEMessageConverter {
         return Fields.contentType(type, parameters.build());
     }
 
+    private String encode(String name) {
+        return EncoderUtil.encodeEncodedWord(name, Usage.TEXT_TOKEN);
+    }
+
     private String contentTypeWithoutParameters(String type) {
         return FluentIterable.from(Splitter.on(FIELD_PARAMETERS_SEPARATOR).split(type)).get(0);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/7443396f/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
index a5f4556..6fbb98d 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
@@ -34,11 +34,14 @@ import org.apache.james.mailbox.model.AttachmentId;
 import org.apache.james.mailbox.model.Cid;
 import org.apache.james.mailbox.model.MessageAttachment;
 import org.apache.james.mime4j.Charsets;
+import org.apache.james.mime4j.codec.EncoderUtil;
+import org.apache.james.mime4j.codec.EncoderUtil.Usage;
 import org.apache.james.mime4j.dom.Entity;
 import org.apache.james.mime4j.dom.Message;
 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.message.BasicBodyFactory;
 import org.apache.james.mime4j.stream.Field;
 import org.junit.Test;
@@ -470,4 +473,51 @@ public class MIMEMessageConverterTest {
         String actual = new String(convert, Charsets.US_ASCII);
         assertThat(actual).contains(expectedEncodedContent);
     }
-}
\ No newline at end of file
+
+    @Test
+    public void convertToMimeShouldAddAttachmentAndContainsIndicationAboutTheWayToEncodeFilenamesAttachmentInTheInputStreamWhenSending()
{
+        // Given
+        MIMEMessageConverter sut = new MIMEMessageConverter();
+
+        CreationMessage testMessage = CreationMessage.builder()
+                .mailboxIds(ImmutableList.of("dead-bada55"))
+                .subject("subject")
+                .from(DraftEmailer.builder().name("sender").build())
+                .htmlBody("Hello <b>all<b>!")
+                .build();
+
+        String expectedCID = "cid";
+        String expectedMimeType = "image/png";
+        String text = "123456";
+        String name = "ديناصور.png";
+        String expectedName = EncoderUtil.encodeEncodedWord(name, Usage.TEXT_TOKEN);
+        MessageAttachment attachment = MessageAttachment.builder()
+                .name(name)
+                .attachment(org.apache.james.mailbox.model.Attachment.builder()
+                    .attachmentId(AttachmentId.from("blodId"))
+                    .bytes(text.getBytes())
+                    .type(expectedMimeType)
+                    .build())
+                .cid(Cid.from(expectedCID))
+                .isInline(true)
+                .build();
+
+        // When
+        Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment));
+
+        // Then
+        assertThat(result.getBody()).isInstanceOf(Multipart.class);
+        assertThat(result.isMultipart()).isTrue();
+        Multipart typedResult = (Multipart)result.getBody();
+        assertThat(typedResult.getBodyParts()).hasSize(2);
+
+        Entity attachmentPart = typedResult.getBodyParts().get(1);
+        String filename = getNameParameterValue(attachmentPart);
+        assertThat(filename).isEqualTo(expectedName);
+    }
+
+    private String getNameParameterValue(Entity attachmentPart) {
+        return ((ContentTypeField) attachmentPart.getHeader().getField("Content-Type")).getParameter("name");
+    }
+}


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


Mime
View raw message