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 895b50b1fbcb375bfc815d88518fc0985dcaafa6
Author: Benoit Tellier <[email protected]>
AuthorDate: Wed May 15 11:56:05 2019 +0700

    JAMES-2770 RRT rewriting can lead to duplicated mails
---
 .../james/transport/mailets/AliasMappingTest.java  | 19 +++++++++++++++++
 .../mailets/RecipientRewriteTableProcessor.java    | 24 ++++++++++++----------
 .../RecipientRewriteTableProcessorTest.java        | 21 +++++++++++++++++++
 3 files changed, 53 insertions(+), 11 deletions(-)

diff --git 
a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/AliasMappingTest.java
 
b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/AliasMappingTest.java
index 116b522..0fe2f42 100644
--- 
a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/AliasMappingTest.java
+++ 
b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/AliasMappingTest.java
@@ -343,4 +343,23 @@ public class AliasMappingTest {
                 .getRepositoryMailCount(RRT_ERROR_REPOSITORY) == 1);
     }
 
+    @Test
+    public void 
userShouldNotReceiveDuplicatesWhenUserAndAliasRegisteredToAGroup() throws 
Exception {
+        webAdminApi.put(AliasRoutes.ROOT_PATH + "/" + BOB_ADDRESS + 
"/sources/" + BOB_ALIAS);
+        webAdminApi.put(GroupsRoutes.ROOT_PATH + "/" + GROUP_ADDRESS + "/" + 
BOB_ADDRESS);
+        webAdminApi.put(GroupsRoutes.ROOT_PATH + "/" + GROUP_ADDRESS + "/" + 
BOB_ALIAS);
+
+        messageSender.connect(LOCALHOST_IP, 
jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort())
+            .sendMessage(FakeMail.builder()
+                .name("name")
+                .mimeMessage(message)
+                .sender(ALICE_ADDRESS)
+                .recipient(GROUP_ADDRESS));
+
+        imapMessageReader.connect(LOCALHOST_IP, 
jamesServer.getProbe(ImapGuiceProbe.class).getImapPort())
+            .login(BOB_ADDRESS, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessageCount(awaitAtMostOneMinute, 1);
+    }
+
 }
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 18ead89..dd890c4 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
@@ -22,6 +22,7 @@ package org.apache.james.transport.mailets;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.function.Function;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
@@ -49,52 +50,53 @@ import com.github.fge.lambdas.Throwing;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 
 public class RecipientRewriteTableProcessor {
     private static final Logger LOGGER = 
LoggerFactory.getLogger(RecipientRewriteTableProcessor.class);
 
     private static class RrtExecutionResult {
         private static RrtExecutionResult empty() {
-            return new RrtExecutionResult(ImmutableList.of(), 
ImmutableList.of());
+            return new RrtExecutionResult(ImmutableSet.of(), 
ImmutableSet.of());
         }
 
         private static RrtExecutionResult error(MailAddress mailAddress) {
-            return new RrtExecutionResult(ImmutableList.of(), 
ImmutableList.of(mailAddress));
+            return new RrtExecutionResult(ImmutableSet.of(), 
ImmutableSet.of(mailAddress));
         }
 
         private static RrtExecutionResult success(MailAddress mailAddress) {
-            return new RrtExecutionResult(ImmutableList.of(mailAddress), 
ImmutableList.of());
+            return new RrtExecutionResult(ImmutableSet.of(mailAddress), 
ImmutableSet.of());
         }
 
         private static RrtExecutionResult success(List<MailAddress> 
mailAddresses) {
-            return new RrtExecutionResult(ImmutableList.copyOf(mailAddresses), 
ImmutableList.of());
+            return new RrtExecutionResult(ImmutableSet.copyOf(mailAddresses), 
ImmutableSet.of());
         }
 
         private static RrtExecutionResult merge(RrtExecutionResult result1, 
RrtExecutionResult result2) {
             return new RrtExecutionResult(
-                ImmutableList.<MailAddress>builder()
+                ImmutableSet.<MailAddress>builder()
                     .addAll(result1.getNewRecipients())
                     .addAll(result2.getNewRecipients())
                     .build(),
-                ImmutableList.<MailAddress>builder()
+                ImmutableSet.<MailAddress>builder()
                     .addAll(result1.getRecipientWithError())
                     .addAll(result2.getRecipientWithError())
                     .build());
         }
 
-        private final ImmutableList<MailAddress> newRecipients;
-        private final ImmutableList<MailAddress> recipientWithError;
+        private final ImmutableSet<MailAddress> newRecipients;
+        private final ImmutableSet<MailAddress> recipientWithError;
 
-        public RrtExecutionResult(ImmutableList<MailAddress> newRecipients, 
ImmutableList<MailAddress> recipientWithError) {
+        public RrtExecutionResult(ImmutableSet<MailAddress> newRecipients, 
ImmutableSet<MailAddress> recipientWithError) {
             this.newRecipients = newRecipients;
             this.recipientWithError = recipientWithError;
         }
 
-        public List<MailAddress> getNewRecipients() {
+        public Set<MailAddress> getNewRecipients() {
             return newRecipients;
         }
 
-        public List<MailAddress> getRecipientWithError() {
+        public Set<MailAddress> getRecipientWithError() {
             return recipientWithError;
         }
 
diff --git 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessorTest.java
 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessorTest.java
index 6290fab..746ebef 100644
--- 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessorTest.java
+++ 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessorTest.java
@@ -35,6 +35,7 @@ import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 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.MappingsImpl;
 import org.apache.james.util.MimeMessageUtil;
 import org.apache.mailet.Mail;
@@ -258,6 +259,26 @@ public class RecipientRewriteTableProcessorTest {
         assertThat(mailetContext.getSentMails()).containsOnly(expected);
         
assertThat(mail.getRecipients()).containsOnly(MailAddressFixture.ANY_AT_LOCAL);
     }
+
+    @Test
+    public void processShouldNotDuplicateRewrittenMailAddresses() throws 
Exception {
+        when(virtualTableStore.getResolvedMappings(eq("other"), 
eq(Domain.of(MailAddressFixture.JAMES_LOCAL))))
+            .thenReturn(MappingsImpl.builder()
+                .add(Mapping.alias(MailAddressFixture.ANY_AT_LOCAL.asString()))
+                .add(Mapping.group(MailAddressFixture.ANY_AT_LOCAL.asString()))
+                .build());
+
+        mail = FakeMail.builder()
+            .name("mail")
+            .sender(MailAddressFixture.ANY_AT_JAMES)
+            .mimeMessage(message)
+            .recipients(MailAddressFixture.OTHER_AT_LOCAL)
+            .build();
+
+        processor.processMail(mail);
+
+        
assertThat(mail.getRecipients()).containsExactly(MailAddressFixture.ANY_AT_LOCAL);
+    }
     
     @Test
     public void 
processShouldSendMailToAllErrorRecipientsWhenRecipientRewriteTableException() 
throws Exception {


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

Reply via email to