From 96a87ae7b06a3fc46744addcf32906a306e883d3 Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Fri, 12 Aug 2011 03:17:33 +0000 Subject: [PATCH] HADOOP-7531. Add servlet util methods for handling paths in requests. Contributed by Eli Collins git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1156947 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-common/CHANGES.txt | 2 + .../org/apache/hadoop/util/ServletUtil.java | 58 ++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/hadoop-common/CHANGES.txt b/hadoop-common/CHANGES.txt index d7c0039241..99c02c6b74 100644 --- a/hadoop-common/CHANGES.txt +++ b/hadoop-common/CHANGES.txt @@ -322,6 +322,8 @@ Trunk (unreleased changes) HADOOP-7526. Add TestPath tests for URI conversion and reserved characters. (eli) + HADOOP-7531. Add servlet util methods for handling paths in requests. (eli) + OPTIMIZATIONS HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole diff --git a/hadoop-common/src/main/java/org/apache/hadoop/util/ServletUtil.java b/hadoop-common/src/main/java/org/apache/hadoop/util/ServletUtil.java index 9d801cd243..993394d59b 100644 --- a/hadoop-common/src/main/java/org/apache/hadoop/util/ServletUtil.java +++ b/hadoop-common/src/main/java/org/apache/hadoop/util/ServletUtil.java @@ -21,10 +21,15 @@ import java.util.Calendar; import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import org.apache.commons.httpclient.URIException; +import org.apache.commons.httpclient.util.URIUtil; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; +import com.google.common.base.Preconditions; + @InterfaceAudience.Private @InterfaceStability.Unstable public class ServletUtil { @@ -107,4 +112,55 @@ public static String percentageGraph(int perc, int width) throws IOException { public static String percentageGraph(float perc, int width) throws IOException { return percentageGraph((int)perc, width); } -} + + /** + * Escape and encode a string regarded as within the query component of an URI. + * @param value the value to encode + * @return encoded query, null if the default charset is not supported + */ + public static String encodeQueryValue(final String value) { + try { + return URIUtil.encodeWithinQuery(value, "UTF-8"); + } catch (URIException e) { + throw new AssertionError("JVM does not support UTF-8"); // should never happen! + } + } + + /** + * Escape and encode a string regarded as the path component of an URI. + * @param path the path component to encode + * @return encoded path, null if UTF-8 is not supported + */ + public static String encodePath(final String path) { + try { + return URIUtil.encodePath(path, "UTF-8"); + } catch (URIException e) { + throw new AssertionError("JVM does not support UTF-8"); // should never happen! + } + } + + /** + * Parse and decode the path component from the given request. + * @param request Http request to parse + * @param servletName the name of servlet that precedes the path + * @return decoded path component, null if UTF-8 is not supported + */ + public static String getDecodedPath(final HttpServletRequest request, String servletName) { + try { + return URIUtil.decode(getRawPath(request, servletName), "UTF-8"); + } catch (URIException e) { + throw new AssertionError("JVM does not support UTF-8"); // should never happen! + } + } + + /** + * Parse the path component from the given request and return w/o decoding. + * @param request Http request to parse + * @param servletName the name of servlet that precedes the path + * @return path component, null if the default charset is not supported + */ + public static String getRawPath(final HttpServletRequest request, String servletName) { + Preconditions.checkArgument(request.getRequestURI().startsWith(servletName+"/")); + return request.getRequestURI().substring(servletName.length()); + } +} \ No newline at end of file