ServletUtil.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import java.io.*;
  20. import java.util.Calendar;
  21. import javax.servlet.*;
  22. import javax.servlet.http.HttpServletRequest;
  23. import org.apache.commons.httpclient.URIException;
  24. import org.apache.commons.httpclient.util.URIUtil;
  25. import org.apache.hadoop.classification.InterfaceAudience;
  26. import org.apache.hadoop.classification.InterfaceStability;
  27. import com.google.common.base.Preconditions;
  28. @InterfaceAudience.Private
  29. @InterfaceStability.Unstable
  30. public class ServletUtil {
  31. /**
  32. * Initial HTML header
  33. */
  34. public static PrintWriter initHTML(ServletResponse response, String title
  35. ) throws IOException {
  36. response.setContentType("text/html");
  37. PrintWriter out = response.getWriter();
  38. out.println("<html>\n"
  39. + "<link rel='stylesheet' type='text/css' href='/static/hadoop.css'>\n"
  40. + "<title>" + title + "</title>\n"
  41. + "<body>\n"
  42. + "<h1>" + title + "</h1>\n");
  43. return out;
  44. }
  45. /**
  46. * Get a parameter from a ServletRequest.
  47. * Return null if the parameter contains only white spaces.
  48. */
  49. public static String getParameter(ServletRequest request, String name) {
  50. String s = request.getParameter(name);
  51. if (s == null) {
  52. return null;
  53. }
  54. s = s.trim();
  55. return s.length() == 0? null: s;
  56. }
  57. public static final String HTML_TAIL = "<hr />\n"
  58. + "<a href='http://hadoop.apache.org/core'>Hadoop</a>, "
  59. + Calendar.getInstance().get(Calendar.YEAR) + ".\n"
  60. + "</body></html>";
  61. /**
  62. * HTML footer to be added in the jsps.
  63. * @return the HTML footer.
  64. */
  65. public static String htmlFooter() {
  66. return HTML_TAIL;
  67. }
  68. /**
  69. * Generate the percentage graph and returns HTML representation string
  70. * of the same.
  71. *
  72. * @param perc The percentage value for which graph is to be generated
  73. * @param width The width of the display table
  74. * @return HTML String representation of the percentage graph
  75. * @throws IOException
  76. */
  77. public static String percentageGraph(int perc, int width) throws IOException {
  78. assert perc >= 0; assert perc <= 100;
  79. StringBuilder builder = new StringBuilder();
  80. builder.append("<table border=\"1px\" width=\""); builder.append(width);
  81. builder.append("px\"><tr>");
  82. if(perc > 0) {
  83. builder.append("<td cellspacing=\"0\" class=\"perc_filled\" width=\"");
  84. builder.append(perc); builder.append("%\"></td>");
  85. }if(perc < 100) {
  86. builder.append("<td cellspacing=\"0\" class=\"perc_nonfilled\" width=\"");
  87. builder.append(100 - perc); builder.append("%\"></td>");
  88. }
  89. builder.append("</tr></table>");
  90. return builder.toString();
  91. }
  92. /**
  93. * Generate the percentage graph and returns HTML representation string
  94. * of the same.
  95. * @param perc The percentage value for which graph is to be generated
  96. * @param width The width of the display table
  97. * @return HTML String representation of the percentage graph
  98. * @throws IOException
  99. */
  100. public static String percentageGraph(float perc, int width) throws IOException {
  101. return percentageGraph((int)perc, width);
  102. }
  103. /**
  104. * Escape and encode a string regarded as within the query component of an URI.
  105. * @param value the value to encode
  106. * @return encoded query, null if the default charset is not supported
  107. */
  108. public static String encodeQueryValue(final String value) {
  109. try {
  110. return URIUtil.encodeWithinQuery(value, "UTF-8");
  111. } catch (URIException e) {
  112. throw new AssertionError("JVM does not support UTF-8"); // should never happen!
  113. }
  114. }
  115. /**
  116. * Escape and encode a string regarded as the path component of an URI.
  117. * @param path the path component to encode
  118. * @return encoded path, null if UTF-8 is not supported
  119. */
  120. public static String encodePath(final String path) {
  121. try {
  122. return URIUtil.encodePath(path, "UTF-8");
  123. } catch (URIException e) {
  124. throw new AssertionError("JVM does not support UTF-8"); // should never happen!
  125. }
  126. }
  127. /**
  128. * Parse and decode the path component from the given request.
  129. * @param request Http request to parse
  130. * @param servletName the name of servlet that precedes the path
  131. * @return decoded path component, null if UTF-8 is not supported
  132. */
  133. public static String getDecodedPath(final HttpServletRequest request, String servletName) {
  134. try {
  135. return URIUtil.decode(getRawPath(request, servletName), "UTF-8");
  136. } catch (URIException e) {
  137. throw new AssertionError("JVM does not support UTF-8"); // should never happen!
  138. }
  139. }
  140. /**
  141. * Parse the path component from the given request and return w/o decoding.
  142. * @param request Http request to parse
  143. * @param servletName the name of servlet that precedes the path
  144. * @return path component, null if the default charset is not supported
  145. */
  146. public static String getRawPath(final HttpServletRequest request, String servletName) {
  147. Preconditions.checkArgument(request.getRequestURI().startsWith(servletName+"/"));
  148. return request.getRequestURI().substring(servletName.length());
  149. }
  150. }