james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [04/24] james-project git commit: JAMES-2366 Handle Forward mappings in RRT mailet
Date Fri, 06 Apr 2018 13:20:23 GMT
JAMES-2366 Handle Forward mappings in RRT mailet


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

Branch: refs/heads/master
Commit: c9078f2569c6cef757f8e6104efb77732bc58164
Parents: d1b0a11
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Thu Mar 29 16:50:35 2018 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Fri Apr 6 14:39:17 2018 +0200

----------------------------------------------------------------------
 .../RecipientRewriteTableIntegrationTest.java   | 69 +++++++++++++++++---
 .../mailets/RecipientRewriteTableProcessor.java | 23 +++++--
 2 files changed, 77 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c9078f25/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
index c176fba..2268a78 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
@@ -25,6 +25,7 @@ import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP;
 import static org.apache.james.mailets.configuration.Constants.PASSWORD;
 import static org.apache.james.mailets.configuration.Constants.SMTP_PORT;
 import static org.apache.james.mailets.configuration.Constants.awaitAtMostOneMinute;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import org.apache.james.probe.DataProbe;
 import org.apache.james.utils.DataProbeImpl;
@@ -39,11 +40,16 @@ import org.junit.rules.TemporaryFolder;
 public class RecipientRewriteTableIntegrationTest {
     private static final String JAMES_ANOTHER_DOMAIN = "james.com";
 
-    private static final String FROM = "fromUser@" + DEFAULT_DOMAIN;
-    private static final String RECIPIENT = "touser@" + DEFAULT_DOMAIN;
-    private static final String ANY_AT_JAMES = "any@" + DEFAULT_DOMAIN;
+    private static final String FROM_LOCAL_PART = "fromUser";
+    private static final String FROM = FROM_LOCAL_PART + "@" + DEFAULT_DOMAIN;
+    private static final String RECIPIENT_LOCAL_PART = "touser";
+    private static final String RECIPIENT = RECIPIENT_LOCAL_PART + "@" + DEFAULT_DOMAIN;
+    private static final String ANY_LOCAL_PART = "any";
+    private static final String ANY_AT_JAMES = ANY_LOCAL_PART + "@" + DEFAULT_DOMAIN;
     private static final String OTHER_AT_JAMES = "other@" + DEFAULT_DOMAIN;
-    private static final String ANY_AT_ANOTHER_DOMAIN = "any@" + JAMES_ANOTHER_DOMAIN;
+    private static final String ANY_AT_ANOTHER_DOMAIN = ANY_LOCAL_PART + "@" + JAMES_ANOTHER_DOMAIN;
+    private static final String GROUP_LOCAL_PART = "group";
+    private static final String GROUP = GROUP_LOCAL_PART + "@" + DEFAULT_DOMAIN;
 
     @Rule
     public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -75,8 +81,8 @@ public class RecipientRewriteTableIntegrationTest {
 
     @Test
     public void rrtServiceShouldDeliverEmailToMappingRecipients() throws Exception {
-        dataProbe.addAddressMapping("touser", DEFAULT_DOMAIN, ANY_AT_JAMES);
-        dataProbe.addAddressMapping("touser", DEFAULT_DOMAIN, OTHER_AT_JAMES);
+        dataProbe.addAddressMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, ANY_AT_JAMES);
+        dataProbe.addAddressMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, OTHER_AT_JAMES);
 
         messageSender.connect(LOCALHOST_IP, SMTP_PORT)
             .sendMessage(FROM, RECIPIENT)
@@ -86,16 +92,18 @@ public class RecipientRewriteTableIntegrationTest {
             .login(ANY_AT_JAMES, PASSWORD)
             .select(IMAPMessageReader.INBOX)
             .awaitMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
         imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT)
             .login(OTHER_AT_JAMES, PASSWORD)
             .select(IMAPMessageReader.INBOX)
             .awaitMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
     }
 
     @Test
     public void rrtServiceShouldNotDeliverEmailToRecipientWhenHaveMappingRecipients() throws
Exception {
-        dataProbe.addAddressMapping("touser", DEFAULT_DOMAIN, ANY_AT_JAMES);
-        dataProbe.addAddressMapping("touser", DEFAULT_DOMAIN, OTHER_AT_JAMES);
+        dataProbe.addAddressMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, ANY_AT_JAMES);
+        dataProbe.addAddressMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, OTHER_AT_JAMES);
 
         messageSender.connect(LOCALHOST_IP, SMTP_PORT)
             .sendMessage(FROM, RECIPIENT)
@@ -105,6 +113,7 @@ public class RecipientRewriteTableIntegrationTest {
             .login(RECIPIENT, PASSWORD)
             .select(IMAPMessageReader.INBOX)
             .awaitNoMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
     }
 
     @Test
@@ -113,8 +122,8 @@ public class RecipientRewriteTableIntegrationTest {
         String localUser = nonDomainUser + "@" + dataProbe.getDefaultDomain();
         dataProbe.addUser(localUser, PASSWORD);
 
-        dataProbe.addAddressMapping("touser", DEFAULT_DOMAIN, nonDomainUser);
-        dataProbe.addAddressMapping("touser", DEFAULT_DOMAIN, OTHER_AT_JAMES);
+        dataProbe.addAddressMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, nonDomainUser);
+        dataProbe.addAddressMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, OTHER_AT_JAMES);
 
         messageSender.connect(LOCALHOST_IP, SMTP_PORT)
             .sendMessage(FROM, RECIPIENT)
@@ -124,10 +133,12 @@ public class RecipientRewriteTableIntegrationTest {
             .login(localUser, PASSWORD)
             .select(IMAPMessageReader.INBOX)
             .awaitMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
         imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT)
             .login(OTHER_AT_JAMES, PASSWORD)
             .select(IMAPMessageReader.INBOX)
             .awaitMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
     }
 
     @Test
@@ -143,6 +154,7 @@ public class RecipientRewriteTableIntegrationTest {
             .login(ANY_AT_ANOTHER_DOMAIN, PASSWORD)
             .select(IMAPMessageReader.INBOX)
             .awaitMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
     }
 
     @Test
@@ -158,6 +170,43 @@ public class RecipientRewriteTableIntegrationTest {
             .login(ANY_AT_JAMES, PASSWORD)
             .select(IMAPMessageReader.INBOX)
             .awaitNoMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
     }
 
+    @Test
+    public void rrtServiceShouldDeliverEmailToForwardRecipients() throws Exception {
+        dataProbe.addForwardMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, ANY_AT_JAMES);
+        dataProbe.addForwardMapping(RECIPIENT_LOCAL_PART, DEFAULT_DOMAIN, OTHER_AT_JAMES);
+
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(FROM, RECIPIENT)
+            .awaitSent(awaitAtMostOneMinute);
+
+        imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(ANY_AT_JAMES, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitAtMostOneMinute);
+        imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(OTHER_AT_JAMES, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
+    }
+
+    @Test
+    public void rrtServiceShouldFollowForwardWhenSendingToAGroup() throws Exception {
+        dataProbe.addAddressMapping(GROUP_LOCAL_PART, DEFAULT_DOMAIN, ANY_AT_JAMES);
+
+        dataProbe.addForwardMapping(ANY_LOCAL_PART, DEFAULT_DOMAIN, OTHER_AT_JAMES);
+
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(FROM, GROUP)
+            .awaitSent(awaitAtMostOneMinute);
+
+        imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(OTHER_AT_JAMES, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitAtMostOneMinute);
+        assertThat(imapMessageReader.readFirstMessage()).isNotNull();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/c9078f25/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
index 76961e4..c1bc666 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessor.java
@@ -36,6 +36,7 @@ import org.apache.james.rrt.api.RecipientRewriteTable;
 import org.apache.james.rrt.api.RecipientRewriteTable.ErrorMappingException;
 import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.lib.Mapping;
+import org.apache.james.rrt.lib.Mapping.Type;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.util.MemoizedSupplier;
 import org.apache.james.util.OptionalUtils;
@@ -105,14 +106,26 @@ public class RecipientRewriteTableProcessor {
     private final Supplier<Domain> defaultDomainSupplier;
 
     private static final Function<Mapping, Optional<MailAddress>> mailAddressFromMapping
=
-        addressMapping -> {
-            try {
-                return Optional.of(new MailAddress(addressMapping.asString()));
-            } catch (AddressException e) {
-                return Optional.empty();
+        mapping -> {
+            Type type = mapping.getType();
+            switch (type) {
+                case Address:
+                    return parseMappingToMailAddress(mapping.asString());
+                case Forward:
+                    return parseMappingToMailAddress(type.withoutPrefix(mapping.asString()));
+                default:
+                    return Optional.empty();
             }
         };
 
+    private static Optional<MailAddress> parseMappingToMailAddress(String mapping)
{
+        try {
+            return Optional.of(new MailAddress(mapping));
+        } catch (AddressException e) {
+            return Optional.empty();
+        }
+    }
+
     public RecipientRewriteTableProcessor(RecipientRewriteTable virtualTableStore, DomainList
domainList, MailetContext mailetContext) {
         this.virtualTableStore = virtualTableStore;
         this.mailetContext = mailetContext;


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