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:51 UTC
[22/27] james-project git commit: JAMES-2366 refactor
RecipientRewriteTableUtil.regexMap
JAMES-2366 refactor RecipientRewriteTableUtil.regexMap
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/620bf692
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/620bf692
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/620bf692
Branch: refs/heads/master
Commit: 620bf69202726a672189c11ecb195a4c543c20f8
Parents: 930e7e5
Author: Matthieu Baechler <ma...@apache.org>
Authored: Tue Apr 17 16:24:50 2018 +0200
Committer: Matthieu Baechler <ma...@apache.org>
Committed: Tue Apr 24 14:45:26 2018 +0200
----------------------------------------------------------------------
.../rrt/lib/AbstractRecipientRewriteTable.java | 2 +-
.../rrt/lib/RecipientRewriteTableUtil.java | 101 ++++++-------------
.../rrt/lib/RecipientRewriteTableUtilTest.java | 51 +++++++++-
.../mailets/AbstractRecipientRewriteTable.java | 3 +-
4 files changed, 83 insertions(+), 74 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/620bf692/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 001b554..8fb522e 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
@@ -182,7 +182,7 @@ public abstract class AbstractRecipientRewriteTable implements RecipientRewriteT
switch (type) {
case Regex:
try {
- return Optional.ofNullable(RecipientRewriteTableUtil.regexMap(new MailAddress(user, domain.asString()), target));
+ return Optional.ofNullable(RecipientRewriteTableUtil.regexMap(new MailAddress(user, domain.asString()), MappingImpl.of(target)));
} catch (PatternSyntaxException | ParseException e) {
LOGGER.error("Exception during regexMap processing: ", e);
return Optional.ofNullable(target);
http://git-wip-us.apache.org/repos/asf/james-project/blob/620bf692/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 ef4febe..d849a8b 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
@@ -19,6 +19,7 @@
package org.apache.james.rrt.lib;
import java.util.HashMap;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
@@ -26,17 +27,26 @@ import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
+import java.util.stream.IntStream;
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;
+import com.github.steveash.guavate.Guavate;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
+
/**
* This helper class contains methods for the RecipientRewriteTable implementations
*/
public class RecipientRewriteTableUtil {
+ private static final int REGEX = 1;
+ private static final int PARAMETERIZED_STRING = 2;
+
private RecipientRewriteTableUtil() {
}
@@ -46,89 +56,38 @@ public class RecipientRewriteTableUtil {
* If a mapped target string begins with the prefix regex:, it must be
* formatted as regex:<regular-expression>:<parameterized-string>, e.g.,
* regex:(.*)@(.*):${1}@tld
- *
- * @param address
- * the MailAddress to be mapped
- * @param targetString
- * a String specifying the mapping
- * @throws MalformedPatternException
*/
- public static String regexMap(MailAddress address, String targetString) {
- String result = null;
- int identifierLength = Type.Regex.asPrefix().length();
-
- int msgPos = targetString.indexOf(':', identifierLength + 1);
+ public static String regexMap(MailAddress address, Mapping mapping) {
+ Preconditions.checkArgument(mapping.getType() == Type.Regex);
- // Throw exception on invalid format
- if (msgPos < identifierLength + 1) {
- throw new PatternSyntaxException("Regex should be formatted as regex:<regular-expression>:<parameterized-string>", targetString, 0);
+ List<String> parts = Splitter.on(':').splitToList(mapping.asString());
+ if (parts.size() != 3) {
+ throw new PatternSyntaxException("Regex should be formatted as regex:<regular-expression>:<parameterized-string>", mapping.asString(), 0);
}
- Pattern pattern = Pattern.compile(targetString.substring(identifierLength, msgPos));
- Matcher match = pattern.matcher(address.toString());
+ Pattern pattern = Pattern.compile(parts.get(REGEX));
+ Matcher match = pattern.matcher(address.asString());
if (match.matches()) {
- Map<String, String> parameters = new HashMap<>(match.groupCount());
- for (int i = 1; i < match.groupCount(); i++) {
- parameters.put(Integer.toString(i), match.group(i));
- }
- result = replaceParameters(targetString.substring(msgPos + 1), parameters);
+ ImmutableList<String> parameters = listMatchingGroups(match);
+ return replaceParameters(parts.get(PARAMETERIZED_STRING), parameters);
}
- return result;
+ return null;
}
- /**
- * Returns a named string, replacing parameters with the values set.
- *
- * @param str
- * the name of the String resource required.
- * @param parameters
- * a map of parameters (name-value string pairs) which are
- * replaced where found in the input strings
- * @return the requested resource
- */
- public static String replaceParameters(String str, Map<String, String> parameters) {
- if (str != null && parameters != null) {
- // Do parameter replacements for this string resource.
- StringBuilder replaceBuffer = new StringBuilder(64);
- for (Map.Entry<String, String> entry : parameters.entrySet()) {
- replaceBuffer.setLength(0);
- replaceBuffer.append("${").append(entry.getKey()).append("}");
- str = substituteSubString(str, replaceBuffer.toString(), entry.getValue());
- }
- }
-
- return str;
+ private static ImmutableList<String> listMatchingGroups(Matcher match) {
+ return IntStream
+ .rangeClosed(1, match.groupCount())
+ .mapToObj(match::group)
+ .collect(Guavate.toImmutableList());
}
- /**
- * Replace substrings of one string with another string and return altered
- * string.
- *
- * @param input
- * input string
- * @param find
- * the string to replace
- * @param replace
- * the string to replace with
- * @return the substituted string
- */
- private static String substituteSubString(String input, String find, String replace) {
- int findLength = find.length();
- int replaceLength = replace.length();
-
- StringBuilder output = new StringBuilder(input);
- int index = input.indexOf(find);
- int outputOffset = 0;
-
- while (index > -1) {
- output.replace(index + outputOffset, index + outputOffset + findLength, replace);
- outputOffset = outputOffset + (replaceLength - findLength);
-
- index = input.indexOf(find, index + findLength);
+ private static String replaceParameters(String input, List<String> parameters) {
+ int i = 1;
+ for (String parameter: parameters) {
+ input = input.replace("${" + i++ + "}", parameter);
}
-
- return output.toString();
+ return input;
}
/**
http://git-wip-us.apache.org/repos/asf/james-project/blob/620bf692/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 3c0b9b4..f9394a9 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
@@ -19,6 +19,9 @@
package org.apache.james.rrt.lib;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.regex.PatternSyntaxException;
import org.apache.james.core.MailAddress;
import org.junit.Test;
@@ -28,11 +31,57 @@ public class RecipientRewriteTableUtilTest {
@Test
public void regexMapShouldCorrectlyReplaceMatchingUsername() throws Exception {
MailAddress mailAddress = new MailAddress("prefix_abc@test");
- assertThat(RecipientRewriteTableUtil.regexMap(mailAddress, "regex:prefix_.*:admin@test"))
+ assertThat(RecipientRewriteTableUtil.regexMap(mailAddress, MappingImpl.regex("prefix_.*:admin@test")))
.isEqualTo("admin@test");
}
@Test
+ public void regexMapShouldThrowOnNullAddress() throws Exception {
+ MailAddress address = null;
+ assertThatThrownBy(() -> RecipientRewriteTableUtil.regexMap(address, MappingImpl.regex("prefix_.*:admin@test")))
+ .isInstanceOf(NullPointerException.class);
+ }
+
+ @Test
+ public void regexMapShouldThrowOnNullRegexMapping() throws Exception {
+ Mapping regexMapping = null;
+ assertThatThrownBy(() -> RecipientRewriteTableUtil.regexMap(new MailAddress("abc@test"), regexMapping))
+ .isInstanceOf(NullPointerException.class);
+ }
+
+ @Test
+ public void regexMapShouldThrowOnNonRegexMapping() throws Exception {
+ assertThatThrownBy(() -> RecipientRewriteTableUtil.regexMap(new MailAddress("abc@test"), MappingImpl.error("mapping")))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void regexMapShouldThrowOnInvalidSyntax() throws Exception {
+ assertThatThrownBy(() -> RecipientRewriteTableUtil.regexMap(new MailAddress("abc@test"), MappingImpl.regex("singlepart")))
+ .isInstanceOf(PatternSyntaxException.class);
+ }
+
+ @Test
+ public void regexMapShouldReturnInputWhenRegexDoesntMatch() throws Exception {
+ assertThat(RecipientRewriteTableUtil.regexMap(new MailAddress("abc@test"), MappingImpl.regex("notmatching:notreplaced")))
+ .isNull();
+ }
+
+ @Test
+ public void regexMapShouldCorrectlyReplaceMatchingGroups() throws Exception {
+ MailAddress mailAddress = new MailAddress("prefix_abc@test");
+ assertThat(RecipientRewriteTableUtil.regexMap(mailAddress, MappingImpl.regex("prefix_(.*)@test:admin@${1}")))
+ .isEqualTo("admin@abc");
+ }
+
+ @Test
+ public void regexMapShouldCorrectlyReplaceSeveralMatchingGroups() throws Exception {
+ MailAddress mailAddress = new MailAddress("prefix_abc_def@test");
+ assertThat(RecipientRewriteTableUtil.regexMap(mailAddress, MappingImpl.regex("prefix_(.*)_(.*)@test:admin@${1}.${2}")))
+ .isEqualTo("admin@abc.def");
+ }
+
+ @Test
public void getSeparatorShouldReturnCommaWhenCommaIsPresent() {
String separator = RecipientRewriteTableUtil.getSeparator("regex:(.*)@localhost, regex:user@test");
assertThat(separator).isEqualTo(",");
http://git-wip-us.apache.org/repos/asf/james-project/blob/620bf692/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 44044e3..1efb4c1 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
@@ -36,6 +36,7 @@ import org.apache.james.core.MailAddress;
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.lib.MappingImpl;
import org.apache.james.rrt.lib.RecipientRewriteTableUtil;
import org.apache.james.server.core.MailImpl;
import org.apache.mailet.Experimental;
@@ -116,7 +117,7 @@ public abstract class AbstractRecipientRewriteTable extends GenericMailet {
if (targetAddress.startsWith("regex:")) {
try {
- targetAddress = RecipientRewriteTableUtil.regexMap(source, targetAddress);
+ targetAddress = RecipientRewriteTableUtil.regexMap(source, MappingImpl.of(targetAddress));
} catch (PatternSyntaxException e) {
LOGGER.error("Exception during regexMap processing: ", e);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org