james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [18/24] james-project git commit: JAMES-2366 Mapping should allow appending a domain if none
Date Tue, 03 Apr 2018 10:03:04 GMT
JAMES-2366 Mapping should allow appending a domain if none

This simplifies logic in RRT processor without impact on other classes.

Furthermore addresses like "a@b@domain.com" makes no sens.


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

Branch: refs/heads/master
Commit: 982c4535a404b7a82405f89edf7762604f8f90f6
Parents: 66596d6
Author: benwa <btellier@linagora.com>
Authored: Fri Mar 30 11:20:38 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Apr 3 17:00:23 2018 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/rrt/lib/Mapping.java  |  4 +-
 .../org/apache/james/rrt/lib/MappingImpl.java   |  8 +++-
 .../apache/james/rrt/lib/MappingImplTest.java   | 12 +++---
 .../mailets/RecipientRewriteTableProcessor.java | 43 ++++++--------------
 .../RecipientRewriteTableProcessorTest.java     |  2 +-
 5 files changed, 29 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/982c4535/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
index 231dd3b..4bbf11f 100644
--- a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
+++ b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java
@@ -20,6 +20,8 @@
 
 package org.apache.james.rrt.lib;
 
+import java.util.function.Supplier;
+
 import org.apache.james.core.Domain;
 
 import com.google.common.base.Preconditions;
@@ -75,7 +77,7 @@ public interface Mapping {
 
     boolean hasDomain();
 
-    Mapping appendDomain(Domain domain);
+    Mapping appendDomainIfNone(Supplier<Domain> domainSupplier);
 
     String getErrorMessage();
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/982c4535/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
index 8e0040e..fcd6572 100644
--- a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
+++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java
@@ -21,6 +21,7 @@
 package org.apache.james.rrt.lib;
 
 import java.io.Serializable;
+import java.util.function.Supplier;
 
 import org.apache.james.core.Domain;
 
@@ -74,9 +75,12 @@ public class MappingImpl implements Mapping, Serializable {
     }
     
     @Override
-    public Mapping appendDomain(Domain domain) {
+    public Mapping appendDomainIfNone(Supplier<Domain> domain) {
         Preconditions.checkNotNull(domain);
-        return new MappingImpl(type, mapping + "@" + domain.asString());
+        if (hasDomain()) {
+            return this;
+        }
+        return new MappingImpl(type, mapping + "@" + domain.get().asString());
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/982c4535/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
index cd353a8..8810d83 100644
--- a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
@@ -73,23 +73,23 @@ public class MappingImplTest {
     }
 
     @Test
-    public void hasDomainshouldReturnFalseWhenMappingDoesntContainAtMark() {
+    public void hasDefaultDomainShouldReturnFalseWhenMappingDoesntContainAtMark() {
         assertThat(MappingImpl.address("abc").hasDomain()).isFalse();
     }
     
     @Test
-    public void appendDomainShouldWorkOnValidDomain() {
-        assertThat(MappingImpl.address("abc").appendDomain(Domain.of("domain"))).isEqualTo(MappingImpl.address("abc@domain"));
+    public void appendDefaultDomainShouldWorkOnValidDomain() {
+        assertThat(MappingImpl.address("abc").appendDomainIfNone(() -> Domain.of("domain"))).isEqualTo(MappingImpl.address("abc@domain"));
     }
     
     @Test
-    public void appendDomainShouldWorkWhenMappingAlreadyContainsDomains() {
-        assertThat(MappingImpl.address("abc@d").appendDomain(Domain.of("domain"))).isEqualTo(MappingImpl.address("abc@d@domain"));
+    public void appendDefaultDomainShouldNotAddDomainWhenMappingAlreadyContainsDomains()
{
+        assertThat(MappingImpl.address("abc@d").appendDomainIfNone(() -> Domain.of("domain"))).isEqualTo(MappingImpl.address("abc@d"));
     }
     
     @Test(expected = NullPointerException.class)
     public void appendDomainShouldThrowWhenNullDomain() {
-        MappingImpl.address("abc@d").appendDomain(null);
+        MappingImpl.address("abc@d").appendDomainIfNone(null);
     }
     
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/982c4535/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 b3bb847..76961e4 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
@@ -23,7 +23,6 @@ import java.util.List;
 import java.util.Optional;
 import java.util.function.Function;
 import java.util.function.Supplier;
-import java.util.stream.Stream;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
@@ -102,8 +101,8 @@ public class RecipientRewriteTableProcessor {
     }
 
     private final RecipientRewriteTable virtualTableStore;
-    private final DomainList domainList;
     private final MailetContext mailetContext;
+    private final Supplier<Domain> defaultDomainSupplier;
 
     private static final Function<Mapping, Optional<MailAddress>> mailAddressFromMapping
=
         addressMapping -> {
@@ -116,8 +115,17 @@ public class RecipientRewriteTableProcessor {
 
     public RecipientRewriteTableProcessor(RecipientRewriteTable virtualTableStore, DomainList
domainList, MailetContext mailetContext) {
         this.virtualTableStore = virtualTableStore;
-        this.domainList = domainList;
         this.mailetContext = mailetContext;
+        this.defaultDomainSupplier = MemoizedSupplier.of(
+            Throwing.supplier(() -> getDefaultDomain(domainList)).sneakyThrow());
+    }
+
+    private Domain getDefaultDomain(DomainList domainList) throws MessagingException {
+        try {
+            return domainList.getDefaultDomain();
+        } catch (DomainListException e) {
+            throw new MessagingException("Unable to access DomainList", e);
+        }
     }
 
     public void processMail(Mail mail) throws MessagingException {
@@ -164,7 +172,8 @@ public class RecipientRewriteTableProcessor {
 
     @VisibleForTesting
     List<MailAddress> handleMappings(Mappings mappings, MailAddress sender, MailAddress
recipient, MimeMessage message) throws MessagingException {
-        ImmutableList<MailAddress> mailAddresses = sanitizedMappings(mappings)
+        ImmutableList<MailAddress> mailAddresses = mappings.asStream()
+            .map(mapping -> mapping.appendDomainIfNone(defaultDomainSupplier))
             .map(mailAddressFromMapping)
             .flatMap(OptionalUtils::toStream)
             .collect(Guavate.toImmutableList());
@@ -174,30 +183,12 @@ public class RecipientRewriteTableProcessor {
         return getLocalAddresses(mailAddresses);
     }
 
-    private Stream<Mapping> sanitizedMappings(Mappings mappings) throws MessagingException
{
-        ImmutableList<Mapping> sanitizedMappings = sanitizeMappingsWithNoDomain(mappings,
domainList);
-
-        return Stream.concat(
-            mappings.asStream().filter(Mapping::hasDomain),
-            sanitizedMappings.stream());
-    }
-
     private ImmutableList<MailAddress> getLocalAddresses(ImmutableList<MailAddress>
mailAddresses) {
         return mailAddresses.stream()
             .filter(mailAddress -> mailetContext.isLocalServer(mailAddress.getDomain()))
             .collect(Guavate.toImmutableList());
     }
 
-    private ImmutableList<Mapping> sanitizeMappingsWithNoDomain(Mappings mappings,
DomainList domainList) throws MessagingException {
-        Supplier<Domain> defaultDomainSupplier = MemoizedSupplier.of(
-            Throwing.supplier(() -> getDefaultDomain(domainList)).sneakyThrow());
-
-        return mappings.asStream()
-            .filter(mapping -> !mapping.hasDomain())
-            .map(mapping -> mapping.appendDomain(defaultDomainSupplier.get()))
-            .collect(Guavate.toImmutableList());
-    }
-
     private void forwardToRemoteAddress(MailAddress sender, MailAddress recipient, MimeMessage
message, ImmutableList<MailAddress> mailAddresses) {
         ImmutableList<MailAddress> remoteAddresses = mailAddresses.stream()
             .filter(mailAddress -> !mailetContext.isLocalServer(mailAddress.getDomain()))
@@ -213,12 +204,4 @@ public class RecipientRewriteTableProcessor {
         }
     }
 
-    private Domain getDefaultDomain(DomainList domainList) throws MessagingException {
-        try {
-            return domainList.getDefaultDomain();
-        } catch (DomainListException e) {
-            throw new MessagingException("Unable to access DomainList", e);
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/982c4535/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RecipientRewriteTableProcessorTest.java
----------------------------------------------------------------------
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 5caf722..41eafac 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
@@ -52,7 +52,6 @@ public class RecipientRewriteTableProcessorTest {
     private static final String NONEDOMAIN = "nonedomain";
     private static final String INVALID_MAIL_ADDRESS = "server-dev@";
 
-
     private FakeMail mail;
     private MimeMessage message;
     private MappingsImpl mappings;
@@ -328,6 +327,7 @@ public class RecipientRewriteTableProcessorTest {
     @Test
     public void processShouldResetMailStateToGhostWhenCanNotBuildNewRecipient() throws Exception
{
         when(virtualTableStore.getMappings(any(String.class), any(Domain.class))).thenReturn(mappings);
+        when(domainList.getDefaultDomain()).thenReturn(Domain.of(MailAddressFixture.JAMES_LOCAL));
 
         mail = FakeMail.builder()
             .mimeMessage(message)


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