james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [2/2] james-project git commit: JAMES-2166 Generate Message-Id header while creating a message via JMAP
Date Mon, 02 Oct 2017 01:59:45 GMT
JAMES-2166 Generate Message-Id header while creating a message via JMAP


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

Branch: refs/heads/master
Commit: f458495a0f6a33a567267ad5dd8ee61fa1c7a7a0
Parents: 8d74d43
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Fri Sep 29 15:44:09 2017 +0200
Committer: benwa <btellier@linagora.com>
Committed: Mon Oct 2 08:58:53 2017 +0700

----------------------------------------------------------------------
 .../jmap/methods/MIMEMessageConverter.java      | 15 +++--
 .../jmap/methods/MIMEMessageConverterTest.java  | 62 +++++++++++++++++++-
 2 files changed, 72 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/f458495a/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 cf6ce7e..0a079e3 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
@@ -31,7 +31,6 @@ import java.util.stream.Collectors;
 
 import org.apache.james.jmap.model.CreationMessage;
 import org.apache.james.jmap.model.CreationMessage.DraftEmailer;
-import org.apache.james.jmap.model.CreationMessageId;
 import org.apache.james.jmap.model.MessageFactory;
 import org.apache.james.mailbox.model.MessageAttachment;
 import org.apache.james.mime4j.Charsets;
@@ -56,6 +55,7 @@ import org.apache.james.mime4j.message.DefaultMessageWriter;
 import org.apache.james.mime4j.message.MultipartBuilder;
 import org.apache.james.mime4j.stream.NameValuePair;
 import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.util.MimeUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -130,11 +130,11 @@ public class MIMEMessageConverter {
             messageBuilder.setBody(createTextBody(creationMessageEntry.getValue()))
                 .setContentTransferEncoding(QUOTED_PRINTABLE);
         }
-        buildMimeHeaders(messageBuilder, creationMessageEntry.getCreationId(), creationMessageEntry.getValue(),
messageAttachments);
+        buildMimeHeaders(messageBuilder, creationMessageEntry.getValue(), messageAttachments);
         return messageBuilder.build();
     }
 
-    private void buildMimeHeaders(Message.Builder messageBuilder, CreationMessageId creationId,
CreationMessage newMessage, ImmutableList<MessageAttachment> messageAttachments) {
+    private void buildMimeHeaders(Message.Builder messageBuilder, CreationMessage newMessage,
ImmutableList<MessageAttachment> messageAttachments) {
         Optional<Mailbox> fromAddress = newMessage.getFrom().filter(DraftEmailer::hasValidEmail).map(this::convertEmailToMimeHeader);
         fromAddress.ifPresent(messageBuilder::setFrom);
         fromAddress.ifPresent(messageBuilder::setSender);
@@ -155,7 +155,7 @@ public class MIMEMessageConverter {
                 .map(this::convertEmailToMimeHeader)
                 .collect(Collectors.toList()));
         messageBuilder.setSubject(newMessage.getSubject());
-        messageBuilder.setMessageId(creationId.getId());
+        messageBuilder.setMessageId(generateUniqueMessageId(fromAddress));
 
         // note that date conversion probably lose milliseconds!
         messageBuilder.setDate(Date.from(newMessage.getDate().toInstant()), TimeZone.getTimeZone(newMessage.getDate().getZone()));
@@ -170,6 +170,13 @@ public class MIMEMessageConverter {
             .forEach(header -> addMultivaluedHeader(messageBuilder, header.getKey(), header.getValue()));
     }
 
+    private String generateUniqueMessageId(Optional<Mailbox> fromAddress) {
+        String noDomain = null;
+        return MimeUtil.createUniqueMessageId(fromAddress
+            .map(Mailbox::getDomain)
+            .orElse(noDomain));
+    }
+
     private void addMultivaluedHeader(Message.Builder messageBuilder, String fieldName, String
multipleValues) {
         Splitter.on(MessageFactory.JMAP_MULTIVALUED_FIELD_DELIMITER).split(multipleValues)
             .forEach(value -> addHeader(messageBuilder, fieldName, value));

http://git-wip-us.apache.org/repos/asf/james-project/blob/f458495a/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 167408b..43bd544 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
@@ -73,6 +73,66 @@ public class MIMEMessageConverterTest {
     }
 
     @Test
+    public void convertToMimeShouldGenerateMessageId() {
+        // Given
+        MIMEMessageConverter sut = new MIMEMessageConverter();
+
+        CreationMessage message = CreationMessage.builder()
+                .mailboxIds(ImmutableList.of("dead-beef-1337"))
+                .subject("subject")
+                .build();
+
+        // When
+        Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
+                CreationMessageId.of("user|mailbox|1"), message), ImmutableList.of());
+
+        // Then
+        assertThat(result.getHeader().getFields("Message-ID")).extracting(Field::getBody)
+                .isNotNull();
+    }
+
+    @Test
+    public void convertToMimeShouldGenerateMessageIdWhenSenderWithoutDomain() {
+        // Given
+        MIMEMessageConverter sut = new MIMEMessageConverter();
+
+        CreationMessage message = CreationMessage.builder()
+                .from(DraftEmailer.builder().email("sender").build())
+                .mailboxIds(ImmutableList.of("dead-beef-1337"))
+                .subject("subject")
+                .build();
+
+        // When
+        Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
+                CreationMessageId.of("user|mailbox|1"), message), ImmutableList.of());
+
+        // Then
+        assertThat(result.getHeader().getFields("Message-ID")).extracting(Field::getBody)
+                .isNotNull();
+    }
+
+    @Test
+    public void convertToMimeShouldGenerateMessageIdContainingSenderDomain() {
+        // Given
+        MIMEMessageConverter sut = new MIMEMessageConverter();
+
+        CreationMessage message = CreationMessage.builder()
+                .from(DraftEmailer.builder().email("email@domain.com").build())
+                .mailboxIds(ImmutableList.of("dead-beef-1337"))
+                .subject("subject")
+                .build();
+
+        // When
+        Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
+                CreationMessageId.of("user|mailbox|1"), message), ImmutableList.of());
+
+        // Then
+        assertThat(result.getHeader().getFields("Message-ID")).hasSize(1);
+        assertThat(result.getHeader().getFields("Message-ID").get(0).getBody())
+            .contains("@domain.com");
+    }
+
+    @Test
     public void convertToMimeShouldAddHeaderWhenProvided() {
         // Given
         MIMEMessageConverter sut = new MIMEMessageConverter();
@@ -406,7 +466,7 @@ public class MIMEMessageConverterTest {
 
         String expectedHeaders = "MIME-Version: 1.0\r\n" +
                 "Content-Type: multipart/alternative;\r\n" +
-                " boundary=\"-=Part.1.";
+                " boundary=\"-=Part.";
         String expectedPart1 = "Content-Type: text/plain; charset=UTF-8\r\n" +
                 "Content-Transfer-Encoding: quoted-printable\r\n" +
                 "\r\n" +


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