From cc384335f963047749c85622b9a4363d9ae89ad8 Mon Sep 17 00:00:00 2001 From: Robert Joseph Evans Date: Mon, 12 Nov 2012 18:54:57 +0000 Subject: [PATCH] svn merge -c 1408401 FIXES: HDFS-4172. namenode does not URI-encode parameters when building URI for datanode request (Derek Dagit via bobby) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1408403 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 ++ .../hdfs/web/resources/BooleanParam.java | 6 ++++ .../hdfs/web/resources/EnumSetParam.java | 5 ++++ .../hdfs/web/resources/HttpOpParam.java | 6 ++++ .../web/resources/InetSocketAddressParam.java | 6 ++++ .../hdfs/web/resources/IntegerParam.java | 6 ++++ .../hadoop/hdfs/web/resources/LongParam.java | 6 ++++ .../hadoop/hdfs/web/resources/Param.java | 30 +++++++++++++++---- .../hadoop/hdfs/web/resources/ShortParam.java | 6 ++++ .../hdfs/web/resources/StringParam.java | 6 ++++ .../hadoop/hdfs/web/resources/TestParam.java | 10 +++++++ 11 files changed, 84 insertions(+), 6 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 52faef6c0d0..606e0006a99 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -1701,6 +1701,9 @@ Release 0.23.5 - UNRELEASED HDFS-4090. getFileChecksum() result incompatible when called against zero-byte files. (Kihwal Lee via daryn) + HDFS-4172. namenode does not URI-encode parameters when building URI for + datanode request (Derek Dagit via bobby) + Release 0.23.4 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/BooleanParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/BooleanParam.java index 14dfdf5334e..3437a0c97a7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/BooleanParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/BooleanParam.java @@ -22,6 +22,12 @@ abstract class BooleanParam extends Param { static final String TRUE = "true"; static final String FALSE = "false"; + /** @return the parameter value as a string */ + @Override + public String getValueString() { + return value.toString(); + } + BooleanParam(final Domain domain, final Boolean value) { super(domain, value); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/EnumSetParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/EnumSetParam.java index feb4128e519..a983d4314dd 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/EnumSetParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/EnumSetParam.java @@ -53,6 +53,11 @@ abstract class EnumSetParam> extends Param, EnumSet return getName() + "=" + toString(value); } + /** @return the parameter value as a string */ + @Override + public String getValueString() { + return toString(value); + } /** The domain of the parameter. */ static final class Domain> extends Param.Domain> { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/HttpOpParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/HttpOpParam.java index 9765f67996c..1d029ec65cd 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/HttpOpParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/HttpOpParam.java @@ -114,6 +114,12 @@ public abstract class HttpOpParam & HttpOpParam.Op> } } + /** @return the parameter value as a string */ + @Override + public String getValueString() { + return value.toString(); + } + HttpOpParam(final Domain domain, final E value) { super(domain, value); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/InetSocketAddressParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/InetSocketAddressParam.java index 9879ba3032c..f7c09d17641 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/InetSocketAddressParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/InetSocketAddressParam.java @@ -31,6 +31,12 @@ abstract class InetSocketAddressParam return getName() + "=" + Domain.toString(getValue()); } + /** @return the parameter value as a string */ + @Override + public String getValueString() { + return Domain.toString(getValue()); + } + /** The domain of the parameter. */ static final class Domain extends Param.Domain { Domain(final String paramName) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/IntegerParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/IntegerParam.java index b80b1a254aa..452552f713d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/IntegerParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/IntegerParam.java @@ -44,6 +44,12 @@ abstract class IntegerParam extends Param { return getName() + "=" + domain.toString(getValue()); } + /** @return the parameter value as a string */ + @Override + public String getValueString() { + return domain.toString(getValue()); + } + /** The domain of the parameter. */ static final class Domain extends Param.Domain { /** The radix of the number. */ diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/LongParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/LongParam.java index 6f102e1c9f3..afb881429a2 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/LongParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/LongParam.java @@ -43,6 +43,12 @@ abstract class LongParam extends Param { return getName() + "=" + domain.toString(getValue()); } + /** @return the parameter value as a string */ + @Override + public String getValueString() { + return domain.toString(getValue()); + } + /** The domain of the parameter. */ static final class Domain extends Param.Domain { /** The radix of the number. */ diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/Param.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/Param.java index b5fd1da241b..79a831b94a6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/Param.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/Param.java @@ -17,6 +17,8 @@ */ package org.apache.hadoop.hdfs.web.resources; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.Arrays; import java.util.Comparator; @@ -32,16 +34,29 @@ public abstract class Param> { } }; - /** Convert the parameters to a sorted String. */ + /** Convert the parameters to a sorted String. + * + * @param separator URI parameter separator character + * @param parameters parameters to encode into a string + * @return the encoded URI string + */ public static String toSortedString(final String separator, final Param... parameters) { Arrays.sort(parameters, NAME_CMP); final StringBuilder b = new StringBuilder(); - for(Param p : parameters) { - if (p.getValue() != null) { - b.append(separator).append(p); + try { + for(Param p : parameters) { + if (p.getValue() != null) { + b.append(separator).append( + URLEncoder.encode(p.getName(), "UTF-8") + + "=" + + URLEncoder.encode(p.getValueString(), "UTF-8")); + } } - } + } catch (UnsupportedEncodingException e) { + // Sane systems know about UTF-8, so this should never happen. + throw new RuntimeException(e); + } return b.toString(); } @@ -60,6 +75,9 @@ public abstract class Param> { return value; } + /** @return the parameter value as a string */ + public abstract String getValueString(); + /** @return the parameter name. */ public abstract String getName(); @@ -101,4 +119,4 @@ public abstract class Param> { } } } -} \ No newline at end of file +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ShortParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ShortParam.java index c1749cf18eb..bb42223dd2f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ShortParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/ShortParam.java @@ -44,6 +44,12 @@ abstract class ShortParam extends Param { return getName() + "=" + domain.toString(getValue()); } + /** @return the parameter value as a string */ + @Override + public final String getValueString() { + return domain.toString(getValue()); + } + /** The domain of the parameter. */ static final class Domain extends Param.Domain { /** The radix of the number. */ diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/StringParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/StringParam.java index d4303f14079..1c9fbe401da 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/StringParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/resources/StringParam.java @@ -25,6 +25,12 @@ abstract class StringParam extends Param { super(domain, domain.parse(str)); } + /** @return the parameter value as a string */ + @Override + public String getValueString() { + return value; + } + /** The domain of the parameter. */ static final class Domain extends Param.Domain { /** The pattern defining the domain; null . */ diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java index 646edd42825..a6825f3ec83 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/resources/TestParam.java @@ -224,4 +224,14 @@ public class TestParam { LOG.info("EXPECTED: " + e); } } + + @Test + public void testToSortedStringEscapesURICharacters() { + final String sep = "&"; + Param ampParam = new TokenArgumentParam("token&ersand"); + Param equalParam = new RenewerParam("renewer=equal"); + final String expected = "&renewer=renewer%3Dequal&token=token%26ampersand"; + final String actual = Param.toSortedString(sep, equalParam, ampParam); + Assert.assertEquals(expected, actual); + } }