james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [4/6] james-project git commit: JAMES-1900 Headers should be decoded and unfolded
Date Tue, 06 Jun 2017 02:04:27 GMT
JAMES-1900 Headers should be decoded and unfolded


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

Branch: refs/heads/master
Commit: bf5a68c0d7b7ea2851cd556dd57d88008d9f8628
Parents: 4254ac9
Author: benwa <btellier@linagora.com>
Authored: Fri May 5 10:32:01 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Mon Jun 5 20:24:51 2017 +0700

----------------------------------------------------------------------
 .../apache/james/jmap/model/MessageFactory.java | 13 +++++
 .../james/jmap/model/MessageFactoryTest.java    | 50 ++++++++++++++++++++
 2 files changed, 63 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/bf5a68c0/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageFactory.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageFactory.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageFactory.java
index cbcdbcd..e0eb1c9 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageFactory.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageFactory.java
@@ -21,6 +21,7 @@ package org.apache.james.jmap.model;
 import java.io.IOException;
 import java.io.InputStream;
 import java.time.Instant;
+import java.time.ZoneId;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -43,11 +44,14 @@ import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MessageAttachment;
 import org.apache.james.mailbox.model.MessageId;
 import org.apache.james.mailbox.model.MessageResult;
+import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.codec.DecoderUtil;
 import org.apache.james.mime4j.dom.address.AddressList;
 import org.apache.james.mime4j.dom.address.Mailbox;
 import org.apache.james.mime4j.dom.address.MailboxList;
 import org.apache.james.mime4j.stream.Field;
 import org.apache.james.mime4j.stream.MimeConfig;
+import org.apache.james.mime4j.util.MimeUtil;
 import org.apache.james.util.mime.MessageContentExtractor;
 import org.apache.james.util.mime.MessageContentExtractor.MessageContent;
 
@@ -69,6 +73,14 @@ public class MessageFactory {
         .setMaxLineLen(-1)
         .build();
 
+    private static String sanitizeHeaderField(String headerValue) {
+        return DecoderUtil.decodeEncodedWords(
+            MimeUtil.unfold(headerValue),
+            DecodeMonitor.SILENT);
+    }
+
+    private static final ZoneId UTC_ZONE_ID = ZoneId.of("Z");
+
     private final MessagePreviewGenerator messagePreview;
     private final MessageContentExtractor messageContentExtractor;
     private final HtmlTextExtractor htmlTextExtractor;
@@ -192,6 +204,7 @@ public class MessageFactory {
         Function<Entry<String, Collection<Field>>, String> bodyConcatenator
= fieldListEntry -> fieldListEntry.getValue()
                 .stream()
                 .map(Field::getBody)
+                .map(MessageFactory::sanitizeHeaderField)
                 .collect(Collectors.toList())
                 .stream()
                 .collect(Collectors.joining(","));

http://git-wip-us.apache.org/repos/asf/james-project/blob/bf5a68c0/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
index 76e3cd5..5076774 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
@@ -169,6 +169,56 @@ public class MessageFactoryTest {
     }
 
     @Test
+    public void headersShouldBeUnfoldedAndDecoded() throws Exception {
+        String headers = "From: user <user@domain>\n"
+            + "Subject: test subject\n"
+            + "To: user1 <user1@domain>,\r\n"
+            + " user2 <user2@domain>\n"
+            + "Cc: =?UTF-8?Q?Beno=c3=aet_TELLIER?= <tellier@linagora.com>";
+        MetaDataWithContent testMail = MetaDataWithContent.builder()
+            .uid(MessageUid.of(2))
+            .flags(new Flags(Flag.SEEN))
+            .size(headers.length())
+            .internalDate(INTERNAL_DATE)
+            .content(new ByteArrayInputStream(headers.getBytes(Charsets.UTF_8)))
+            .attachments(ImmutableList.of())
+            .mailboxId(MAILBOX_ID)
+            .messageId(TestMessageId.of(2))
+            .build();
+
+        Emailer user = Emailer.builder().name("user").email("user@domain").build();
+        Emailer user1 = Emailer.builder().name("user1").email("user1@domain").build();
+        Emailer user2 = Emailer.builder().name("user2").email("user2@domain").build();
+        Emailer usercc = Emailer.builder().name("Benoît TELLIER").email("tellier@linagora.com").build();
+        ImmutableMap<String, String> headersMap = ImmutableMap.<String, String>builder()
+            .put("Cc", "Benoît TELLIER <tellier@linagora.com>")
+            .put("Subject", "test subject")
+            .put("From", "user <user@domain>")
+            .put("To", "user1 <user1@domain>, user2 <user2@domain>")
+            .put("Date", "Tue, 14 Jul 2015 12:30:42 +0000")
+            .put("MIME-Version", "1.0")
+            .build();
+        Message testee = messageFactory.fromMetaDataWithContent(testMail);
+        Message expected = Message.builder()
+            .id(TestMessageId.of(2))
+            .blobId(BlobId.of("2"))
+            .threadId("2")
+            .mailboxId(MAILBOX_ID)
+            .headers(headersMap)
+            .from(user)
+            .to(ImmutableList.of(user1, user2))
+            .cc(ImmutableList.of(usercc))
+            .subject("test subject")
+            .date(ZONED_DATE)
+            .size(headers.length())
+            .preview("(Empty)")
+            .textBody(Optional.of(""))
+            .htmlBody(Optional.empty())
+            .build();
+        assertThat(testee).isEqualToComparingFieldByField(expected);
+    }
+
+    @Test
     public void textBodyShouldBeSetIntoMessage() throws Exception {
         String headers = "Subject: test subject\n";
         String body = "Mail body";


---------------------------------------------------------------------
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