james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [07/24] james-project git commit: JAMES-2366 Mapping::getAddress should return Optional<MailAddress>
Date Fri, 06 Apr 2018 13:20:26 GMT
JAMES-2366 Mapping::getAddress should return Optional<MailAddress>


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

Branch: refs/heads/master
Commit: 53d760bb37b494f3ebfb095d0a3a1c101efd58e1
Parents: 57c417c
Author: benwa <btellier@linagora.com>
Authored: Fri Mar 30 09:57:05 2018 +0700
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Fri Apr 6 14:46:01 2018 +0200

----------------------------------------------------------------------
 .../java/org/apache/james/rrt/lib/Mapping.java  |  4 ++-
 .../org/apache/james/rrt/lib/MappingImpl.java   | 16 +++++++++--
 .../apache/james/rrt/lib/MappingImplTest.java   | 29 +++++++++++++-------
 .../mailets/RecipientRewriteTableProcessor.java | 27 +-----------------
 .../james/webadmin/routes/GroupsRoutes.java     |  5 +++-
 5 files changed, 40 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/53d760bb/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 bb3ec7e..95c0599 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
@@ -20,9 +20,11 @@
 
 package org.apache.james.rrt.lib;
 
+import java.util.Optional;
 import java.util.function.Supplier;
 
 import org.apache.james.core.Domain;
+import org.apache.james.core.MailAddress;
 
 import com.google.common.base.Preconditions;
 
@@ -44,7 +46,7 @@ public interface Mapping {
         return Type.Address;
     }
 
-    String getAddress();
+    Optional<MailAddress> asMailAddress();
 
     enum Type {
         Regex("regex:", 3),

http://git-wip-us.apache.org/repos/asf/james-project/blob/53d760bb/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 0c663a9..48ed54b 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
@@ -21,9 +21,13 @@
 package org.apache.james.rrt.lib;
 
 import java.io.Serializable;
+import java.util.Optional;
 import java.util.function.Supplier;
 
+import javax.mail.internet.AddressException;
+
 import org.apache.james.core.Domain;
+import org.apache.james.core.MailAddress;
 
 import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
@@ -103,9 +107,15 @@ public class MappingImpl implements Mapping, Serializable {
     }
 
     @Override
-    public String getAddress() {
-        Preconditions.checkState(getType() == Type.Address);
-        return mapping;
+    public Optional<MailAddress> asMailAddress() {
+        if (type != Type.Address && type != Type.Forward) {
+            return Optional.empty();
+        }
+        try {
+            return Optional.of(new MailAddress(mapping));
+        } catch (AddressException e) {
+            return Optional.empty();
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/53d760bb/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
index e18823f..ab1e8d3 100644
--- a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java
@@ -24,6 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import org.apache.james.core.Domain;
+import org.apache.james.core.MailAddress;
 import org.junit.Test;
 
 import nl.jqno.equalsverifier.EqualsVerifier;
@@ -148,27 +149,35 @@ public class MappingImplTest {
     }
 
     @Test
-    public void getAddressShouldReturnMappingValueForAddress() {
-        assertThat(MappingImpl.address("value").getAddress()).isEqualTo("value");
+    public void asMailAddressShouldReturnMappingValueForAddress() throws Exception {
+        assertThat(MappingImpl.address("value@domain").asMailAddress())
+            .contains(new MailAddress("value@domain"));
     }
 
     @Test
-    public void getAddressShouldThrowForError() {
-        assertThatThrownBy(() -> MappingImpl.error("value").getAddress()).isInstanceOf(IllegalStateException.class);
+    public void asMailAddressShouldReturnEmptyOnInvalidAddress() {
+        assertThat(MappingImpl.address("value").asMailAddress())
+            .isEmpty();
     }
 
     @Test
-    public void getAddressShouldThrowForRegex() {
-        assertThatThrownBy(() -> MappingImpl.regex("value").getAddress()).isInstanceOf(IllegalStateException.class);
+    public void asMailAddressShouldReturnEmptyForError() {
+        assertThat(MappingImpl.error("value").asMailAddress()).isEmpty();
     }
 
     @Test
-    public void getAddressShouldThrowForDomain() {
-        assertThatThrownBy(() -> MappingImpl.domain(Domain.of("value")).getAddress()).isInstanceOf(IllegalStateException.class);
+    public void asMailAddressShouldReturnEmptyForRegex() {
+        assertThat(MappingImpl.regex("value").asMailAddress()).isEmpty();
     }
 
     @Test
-    public void getAddressShouldThrowForForward() {
-        assertThatThrownBy(() -> MappingImpl.forward("value").getAddress()).isInstanceOf(IllegalStateException.class);
+    public void asMailAddressShouldReturnEmptyForDomain() {
+        assertThat(MappingImpl.domain(Domain.of("value")).asMailAddress()).isEmpty();
+    }
+
+    @Test
+    public void asMailAddressShouldReturnMappingValueForForward() throws Exception {
+        assertThat(MappingImpl.forward("value@domain").asMailAddress())
+            .contains(new MailAddress("value@domain"));
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/53d760bb/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 c1bc666..7a30cef 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
@@ -20,12 +20,10 @@
 package org.apache.james.transport.mailets;
 
 import java.util.List;
-import java.util.Optional;
 import java.util.function.Function;
 import java.util.function.Supplier;
 
 import javax.mail.MessagingException;
-import javax.mail.internet.AddressException;
 import javax.mail.internet.MimeMessage;
 
 import org.apache.james.core.Domain;
@@ -35,8 +33,6 @@ import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.rrt.api.RecipientRewriteTable;
 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.Mapping.Type;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.util.MemoizedSupplier;
 import org.apache.james.util.OptionalUtils;
@@ -105,27 +101,6 @@ public class RecipientRewriteTableProcessor {
     private final MailetContext mailetContext;
     private final Supplier<Domain> defaultDomainSupplier;
 
-    private static final Function<Mapping, Optional<MailAddress>> mailAddressFromMapping
=
-        mapping -> {
-            Type type = mapping.getType();
-            switch (type) {
-                case Address:
-                    return parseMappingToMailAddress(mapping.asString());
-                case Forward:
-                    return parseMappingToMailAddress(type.withoutPrefix(mapping.asString()));
-                default:
-                    return Optional.empty();
-            }
-        };
-
-    private static Optional<MailAddress> parseMappingToMailAddress(String mapping)
{
-        try {
-            return Optional.of(new MailAddress(mapping));
-        } catch (AddressException e) {
-            return Optional.empty();
-        }
-    }
-
     public RecipientRewriteTableProcessor(RecipientRewriteTable virtualTableStore, DomainList
domainList, MailetContext mailetContext) {
         this.virtualTableStore = virtualTableStore;
         this.mailetContext = mailetContext;
@@ -187,7 +162,7 @@ public class RecipientRewriteTableProcessor {
     List<MailAddress> handleMappings(Mappings mappings, MailAddress sender, MailAddress
recipient, MimeMessage message) throws MessagingException {
         ImmutableList<MailAddress> mailAddresses = mappings.asStream()
             .map(mapping -> mapping.appendDomainIfNone(defaultDomainSupplier))
-            .map(mailAddressFromMapping)
+            .map(mapping -> mapping.asMailAddress())
             .flatMap(OptionalUtils::toStream)
             .collect(Guavate.toImmutableList());
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/53d760bb/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/GroupsRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/GroupsRoutes.java
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/GroupsRoutes.java
index 5126804..29ec2f8 100644
--- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/GroupsRoutes.java
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/GroupsRoutes.java
@@ -48,6 +48,7 @@ import org.apache.james.rrt.lib.Mapping;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.api.UsersRepositoryException;
+import org.apache.james.util.OptionalUtils;
 import org.apache.james.webadmin.Constants;
 import org.apache.james.webadmin.Routes;
 import org.apache.james.webadmin.utils.ErrorResponder;
@@ -228,7 +229,9 @@ public class GroupsRoutes implements Routes {
 
         return mappings.select(Mapping.Type.Address)
                 .asStream()
-                .map(Mapping::getAddress)
+                .map(Mapping::asMailAddress)
+                .flatMap(OptionalUtils::toStream)
+                .map(MailAddress::asString)
                 .collect(Guavate.toImmutableSortedSet());
     }
 


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