james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [13/24] james-project git commit: JAMES-2366 Improve AbstractRRT::getFixedUser
Date Fri, 20 Apr 2018 02:12:58 GMT
JAMES-2366 Improve AbstractRRT::getFixedUser


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

Branch: refs/heads/master
Commit: e4bc90382a284b0956d6ab61d6bd8629dbf56d52
Parents: 7222968
Author: benwa <btellier@linagora.com>
Authored: Fri Apr 6 10:38:41 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Thu Apr 19 11:13:59 2018 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/rrt/lib/Mapping.java  |  7 ++++
 .../rrt/lib/AbstractRecipientRewriteTable.java  | 37 ++++++++------------
 .../org/apache/james/rrt/lib/MappingImpl.java   | 20 +++++++++--
 .../apache/james/rrt/lib/MappingImplTest.java   | 34 ++++++++++++++++--
 .../mailets/RecipientRewriteTableProcessor.java |  3 +-
 5 files changed, 71 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e4bc9038/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 95c0599..1c7d3a6 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
@@ -25,6 +25,7 @@ import java.util.function.Supplier;
 
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
+import org.apache.james.rrt.api.RecipientRewriteTableException;
 
 import com.google.common.base.Preconditions;
 
@@ -90,6 +91,12 @@ public interface Mapping {
 
     boolean hasDomain();
 
+    interface ThrowingDomainSupplier {
+        Domain get() throws RecipientRewriteTableException;
+    }
+
+    Mapping appendDomainFromThrowingSupplierIfNone(ThrowingDomainSupplier supplier) throws
RecipientRewriteTableException;
+
     Mapping appendDomainIfNone(Supplier<Domain> domainSupplier);
 
     String getErrorMessage();

http://git-wip-us.apache.org/repos/asf/james-project/blob/e4bc9038/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
index 6058f81..c9c54b1 100644
--- a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
+++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
@@ -20,7 +20,6 @@ package org.apache.james.rrt.lib;
 
 import java.util.Map;
 import java.util.Optional;
-import java.util.function.Supplier;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 
@@ -40,7 +39,7 @@ import org.apache.james.rrt.lib.Mapping.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.github.fge.lambdas.Throwing;
+import com.google.common.base.Preconditions;
 
 public abstract class AbstractRecipientRewriteTable implements RecipientRewriteTable, Configurable
{
     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRecipientRewriteTable.class);
@@ -206,7 +205,7 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
     @Override
     public void addAddressMapping(String user, Domain domain, String address) throws RecipientRewriteTableException
{
         Mapping mapping = MappingImpl.address(address)
-            .appendDomainIfNone(defaultDomainSupplier());
+            .appendDomainFromThrowingSupplierIfNone(this::defaultDomain);
 
         checkHasValidAddress(mapping);
         checkMapping(user, domain, mapping);
@@ -215,14 +214,12 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
         addMapping(user, domain, mapping);
     }
 
-    private Supplier<Domain> defaultDomainSupplier() throws RecipientRewriteTableException
{
-        return Throwing.supplier(() -> {
-            try {
-                return domainList.getDefaultDomain();
-            } catch (DomainListException e) {
-                throw new RecipientRewriteTableException("Unable to retrieve default domain",
e);
-            }
-        }).sneakyThrow();
+    private Domain defaultDomain() throws RecipientRewriteTableException {
+        try {
+            return domainList.getDefaultDomain();
+        } catch (DomainListException e) {
+            throw new RecipientRewriteTableException("Unable to retrieve default domain",
e);
+        }
     }
 
     private void checkHasValidAddress(Mapping mapping) throws RecipientRewriteTableException
{
@@ -234,7 +231,7 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
     @Override
     public void removeAddressMapping(String user, Domain domain, String address) throws RecipientRewriteTableException
{
         Mapping mapping = MappingImpl.address(address)
-            .appendDomainIfNone(defaultDomainSupplier());
+            .appendDomainFromThrowingSupplierIfNone(this::defaultDomain);
 
         LOGGER.info("Remove address mapping => {} for user: {} domain: {}", mapping, user,
domain.name());
         removeMapping(user, domain, mapping);
@@ -279,7 +276,7 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
     @Override
     public void addForwardMapping(String user, Domain domain, String address) throws RecipientRewriteTableException
{
         Mapping mapping = MappingImpl.forward(address)
-            .appendDomainIfNone(defaultDomainSupplier());
+            .appendDomainFromThrowingSupplierIfNone(this::defaultDomain);
 
         checkHasValidAddress(mapping);
         checkMapping(user, domain, mapping);
@@ -291,7 +288,7 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
     @Override
     public void removeForwardMapping(String user, Domain domain, String address) throws RecipientRewriteTableException
{
         Mapping mapping = MappingImpl.forward(address)
-            .appendDomainIfNone(defaultDomainSupplier());
+            .appendDomainFromThrowingSupplierIfNone(this::defaultDomain);
 
         LOGGER.info("Remove forward mapping => {} for user: {} domain: {}", mapping, user,
domain.name());
         removeMapping(user, domain, mapping);
@@ -326,15 +323,9 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
      * @return fixedUser the fixed user String
      */
     protected String getFixedUser(String user) {
-        if (user != null) {
-            if (user.equals(WILDCARD) || !user.contains("@")) {
-                return user;
-            } else {
-                throw new IllegalArgumentException("Invalid user: " + user);
-            }
-        } else {
-            return WILDCARD;
-        }
+        String sanitizedUser = Optional.ofNullable(user).orElse(WILDCARD);
+        Preconditions.checkArgument(sanitizedUser.equals(WILDCARD) || !sanitizedUser.contains("@"));
+        return sanitizedUser;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/e4bc9038/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 48ed54b..04ca426 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
@@ -28,6 +28,7 @@ import javax.mail.internet.AddressException;
 
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
+import org.apache.james.rrt.api.RecipientRewriteTableException;
 
 import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
@@ -85,16 +86,29 @@ public class MappingImpl implements Mapping, Serializable {
     public boolean hasDomain() {
         return mapping.contains("@");
     }
-    
+
+    @Override
+    public Mapping appendDomainFromThrowingSupplierIfNone(ThrowingDomainSupplier supplier)
throws RecipientRewriteTableException {
+        Preconditions.checkNotNull(supplier);
+        if (hasDomain()) {
+            return this;
+        }
+        return appendDomain(supplier.get());
+    }
+
     @Override
     public Mapping appendDomainIfNone(Supplier<Domain> domain) {
         Preconditions.checkNotNull(domain);
         if (hasDomain()) {
             return this;
         }
-        return new MappingImpl(type, mapping + "@" + domain.get().asString());
+        return appendDomain(domain.get());
     }
-    
+
+    private MappingImpl appendDomain(Domain domain) {
+        return new MappingImpl(type, mapping + "@" + domain.asString());
+    }
+
     @Override
     public Type getType() {
         return type;

http://git-wip-us.apache.org/repos/asf/james-project/blob/e4bc9038/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 ab1e8d3..72c6439 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
@@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
+import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.junit.Test;
 
 import nl.jqno.equalsverifier.EqualsVerifier;
@@ -97,11 +98,38 @@ public class MappingImplTest {
         assertThat(MappingImpl.address("abc@d").appendDomainIfNone(() -> Domain.of("domain"))).isEqualTo(MappingImpl.address("abc@d"));
     }
     
-    @Test(expected = NullPointerException.class)
+    @Test
     public void appendDomainShouldThrowWhenNullDomain() {
-        MappingImpl.address("abc@d").appendDomainIfNone(null);
+        assertThatThrownBy(() -> MappingImpl.address("abc@d").appendDomainIfNone(null)).isInstanceOf(NullPointerException.class);
     }
-    
+
+    @Test
+    public void appendDomainFromThrowingSupplierIfNoneShouldWorkOnValidDomain() throws RecipientRewriteTableException
{
+        assertThat(MappingImpl.address("abc").appendDomainFromThrowingSupplierIfNone(() ->
Domain.of("domain")))
+            .isEqualTo(MappingImpl.address("abc@domain"));
+    }
+
+    @Test
+    public void appendDomainFromThrowingSupplierIfNoneShouldNotAddDomainWhenMappingAlreadyContainsDomains()
throws RecipientRewriteTableException {
+        assertThat(MappingImpl.address("abc@d").appendDomainFromThrowingSupplierIfNone(()
-> Domain.of("domain")))
+            .isEqualTo(MappingImpl.address("abc@d"));
+    }
+
+    @Test
+    public void appendDomainFromThrowingSupplierIfNoneShouldThrowWhenNullDomain() {
+        assertThatThrownBy(() -> MappingImpl.address("abc@d").appendDomainFromThrowingSupplierIfNone(null))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void appendDomainFromThrowingSupplierIfNoneShouldNotCatchRecipientRewriteTableException()
{
+        Mapping.ThrowingDomainSupplier supplier = () -> {
+            throw new RecipientRewriteTableException("message");
+        };
+        assertThatThrownBy(() -> MappingImpl.address("abc").appendDomainFromThrowingSupplierIfNone(supplier))
+            .isInstanceOf(RecipientRewriteTableException.class);
+    }
+
     @Test
     public void getTypeShouldReturnAddressWhenNoPrefix() {
         assertThat(MappingImpl.address("abc").getType()).isEqualTo(Mapping.Type.Address);

http://git-wip-us.apache.org/repos/asf/james-project/blob/e4bc9038/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 7a30cef..d015f54 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
@@ -33,6 +33,7 @@ import org.apache.james.domainlist.api.DomainListException;
 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.Mappings;
 import org.apache.james.util.MemoizedSupplier;
 import org.apache.james.util.OptionalUtils;
@@ -162,7 +163,7 @@ public class RecipientRewriteTableProcessor {
     List<MailAddress> handleMappings(Mappings mappings, MailAddress sender, MailAddress
recipient, MimeMessage message) throws MessagingException {
         ImmutableList<MailAddress> mailAddresses = mappings.asStream()
             .map(mapping -> mapping.appendDomainIfNone(defaultDomainSupplier))
-            .map(mapping -> mapping.asMailAddress())
+            .map(Mapping::asMailAddress)
             .flatMap(OptionalUtils::toStream)
             .collect(Guavate.toImmutableList());
 


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