From ef8c032a86df8b34db1e4b77fc47d76bb9689e65 Mon Sep 17 00:00:00 2001 From: Uma Maheswara Rao G Date: Sat, 2 Jun 2012 19:49:12 +0000 Subject: [PATCH] Merge r:1345563 HDFS-2025. Go Back to File View link is not working in tail.jsp. Contributed by Ashish and Sravan. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1345564 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 2 + .../server/datanode/DatanodeJspHelper.java | 8 ++- .../hdfs/server/datanode/TestDatanodeJsp.java | 60 ++++++++++++++----- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 60fdc2d58f3..7ba185c7521 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -146,6 +146,8 @@ Release 2.0.1-alpha - UNRELEASED HDFS-3442. Incorrect count for Missing Replicas in FSCK report. (Andrew Wang via atm) + HDFS-2025. Go Back to File View link is not working in tail.jsp.(Ashish and Sravan via umamahesh) + Release 2.0.0-alpha - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java index 62a2f53ac7d..d8ff408b4c9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeJspHelper.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.URL; import java.net.URLEncoder; import java.security.PrivilegedExceptionAction; import java.text.SimpleDateFormat; @@ -616,9 +617,12 @@ public class DatanodeJspHelper { Configuration conf ) throws IOException, InterruptedException { - final String referrer = JspHelper.validateURL(req.getParameter("referrer")); + String referrer = null; boolean noLink = false; - if (referrer == null) { + try { + referrer = new URL(req.getParameter("referrer")).toString(); + } catch (IOException e) { + referrer = null; noLink = true; } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java index 973f05b61c5..630f19a9ee4 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDatanodeJsp.java @@ -18,16 +18,20 @@ package org.apache.hadoop.hdfs.server.datanode; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.URL; import java.net.URLEncoder; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspWriter; +import org.apache.commons.lang.StringEscapeUtils; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hdfs.DFSTestUtil; @@ -44,9 +48,10 @@ public class TestDatanodeJsp { private static final String FILE_DATA = "foo bar baz biz buz"; private static final HdfsConfiguration CONF = new HdfsConfiguration(); + private static String viewFilePage; - private static void testViewingFile(MiniDFSCluster cluster, String filePath, - boolean doTail) throws IOException { + private static void testViewingFile(MiniDFSCluster cluster, String filePath) + throws IOException { FileSystem fs = cluster.getFileSystem(); Path testPath = new Path(filePath); @@ -58,23 +63,46 @@ public class TestDatanodeJsp { InetSocketAddress nnHttpAddress = cluster.getNameNode().getHttpAddress(); int dnInfoPort = cluster.getDataNodes().get(0).getInfoPort(); - String jspName = doTail ? "tail.jsp" : "browseDirectory.jsp"; - String fileParamName = doTail ? "filename" : "dir"; + URL url = new URL("http://localhost:" + dnInfoPort + "/" + + "browseDirectory.jsp" + JspHelper.getUrlParam("dir", + URLEncoder.encode(testPath.toString(), "UTF-8"), true) + + JspHelper.getUrlParam("namenodeInfoPort", Integer + .toString(nnHttpAddress.getPort())) + JspHelper + .getUrlParam("nnaddr", "localhost:" + nnIpcAddress.getPort())); - 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); + viewFilePage = StringEscapeUtils.unescapeHtml(DFSTestUtil.urlGet(url)); assertTrue("page should show preview of file contents, got: " + viewFilePage, viewFilePage.contains(FILE_DATA)); - if (!doTail) { - assertTrue("page should show link to download file", viewFilePage - .contains("/streamFile" + ServletUtil.encodePath(testPath.toString()) + - "?nnaddr=localhost:" + nnIpcAddress.getPort())); + assertTrue("page should show link to download file", viewFilePage + .contains("/streamFile" + ServletUtil.encodePath(filePath) + + "?nnaddr=localhost:" + nnIpcAddress.getPort())); + + // check whether able to tail the file + String regex = "Tail\\s*this\\s*file\\<\\/a\\>"; + assertFileContents(regex, "Tail this File"); + + // check whether able to 'Go Back to File View' after tailing the file + regex = "Go\\s*Back\\s*to\\s*File\\s*View\\<\\/a\\>"; + assertFileContents(regex, "Go Back to File View"); + } + + private static void assertFileContents(String regex, String text) + throws IOException { + Pattern compile = Pattern.compile(regex); + Matcher matcher = compile.matcher(viewFilePage); + URL hyperlink = null; + if (matcher.find()) { + // got hyperlink for Tail this file + hyperlink = new URL(matcher.group(1)); + viewFilePage = StringEscapeUtils.unescapeHtml(DFSTestUtil + .urlGet(hyperlink)); + assertTrue("page should show preview of file contents", viewFilePage + .contains(FILE_DATA)); + } else { + fail(text + " hyperlink should be there in the page content : " + + viewFilePage); } } @@ -97,8 +125,8 @@ public class TestDatanodeJsp { "/foo\">bar/foo\">bar" }; for (String p : paths) { - testViewingFile(cluster, p, false); - testViewingFile(cluster, p, true); + testViewingFile(cluster, p); + testViewingFile(cluster, p); } } finally { if (cluster != null) {