james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [12/24] james-project git commit: JAMES-2366 Refactor RecipientRewriteTableUtil.getSeparator method
Date Tue, 03 Apr 2018 10:02:58 GMT
JAMES-2366 Refactor RecipientRewriteTableUtil.getSeparator method


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

Branch: refs/heads/master
Commit: f5ebe4f7f53072e903bcadcd81c4052b0172fe7a
Parents: 169190c
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Tue Mar 27 15:23:25 2018 +0200
Committer: benwa <btellier@linagora.com>
Committed: Tue Apr 3 16:59:44 2018 +0700

----------------------------------------------------------------------
 server/data/data-api/pom.xml                    |  5 ++
 .../java/org/apache/james/rrt/lib/Mapping.java  |  8 ++-
 .../org/apache/james/rrt/lib/MappingTest.java   | 51 ++++++++++++++++++++
 .../rrt/lib/RecipientRewriteTableUtil.java      | 35 +++++++++++---
 .../rrt/lib/RecipientRewriteTableUtilTest.java  | 23 +++++++++
 .../mailets/JDBCRecipientRewriteTable.java      |  8 ++-
 6 files changed, 120 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/f5ebe4f7/server/data/data-api/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-api/pom.xml b/server/data/data-api/pom.xml
index 45f3aa2..f04f3a7 100644
--- a/server/data/data-api/pom.xml
+++ b/server/data/data-api/pom.xml
@@ -60,6 +60,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-engine</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/james-project/blob/f5ebe4f7/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 21a6741..231dd3b 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
@@ -49,7 +49,7 @@ public interface Mapping {
 
         private final String asPrefix;
 
-        private Type(String asPrefix) {
+        Type(String asPrefix) {
             this.asPrefix = asPrefix;
         }
 
@@ -61,6 +61,12 @@ public interface Mapping {
             Preconditions.checkArgument(input.startsWith(asPrefix));
             return input.substring(asPrefix.length());
         }
+
+        public static boolean hasPrefix(String mapping) {
+            return mapping.startsWith(Regex.asPrefix())
+                || mapping.startsWith(Domain.asPrefix())
+                || mapping.startsWith(Error.asPrefix());
+        }
     }
 
     Type getType();

http://git-wip-us.apache.org/repos/asf/james-project/blob/f5ebe4f7/server/data/data-api/src/test/java/org/apache/james/rrt/lib/MappingTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/test/java/org/apache/james/rrt/lib/MappingTest.java
b/server/data/data-api/src/test/java/org/apache/james/rrt/lib/MappingTest.java
new file mode 100644
index 0000000..5277358
--- /dev/null
+++ b/server/data/data-api/src/test/java/org/apache/james/rrt/lib/MappingTest.java
@@ -0,0 +1,51 @@
+/****************************************************************
+ * 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.rrt.lib.Mapping.Type;
+import org.junit.jupiter.api.Test;
+
+public class MappingTest {
+
+    @Test
+    public void hasPrefixShouldReturnTrueWhenRegex() {
+        boolean hasPrefix = Mapping.Type.hasPrefix(Type.Regex.asPrefix() + "myRegex");
+        assertThat(hasPrefix).isTrue();
+    }
+
+    @Test
+    public void hasPrefixShouldReturnTrueWhenDomain() {
+        boolean hasPrefix = Mapping.Type.hasPrefix(Type.Domain.asPrefix() + "myRegex");
+        assertThat(hasPrefix).isTrue();
+    }
+
+    @Test
+    public void hasPrefixShouldReturnTrueWhenError() {
+        boolean hasPrefix = Mapping.Type.hasPrefix(Type.Error.asPrefix() + "myRegex");
+        assertThat(hasPrefix).isTrue();
+    }
+
+    @Test
+    public void hasPrefixShouldReturnFalseWhenAddress() {
+        boolean hasPrefix = Mapping.Type.hasPrefix(Type.Address.asPrefix() + "myRegex");
+        assertThat(hasPrefix).isFalse();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/f5ebe4f7/server/data/data-library/src/main/java/org/apache/james/rrt/lib/RecipientRewriteTableUtil.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/RecipientRewriteTableUtil.java
b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/RecipientRewriteTableUtil.java
index 44c5db1..ef4febe 100644
--- a/server/data/data-library/src/main/java/org/apache/james/rrt/lib/RecipientRewriteTableUtil.java
+++ b/server/data/data-library/src/main/java/org/apache/james/rrt/lib/RecipientRewriteTableUtil.java
@@ -21,6 +21,7 @@ package org.apache.james.rrt.lib;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Optional;
 import java.util.StringTokenizer;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -29,6 +30,7 @@ import java.util.regex.PatternSyntaxException;
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
 import org.apache.james.rrt.lib.Mapping.Type;
+import org.apache.james.util.OptionalUtils;
 
 /**
  * This helper class contains methods for the RecipientRewriteTable implementations
@@ -38,10 +40,6 @@ public class RecipientRewriteTableUtil {
     private RecipientRewriteTableUtil() {
     }
 
-    // @deprecated QUERY is deprecated - SQL queries are now located in
-    // sqlResources.xml
-    public static final String QUERY = "select RecipientRewriteTable.target_address from
RecipientRewriteTable, RecipientRewriteTable as VUTDomains where (RecipientRewriteTable.user
like ? or RecipientRewriteTable.user like '\\%') and (RecipientRewriteTable.domain like ?
or (RecipientRewriteTable.domain like '%*%' and VUTDomains.domain like ?)) order by concat(RecipientRewriteTable.user,'@',RecipientRewriteTable.domain)
desc limit 1";
-
     /**
      * Processes regex virtual user mapping
      * 
@@ -130,8 +128,7 @@ public class RecipientRewriteTableUtil {
             index = input.indexOf(find, index + findLength);
         }
 
-        String result = output.toString();
-        return result;
+        return output.toString();
     }
 
     /**
@@ -180,7 +177,31 @@ public class RecipientRewriteTableUtil {
      * @return the character to tokenize on
      */
     public static String getSeparator(String targetString) {
-        return (targetString.indexOf(',') > -1 ? "," : (targetString.indexOf(';') >
-1 ? ";" : ((targetString.contains(Type.Error.asPrefix()) || targetString.contains(Type.Regex.asPrefix())
|| targetString.contains(Type.Domain.asPrefix())) ? "" : ":")));
+        return OptionalUtils.or(
+                mayContainComma(targetString),
+                mayContainSemicolon(targetString),
+                mayContainColon(targetString))
+            .orElse("");
+    }
+
+    private static Optional<String> mayContainComma(String targetString) {
+        return mayContain(targetString, ",");
+    }
+
+    private static Optional<String> mayContainSemicolon(String targetString) {
+        return mayContain(targetString, ";");
+    }
+
+    private static Optional<String> mayContainColon(String targetString) {
+        if (Type.hasPrefix(targetString)) {
+            return Optional.empty();
+        }
+        return Optional.of(":");
+    }
+
+    private static Optional<String> mayContain(String targetString, String expectedCharacter)
{
+        return Optional.of(expectedCharacter)
+            .filter(targetString::contains);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/f5ebe4f7/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableUtilTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableUtilTest.java
b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableUtilTest.java
index 50b86b8..3c0b9b4 100644
--- a/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableUtilTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/rrt/lib/RecipientRewriteTableUtilTest.java
@@ -32,4 +32,27 @@ public class RecipientRewriteTableUtilTest {
             .isEqualTo("admin@test");
     }
 
+    @Test
+    public void getSeparatorShouldReturnCommaWhenCommaIsPresent() {
+        String separator = RecipientRewriteTableUtil.getSeparator("regex:(.*)@localhost,
regex:user@test");
+        assertThat(separator).isEqualTo(",");
+    }
+
+    @Test
+    public void getSeparatorShouldReturnEmptyWhenColonIsPresentInPrefix() {
+        String separator = RecipientRewriteTableUtil.getSeparator("regex:(.*)@localhost");
+        assertThat(separator).isEqualTo("");
+    }
+
+    @Test
+    public void getSeparatorShouldReturnEmptyWhenColonIsPresent() {
+        String separator = RecipientRewriteTableUtil.getSeparator("(.*)@localhost: user@test");
+        assertThat(separator).isEqualTo(":");
+    }
+
+    @Test
+    public void getSeparatorShouldReturnColonWhenNoSeparator() {
+        String separator = RecipientRewriteTableUtil.getSeparator("user@test");
+        assertThat(separator).isEqualTo(":");
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/f5ebe4f7/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/JDBCRecipientRewriteTable.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/JDBCRecipientRewriteTable.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/JDBCRecipientRewriteTable.java
index 30058ea..3cdf57f 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/JDBCRecipientRewriteTable.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/JDBCRecipientRewriteTable.java
@@ -32,7 +32,6 @@ import javax.mail.MessagingException;
 import javax.sql.DataSource;
 
 import org.apache.james.core.MailAddress;
-import org.apache.james.rrt.lib.RecipientRewriteTableUtil;
 import org.apache.james.util.sql.JDBCUtil;
 import org.apache.mailet.Experimental;
 import org.apache.mailet.MailetException;
@@ -111,6 +110,11 @@ import org.apache.mailet.MailetException;
 @Experimental
 @Deprecated
 public class JDBCRecipientRewriteTable extends AbstractRecipientRewriteTable {
+
+    // @deprecated QUERY is deprecated - SQL queries are now located in
+    // sqlResources.xml
+    private static final String QUERY = "select RecipientRewriteTable.target_address from
RecipientRewriteTable, RecipientRewriteTable as VUTDomains where (RecipientRewriteTable.user
like ? or RecipientRewriteTable.user like '\\%') and (RecipientRewriteTable.domain like ?
or (RecipientRewriteTable.domain like '%*%' and VUTDomains.domain like ?)) order by concat(RecipientRewriteTable.user,'@',RecipientRewriteTable.domain)
desc limit 1";
+
     protected DataSource datasource;
 
     /**
@@ -156,7 +160,7 @@ public class JDBCRecipientRewriteTable extends AbstractRecipientRewriteTable
{
             }
 
             // Build the query
-            query = getInitParameter("sqlquery", RecipientRewriteTableUtil.QUERY);
+            query = getInitParameter("sqlquery", QUERY);
         } catch (MailetException me) {
             throw me;
         } catch (Exception e) {


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