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 8f0c332216a06cee2154d14336333f9c6058996e
Author: Benoit Tellier <btell...@linagora.com>
AuthorDate: Mon Apr 20 12:41:17 2020 +0700

    JAMES-3151 HasMimeType matchers fails on empty charset
---
 .../james/transport/matchers/HasMimeType.java      | 32 ++++++++++----------
 .../james/transport/matchers/HasMimeTypeTest.java  | 35 ++++++++++++++++++----
 2 files changed, 46 insertions(+), 21 deletions(-)

diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
index 1dc2ecf..2dcbc7e 100644
--- 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
@@ -20,16 +20,15 @@
 
 package org.apache.james.transport.matchers;
 
+import static org.apache.mailet.base.RFC2822Headers.CONTENT_TYPE;
+
 import java.util.Collection;
-import java.util.Optional;
 import java.util.Set;
-
-import javax.activation.MimeType;
-import javax.activation.MimeTypeParseException;
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
+import java.util.stream.Stream;
 
 import org.apache.james.core.MailAddress;
+import org.apache.james.mime4j.field.Fields;
+import org.apache.james.util.StreamUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.GenericMatcher;
 import org.slf4j.Logger;
@@ -58,19 +57,20 @@ public class HasMimeType extends GenericMatcher {
 
     @Override
     public Collection<MailAddress> match(Mail mail) throws 
javax.mail.MessagingException {
-        Optional<String> mimeTypes = getMimeTypeFromMessage(mail.getMessage());
-
-        return mimeTypes.filter(acceptedContentTypes::contains)
-                .map(any -> mail.getRecipients())
-                .orElse(ImmutableList.of());
+        return 
StreamUtils.ofNullable(mail.getMessage().getHeader(CONTENT_TYPE))
+            .flatMap(this::getMimeType)
+            .filter(acceptedContentTypes::contains)
+            .findAny()
+            .map(any -> mail.getRecipients())
+            .orElse(ImmutableList.of());
     }
 
-    private static Optional<String> getMimeTypeFromMessage(MimeMessage 
message) throws MessagingException {
+    private Stream<String> getMimeType(String rawValue) {
         try {
-            return Optional.of(new 
MimeType(message.getContentType()).getBaseType());
-        } catch (MimeTypeParseException e) {
-            LOGGER.warn("Error while parsing message's mimeType {}", 
message.getContentType(), e);
-            return Optional.empty();
+            return Stream.of(Fields.contentType(rawValue).getMimeType());
+        } catch (Exception e) {
+            LOGGER.warn("Error while parsing message's mimeType {}", rawValue, 
e);
+            return Stream.empty();
         }
     }
 
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
index 46399b4..6ff4620 100644
--- 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
@@ -19,7 +19,12 @@
 
 package org.apache.james.transport.matchers;
 
+import static org.apache.mailet.base.RFC2822Headers.CONTENT_TYPE;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.mail.internet.MimeMessage;
 
 import org.apache.james.core.builder.MimeMessageBuilder;
 import org.apache.mailet.Mail;
@@ -28,7 +33,7 @@ import org.apache.mailet.base.test.FakeMatcherConfig;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
-public class HasMimeTypeTest {
+class HasMimeTypeTest {
 
     private static final String RECIPIENT = "t...@james.apache.org";
     private static final String FROM = "t...@james.apache.org";
@@ -38,12 +43,12 @@ public class HasMimeTypeTest {
     private HasMimeType matcher;
 
     @BeforeEach
-    public void setUp() throws Exception {
+    void setUp() throws Exception {
         matcher = new HasMimeType();
     }
 
     @Test
-    public void hasMimeType() throws Exception {
+    void hasMimeType() throws Exception {
         matcher.init(FakeMatcherConfig.builder()
                 .matcherName("HasMimeType")
                 .condition(MIME_TYPES)
@@ -74,7 +79,7 @@ public class HasMimeTypeTest {
     }
 
     @Test
-    public void doesNotHaveMimeType() throws Exception {
+    void doesNotHaveMimeType() throws Exception {
         matcher.init(FakeMatcherConfig.builder()
                 .matcherName("HasMimeType")
                 .condition(NON_MATCHING_MIME_TYPES)
@@ -105,7 +110,7 @@ public class HasMimeTypeTest {
     }
 
     @Test
-    public void matchShouldReturnRecipientsWhenAtLeastOneMimeTypeMatch() 
throws Exception {
+    void matchShouldReturnRecipientsWhenAtLeastOneMimeTypeMatch() throws 
Exception {
         matcher.init(FakeMatcherConfig.builder()
             .matcherName("HasMimeType")
             .condition("text/md, text/html")
@@ -124,4 +129,24 @@ public class HasMimeTypeTest {
 
         
assertThat(matcher.match(mail)).containsExactlyElementsOf(mail.getRecipients());
     }
+
+    @Test
+    void matchShouldNotFailOnEmptyCharset() throws Exception {
+        matcher.init(FakeMatcherConfig.builder()
+            .matcherName("HasMimeType")
+            .condition("text/html")
+            .build());
+
+        MimeMessage message = mock(MimeMessage.class);
+        when(message.getHeader(CONTENT_TYPE)).thenReturn(new String[] 
{"text/html; charset="});
+
+        Mail mail = FakeMail.builder()
+            .name("mail")
+            .mimeMessage(message)
+            .sender(FROM)
+            .recipient(RECIPIENT)
+            .build();
+
+        
assertThat(matcher.match(mail)).containsExactlyElementsOf(mail.getRecipients());
+    }
 }


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