diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/main/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/Separator.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/main/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/Separator.java index 8a178db..6c091e3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/main/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/Separator.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/main/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/Separator.java @@ -20,6 +20,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.hadoop.hbase.util.Bytes; @@ -53,26 +54,45 @@ */ TAB("\t", "%3$"); + // a reserved character that starts each of the encoded values and is encoded + // first in order to escape naturally occurring instances of encoded values + // although it can be expressed as an enum instance, we define them as private + // variables to hide it from callers + private static final String PERCENT = "%"; + private static final String PERCENT_ENCODED = "%9$"; + + private static final Pattern PERCENT_PATTERN = + Pattern.compile(PERCENT, Pattern.LITERAL); + private static final String PERCENT_REPLACEMENT = + Matcher.quoteReplacement(PERCENT); + + private static final Pattern PERCENT_ENCODED_PATTERN = + Pattern.compile(PERCENT_ENCODED, Pattern.LITERAL); + private static final String PERCENT_ENCODED_REPLACEMENT = + Matcher.quoteReplacement(PERCENT_ENCODED); + /** * The string value of this separator. */ private final String value; /** - * The URLEncoded version of this separator. - */ - private final String encodedValue; - - /** * The bye representation of value. */ private final byte[] bytes; + // pre-compiled patterns and quoted replacements for optimization /** * The value quoted so that it can be used as a safe regex. */ private final String quotedValue; + private final Pattern valuePattern; + private final String valueReplacement; + + private final Pattern encodedValuePattern; + private final String encodedValueReplacement; + /** * Indicator for variable size of an individual segment in a split. The * segment ends wherever separator is encountered. @@ -97,7 +117,6 @@ */ private Separator(String value, String encodedValue) { this.value = value; - this.encodedValue = encodedValue; // validation if (value == null || value.length() == 0 || encodedValue == null @@ -108,6 +127,11 @@ private Separator(String value, String encodedValue) { this.bytes = Bytes.toBytes(value); this.quotedValue = Pattern.quote(value); + this.valuePattern = Pattern.compile(value, Pattern.LITERAL); + this.valueReplacement = Matcher.quoteReplacement(value); + + this.encodedValuePattern = Pattern.compile(encodedValue, Pattern.LITERAL); + this.encodedValueReplacement = Matcher.quoteReplacement(encodedValue); } /** @@ -119,6 +143,8 @@ public String getValue() { /** * Used to make token safe to be used with this separator without collisions. + * It must be paired with the opposite {@link #decode(String)} method + * or you will not recover the original. * * @param token Token to be encoded. * @return the token with any occurrences of this separator URLEncoded. @@ -128,7 +154,23 @@ public String encode(String token) { // Nothing to replace return token; } - return token.replace(value, encodedValue); + // first encode the percent to escape naturally occurring encoded values + String escaped = encodePercent(token); + return encodeSingle(escaped, this); + } + + private static String replace(String token, Pattern pattern, + String replacement) { + return pattern.matcher(token).replaceAll(replacement); + } + + private static String encodeSingle(String token, Separator separator) { + return replace(token, separator.valuePattern, + separator.encodedValueReplacement); + } + + private static String encodePercent(String token) { + return replace(token, PERCENT_PATTERN, PERCENT_ENCODED_REPLACEMENT); } /** @@ -143,7 +185,18 @@ public String decode(String token) { // Nothing to replace return token; } - return token.replace(encodedValue, value); + String escaped = decodeSingle(token, this); + // decode percent to de-escape + return decodePercent(escaped); + } + + private static String decodeSingle(String token, Separator separator) { + return replace(token, separator.encodedValuePattern, + separator.valueReplacement); + } + + private static String decodePercent(String token) { + return replace(token, PERCENT_ENCODED_PATTERN, PERCENT_REPLACEMENT); } /** @@ -158,13 +211,15 @@ public String decode(String token) { * separators encoded. */ public static byte[] encode(String token, Separator... separators) { - if (token == null) { + if (token == null || token.length() == 0) { return EMPTY_BYTES; } String result = token; + // first encode the percent to escape naturally occurring encoded values + result = encodePercent(token); for (Separator separator : separators) { if (separator != null) { - result = separator.encode(result); + result = encodeSingle(result, separator); } } return Bytes.toBytes(result); @@ -173,7 +228,9 @@ public String decode(String token) { /** * Decode the given separators in the token with their decoding equivalent. * This means that when encoding is already present in the token itself, this - * is not a reversible process. + * is not a reversible process. It must be paired with + * {@link #encode(String, Separator...)} with the same separators to recover + * the original. * * @param token containing possible separators that need to be encoded. * @param separators to be encoded in the token with their URLEncoding @@ -191,7 +248,9 @@ public static String decode(byte[] token, Separator... separators) { /** * Decode the given separators in the token with their decoding equivalent. * This means that when encoding is already present in the token itself, this - * is not a reversible process. + * is not a reversible process. It must be paired with + * {@link #encode(String, Separator...)} with the same separators to recover + * the original. * * @param token containing possible separators that need to be encoded. * @param separators to be encoded in the token with their URLEncoding @@ -206,10 +265,11 @@ public static String decode(String token, Separator... separators) { String result = token; for (Separator separator : separators) { if (separator != null) { - result = separator.decode(result); + result = decodeSingle(result, separator); } } - return result; + // decode percent to de-escape + return decodePercent(result); } /** diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/TestSeparator.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/TestSeparator.java index 0cda97c..27750f3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/TestSeparator.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-timelineservice/src/test/java/org/apache/hadoop/yarn/server/timelineservice/storage/common/TestSeparator.java @@ -34,7 +34,7 @@ private static String villain = "Dr. Heinz Doofenshmirtz"; private static String special = - ". * | ? + \t ( ) [ ] { } ^ $ \\ \""; + ". * | ? + \t ( ) [ ] { } ^ $ \\ \" %"; /** * @@ -81,6 +81,12 @@ public void testEncodeDecode() { Separator.VALUES, Separator.SPACE); } + @Test + public void testEncodedValues() { + testEncodeDecode("Double-escape %2$ and %9$ or %%2$ or %%3$, nor %%%2$" + + "= no problem!", + Separator.QUALIFIERS, Separator.VALUES, Separator.SPACE, Separator.TAB); + } @Test public void testSplits() {