You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ma...@apache.org on 2018/04/24 12:50:45 UTC

[16/27] james-project git commit: JAMES-2366 Error mapping should throw when trying to rewrite a User

JAMES-2366 Error mapping should throw when trying to rewrite a User


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

Branch: refs/heads/master
Commit: 6a5b348d5538adbc3baf4ff64eb08b1e310448bb
Parents: ef5ff3f
Author: Matthieu Baechler <ma...@apache.org>
Authored: Wed Apr 18 15:07:05 2018 +0200
Committer: Matthieu Baechler <ma...@apache.org>
Committed: Tue Apr 24 14:45:26 2018 +0200

----------------------------------------------------------------------
 .../java/org/apache/james/rrt/lib/Mapping.java  |  7 ++--
 .../org/apache/james/rrt/lib/UserRewritter.java |  9 +++--
 .../apache/james/rrt/lib/NoneRewiterTest.java   | 36 -------------------
 .../james/rrt/lib/ThrowingRewriterTest.java     | 38 ++++++++++++++++++++
 .../rrt/lib/AbstractRecipientRewriteTable.java  |  3 --
 .../mailets/AbstractRecipientRewriteTable.java  |  3 ++
 6 files changed, 51 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6a5b348d/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 7f0e684..7795014 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
@@ -29,6 +29,7 @@ import javax.mail.internet.AddressException;
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
 import org.apache.james.core.User;
+import org.apache.james.rrt.api.RecipientRewriteTable;
 import org.apache.james.rrt.api.RecipientRewriteTableException;
 
 import com.google.common.base.Objects;
@@ -55,7 +56,7 @@ public interface Mapping {
             case Domain:
                 return new UserRewritter.DomainRewriter();
             case Error:
-                return new UserRewritter.NoneRewriter();
+                return new UserRewritter.ThrowingRewriter();
             case Forward:
             case Group:
             case Address:
@@ -276,7 +277,7 @@ public interface Mapping {
         }
 
         @Override
-        public Optional<User> rewriteUser(User user) throws AddressException {
+        public Optional<User> rewriteUser(User user) throws AddressException, RecipientRewriteTable.ErrorMappingException {
             return rewriter.rewrite(user);
         }
 
@@ -328,6 +329,6 @@ public interface Mapping {
 
     String getErrorMessage();
 
-    Optional<User> rewriteUser(User user) throws AddressException;
+    Optional<User> rewriteUser(User user) throws AddressException, RecipientRewriteTable.ErrorMappingException;
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/6a5b348d/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java
index df97d17..b9e96de 100644
--- a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java
+++ b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java
@@ -31,6 +31,7 @@ import javax.mail.internet.AddressException;
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
 import org.apache.james.core.User;
+import org.apache.james.rrt.api.RecipientRewriteTable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,7 +42,7 @@ import com.google.common.collect.ImmutableList;
 @FunctionalInterface
 public interface UserRewritter {
 
-    Optional<User> rewrite(User user) throws AddressException;
+    Optional<User> rewrite(User user) throws AddressException, RecipientRewriteTable.ErrorMappingException;
 
     interface MappingUserRewriter {
         UserRewritter generateUserRewriter(String mapping);
@@ -65,10 +66,12 @@ public interface UserRewritter {
         }
     }
 
-    class NoneRewriter implements MappingUserRewriter {
+    class ThrowingRewriter implements MappingUserRewriter {
         @Override
         public UserRewritter generateUserRewriter(String mapping) {
-            return oldUser -> Optional.empty();
+            return user -> {
+                throw new RecipientRewriteTable.ErrorMappingException(mapping);
+            };
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/6a5b348d/server/data/data-api/src/test/java/org/apache/james/rrt/lib/NoneRewiterTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/test/java/org/apache/james/rrt/lib/NoneRewiterTest.java b/server/data/data-api/src/test/java/org/apache/james/rrt/lib/NoneRewiterTest.java
deleted file mode 100644
index c1cdd8c..0000000
--- a/server/data/data-api/src/test/java/org/apache/james/rrt/lib/NoneRewiterTest.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one   *
- * or more contributor license agreements.  See the NOTICE file *
- * distributed with this work for additional information        *
- * regarding copyright ownership.  The ASF licenses this file   *
- * to you under the Apache License, Version 2.0 (the            *
- * "License"); you may not use this file except in compliance   *
- * with the License.  You may obtain a copy of the License at   *
- *                                                              *
- *   http://www.apache.org/licenses/LICENSE-2.0                 *
- *                                                              *
- * Unless required by applicable law or agreed to in writing,   *
- * software distributed under the License is distributed on an  *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
- * KIND, either express or implied.  See the License for the    *
- * specific language governing permissions and limitations      *
- * under the License.                                           *
- ****************************************************************/
-
-package org.apache.james.rrt.lib;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import org.apache.james.core.User;
-import org.junit.jupiter.api.Test;
-
-public class NoneRewiterTest {
-    @Test
-    public void rewriteShouldReturnEmpty() throws Exception {
-        assertThat(
-            new UserRewritter.NoneRewriter()
-                .generateUserRewriter("any")
-                .rewrite(User.fromUsername("any")))
-            .isEmpty();
-    }
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/6a5b348d/server/data/data-api/src/test/java/org/apache/james/rrt/lib/ThrowingRewriterTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/test/java/org/apache/james/rrt/lib/ThrowingRewriterTest.java b/server/data/data-api/src/test/java/org/apache/james/rrt/lib/ThrowingRewriterTest.java
new file mode 100644
index 0000000..8bcf06e
--- /dev/null
+++ b/server/data/data-api/src/test/java/org/apache/james/rrt/lib/ThrowingRewriterTest.java
@@ -0,0 +1,38 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.rrt.lib;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.core.User;
+import org.apache.james.rrt.api.RecipientRewriteTable;
+import org.junit.jupiter.api.Test;
+
+public class ThrowingRewriterTest {
+    @Test
+    public void rewriteShouldReturnEmpty() throws Exception {
+        assertThatThrownBy(() ->
+            new UserRewritter.ThrowingRewriter()
+                .generateUserRewriter("any")
+                .rewrite(User.fromUsername("any")))
+            .isInstanceOf(RecipientRewriteTable.ErrorMappingException.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/6a5b348d/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 4a22f4b..b842dc7 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
@@ -110,9 +110,6 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
 
         Mappings targetMappings = mapAddress(user.getLocalPart(), user.getDomainPart().get());
 
-        if (targetMappings.contains(Type.Error)) {
-            throw new ErrorMappingException(targetMappings.getError().getErrorMessage());
-        }
 
         try {
             return MappingsImpl.fromMappings(

http://git-wip-us.apache.org/repos/asf/james-project/blob/6a5b348d/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTable.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTable.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTable.java
index 94b9794..391da56 100755
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTable.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTable.java
@@ -38,6 +38,7 @@ import org.apache.james.core.User;
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
+import org.apache.james.rrt.api.RecipientRewriteTable;
 import org.apache.james.rrt.lib.Mapping;
 import org.apache.james.rrt.lib.UserRewritter;
 import org.apache.james.server.core.MailImpl;
@@ -129,6 +130,8 @@ public abstract class AbstractRecipientRewriteTable extends GenericMailet {
                                 targetAddress = maybeTarget.get();
                             } catch (PatternSyntaxException e) {
                                 LOGGER.error("Exception during regexMap processing: ", e);
+                            } catch (RecipientRewriteTable.ErrorMappingException e) {
+                                LOGGER.error("Regex mapping should not throw ErrorMappingException", e);
                             }
                         }
 


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