1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.util; |
---|
19 | |
---|
20 | import java.io.*; |
---|
21 | import java.util.Calendar; |
---|
22 | |
---|
23 | import javax.servlet.*; |
---|
24 | |
---|
25 | public class ServletUtil { |
---|
26 | /** |
---|
27 | * Initial HTML header |
---|
28 | */ |
---|
29 | public static PrintWriter initHTML(ServletResponse response, String title |
---|
30 | ) throws IOException { |
---|
31 | response.setContentType("text/html"); |
---|
32 | PrintWriter out = response.getWriter(); |
---|
33 | out.println("<html>\n" |
---|
34 | + "<link rel='stylesheet' type='text/css' href='/static/hadoop.css'>\n" |
---|
35 | + "<title>" + title + "</title>\n" |
---|
36 | + "<body>\n" |
---|
37 | + "<h1>" + title + "</h1>\n"); |
---|
38 | return out; |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Get a parameter from a ServletRequest. |
---|
43 | * Return null if the parameter contains only white spaces. |
---|
44 | */ |
---|
45 | public static String getParameter(ServletRequest request, String name) { |
---|
46 | String s = request.getParameter(name); |
---|
47 | if (s == null) { |
---|
48 | return null; |
---|
49 | } |
---|
50 | s = s.trim(); |
---|
51 | return s.length() == 0? null: s; |
---|
52 | } |
---|
53 | |
---|
54 | public static final String HTML_TAIL = "<hr />\n" |
---|
55 | + "<a href='http://hadoop.apache.org/core'>Hadoop</a>, " |
---|
56 | + Calendar.getInstance().get(Calendar.YEAR) + ".\n" |
---|
57 | + "</body></html>"; |
---|
58 | |
---|
59 | /** |
---|
60 | * HTML footer to be added in the jsps. |
---|
61 | * @return the HTML footer. |
---|
62 | */ |
---|
63 | public static String htmlFooter() { |
---|
64 | return HTML_TAIL; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Generate the percentage graph and returns HTML representation string |
---|
69 | * of the same. |
---|
70 | * |
---|
71 | * @param perc The percentage value for which graph is to be generated |
---|
72 | * @param width The width of the display table |
---|
73 | * @return HTML String representation of the percentage graph |
---|
74 | * @throws IOException |
---|
75 | */ |
---|
76 | public static String percentageGraph(int perc, int width) throws IOException { |
---|
77 | assert perc >= 0; assert perc <= 100; |
---|
78 | |
---|
79 | StringBuilder builder = new StringBuilder(); |
---|
80 | |
---|
81 | builder.append("<table border=\"1px\" width=\""); builder.append(width); |
---|
82 | builder.append("px\"><tr>"); |
---|
83 | if(perc > 0) { |
---|
84 | builder.append("<td cellspacing=\"0\" class=\"perc_filled\" width=\""); |
---|
85 | builder.append(perc); builder.append("%\"></td>"); |
---|
86 | }if(perc < 100) { |
---|
87 | builder.append("<td cellspacing=\"0\" class=\"perc_nonfilled\" width=\""); |
---|
88 | builder.append(100 - perc); builder.append("%\"></td>"); |
---|
89 | } |
---|
90 | builder.append("</tr></table>"); |
---|
91 | return builder.toString(); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Generate the percentage graph and returns HTML representation string |
---|
96 | * of the same. |
---|
97 | * @param perc The percentage value for which graph is to be generated |
---|
98 | * @param width The width of the display table |
---|
99 | * @return HTML String representation of the percentage graph |
---|
100 | * @throws IOException |
---|
101 | */ |
---|
102 | public static String percentageGraph(float perc, int width) throws IOException { |
---|
103 | return percentageGraph((int)perc, width); |
---|
104 | } |
---|
105 | } |
---|