1 | package org.apache.hadoop.hdfs.server.datanode; |
---|
2 | |
---|
3 | import javax.servlet.*; |
---|
4 | import javax.servlet.http.*; |
---|
5 | import javax.servlet.jsp.*; |
---|
6 | import javax.servlet.*; |
---|
7 | import javax.servlet.http.*; |
---|
8 | import java.io.*; |
---|
9 | import java.util.*; |
---|
10 | import java.net.*; |
---|
11 | import org.apache.hadoop.fs.*; |
---|
12 | import org.apache.hadoop.hdfs.*; |
---|
13 | import org.apache.hadoop.hdfs.server.namenode.*; |
---|
14 | import org.apache.hadoop.hdfs.server.datanode.*; |
---|
15 | import org.apache.hadoop.hdfs.protocol.*; |
---|
16 | import org.apache.hadoop.io.*; |
---|
17 | import org.apache.hadoop.conf.*; |
---|
18 | import org.apache.hadoop.net.DNS; |
---|
19 | import org.apache.hadoop.util.*; |
---|
20 | import java.text.DateFormat; |
---|
21 | |
---|
22 | public final class browseDirectory_jsp extends org.apache.jasper.runtime.HttpJspBase |
---|
23 | implements org.apache.jasper.runtime.JspSourceDependent { |
---|
24 | |
---|
25 | |
---|
26 | static JspHelper jspHelper = new JspHelper(); |
---|
27 | |
---|
28 | public void generateDirectoryStructure( JspWriter out, |
---|
29 | HttpServletRequest req, |
---|
30 | HttpServletResponse resp) |
---|
31 | throws IOException { |
---|
32 | String dir = req.getParameter("dir"); |
---|
33 | if (dir == null || dir.length() == 0) { |
---|
34 | out.print("Invalid input"); |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | String namenodeInfoPortStr = req.getParameter("namenodeInfoPort"); |
---|
39 | int namenodeInfoPort = -1; |
---|
40 | if (namenodeInfoPortStr != null) |
---|
41 | namenodeInfoPort = Integer.parseInt(namenodeInfoPortStr); |
---|
42 | |
---|
43 | DFSClient dfs = new DFSClient(jspHelper.nameNodeAddr, jspHelper.conf); |
---|
44 | String target = dir; |
---|
45 | if (!dfs.exists(target)) { |
---|
46 | out.print("<h3>File or directory : " + target + " does not exist</h3>"); |
---|
47 | JspHelper.printGotoForm(out, namenodeInfoPort, target); |
---|
48 | } |
---|
49 | else { |
---|
50 | if( !dfs.isDirectory(target) ) { // a file |
---|
51 | List<LocatedBlock> blocks = |
---|
52 | dfs.namenode.getBlockLocations(dir, 0, 1).getLocatedBlocks(); |
---|
53 | |
---|
54 | LocatedBlock firstBlock = null; |
---|
55 | DatanodeInfo [] locations = null; |
---|
56 | if (blocks.size() > 0) { |
---|
57 | firstBlock = blocks.get(0); |
---|
58 | locations = firstBlock.getLocations(); |
---|
59 | } |
---|
60 | if (locations == null || locations.length == 0) { |
---|
61 | out.print("Empty file"); |
---|
62 | } else { |
---|
63 | DatanodeInfo chosenNode = jspHelper.bestNode(firstBlock); |
---|
64 | String fqdn = InetAddress.getByName(chosenNode.getHost()). |
---|
65 | getCanonicalHostName(); |
---|
66 | String datanodeAddr = chosenNode.getName(); |
---|
67 | int datanodePort = Integer.parseInt( |
---|
68 | datanodeAddr.substring( |
---|
69 | datanodeAddr.indexOf(':') + 1, |
---|
70 | datanodeAddr.length())); |
---|
71 | String redirectLocation = "http://"+fqdn+":" + |
---|
72 | chosenNode.getInfoPort() + |
---|
73 | "/browseBlock.jsp?blockId=" + |
---|
74 | firstBlock.getBlock().getBlockId() + |
---|
75 | "&blockSize=" + firstBlock.getBlock().getNumBytes() + |
---|
76 | "&genstamp=" + firstBlock.getBlock().getGenerationStamp() + |
---|
77 | "&filename=" + URLEncoder.encode(dir, "UTF-8") + |
---|
78 | "&datanodePort=" + datanodePort + |
---|
79 | "&namenodeInfoPort=" + namenodeInfoPort; |
---|
80 | resp.sendRedirect(redirectLocation); |
---|
81 | } |
---|
82 | return; |
---|
83 | } |
---|
84 | // directory |
---|
85 | FileStatus[] files = dfs.listPaths(target); |
---|
86 | //generate a table and dump the info |
---|
87 | String [] headings = { "Name", "Type", "Size", "Replication", |
---|
88 | "Block Size", "Modification Time", |
---|
89 | "Permission", "Owner", "Group" }; |
---|
90 | out.print("<h3>Contents of directory "); |
---|
91 | JspHelper.printPathWithLinks(dir, out, namenodeInfoPort); |
---|
92 | out.print("</h3><hr>"); |
---|
93 | JspHelper.printGotoForm(out, namenodeInfoPort, dir); |
---|
94 | out.print("<hr>"); |
---|
95 | |
---|
96 | File f = new File(dir); |
---|
97 | String parent; |
---|
98 | if ((parent = f.getParent()) != null) |
---|
99 | out.print("<a href=\"" + req.getRequestURL() + "?dir=" + parent + |
---|
100 | "&namenodeInfoPort=" + namenodeInfoPort + |
---|
101 | "\">Go to parent directory</a><br>"); |
---|
102 | |
---|
103 | if (files == null || files.length == 0) { |
---|
104 | out.print("Empty directory"); |
---|
105 | } |
---|
106 | else { |
---|
107 | jspHelper.addTableHeader(out); |
---|
108 | int row=0; |
---|
109 | jspHelper.addTableRow(out, headings, row++); |
---|
110 | String cols [] = new String[headings.length]; |
---|
111 | for (int i = 0; i < files.length; i++) { |
---|
112 | //Get the location of the first block of the file |
---|
113 | if (files[i].getPath().toString().endsWith(".crc")) continue; |
---|
114 | if (!files[i].isDir()) { |
---|
115 | cols[1] = "file"; |
---|
116 | cols[2] = StringUtils.byteDesc(files[i].getLen()); |
---|
117 | cols[3] = Short.toString(files[i].getReplication()); |
---|
118 | cols[4] = StringUtils.byteDesc(files[i].getBlockSize()); |
---|
119 | } |
---|
120 | else { |
---|
121 | cols[1] = "dir"; |
---|
122 | cols[2] = ""; |
---|
123 | cols[3] = ""; |
---|
124 | cols[4] = ""; |
---|
125 | } |
---|
126 | String datanodeUrl = req.getRequestURL()+"?dir="+ |
---|
127 | URLEncoder.encode(files[i].getPath().toString(), "UTF-8") + |
---|
128 | "&namenodeInfoPort=" + namenodeInfoPort; |
---|
129 | cols[0] = "<a href=\""+datanodeUrl+"\">"+files[i].getPath().getName()+"</a>"; |
---|
130 | cols[5] = FsShell.dateForm.format(new Date((files[i].getModificationTime()))); |
---|
131 | cols[6] = files[i].getPermission().toString(); |
---|
132 | cols[7] = files[i].getOwner(); |
---|
133 | cols[8] = files[i].getGroup(); |
---|
134 | jspHelper.addTableRow(out, cols, row++); |
---|
135 | } |
---|
136 | jspHelper.addTableFooter(out); |
---|
137 | } |
---|
138 | } |
---|
139 | String namenodeHost = jspHelper.nameNodeAddr.getHostName(); |
---|
140 | out.print("<br><a href=\"http://" + |
---|
141 | InetAddress.getByName(namenodeHost).getCanonicalHostName() + ":" + |
---|
142 | namenodeInfoPort + "/dfshealth.jsp\">Go back to DFS home</a>"); |
---|
143 | dfs.close(); |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | private static java.util.List _jspx_dependants; |
---|
148 | |
---|
149 | public Object getDependants() { |
---|
150 | return _jspx_dependants; |
---|
151 | } |
---|
152 | |
---|
153 | public void _jspService(HttpServletRequest request, HttpServletResponse response) |
---|
154 | throws java.io.IOException, ServletException { |
---|
155 | |
---|
156 | JspFactory _jspxFactory = null; |
---|
157 | PageContext pageContext = null; |
---|
158 | HttpSession session = null; |
---|
159 | ServletContext application = null; |
---|
160 | ServletConfig config = null; |
---|
161 | JspWriter out = null; |
---|
162 | Object page = this; |
---|
163 | JspWriter _jspx_out = null; |
---|
164 | PageContext _jspx_page_context = null; |
---|
165 | |
---|
166 | |
---|
167 | try { |
---|
168 | _jspxFactory = JspFactory.getDefaultFactory(); |
---|
169 | response.setContentType("text/html; charset=UTF-8"); |
---|
170 | pageContext = _jspxFactory.getPageContext(this, request, response, |
---|
171 | null, true, 8192, true); |
---|
172 | _jspx_page_context = pageContext; |
---|
173 | application = pageContext.getServletContext(); |
---|
174 | config = pageContext.getServletConfig(); |
---|
175 | session = pageContext.getSession(); |
---|
176 | out = pageContext.getOut(); |
---|
177 | _jspx_out = out; |
---|
178 | |
---|
179 | out.write('\n'); |
---|
180 | out.write("\n\n<html>\n<head>\n<style type=text/css>\n<!--\nbody \n {\n font-face:sanserif;\n }\n-->\n</style>\n"); |
---|
181 | JspHelper.createTitle(out, request, request.getParameter("dir")); |
---|
182 | out.write("\n</head>\n\n<body onload=\"document.goto.dir.focus()\">\n"); |
---|
183 | |
---|
184 | try { |
---|
185 | generateDirectoryStructure(out,request,response); |
---|
186 | } |
---|
187 | catch(IOException ioe) { |
---|
188 | String msg = ioe.getLocalizedMessage(); |
---|
189 | int i = msg.indexOf("\n"); |
---|
190 | if (i >= 0) { |
---|
191 | msg = msg.substring(0, i); |
---|
192 | } |
---|
193 | out.print("<h3>" + msg + "</h3>"); |
---|
194 | } |
---|
195 | |
---|
196 | out.write("\n<hr>\n\n<h2>Local logs</h2>\n<a href=\"/logs/\">Log</a> directory\n\n"); |
---|
197 | |
---|
198 | out.println(ServletUtil.htmlFooter()); |
---|
199 | |
---|
200 | out.write('\n'); |
---|
201 | } catch (Throwable t) { |
---|
202 | if (!(t instanceof SkipPageException)){ |
---|
203 | out = _jspx_out; |
---|
204 | if (out != null && out.getBufferSize() != 0) |
---|
205 | out.clearBuffer(); |
---|
206 | if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); |
---|
207 | } |
---|
208 | } finally { |
---|
209 | if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|