You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2021/03/15 09:54:20 UTC

[james-project] 32/32: JAMES-3506 Avoid a full body read within VacationMailet

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 0b3f9007eb8de370d88b0b841310d3d2f8da008c
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Mar 11 09:12:17 2021 +0700

    JAMES-3506 Avoid a full body read within VacationMailet
    
    In order to see if a part have the message/disposition-notification content type
    a full parsing of the message is required.
    
    MDNs falls in the category of automatically
    generated reports described in RFC-1892
    
    The Multipart/Report Content Type for the Reporting of Mail System Administrative Messages
    
    The Multipart/Report MIME content-type is a general "family" or
       "container" type for electronic mail reports of any kind.
    
    So:
    
     - Not only we are more generic
     - But also we save a full message read and parsing
---
 .../AutomaticallySentMailDetectorImpl.java         | 66 +++-------------------
 .../AutomaticallySentMailDetectorImplTest.java     |  2 +
 2 files changed, 11 insertions(+), 57 deletions(-)

diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImpl.java b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImpl.java
index 6f91048..a5a8428 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImpl.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImpl.java
@@ -19,21 +19,15 @@
 
 package org.apache.james.mailetcontainer;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.Arrays;
+import java.util.Optional;
 
 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;
-import org.apache.james.mime4j.stream.BodyDescriptor;
-import org.apache.james.mime4j.stream.MimeConfig;
-import org.apache.james.server.core.MimeMessageInputStream;
+import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.field.ContentTypeFieldImpl;
+import org.apache.james.mime4j.stream.RawField;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.AutomaticallySentMailDetector;
 
@@ -98,52 +92,10 @@ public class AutomaticallySentMailDetectorImpl implements AutomaticallySentMailD
 
     @Override
     public boolean isMdnSentAutomatically(Mail mail) throws MessagingException {
-        ResultCollector resultCollector = new ResultCollector(false);
-        MimeStreamParser parser = new MimeStreamParser(MimeConfig.custom()
-            .setMaxContentLen(100 * 1024 * 1024)
-            .setMaxHeaderCount(-1)
-            .setMaxHeaderLen(-1)
-            .setMaxLineLen(-1)
-            .setHeadlessParsing(mail.getMessage().getContentType())
-            .build());
-        parser.setContentHandler(createMdnContentHandler(resultCollector));
-        try {
-            parser.parse(new MimeMessageInputStream(mail.getMessage()));
-        } catch (MimeException e) {
-            throw new MessagingException("Can not parse Mime", e);
-        } catch (IOException e) {
-            throw new MessagingException("Can not read content", e);
-        }
-        return resultCollector.getResult();
-    }
-
-    private AbstractContentHandler createMdnContentHandler(final ResultCollector resultCollector) {
-        return new AbstractContentHandler() {
-            @Override
-            public void body(BodyDescriptor bodyDescriptor, InputStream inputStream) throws MimeException, IOException {
-                if (bodyDescriptor.getMimeType().equalsIgnoreCase("message/disposition-notification")) {
-                    resultCollector.setResult(MDNReportParser.parse(inputStream, bodyDescriptor.getCharset())
-                        .map(report -> report.getDispositionField().getSendingMode() == DispositionSendingMode.Automatic)
-                        .getOrElse(() -> false));
-                }
-            }
-        };
-    }
-
-    private static class ResultCollector {
-        private boolean result;
-
-        public ResultCollector(boolean result) {
-            this.result = result;
-        }
-
-        public boolean getResult() {
-            return result;
-        }
-
-        public void setResult(boolean result) {
-            this.result = result;
-        }
+        return Optional.ofNullable(mail.getMessage().getContentType())
+            .map(field -> ContentTypeFieldImpl.PARSER.parse(new RawField("Content-Type", field), DecodeMonitor.SILENT))
+            .map(field -> field.getMimeType())
+            .stream()
+            .anyMatch("multipart/report"::equals);
     }
-
 }
diff --git a/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImplTest.java b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImplTest.java
index 8996565..624f3b1 100644
--- a/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImplTest.java
+++ b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/AutomaticallySentMailDetectorImplTest.java
@@ -261,6 +261,7 @@ class AutomaticallySentMailDetectorImplTest {
     void isMdnSentAutomaticallyShouldBeDetected() throws Exception {
         MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
         MimeMultipart multipart = new MimeMultipart();
+        multipart.setSubType("report");
         MimeBodyPart scriptPart = new MimeBodyPart();
         scriptPart.setDataHandler(
                 new DataHandler(
@@ -380,6 +381,7 @@ class AutomaticallySentMailDetectorImplTest {
     void isMdnSentAutomaticallyShouldDetectBigMDN() throws Exception {
         MimeMessage message = MimeMessageUtil.defaultMimeMessage();
         MimeMultipart multipart = new MimeMultipart();
+        multipart.setSubType("report");
         MimeBodyPart scriptPart = new MimeBodyPart();
         scriptPart.setDataHandler(
             new DataHandler(


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