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

commit 0e8c9803e329861f9b0dcdebb1eb9806c2322c70
Author: Benoit Tellier <btell...@linagora.com>
AuthorDate: Wed Apr 22 19:03:31 2020 +0700

    JAMES-2997 MessageParser should preserve the full ContentType header
---
 .../store/mail/model/impl/MessageParser.java       | 26 +----------
 .../store/mail/model/impl/MessageParserTest.java   | 10 +++--
 .../methods/integration/SetMessagesMethodTest.java |  4 +-
 .../test/resources/cucumber/GetMessages.feature    | 52 +++++++++++-----------
 4 files changed, 36 insertions(+), 56 deletions(-)

diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
index 53649fb..b8fc5ba 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/impl/MessageParser.java
@@ -19,14 +19,11 @@
 
 package org.apache.james.mailbox.store.mail.model.impl;
 
-import static org.apache.james.mime4j.dom.field.ContentTypeField.PARAM_CHARSET;
-
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 import java.util.Locale;
-import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Stream;
 
@@ -41,7 +38,6 @@ import 
org.apache.james.mime4j.dom.field.ContentDispositionField;
 import org.apache.james.mime4j.dom.field.ContentIdField;
 import org.apache.james.mime4j.dom.field.ContentTypeField;
 import org.apache.james.mime4j.dom.field.ParsedField;
-import org.apache.james.mime4j.field.Fields;
 import org.apache.james.mime4j.message.DefaultMessageBuilder;
 import org.apache.james.mime4j.message.DefaultMessageWriter;
 import org.apache.james.mime4j.stream.Field;
@@ -126,7 +122,8 @@ public class MessageParser {
     private ParsedAttachment retrieveAttachment(Entity entity) throws 
IOException {
         Optional<ContentTypeField> contentTypeField = 
getContentTypeField(entity);
         Optional<ContentDispositionField> contentDispositionField = 
getContentDispositionField(entity);
-        Optional<String> contentType = contentType(contentTypeField);
+        Optional<String> contentType = 
contentTypeField.map(ContentTypeField::getBody)
+            .filter(string -> !string.isEmpty());
         Optional<String> name = name(contentTypeField, 
contentDispositionField);
         Optional<Cid> cid = cid(readHeader(entity, CONTENT_ID, 
ContentIdField.class));
         boolean isInline = isInline(readHeader(entity, CONTENT_DISPOSITION, 
ContentDispositionField.class)) && cid.isPresent();
@@ -159,25 +156,6 @@ public class MessageParser {
         return Optional.of((U) field);
     }
 
-    private Optional<String> contentType(Optional<ContentTypeField> 
contentTypeField) {
-        return contentTypeField.map(this::contentTypePreserveCharset);
-    }
-
-    private String contentTypePreserveCharset(ContentTypeField 
contentTypeField) {
-        Map<String, String> params = contentTypeField.getParameters()
-            .entrySet()
-            .stream()
-            .filter(param -> param.getKey().equals(PARAM_CHARSET))
-            .collect(Guavate.toImmutableMap(Map.Entry::getKey, 
Map.Entry::getValue));
-
-        try {
-            return Fields.contentType(contentTypeField.getMimeType(), params)
-                .getBody();
-        } catch (IllegalArgumentException e) {
-            return contentTypeField.getMimeType();
-        }
-    }
-
     private Optional<String> name(Optional<ContentTypeField> contentTypeField, 
Optional<ContentDispositionField> contentDispositionField) {
         return contentTypeField
             .map(field -> Optional.ofNullable(field.getParameter("name")))
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
index 8fe2009..3f29cee 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/impl/MessageParserTest.java
@@ -106,7 +106,7 @@ class MessageParserTest {
         List<ParsedAttachment> attachments = 
testee.retrieveAttachments(ClassLoader.getSystemResourceAsStream("eml/oneAttachmentAndSomeTextInlined.eml"));
 
         assertThat(attachments).hasSize(1);
-        
assertThat(attachments.get(0).getContentType()).isEqualTo("application/octet-stream");
+        
assertThat(attachments.get(0).getContentType()).isEqualTo("application/octet-stream;\tname=\"exploits_of_a_mom.png\"");
     }
 
     @Test
@@ -270,7 +270,7 @@ class MessageParserTest {
 
         assertThat(attachments).hasSize(1)
             .first()
-            .satisfies(attachment -> 
assertThat(attachment.getContentType()).isEqualTo("text/calendar; 
charset=iso-8859-1"));
+            .satisfies(attachment -> 
assertThat(attachment.getContentType()).isEqualTo("text/calendar; 
charset=\"iso-8859-1\"; method=COUNTER"));
     }
 
     @Test
@@ -280,7 +280,8 @@ class MessageParserTest {
 
         assertThat(attachments).hasSize(2)
             .extracting(ParsedAttachment::getContentType)
-            .containsOnly("text/calendar; charset=iso-8859-1", "text/calendar; 
charset=iso-4444-5");
+            .containsOnly("text/calendar; charset=\"iso-8859-1\"; 
method=COUNTER",
+                "text/calendar; charset=\"iso-4444-5\"; method=COUNTER");
     }
 
     @Test
@@ -290,7 +291,8 @@ class MessageParserTest {
 
         assertThat(attachments)
             .hasSize(1)
-            .allMatch(messageAttachment -> 
messageAttachment.getContentType().equals("text/calendar; charset=utf-8"));
+            .extracting(ParsedAttachment::getContentType)
+            .containsExactly("text/calendar; charset=\"utf-8\"; 
method=COUNTER");
     }
 
     @Test
diff --git 
a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/draft/methods/integration/SetMessagesMethodTest.java
 
b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/draft/methods/integration/SetMessagesMethodTest.java
index c65bcda..f17efc6 100644
--- 
a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/draft/methods/integration/SetMessagesMethodTest.java
+++ 
b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/draft/methods/integration/SetMessagesMethodTest.java
@@ -5444,7 +5444,7 @@ public abstract class SetMessagesMethodTest {
             .body(ARGUMENTS + ".notCreated", aMapWithSize(0))
             .body(ARGUMENTS + ".created", aMapWithSize(1))
             .body(createdPath + ".attachments", hasSize(1))
-            .body(singleAttachment + ".type", equalTo("text/html; 
charset=UTF-8"))
+            .body(singleAttachment + ".type", equalTo("text/html; 
charset=UTF-8; name=\"=?US-ASCII?Q?nonIndexableAttachment.html?=\""))
             .body(singleAttachment + ".size", equalTo((int) 
uploadedAttachment.getSize()));
     }
 
@@ -5754,7 +5754,7 @@ public abstract class SetMessagesMethodTest {
             .body(NAME, equalTo("messages"))
             .body(ARGUMENTS + ".list", hasSize(1))
             .body(message + ".attachments", hasSize(1))
-            .body(firstAttachment + ".type", equalTo("text/calendar; 
charset=UTF-8"))
+            .body(firstAttachment + ".type", equalTo("text/calendar; 
method=REPLY; charset=UTF-8"))
             .body(firstAttachment + ".blobId", not(isEmptyOrNullString()));
     }
 
diff --git 
a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
 
b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
index d65a73e..273ae57 100644
--- 
a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
+++ 
b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
@@ -165,13 +165,13 @@ Feature: GetMessages method
     And the list of attachments of the message contains 2 attachments
     And the first attachment is:
       |key      | value                                                        
     |
-      |type     |"image/jpeg"                                                  
     |
+      |type     |"image/jpeg; name="4037_014.jpg""                             
   |
       |size     |846                                                           
     |
       |cid      |null                                                          
     |
       |isInline |false                                                         
     |
     And the second attachment is:
       |key      | value                                                        
     |
-      |type     |"image/jpeg"                                                  
     |
+      |type     |"image/jpeg; name="4037_015.jpg""                             
   |
       |size     |597                                                           
     |
       |cid      |"part1.37a15c92.a7c34...@linagora.com"                        
     |
       |isInline |true                                                          
     |
@@ -274,10 +274,10 @@ Feature: GetMessages method
     And the hasAttachment of the message is "true"
     And the list of attachments of the message contains 1 attachments
     And the first attachment is:
-      |key      | value                                     |
-      |type     |"application/pdf"                          |
-      |cid      |null                                       |
-      |isInline |false                                      |
+      |key      | value                                                        
          |
+      |type     |"application/pdf;     x-unix-mode=0644;       
name="deromaCollection-628.pdf"" |
+      |cid      |null                                                          
          |
+      |isInline |false                                                         
          |
 
   Scenario: Retrieving message with inline attachment and blank CID should 
convert that inlined attachment to normal attachment
     Given "al...@domain.tld" has a message "m1" in "INBOX" mailbox with inline 
attachment and blank CID
@@ -287,10 +287,10 @@ Feature: GetMessages method
     And the hasAttachment of the message is "true"
     And the list of attachments of the message contains 1 attachments
     And the first attachment is:
-        |key      | value            |
-        |type     |"application/pdf" |
-        |cid      |null              |
-        |isInline |false             |
+        |key      | value                                                      
              |
+        |type     |"application/pdf;   x-unix-mode=0644;       
name="deromaCollection-628.pdf"" |
+        |cid      |null                                                        
              |
+        |isInline |false                                                       
              |
 
   Scenario: Preview should be computed even when HTML body contains many tags 
without content
     Given "al...@domain.tld" has a message "m1" in "INBOX" mailbox with HTML 
body with many empty tags
@@ -403,11 +403,11 @@ Feature: GetMessages method
     And the hasAttachment of the message is "true"
     And the list of attachments of the message contains 1 attachments
     And the first attachment is:
-      |key      | value                        |
-      |type     |"application/octet-stream"    |
-      |cid      |null                          |
-      |name     |"encrypted.asc"               |
-      |isInline |false                         |
+      |key      | value                                           |
+      |type     |"application/octet-stream; name="encrypted.asc"" |
+      |cid      |null                                             |
+      |name     |"encrypted.asc"                                  |
+      |isInline |false                                            |
 
   Scenario: Retrieving message should be possible when message with inlined 
image but without content disposition
     Given "al...@domain.tld" has a message "m1" in the "INBOX" mailbox with 
inlined image without content disposition
@@ -418,7 +418,7 @@ Feature: GetMessages method
     And the list of attachments of the message contains 1 attachments
     And the first attachment is:
       |key      | value                                           |
-      |type     |"image/png"                                      |
+      |type     |"image/png; name=vlc.png"                        |
       |cid      |"14672787885774e5c4d4cee471352...@linagora.com"  |
       |name     |"vlc.png"                                        |
       |isInline |false                                            |
@@ -431,11 +431,11 @@ Feature: GetMessages method
     And the hasAttachment of the message is "true"
     And the list of attachments of the message contains 1 attachments
     And the first attachment is:
-    |key      | value                        |
-    |type     |"image/jpeg"                  |
-    |cid      |null                          |
-    |name     |"IMG_6112.JPG"                |
-    |isInline |false                         |
+    |key      | value                                                          
                        |
+    |type     |"image/jpeg;    name=IMG_6112.JPG;      
x-apple-part-url=B11616AF-86EB-47AF-863A-176A823498DB" |
+    |cid      |null                                                            
                        |
+    |name     |"IMG_6112.JPG"                                                  
                        |
+    |isInline |false                                                           
                        |
 
   Scenario: Header only text calendar should be read as normal calendar 
attachment by JMAP
     Given "al...@domain.tld" receives a SMTP message specified in file 
"eml/ics_in_header.eml" as message "m1"
@@ -445,8 +445,8 @@ Feature: GetMessages method
     And the hasAttachment of the message is "true"
     And the list of attachments of the message contains 1 attachments
     And the first attachment is:
-    |key      | value                         |
-    |type     |"text/calendar; charset=UTF-8" |
-    |size     |1096                           |
-    |name     |"event.ics"                    |
-    |isInline |false                          |
+    |key      | value                                                       |
+    |type     |"text/calendar; method=REPLY; charset=UTF-8; name=event.ics" |
+    |size     |1096                                                         |
+    |name     |"event.ics"                                                  |
+    |isInline |false                                                        |


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

Reply via email to