This is an automated email from the ASF dual-hosted git repository.

rouazana 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 2331f14  MAILET-164 use a real MDN parser instead of parsing MDN 
report as a list of headers
     new 2d149c3  Merge remote-tracking branch 'mbaechler/MAILET-164'
2331f14 is described below

commit 2331f145159aa5fd528c995b2351c1896bd4ab9d
Author: Matthieu Baechler <[email protected]>
AuthorDate: Tue Mar 5 15:37:07 2019 +0100

    MAILET-164 use a real MDN parser instead of parsing MDN report as a list of 
headers
    
        This starts by fixing tests:
                x mdn conformance
                x call to saveChange on message to make things work
        It then introduce our beloved MDN report parser instead of the
        hack we used to parse the report as some mail headers.
        Finally, we also tells MimeMessageParser that we don't provide it
        with the mails header as javax.mail getInputstream return only the mail
        body.
---
 mailet/base/pom.xml                                |  4 ++++
 .../base/AutomaticallySentMailDetectorImpl.java    | 26 +++++++++++-----------
 .../AutomaticallySentMailDetectorImplTest.java     | 17 +++++++++++---
 3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/mailet/base/pom.xml b/mailet/base/pom.xml
index 80c7893..1501426 100644
--- a/mailet/base/pom.xml
+++ b/mailet/base/pom.xml
@@ -61,6 +61,10 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>james-mdn</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.github.steveash.guavate</groupId>
             <artifactId>guavate</artifactId>
         </dependency>
diff --git 
a/mailet/base/src/main/java/org/apache/mailet/base/AutomaticallySentMailDetectorImpl.java
 
b/mailet/base/src/main/java/org/apache/mailet/base/AutomaticallySentMailDetectorImpl.java
index f7cb0af..5f42cb6 100644
--- 
a/mailet/base/src/main/java/org/apache/mailet/base/AutomaticallySentMailDetectorImpl.java
+++ 
b/mailet/base/src/main/java/org/apache/mailet/base/AutomaticallySentMailDetectorImpl.java
@@ -19,15 +19,15 @@
 
 package org.apache.mailet.base;
 
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.util.Arrays;
 
 import javax.mail.MessagingException;
 
 import org.apache.james.core.MailAddress;
+import org.apache.james.mdn.MDNReportParser;
+import org.apache.james.mdn.sending.mode.DispositionSendingMode;
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.parser.AbstractContentHandler;
 import org.apache.james.mime4j.parser.MimeStreamParser;
@@ -91,7 +91,13 @@ public class AutomaticallySentMailDetectorImpl implements 
AutomaticallySentMailD
     @Override
     public boolean isMdnSentAutomatically(Mail mail) throws MessagingException 
{
         ResultCollector resultCollector = new ResultCollector(false);
-        MimeStreamParser parser = new MimeStreamParser(MimeConfig.PERMISSIVE);
+        MimeStreamParser parser = new MimeStreamParser(MimeConfig.custom()
+            .setMaxContentLen(1024 * 1024) // there's no reason for a mdn 
report to be bigger than 1MiB
+            .setMaxHeaderCount(-1)
+            .setMaxHeaderLen(-1)
+            .setMaxLineLen(-1)
+            .setHeadlessParsing(mail.getMessage().getContentType())
+            .build());
         parser.setContentHandler(createMdnContentHandler(resultCollector));
         try {
             parser.parse(mail.getMessage().getInputStream());
@@ -108,16 +114,10 @@ public class AutomaticallySentMailDetectorImpl implements 
AutomaticallySentMailD
             @Override
             public void body(BodyDescriptor bodyDescriptor, InputStream 
inputStream) throws MimeException, IOException {
                 if 
(bodyDescriptor.getMimeType().equalsIgnoreCase("message/disposition-notification"))
 {
-                    try (BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream))) {
-                        String line;
-                        while ((line = reader.readLine()) != null) {
-                            if (line.startsWith("Disposition:")) {
-                                if (line.contains("MDN-sent-automatically") || 
line.contains("automatic-action")) {
-                                    resultCollector.setResult(true);
-                                }
-                            }
-                        }
-                    }
+                    resultCollector.setResult(new MDNReportParser()
+                        .parse(inputStream, bodyDescriptor.getCharset())
+                        .map(report -> 
report.getDispositionField().getSendingMode() == 
DispositionSendingMode.Automatic)
+                        .orElse(false));
                 }
             }
         };
diff --git 
a/mailet/base/src/test/java/org/apache/mailet/base/AutomaticallySentMailDetectorImplTest.java
 
b/mailet/base/src/test/java/org/apache/mailet/base/AutomaticallySentMailDetectorImplTest.java
index 1e6a623..ad4e551 100644
--- 
a/mailet/base/src/test/java/org/apache/mailet/base/AutomaticallySentMailDetectorImplTest.java
+++ 
b/mailet/base/src/test/java/org/apache/mailet/base/AutomaticallySentMailDetectorImplTest.java
@@ -36,6 +36,8 @@ import org.apache.james.util.MimeMessageUtil;
 import org.apache.mailet.base.test.FakeMail;
 import org.junit.Test;
 
+import com.google.common.base.Joiner;
+
 public class AutomaticallySentMailDetectorImplTest {
 
     @Test
@@ -238,13 +240,17 @@ public class AutomaticallySentMailDetectorImplTest {
         scriptPart.setDataHandler(
                 new DataHandler(
                         new ByteArrayDataSource(
-                                "Disposition: MDN-sent-automatically",
+                            Joiner.on("\r\n").join(
+                                "Final-Recipient: rfc822;[email protected]",
+                                "Disposition: 
automatic-action/MDN-sent-automatically; displayed",
+                                ""),
                                 "message/disposition-notification;")
                         ));
         scriptPart.setHeader("Content-Type", 
"message/disposition-notification");
         multipart.addBodyPart(scriptPart);
         message.setContent(multipart);
-        
+        message.saveChanges();
+
         FakeMail fakeMail = FakeMail.builder()
                 .name("mail")
                 .sender("[email protected]")
@@ -262,12 +268,16 @@ public class AutomaticallySentMailDetectorImplTest {
         scriptPart.setDataHandler(
                 new DataHandler(
                         new ByteArrayDataSource(
-                                "Disposition: MDN-sent-manually",
+                            Joiner.on("\r\n").join(
+                                "Final-Recipient: rfc822;[email protected]",
+                                "Disposition: manual-action/MDN-sent-manually; 
displayed",
+                                ""),
                                 "message/disposition-notification; 
charset=UTF-8")
                         ));
         scriptPart.setHeader("Content-Type", 
"message/disposition-notification");
         multipart.addBodyPart(scriptPart);
         message.setContent(multipart);
+        message.saveChanges();
         
         FakeMail fakeMail = FakeMail.builder()
                 .name("mail")
@@ -292,6 +302,7 @@ public class AutomaticallySentMailDetectorImplTest {
         scriptPart.setHeader("Content-Type", "text/plain");
         multipart.addBodyPart(scriptPart);
         message.setContent(multipart);
+        message.saveChanges();
         
         FakeMail fakeMail = FakeMail.builder()
                 .name("mail")


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

Reply via email to