james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [08/24] james-project git commit: JAMES-2366 Improve style in RecipientRewriteTableProcessor
Date Tue, 03 Apr 2018 10:02:54 GMT
JAMES-2366 Improve style in RecipientRewriteTableProcessor

 - Avoid confusions between mailAddress and mapping
 - New mappings means "sanitized" (a sanitized mapping being one with a domain)
 - Rely on 'memoization'
 - Avoid intermediary collects
 - Review and rework method encapsulation (it lead to bad reading and understanding)
 - Use plural when needed


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

Branch: refs/heads/master
Commit: 9d13366ab4810dafdef8ef7e8fe7131c8f2a7c14
Parents: fc278a8
Author: benwa <btellier@linagora.com>
Authored: Wed Mar 28 13:46:34 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Apr 3 16:59:44 2018 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/util/GuavaUtils.java  |  4 ++
 .../mailets/RecipientRewriteTableProcessor.java | 62 +++++++++-----------
 2 files changed, 31 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9d13366a/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
b/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
index 518ead1..f5493c9 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/GuavaUtils.java
@@ -34,4 +34,8 @@ public class GuavaUtils {
             .flatMap(e -> e.getValue().stream().map(right -> Pair.of(e.getKey(), right)))
             .collect(Guavate.toImmutableListMultimap(Pair::getKey, Pair::getValue));
     }
+
+    public static <T> com.google.common.base.Supplier<T> toGuava(java.util.function.Supplier<T>
javaSupplier) {
+        return javaSupplier::get;
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9d13366a/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 f48e132..2ca1e40 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
@@ -39,15 +39,19 @@ 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.GuavaUtils;
 import org.apache.james.util.OptionalUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailetContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.github.fge.lambdas.Throwing;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 
@@ -135,24 +139,23 @@ public class RecipientRewriteTableProcessor {
     }
 
     @VisibleForTesting
-    List<MailAddress> handleMappings(Mappings mappings, MailAddress sender, MailAddress
recipient, MimeMessage message) 
-            throws MessagingException {
-        ImmutableList<Mapping> addressMappingWithoutDomains = getAddressWithNoDomain(mappings,
domainList);
-
-        ImmutableList<Mapping> newAddressMappings = convertToNewMappings(mappings,
addressMappingWithoutDomains);
-
-        ImmutableList<MailAddress> mailAddresses = buildMailAddressFromMappingAddress(newAddressMappings);
+    List<MailAddress> handleMappings(Mappings mappings, MailAddress sender, MailAddress
recipient, MimeMessage message) throws MessagingException {
+        ImmutableList<MailAddress> mailAddresses = sanitizedMappings(mappings)
+            .map(mailAddressFromMapping)
+            .flatMap(OptionalUtils::toStream)
+            .collect(Guavate.toImmutableList());
 
         forwardToRemoteAddress(sender, recipient, message, mailAddresses);
 
         return getLocalAddresses(mailAddresses);
     }
 
-    private ImmutableList<Mapping> convertToNewMappings(Mappings mappings, ImmutableList<Mapping>
addressWithoutDomains) {
+    private Stream<Mapping> sanitizedMappings(Mappings mappings) throws MessagingException
{
+        ImmutableList<Mapping> sanitizedMappings = sanitizeMappingsWithNoDomain(mappings,
domainList);
+
         return Stream.concat(
-                mappings.asStream().filter(Mapping::hasDomain),
-                addressWithoutDomains.stream())
-            .collect(Guavate.toImmutableList());
+            mappings.asStream().filter(Mapping::hasDomain),
+            sanitizedMappings.stream());
     }
 
     private ImmutableList<MailAddress> getLocalAddresses(ImmutableList<MailAddress>
mailAddresses) {
@@ -161,40 +164,29 @@ public class RecipientRewriteTableProcessor {
             .collect(Guavate.toImmutableList());
     }
 
-    private ImmutableList<MailAddress> buildMailAddressFromMappingAddress(ImmutableList<Mapping>
newMappings) {
-        return newMappings.stream()
-            .map(mailAddressFromMapping)
-            .filter(Optional::isPresent)
-            .map(Optional::get)
-            .collect(Guavate.toImmutableList());
-    }
+    private ImmutableList<Mapping> sanitizeMappingsWithNoDomain(Mappings mappings,
DomainList domainList) throws MessagingException {
+        Supplier<Domain> defaultDomainSupplier = Suppliers.memoize(
+            GuavaUtils.toGuava(
+            Throwing.supplier(() -> getDefaultDomain(domainList))
+                .sneakyThrow()));
 
-    private ImmutableList<Mapping> getAddressWithNoDomain(Mappings mappings, DomainList
domainList) throws MessagingException {
-        ImmutableList<Mapping> addressWithoutDomains = mappings.asStream()
-            .filter(address -> !address.hasDomain())
+        return mappings.asStream()
+            .filter(mapping -> !mapping.hasDomain())
+            .map(mapping -> mapping.appendDomain(defaultDomainSupplier.get()))
             .collect(Guavate.toImmutableList());
-        
-        if (!addressWithoutDomains.isEmpty()) {
-            Domain defaultDomain = getDefaultDomain(domainList);
-
-            return addressWithoutDomains.stream()
-                .map(address -> address.appendDomain(defaultDomain))
-                .collect(Guavate.toImmutableList());
-        }
-        return ImmutableList.of();
     }
 
     private void forwardToRemoteAddress(MailAddress sender, MailAddress recipient, MimeMessage
message, ImmutableList<MailAddress> mailAddresses) {
-        ImmutableList<MailAddress> remoteAddress = mailAddresses.stream()
+        ImmutableList<MailAddress> remoteAddresses = mailAddresses.stream()
             .filter(mailAddress -> !mailetContext.isLocalServer(mailAddress.getDomain()))
             .collect(Guavate.toImmutableList());
 
-        if (!remoteAddress.isEmpty()) {
+        if (!remoteAddresses.isEmpty()) {
             try {
-                mailetContext.sendMail(sender, remoteAddress, message);
-                LOGGER.info("Mail for {} forwarded to {}", recipient, remoteAddress);
+                mailetContext.sendMail(sender, remoteAddresses, message);
+                LOGGER.info("Mail for {} forwarded to {}", recipient, remoteAddresses);
             } catch (MessagingException ex) {
-                LOGGER.warn("Error forwarding mail to {}", remoteAddress);
+                LOGGER.warn("Error forwarding mail to {}", remoteAddresses);
             }
         }
     }


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