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/trunk@1345563 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uma Maheswara Rao G 2012-06-02 19:41:33 +00:00
parent 18ba716922
commit 28ca281cd6
2 changed files with 50 additions and 18 deletions

View File

@ -21,6 +21,7 @@
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -616,9 +617,12 @@ static void generateFileChunksForTail(JspWriter out, HttpServletRequest req,
Configuration conf Configuration conf
) throws IOException, ) throws IOException,
InterruptedException { InterruptedException {
final String referrer = JspHelper.validateURL(req.getParameter("referrer")); String referrer = null;
boolean noLink = false; boolean noLink = false;
if (referrer == null) { try {
referrer = new URL(req.getParameter("referrer")).toString();
} catch (IOException e) {
referrer = null;
noLink = true; noLink = true;
} }

View File

@ -18,16 +18,20 @@
package org.apache.hadoop.hdfs.server.datanode; package org.apache.hadoop.hdfs.server.datanode;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.JspWriter;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSTestUtil; 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 String FILE_DATA = "foo bar baz biz buz";
private static final HdfsConfiguration CONF = new HdfsConfiguration(); private static final HdfsConfiguration CONF = new HdfsConfiguration();
private static String viewFilePage;
private static void testViewingFile(MiniDFSCluster cluster, String filePath, private static void testViewingFile(MiniDFSCluster cluster, String filePath)
boolean doTail) throws IOException { throws IOException {
FileSystem fs = cluster.getFileSystem(); FileSystem fs = cluster.getFileSystem();
Path testPath = new Path(filePath); Path testPath = new Path(filePath);
@ -58,23 +63,46 @@ private static void testViewingFile(MiniDFSCluster cluster, String filePath,
InetSocketAddress nnHttpAddress = cluster.getNameNode().getHttpAddress(); InetSocketAddress nnHttpAddress = cluster.getNameNode().getHttpAddress();
int dnInfoPort = cluster.getDataNodes().get(0).getInfoPort(); int dnInfoPort = cluster.getDataNodes().get(0).getInfoPort();
String jspName = doTail ? "tail.jsp" : "browseDirectory.jsp"; URL url = new URL("http://localhost:" + dnInfoPort + "/"
String fileParamName = doTail ? "filename" : "dir"; + "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 + viewFilePage = StringEscapeUtils.unescapeHtml(DFSTestUtil.urlGet(url));
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, got: " + viewFilePage, assertTrue("page should show preview of file contents, got: " + viewFilePage,
viewFilePage.contains(FILE_DATA)); viewFilePage.contains(FILE_DATA));
if (!doTail) {
assertTrue("page should show link to download file", viewFilePage assertTrue("page should show link to download file", viewFilePage
.contains("/streamFile" + ServletUtil.encodePath(testPath.toString()) + .contains("/streamFile" + ServletUtil.encodePath(filePath)
"?nnaddr=localhost:" + nnIpcAddress.getPort())); + "?nnaddr=localhost:" + nnIpcAddress.getPort()));
// check whether able to tail the file
String regex = "<a.+href=\"(.+?)\">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 = "<a.+href=\"(.+?)\">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 void testViewFileJsp() throws IOException {
"/foo\">bar/foo\">bar" "/foo\">bar/foo\">bar"
}; };
for (String p : paths) { for (String p : paths) {
testViewingFile(cluster, p, false); testViewingFile(cluster, p);
testViewingFile(cluster, p, true); testViewingFile(cluster, p);
} }
} finally { } finally {
if (cluster != null) { if (cluster != null) {