james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [45/50] [abbrv] james-project git commit: MAILET-155 Improve style of SpecialAddressesUtils
Date Wed, 11 Jan 2017 09:26:33 GMT
MAILET-155 Improve style of SpecialAddressesUtils


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

Branch: refs/heads/master
Commit: 2bf97805547b003d4b9540fc6ca095e50fb459dc
Parents: ae9b5d9
Author: Benoit Tellier <btellier@linagora.com>
Authored: Wed Jan 11 10:16:29 2017 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Jan 11 11:01:50 2017 +0700

----------------------------------------------------------------------
 .../transport/util/SpecialAddressesUtils.java   | 110 ++++++++-----------
 1 file changed, 47 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2bf97805/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
index d2ee392..223162a 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.james.transport.util;
 
+import java.util.Collection;
 import java.util.List;
 import java.util.Set;
 
@@ -73,46 +74,37 @@ public class SpecialAddressesUtils {
     public List<MailAddress> replaceSpecialAddresses(Mail mailWithReplacementAddresses,
List<MailAddress> mailAddresses) {
         ImmutableList.Builder<MailAddress> builder = ImmutableList.builder();
         for (MailAddress mailAddress : mailAddresses) {
-            if (!SpecialAddress.isSpecialAddress(mailAddress)) {
-                builder.add(mailAddress);
-                continue;
-            }
+            builder.addAll(getCorrespondingAddress(mailWithReplacementAddresses, mailAddress));
+        }
+        return builder.build();
+    }
 
-            SpecialAddressKind specialAddressKind = SpecialAddressKind.forValue(mailAddress.getLocalPart());
-            if (specialAddressKind == null) {
-                builder.add(mailAddress);
-                continue;
-            }
-            switch (specialAddressKind) {
+    private Collection<MailAddress> getCorrespondingAddress(Mail mail, MailAddress
mailAddress) {
+        if (!SpecialAddress.isSpecialAddress(mailAddress)) {
+            return ImmutableSet.of(mailAddress);
+        }
+
+        SpecialAddressKind specialAddressKind = SpecialAddressKind.forValue(mailAddress.getLocalPart());
+        if (specialAddressKind == null) {
+            return ImmutableSet.of(mailAddress);
+        }
+        switch (specialAddressKind) {
             case SENDER:
             case FROM:
-                MailAddress sender = mailWithReplacementAddresses.getSender();
-                if (sender != null) {
-                    builder.add(sender);
-                }
-                break;
-            case REPLY_TO:
-                builder.addAll(getReplyTosFromMail(mailWithReplacementAddresses));
-                break;
             case REVERSE_PATH:
-                MailAddress reversePath = mailWithReplacementAddresses.getSender();
-                if (reversePath != null) {
-                    builder.add(reversePath);
-                }
-                break;
+                return Optional.fromNullable(mail.getSender()).asSet();
+            case REPLY_TO:
+                return getReplyTosFromMail(mail);
             case RECIPIENTS:
             case TO:
-                builder.addAll(mailWithReplacementAddresses.getRecipients());
-                break;
+                return mail.getRecipients();
             case UNALTERED:
             case NULL:
                 break;
             case DELETE:
-                builder.add(mailAddress);
-                break;
-            }
+                return ImmutableSet.of(mailAddress);
         }
-        return builder.build();
+        return ImmutableList.of();
     }
 
     private Set<MailAddress> getReplyTosFromMail(Mail mail) {
@@ -170,60 +162,52 @@ public class SpecialAddressesUtils {
     public List<MailAddress> replaceInternetAddresses(Mail mailWithReplacementAddresses,
List<InternetAddress> internetAddresses) throws MessagingException {
         ImmutableList.Builder<MailAddress> builder = ImmutableList.builder();
         for (InternetAddress internetAddress : internetAddresses) {
-            MailAddress mailAddress = new MailAddress(internetAddress);
-            if (!SpecialAddress.isSpecialAddress(mailAddress)) {
-                builder.add(new MailAddress(internetAddress));
-                continue;
-            }
+            builder.addAll(getCorrespondingAddress(internetAddress, mailWithReplacementAddresses));
+        }
+        return builder.build();
+    }
 
-            SpecialAddressKind specialAddressKind = SpecialAddressKind.forValue(mailAddress.getLocalPart());
-            if (specialAddressKind == null) {
-                builder.add(mailAddress);
-                continue;
-            }
+    private Collection<MailAddress> getCorrespondingAddress(InternetAddress internetAddress,
Mail mail) throws AddressException {
+        MailAddress mailAddress = new MailAddress(internetAddress);
+        if (!SpecialAddress.isSpecialAddress(mailAddress)) {
+            return ImmutableSet.of(new MailAddress(internetAddress));
+        }
+
+        SpecialAddressKind specialAddressKind = SpecialAddressKind.forValue(mailAddress.getLocalPart());
+        if (specialAddressKind == null) {
+            return ImmutableSet.of(new MailAddress(internetAddress));
+        }
 
-            switch (specialAddressKind) {
+        switch (specialAddressKind) {
             case SENDER:
-                MailAddress sender = mailWithReplacementAddresses.getSender();
-                if (sender != null) {
-                    builder.add(sender);
-                }
-                break;
             case REVERSE_PATH:
-                MailAddress reversePath = mailWithReplacementAddresses.getSender();
-                if (reversePath != null) {
-                    builder.add(reversePath);
-                }
-                break;
+                return Optional.fromNullable(mail.getSender()).asSet();
             case FROM:
                 try {
-                    InternetAddress[] fromArray = (InternetAddress[]) mailWithReplacementAddresses.getMessage().getFrom();
-                    builder.addAll(allOrSender(mailWithReplacementAddresses, fromArray));
+                    InternetAddress[] fromArray = (InternetAddress[]) mail.getMessage().getFrom();
+                    return allOrSender(mail, fromArray);
                 } catch (MessagingException me) {
                     mailet.log("Unable to parse the \"FROM\" header in the original message;
ignoring.");
+                    return ImmutableSet.of();
                 }
-                break;
             case REPLY_TO:
                 try {
-                    InternetAddress[] replyToArray = (InternetAddress[]) mailWithReplacementAddresses.getMessage().getReplyTo();
-                    builder.addAll(allOrSender(mailWithReplacementAddresses, replyToArray));
+                    InternetAddress[] replyToArray = (InternetAddress[]) mail.getMessage().getReplyTo();
+                    return allOrSender(mail, replyToArray);
                 } catch (MessagingException me) {
                     mailet.log("Unable to parse the \"REPLY_TO\" header in the original message;
ignoring.");
+                    return ImmutableSet.of();
                 }
-                break;
             case TO:
             case RECIPIENTS:
-                builder.addAll(toHeaders(mailWithReplacementAddresses));
-                break;
+                return toHeaders(mail);
             case NULL:
             case UNALTERED:
-                break;
+                return ImmutableList.of();
             case DELETE:
-                builder.add(new MailAddress(internetAddress));
-                break;
-            }
+                return ImmutableSet.of(new MailAddress(internetAddress));
         }
-        return builder.build();
+        return ImmutableList.of();
     }
 
     private List<MailAddress> allOrSender(Mail mail, InternetAddress[] addresses) throws
AddressException {


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