HDFS-1964. Fix incorrect HTML unescaping in DatanodeJspHelper. Contributed by Aaron T. Myers.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hdfs/trunk@1127390 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 88b5cef..9c64d8b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1052,6 +1052,9 @@
 
     HDFS-1978. All but first option in LIBHDFS_OPTS is ignored. (eli)
 
+    HDFS-1964. Fix incorrect HTML unescaping in DatanodeJspHelper
+    (Aaron T. Myers via todd)
+
 Release 0.21.1 - Unreleased
     HDFS-1466. TestFcHdfsSymlink relies on /tmp/test not existing. (eli)
 
diff --git a/src/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java b/src/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java
index 15ac0f7..b1e40cf 100644
--- a/src/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java
+++ b/src/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java
@@ -84,8 +84,8 @@
                                          Configuration conf
                                          ) throws IOException,
                                                   InterruptedException {
-    final String dir = StringEscapeUtils.unescapeHtml(
-        JspHelper.validatePath(req.getParameter("dir")));
+    final String dir = JspHelper.validatePath(
+        StringEscapeUtils.unescapeHtml(req.getParameter("dir")));
     if (dir == null) {
       out.print("Invalid input");
       return;
@@ -621,7 +621,7 @@
     }
 
     final String filename = JspHelper
-        .validatePath(req.getParameter(StringEscapeUtils.unescapeHtml("filename")));
+        .validatePath(StringEscapeUtils.unescapeHtml(req.getParameter("filename")));
     if (filename == null) {
       out.print("Invalid input (file name absent)");
       return;
diff --git a/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java b/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java
index 8926d90..6b0255c 100644
--- a/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java
+++ b/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java
@@ -38,27 +38,36 @@
   
   private static final String FILE_DATA = "foo bar baz biz buz";
   
-  private static void testViewingFile(MiniDFSCluster cluster, String filePath) throws IOException {
+  private static void testViewingFile(MiniDFSCluster cluster, String filePath,
+      boolean doTail) throws IOException {
     FileSystem fs = cluster.getFileSystem();
     
     Path testPath = new Path(filePath);
-    DFSTestUtil.writeFile(fs, testPath, FILE_DATA);
+    if (!fs.exists(testPath)) {
+      DFSTestUtil.writeFile(fs, testPath, FILE_DATA);
+    }
     
     InetSocketAddress nnIpcAddress = cluster.getNameNode().getNameNodeAddress();
     InetSocketAddress nnHttpAddress = cluster.getNameNode().getHttpAddress();
     int dnInfoPort = cluster.getDataNodes().get(0).getInfoPort();
     
-    URL url = new URL("http://localhost:" + dnInfoPort + "/browseDirectory.jsp" +
-        JspHelper.getUrlParam("dir", URLEncoder.encode(testPath.toString(), "UTF-8"), true) +
-        JspHelper.getUrlParam("namenodeInfoPort", nnHttpAddress.getPort() + 
-        JspHelper.getUrlParam("nnaddr", "localhost:" + nnIpcAddress.getPort())));
+    String jspName = doTail ? "tail.jsp" : "browseDirectory.jsp";
+    String fileParamName = doTail ? "filename" : "dir";
+    
+    URL url = new URL("http://localhost:" + dnInfoPort + "/" + jspName +
+        JspHelper.getUrlParam(fileParamName, URLEncoder.encode(testPath.toString(), "UTF-8"), true) +
+        JspHelper.getUrlParam("namenodeInfoPort", Integer.toString(nnHttpAddress.getPort())) + 
+        JspHelper.getUrlParam("nnaddr", "localhost:" + nnIpcAddress.getPort()));
     
     String viewFilePage = DFSTestUtil.urlGet(url);
     
     assertTrue("page should show preview of file contents", viewFilePage.contains(FILE_DATA));
-    assertTrue("page should show link to download file", viewFilePage
-        .contains("/streamFile" + URIUtil.encodePath(testPath.toString()) +
-            "?nnaddr=localhost:" + nnIpcAddress.getPort()));
+    
+    if (!doTail) {
+      assertTrue("page should show link to download file", viewFilePage
+          .contains("/streamFile" + URIUtil.encodePath(testPath.toString()) +
+              "?nnaddr=localhost:" + nnIpcAddress.getPort()));
+    }
   }
   
   @Test
@@ -69,9 +78,13 @@
       cluster = new MiniDFSCluster.Builder(conf).build();
       cluster.waitActive();
       
-      testViewingFile(cluster, "/test-file");
-      testViewingFile(cluster, "/tmp/test-file");
-      testViewingFile(cluster, "/tmp/test-file%with goofy&characters");
+      testViewingFile(cluster, "/test-file", false);
+      testViewingFile(cluster, "/tmp/test-file", false);
+      testViewingFile(cluster, "/tmp/test-file%with goofy&characters", false);
+      
+      testViewingFile(cluster, "/test-file", true);
+      testViewingFile(cluster, "/tmp/test-file", true);
+      testViewingFile(cluster, "/tmp/test-file%with goofy&characters", true);
       
     } finally {
       if (cluster != null) {