james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [01/24] james-project git commit: JAMES-2366 Rely on enhanced Mapping.Type where applicable
Date Tue, 03 Apr 2018 10:02:47 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 213965f40 -> 12bbac5c5


JAMES-2366 Rely on enhanced Mapping.Type where applicable


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

Branch: refs/heads/master
Commit: 8f1689e3e516046d67f39748a9d111b9d1be040f
Parents: e2cc20f
Author: benwa <btellier@linagora.com>
Authored: Wed Mar 28 10:06:21 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Apr 3 16:58:07 2018 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/rrt/lib/Mapping.java  | 22 +++++++++-
 .../rrt/lib/AbstractRecipientRewriteTable.java  | 46 ++++++++++----------
 .../org/apache/james/rrt/lib/MappingImpl.java   | 12 +----
 .../lib/AbstractRecipientRewriteTableTest.java  | 13 ++----
 4 files changed, 49 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8f1689e3/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 51645d0..21a6741 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
@@ -22,11 +22,26 @@ package org.apache.james.rrt.lib;
 
 import org.apache.james.core.Domain;
 
+import com.google.common.base.Preconditions;
+
 public interface Mapping {
 
+    static Type detectType(String input) {
+        if (input.startsWith(Type.Regex.asPrefix())) {
+            return Type.Regex;
+        }
+        if (input.startsWith(Type.Domain.asPrefix())) {
+            return Type.Domain;
+        }
+        if (input.startsWith(Type.Error.asPrefix())) {
+            return Type.Error;
+        }
+        return Type.Address;
+    }
+
     String getAddress();
 
-    enum Type { 
+    enum Type {
         Regex("regex:"), 
         Domain("domain:"), 
         Error("error:"), 
@@ -41,6 +56,11 @@ public interface Mapping {
         public String asPrefix() {
             return asPrefix;
         }
+
+        public String withoutPrefix(String input) {
+            Preconditions.checkArgument(input.startsWith(asPrefix));
+            return input.substring(asPrefix.length());
+        }
     }
 
     Type getType();

http://git-wip-us.apache.org/repos/asf/james-project/blob/8f1689e3/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 764981e..d5651fc 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,14 +119,14 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
                 MappingsImpl.Builder mappings = MappingsImpl.builder();
 
                 for (String target : targetMappings.asStrings()) {
-                    if (target.startsWith(Type.Regex.asPrefix())) {
+                    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 (target.startsWith(Type.Domain.asPrefix())) {
-                        target = user + "@" + target.substring(Type.Domain.asPrefix().length());
+                    } else if (Mapping.detectType(target).equals(Type.Domain)) {
+                        target = user + "@" + Type.Domain.withoutPrefix(target);
                     }
 
                     if (target == null) {
@@ -246,44 +246,42 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
 
     @Override
     public void addMapping(String user, Domain domain, String mapping) throws RecipientRewriteTableException
{
-
         String map = mapping.toLowerCase(Locale.US);
-
-        if (map.startsWith(Type.Error.asPrefix())) {
-            addErrorMapping(user, domain, map.substring(Type.Error.asPrefix().length()));
-        } else if (map.startsWith(Type.Regex.asPrefix())) {
-            addRegexMapping(user, domain, map.substring(Type.Regex.asPrefix().length()));
-        } else if (map.startsWith(Type.Domain.asPrefix())) {
+        Type mappingType = Mapping.detectType(map);
+        String mappingSuffix = mappingType.withoutPrefix(map);
+
+        if (mappingType.equals(Type.Error)) {
+            addErrorMapping(user, domain, mappingSuffix);
+        } else if (mappingType.equals(Type.Regex)) {
+            addRegexMapping(user, domain, mappingSuffix);
+        } else if (mappingType.equals(Type.Domain)) {
             if (user != null) {
                 throw new RecipientRewriteTableException("User must be null for aliasDomain
mappings");
             }
-            String domainName = map.substring(Type.Domain.asPrefix().length());
-            addAliasDomainMapping(domain, Domain.of(domainName));
+            addAliasDomainMapping(domain, Domain.of(mappingSuffix));
         } else {
-            addAddressMapping(user, domain, map);
+            addAddressMapping(user, domain, mappingSuffix);
         }
-
     }
 
     @Override
     public void removeMapping(String user, Domain domain, String mapping) throws RecipientRewriteTableException
{
-
         String map = mapping.toLowerCase(Locale.US);
-
-        if (map.startsWith(Type.Error.asPrefix())) {
-            removeErrorMapping(user, domain, map.substring(Type.Error.asPrefix().length()));
-        } else if (map.startsWith(Type.Regex.asPrefix())) {
-            removeRegexMapping(user, domain, map.substring(Type.Regex.asPrefix().length()));
-        } else if (map.startsWith(Type.Domain.asPrefix())) {
+        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");
             }
-            String domainName = map.substring(Type.Domain.asPrefix().length());
-            removeAliasDomainMapping(domain, Domain.of(domainName));
+            removeAliasDomainMapping(domain, Domain.of(mappingSuffix));
         } else {
             removeAddressMapping(user, domain, map);
         }
-
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/8f1689e3/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 87e093a..8e0040e 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
@@ -33,16 +33,8 @@ public class MappingImpl implements Mapping, Serializable {
     private static final long serialVersionUID = 1L;
 
     public static MappingImpl of(String mapping) {
-        if (mapping.startsWith(Type.Regex.asPrefix())) {
-            return new MappingImpl(Type.Regex, mapping.substring(Type.Regex.asPrefix().length()));
-        }
-        if (mapping.startsWith(Type.Error.asPrefix())) {
-            return new MappingImpl(Type.Error, mapping.substring(Type.Error.asPrefix().length()));
-        }
-        if (mapping.startsWith(Type.Domain.asPrefix())) {
-            return new MappingImpl(Type.Domain, mapping.substring(Type.Domain.asPrefix().length()));
-        }
-        return new MappingImpl(Type.Address, mapping.substring(Type.Address.asPrefix().length()));
+        Type type = Mapping.detectType(mapping);
+        return new MappingImpl(type, type.withoutPrefix(mapping));
     }
     
     public static MappingImpl address(String mapping) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/8f1689e3/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
index 6cfc49b..6877e2e 100644
--- a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTableTest.java
@@ -300,14 +300,9 @@ public abstract class AbstractRecipientRewriteTableTest {
             RecipientRewriteTableException;
 
     private void removeMapping(String user, Domain domain, String rawMapping) throws RecipientRewriteTableException
{
-        if (rawMapping.startsWith(Type.Error.asPrefix())) {
-            removeMapping(user, domain, rawMapping.substring(Type.Error.asPrefix().length()),
Type.Error);
-        } else if (rawMapping.startsWith(Type.Regex.asPrefix())) {
-            removeMapping(user, domain, rawMapping.substring(Type.Regex.asPrefix().length()),
Type.Regex);
-        } else if (rawMapping.startsWith(Type.Domain.asPrefix())) {
-            removeMapping(user, domain, rawMapping.substring(Type.Domain.asPrefix().length()),
Type.Domain);
-        } else {
-            removeMapping(user, domain, rawMapping, Type.Address);
-        }
+        Type type = Mapping.detectType(rawMapping);
+        String mappingSuffix = type.withoutPrefix(rawMapping);
+
+        removeMapping(user, domain, mappingSuffix, type);
     }
 }


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