james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [15/24] james-project git commit: JAMES-2366 Use switch/case when possible on Mapping.Type
Date Tue, 03 Apr 2018 10:03:01 GMT
JAMES-2366 Use switch/case when possible on Mapping.Type


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

Branch: refs/heads/master
Commit: 146208d753d629acad9226b6617ade0114186a69
Parents: 9b3027e
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Wed Mar 28 16:13:46 2018 +0200
Committer: benwa <btellier@linagora.com>
Committed: Tue Apr 3 17:00:14 2018 +0700

----------------------------------------------------------------------
 .../rrt/lib/AbstractRecipientRewriteTable.java  | 68 ++++++++++++--------
 1 file changed, 41 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/146208d7/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 d5651fc..346f1cf 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
@@ -119,35 +119,28 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
                 MappingsImpl.Builder mappings = MappingsImpl.builder();
 
                 for (String target : targetMappings.asStrings()) {
-                    if (Mapping.detectType(target).equals(Type.Regex)) {
-                        try {
-                            target = RecipientRewriteTableUtil.regexMap(new MailAddress(user,
domain.asString()), target);
-                        } catch (PatternSyntaxException | ParseException e) {
-                            LOGGER.error("Exception during regexMap processing: ", e);
-                        }
-                    } else if (Mapping.detectType(target).equals(Type.Domain)) {
-                        target = user + "@" + Type.Domain.withoutPrefix(target);
-                    }
+                    Type type = Mapping.detectType(target);
+                    Optional<String> maybeAddressWithMappingApplied = applyMapping(user,
domain, target, type);
 
-                    if (target == null) {
+                    if (!maybeAddressWithMappingApplied.isPresent()) {
                         continue;
                     }
-
-                    String buf = "Valid virtual user mapping " + user + "@" + domain.name()
+ " to " + target;
+                    String addressWithMappingApplied = maybeAddressWithMappingApplied.get();
+                    String buf = "Valid virtual user mapping " + user + "@" + domain.name()
+ " to " + maybeAddressWithMappingApplied;
                     LOGGER.debug(buf);
 
                     if (recursive) {
 
                         String userName;
                         Domain targetDomain;
-                        String[] args = target.split("@");
+                        String[] args = addressWithMappingApplied.split("@");
 
                         if (args.length > 1) {
                             userName = args[0];
                             targetDomain = Domain.of(args[1]);
                         } else {
                             // TODO Is that the right todo here?
-                            userName = target;
+                            userName = addressWithMappingApplied;
                             targetDomain = domain;
                         }
 
@@ -161,13 +154,13 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
 
                         if (childMappings == null || childMappings.isEmpty()) {
                             // add mapping
-                            mappings.add(target);
+                            mappings.add(addressWithMappingApplied);
                         } else {
                             mappings = mappings.addAll(childMappings);
                         }
 
                     } else {
-                        mappings.add(target);
+                        mappings.add(addressWithMappingApplied);
                     }
                 }
                 return mappings.build();
@@ -177,6 +170,22 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
         return null;
     }
 
+    private Optional<String> applyMapping(String user, Domain domain, String target,
Type type) {
+        switch (type) {
+            case Regex:
+                try {
+                    return Optional.ofNullable(RecipientRewriteTableUtil.regexMap(new MailAddress(user,
domain.asString()), target));
+                } catch (PatternSyntaxException | ParseException e) {
+                    LOGGER.error("Exception during regexMap processing: ", e);
+                    return Optional.ofNullable(target);
+                }
+            case Domain:
+                return Optional.of(user + "@" + Type.Domain.withoutPrefix(target));
+            default:
+                return Optional.ofNullable(target);
+        }
+    }
+
     @Override
     public void addRegexMapping(String user, Domain domain, String regex) throws RecipientRewriteTableException
{
         try {
@@ -270,17 +279,22 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
         Type mappingType = Mapping.detectType(map);
         String mappingSuffix = mappingType.withoutPrefix(map);
 
-        if (mappingType.equals(Type.Error)) {
-            removeErrorMapping(user, domain, mappingSuffix);
-        } else if (mappingType.equals(Type.Regex)) {
-            removeRegexMapping(user, domain, mappingSuffix);
-        } else if (mappingType.equals(Type.Domain)) {
-            if (user != null) {
-                throw new RecipientRewriteTableException("User must be null for aliasDomain
mappings");
-            }
-            removeAliasDomainMapping(domain, Domain.of(mappingSuffix));
-        } else {
-            removeAddressMapping(user, domain, map);
+        switch (mappingType) {
+            case Error:
+                removeErrorMapping(user, domain, mappingSuffix);
+                break;
+            case Regex:
+                removeRegexMapping(user, domain, mappingSuffix);
+                break;
+            case Domain:
+                if (user != null) {
+                    throw new RecipientRewriteTableException("User must be null for aliasDomain
mappings");
+                }
+                removeAliasDomainMapping(domain, Domain.of(mappingSuffix));
+                break;
+            case Address:
+                removeAddressMapping(user, domain, map);
+                break;
         }
     }
 


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