james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [01/10] james-project git commit: JAMES-2557 MailAddress.nullSender() support for SenderIs* matchers
Date Wed, 24 Oct 2018 01:51:27 GMT
Repository: james-project
Updated Branches:
  refs/heads/master cfca550ac -> db9928165


JAMES-2557 MailAddress.nullSender() support for SenderIs* matchers

These matchers were throwing upon nullSenders.


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

Branch: refs/heads/master
Commit: 9dcc8995aa724152224f2d90fad243fa06885297
Parents: ebb7494
Author: Benoit Tellier <btellier@linagora.com>
Authored: Thu Oct 18 18:12:32 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Oct 24 08:47:34 2018 +0700

----------------------------------------------------------------------
 .../james/transport/matchers/SenderHostIs.java  |  7 ++-
 .../transport/matchers/SenderHostIsLocal.java   |  3 +
 .../james/transport/matchers/SenderIs.java      |  4 +-
 .../james/transport/matchers/SenderIsLocal.java | 10 ++--
 .../james/transport/matchers/SenderIsNull.java  |  7 ++-
 .../james/transport/matchers/SenderIsRegex.java |  2 +-
 .../utils/MailAddressCollectionReader.java      |  5 +-
 .../matchers/SenderHostIsLocalTest.java         | 25 +++++++++
 .../transport/matchers/SenderHostIsTest.java    | 19 +++++++
 .../transport/matchers/SenderIsLocalTest.java   | 25 +++++++++
 .../transport/matchers/SenderIsNullTest.java    | 11 ++++
 .../transport/matchers/SenderIsRegexTest.java   | 30 ++++++++++
 .../james/transport/matchers/SenderIsTest.java  | 58 ++++++++++++++++++++
 .../util/MailAddressCollectionReaderTest.java   |  6 ++
 14 files changed, 203 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
index 6c83b85..c9caffb 100755
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
@@ -78,7 +78,7 @@ public class SenderHostIs extends GenericMatcher {
     @Override
     public Collection<MailAddress> match(Mail mail) {
         try {
-            if (mail.getSender() != null && senderHosts.contains(mail.getSender().getDomain()))
{
+            if (hasSender(mail) && senderHosts.contains(mail.getSender().getDomain()))
{
                 return mail.getRecipients();
             }
         } catch (Exception e) {
@@ -87,4 +87,9 @@ public class SenderHostIs extends GenericMatcher {
 
         return null;    //No match.
     }
+
+    private boolean hasSender(Mail mail) {
+        return mail.getSender() != null
+            && !mail.getSender().isNullSender();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java
index 1500aed..a248c08 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java
@@ -47,6 +47,9 @@ public class SenderHostIsLocal extends GenericMatcher {
     }
 
     private boolean isLocalServer(Mail mail) {
+        if (mail.getSender().isNullSender()) {
+            return false;
+        }
         return this.getMailetContext().isLocalServer(mail.getSender().getDomain());
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIs.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIs.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIs.java
index 9a70406..821692d 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIs.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIs.java
@@ -20,6 +20,7 @@
 package org.apache.james.transport.matchers;
 
 import java.util.Collection;
+import java.util.Optional;
 import java.util.Set;
 
 import javax.mail.MessagingException;
@@ -66,7 +67,8 @@ public class SenderIs extends GenericMatcher {
 
     @Override
     public Collection<MailAddress> match(Mail mail) {
-        if (senders.contains(mail.getSender())) {
+        MailAddress sanitizedSender = Optional.ofNullable(mail.getSender()).orElse(MailAddress.nullSender());
+        if (senders.contains(sanitizedSender)) {
             return mail.getRecipients();
         } else {
             return null;

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsLocal.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsLocal.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsLocal.java
index 241dd7c..925c596 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsLocal.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsLocal.java
@@ -19,8 +19,7 @@
 package org.apache.james.transport.matchers;
 
 import java.util.Collection;
-
-import javax.mail.MessagingException;
+import java.util.Optional;
 
 import org.apache.james.core.MailAddress;
 import org.apache.mailet.Mail;
@@ -32,7 +31,7 @@ import org.apache.mailet.base.GenericMatcher;
 public class SenderIsLocal extends GenericMatcher {
 
     @Override
-    public final Collection<MailAddress> match(Mail mail) throws MessagingException
{
+    public final Collection<MailAddress> match(Mail mail) {
         if (isLocal(mail.getSender())) {
             return mail.getRecipients();
         }
@@ -40,7 +39,10 @@ public class SenderIsLocal extends GenericMatcher {
     }
 
     private boolean isLocal(MailAddress mailAddress) {
-        return getMailetContext().isLocalEmail(mailAddress);
+        return Optional.ofNullable(mailAddress)
+            .filter(address -> !address.isNullSender())
+            .map(address -> getMailetContext().isLocalEmail(address))
+            .orElse(false);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsNull.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsNull.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsNull.java
index 9dda490..6fba3c2 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsNull.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsNull.java
@@ -38,10 +38,15 @@ public class SenderIsNull extends GenericMatcher {
 
     @Override
     public Collection<MailAddress> match(Mail mail) {
-        if (mail.getSender() == null) {
+        if (isNullSender(mail)) {
             return mail.getRecipients();
         } else {
             return null;
         }
     }
+
+    private boolean isNullSender(Mail mail) {
+        return mail.getSender() == null
+            || mail.getSender().isNullSender();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsRegex.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsRegex.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsRegex.java
index dcb43d9..a72272f 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsRegex.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderIsRegex.java
@@ -78,7 +78,7 @@ public class SenderIsRegex extends GenericMatcher {
         if (mailAddress == null) {
             return null;
         }
-        String senderString = mailAddress.toString();
+        String senderString = mailAddress.asString();
         if (pattern.matcher(senderString).matches()) {
             return mail.getRecipients();
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java
index 5bf5ea1..e0ea2fd 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/utils/MailAddressCollectionReader.java
@@ -38,12 +38,15 @@ public class MailAddressCollectionReader {
         return Splitter.onPattern("(,| |\t)").splitToList(condition)
             .stream()
             .filter(s -> !Strings.isNullOrEmpty(s))
-            .map(s -> getMailAddress(s))
+            .map(MailAddressCollectionReader::getMailAddress)
             .collect(Guavate.toImmutableSet());
     }
 
     private static MailAddress getMailAddress(String s) {
         try {
+            if (s.equals(MailAddress.NULL_SENDER_AS_STRING)) {
+                return MailAddress.nullSender();
+            }
             return new MailAddress(s);
         } catch (AddressException e) {
             throw new RuntimeException(e);

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java
index d1a6759..53cdb6f 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java
@@ -87,4 +87,29 @@ public class SenderHostIsLocalTest {
         assertThat(actual).isNull();
     }
 
+    @Test
+    public void shouldNotMatchWhenNullSender() throws MessagingException {
+        //Given
+        Mail mail = FakeMail.builder()
+            .sender(MailAddress.nullSender())
+            .recipient(ANY_AT_JAMES)
+            .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+    }
+
+    @Test
+    public void shouldNotMatchWhenNoSender() throws MessagingException {
+        //Given
+        Mail mail = FakeMail.builder()
+            .recipient(ANY_AT_JAMES)
+            .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
index 48437dc..8089e25 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
@@ -116,6 +116,25 @@ public class SenderHostIsTest {
         matcher.init(FakeMatcherConfig.builder()
                 .matcherName("SenderHostIs")
                 .mailetContext(mailContext)
+                .condition("domain.tld")
+                .build());
+
+        Mail mail = FakeMail.builder()
+                .sender(ANY_AT_JAMES2)
+                .recipient(ANY_AT_JAMES2)
+                .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+    }
+
+    @Test
+    void shouldNotMatchWhenNoSender() throws MessagingException {
+        //Given
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderHostIs")
+                .mailetContext(mailContext)
                 .condition("")
                 .build());
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsLocalTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsLocalTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsLocalTest.java
index 97498a0..1736ce3 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsLocalTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsLocalTest.java
@@ -98,4 +98,29 @@ public class SenderIsLocalTest {
         assertThat(actual).isNull();
     }
 
+    @Test
+    public void shouldNotMatchWhenNullSender() throws MessagingException {
+        //Given
+        Mail mail = FakeMail.builder()
+            .sender(MailAddress.nullSender())
+            .recipient(ANY_AT_JAMES)
+            .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+    }
+
+    @Test
+    public void shouldNotMatchWhenNoSender() throws MessagingException {
+        //Given
+        Mail mail = FakeMail.builder()
+            .recipient(ANY_AT_JAMES)
+            .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsNullTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsNullTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsNullTest.java
index 0b38c45..78d898a 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsNullTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsNullTest.java
@@ -23,6 +23,7 @@ package org.apache.james.transport.matchers;
 import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES;
 import static org.assertj.core.api.Assertions.assertThat;
 
+import org.apache.james.core.MailAddress;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMatcherConfig;
 import org.junit.jupiter.api.BeforeEach;
@@ -41,9 +42,19 @@ class SenderIsNullTest {
     }
 
     @Test
+    void shouldMatchWhenNoSender() throws Exception {
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(ANY_AT_JAMES)
+            .build();
+
+        assertThat(matcher.match(fakeMail)).containsExactly(ANY_AT_JAMES);
+    }
+
+    @Test
     void shouldMatchWhenNullSender() throws Exception {
         FakeMail fakeMail = FakeMail.builder()
             .recipient(ANY_AT_JAMES)
+            .sender(MailAddress.nullSender())
             .build();
 
         assertThat(matcher.match(fakeMail)).containsExactly(ANY_AT_JAMES);

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java
index 7bf5c49..d1249e8 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java
@@ -74,6 +74,20 @@ class SenderIsRegexTest {
     }
 
     @Test
+    void shouldNotMatchWhenNoSender() throws Exception {
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderIsRegex")
+                .condition(".*@.*")
+                .build());
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(recipient)
+            .build();
+
+        assertThat(matcher.match(fakeMail)).isNull();
+    }
+
+    @Test
     void shouldNotMatchWhenNullSender() throws Exception {
         matcher.init(FakeMatcherConfig.builder()
                 .matcherName("SenderIsRegex")
@@ -82,12 +96,28 @@ class SenderIsRegexTest {
 
         FakeMail fakeMail = FakeMail.builder()
             .recipient(recipient)
+            .sender(MailAddress.nullSender())
             .build();
 
         assertThat(matcher.match(fakeMail)).isNull();
     }
 
     @Test
+    void shouldMatchNullSenderWhenConfigured() throws Exception {
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderIsRegex")
+                .condition("<>")
+                .build());
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(recipient)
+            .sender(MailAddress.nullSender())
+            .build();
+
+        assertThat(matcher.match(fakeMail)).containsExactly(recipient);
+    }
+
+    @Test
     void shouldNotMatchOnNonMatchingPattern() throws Exception {
         matcher.init(FakeMatcherConfig.builder()
                 .matcherName("SenderIsRegex")

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsTest.java
index fbfffa5..ac9ae53 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsTest.java
@@ -60,6 +60,64 @@ class SenderIsTest {
     }
 
     @Test
+    void shouldMatchNotMatchWhenNullSender() throws Exception {
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderIs")
+                .condition(SENDER_NAME)
+                .build());
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(recipient)
+            .sender(MailAddress.nullSender())
+            .build();
+
+        assertThat(matcher.match(fakeMail)).isNull();
+    }
+
+    @Test
+    void shouldMatchNotMatchWhenNoSender() throws Exception {
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderIs")
+                .condition(SENDER_NAME)
+                .build());
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(recipient)
+            .build();
+
+        assertThat(matcher.match(fakeMail)).isNull();
+    }
+
+    @Test
+    void shouldMatchMatchWhenNullSenderWhenConfigured() throws Exception {
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderIs")
+                .condition(MailAddress.NULL_SENDER_AS_STRING)
+                .build());
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(recipient)
+            .sender(MailAddress.nullSender())
+            .build();
+
+        assertThat(matcher.match(fakeMail)).containsExactly(recipient);
+    }
+
+    @Test
+    void shouldMatchMatchWhenNoSenderWhenConfigured() throws Exception {
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderIs")
+                .condition(MailAddress.NULL_SENDER_AS_STRING)
+                .build());
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(recipient)
+            .build();
+
+        assertThat(matcher.match(fakeMail)).containsExactly(recipient);
+    }
+
+    @Test
     void shouldNotMatchWhenWrongSender() throws Exception {
         matcher.init(FakeMatcherConfig.builder()
                 .matcherName("SenderIs")

http://git-wip-us.apache.org/repos/asf/james-project/blob/9dcc8995/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java
index cb3a296..6bad784 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/util/MailAddressCollectionReaderTest.java
@@ -61,6 +61,12 @@ class MailAddressCollectionReaderTest {
     }
 
     @Test
+    void readShouldParseNullSender() {
+        assertThat(MailAddressCollectionReader.read("<>"))
+            .containsExactly(MailAddress.nullSender());
+    }
+
+    @Test
     void readShouldParseTwoEmailSeparatedByComaOnly() throws Exception {
         MailAddress mailAddress1 = new MailAddress("valid@apache.org");
         MailAddress mailAddress2 = new MailAddress("bis@apache.org");


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