JAMES-2366 Avoid String parsing for default domain appending

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

Branch: refs/heads/master
Commit: a71d033fa4b9aff6aa7563e4fae2482705c8421a
Parents: a64ee17
Author: benwa <btell...@linagora.com>
Authored: Wed Apr 18 09:17:31 2018 +0700
Committer: Matthieu Baechler <matth...@apache.org>
Committed: Tue Apr 24 14:45:26 2018 +0200

----------------------------------------------------------------------
 .../main/java/org/apache/james/core/User.java   | 22 +++++++++++++++-----
 .../java/org/apache/james/core/UserTest.java    | 16 +++++++++++++-
 .../rrt/lib/AbstractRecipientRewriteTable.java  | 16 +++++---------
 3 files changed, 37 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/a71d033f/core/src/main/java/org/apache/james/core/User.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/james/core/User.java 
b/core/src/main/java/org/apache/james/core/User.java
index 40f5069..8b5421e 100644
--- a/core/src/main/java/org/apache/james/core/User.java
+++ b/core/src/main/java/org/apache/james/core/User.java
@@ -47,8 +47,13 @@ public class User {
     public static User fromLocalPartWithDomain(String localPart, String 
domain) {
         Preconditions.checkNotNull(domain);
 
-        return from(localPart,
-            Optional.of(domain));
+        return fromLocalPartWithDomain(localPart, Domain.of(domain));
+    }
+
+    public static User fromLocalPartWithDomain(String localPart, Domain 
domain) {
+        Preconditions.checkNotNull(domain);
+
+        return new User(localPart, Optional.of(domain));
     }
 
     public static User fromLocalPartWithoutDomain(String localPart) {
@@ -57,19 +62,19 @@ public class User {
     }
 
     public static User from(String localPart, Optional<String> domain) {
-       return new User(localPart, domain);
+       return new User(localPart, domain.map(Domain::of));
     }
 
     private final String localPart;
     private final Optional<Domain> domainPart;
 
-    private User(String localPart, Optional<String> domainPart) {
+    private User(String localPart, Optional<Domain> domainPart) {
         Preconditions.checkNotNull(localPart);
         Preconditions.checkArgument(!localPart.isEmpty(), "username should not 
be empty");
         Preconditions.checkArgument(!localPart.contains("@"), "username can 
not contain domain delimiter");
 
         this.localPart = localPart;
-        this.domainPart = domainPart.map(Domain::of);
+        this.domainPart = domainPart;
     }
 
     public String getLocalPart() {
@@ -84,6 +89,13 @@ public class User {
         return domainPart.isPresent();
     }
 
+    public User withDefaultDomain(Domain defaultDomain) {
+        if (hasDomainPart()) {
+            return this;
+        }
+        return new User(localPart, Optional.of(defaultDomain));
+    }
+
     public String asString() {
         return domainPart.map(domain -> localPart + "@" + domain.asString())
             .orElse(localPart);

http://git-wip-us.apache.org/repos/asf/james-project/blob/a71d033f/core/src/test/java/org/apache/james/core/UserTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/james/core/UserTest.java 
b/core/src/test/java/org/apache/james/core/UserTest.java
index bc97eba..cd4e60a 100644
--- a/core/src/test/java/org/apache/james/core/UserTest.java
+++ b/core/src/test/java/org/apache/james/core/UserTest.java
@@ -78,7 +78,8 @@ public class UserTest {
 
     @Test
     public void 
fromLocalPartWithDomainStringVersionShouldThrowOnNullDomainPart() {
-        assertThatThrownBy(() -> User.fromLocalPartWithDomain("local", null))
+        String domain = null;
+        assertThatThrownBy(() -> User.fromLocalPartWithDomain("local", domain))
             .isInstanceOf(NullPointerException.class);
     }
 
@@ -178,4 +179,17 @@ public class UserTest {
             .isTrue();
     }
 
+    @Test
+    public void withDefaultDomainShouldAppendDefaultDomainWhenNone() {
+        assertThat(User.fromUsername("user")
+            .withDefaultDomain(Domain.LOCALHOST))
+            .isEqualTo(User.fromLocalPartWithDomain("user", Domain.LOCALHOST));
+    }
+
+    @Test
+    public void 
withDefaultDomainShouldNotAppendDefaultDomainWhenDomainIsPresent() {
+        assertThat(User.fromUsername("user@domain")
+            .withDefaultDomain(Domain.LOCALHOST))
+            .isEqualTo(User.fromUsername("user@domain"));
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/a71d033f/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 ce9701c..23c60c2 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
@@ -31,6 +31,7 @@ import 
org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
+import org.apache.james.core.User;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.lifecycle.api.Configurable;
@@ -139,18 +140,11 @@ public abstract class AbstractRecipientRewriteTable 
implements RecipientRewriteT
             return Stream.of(toMapping(addressWithMappingApplied, 
target.getType()));
         }
 
-        String userName;
-        Domain targetDomain;
-        String[] args = addressWithMappingApplied.split("@");
+        User coreUser = User.fromUsername(addressWithMappingApplied)
+            .withDefaultDomain(domain);
 
-        if (args.length > 1) {
-            userName = args[0];
-            targetDomain = Domain.of(args[1]);
-        } else {
-            // TODO Is that the right todo here?
-            userName = addressWithMappingApplied;
-            targetDomain = domain;
-        }
+        String userName = coreUser.getLocalPart();
+        Domain targetDomain = coreUser.getDomainPart().get();
 
         // Check if the returned mapping is the same as the
         // input. If so return null to avoid loops


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to