PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/tomcat-7.0.2/java/org/apache/catalina/manager/host/HTMLHostManagerServlet.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 537 lines | 347 code | 79 blank | 111 comment | 41 complexity | b1dcf0104797082532e7962b8b686e13 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.catalina.manager.host;
  18. import java.io.IOException;
  19. import java.io.PrintWriter;
  20. import java.io.StringWriter;
  21. import java.net.URLEncoder;
  22. import java.text.MessageFormat;
  23. import java.util.Iterator;
  24. import java.util.Map;
  25. import java.util.TreeMap;
  26. import javax.servlet.ServletException;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import org.apache.catalina.Container;
  30. import org.apache.catalina.Host;
  31. import org.apache.catalina.util.RequestUtil;
  32. import org.apache.catalina.util.ServerInfo;
  33. /**
  34. * Servlet that enables remote management of the virtual hosts deployed
  35. * on the server. Normally, this functionality will be protected by a security
  36. * constraint in the web application deployment descriptor. However,
  37. * this requirement can be relaxed during testing.
  38. * <p>
  39. * The difference between the <code>HostManagerServlet</code> and this
  40. * Servlet is that this Servlet prints out a HTML interface which
  41. * makes it easier to administrate.
  42. * <p>
  43. * However if you use a software that parses the output of
  44. * <code>HostManagerServlet</code> you won't be able to upgrade
  45. * to this Servlet since the output are not in the
  46. * same format as from <code>HostManagerServlet</code>
  47. *
  48. * @author Bip Thelin
  49. * @author Malcolm Edgar
  50. * @author Glenn L. Nielsen
  51. * @author Peter Rossbach
  52. * @version $Id: HTMLHostManagerServlet.java 981816 2010-08-03 10:44:58Z markt $
  53. * @see org.apache.catalina.manager.ManagerServlet
  54. */
  55. public final class HTMLHostManagerServlet extends HostManagerServlet {
  56. private static final long serialVersionUID = 1L;
  57. // --------------------------------------------------------- Public Methods
  58. /**
  59. * Process a GET request for the specified resource.
  60. *
  61. * @param request The servlet request we are processing
  62. * @param response The servlet response we are creating
  63. *
  64. * @exception IOException if an input/output error occurs
  65. * @exception ServletException if a servlet-specified error occurs
  66. */
  67. @Override
  68. public void doGet(HttpServletRequest request,
  69. HttpServletResponse response)
  70. throws IOException, ServletException {
  71. // Identify the request parameters that we need
  72. String command = request.getPathInfo();
  73. // Prepare our output writer to generate the response message
  74. response.setContentType("text/html; charset=" + Constants.CHARSET);
  75. String message = "";
  76. // Process the requested command
  77. if (command == null) {
  78. // No command == list
  79. } else if (command.equals("/list")) {
  80. // Nothing to do - always generate list
  81. } else if (command.equals("/add") || command.equals("/remove") ||
  82. command.equals("/start") || command.equals("/stop")) {
  83. message =
  84. sm.getString("hostManagerServlet.postCommand", command);
  85. } else {
  86. message =
  87. sm.getString("hostManagerServlet.unknownCommand", command);
  88. }
  89. list(request, response, message);
  90. }
  91. /**
  92. * Process a POST request for the specified resource.
  93. *
  94. * @param request The servlet request we are processing
  95. * @param response The servlet response we are creating
  96. *
  97. * @exception IOException if an input/output error occurs
  98. * @exception ServletException if a servlet-specified error occurs
  99. */
  100. @Override
  101. public void doPost(HttpServletRequest request, HttpServletResponse response)
  102. throws ServletException, IOException {
  103. // Identify the request parameters that we need
  104. String command = request.getPathInfo();
  105. String name = request.getParameter("name");
  106. // Prepare our output writer to generate the response message
  107. response.setContentType("text/html; charset=" + Constants.CHARSET);
  108. String message = "";
  109. // Process the requested command
  110. if (command == null) {
  111. // No command == list
  112. } else if (command.equals("/add")) {
  113. message = add(request, name);
  114. } else if (command.equals("/remove")) {
  115. message = remove(name);
  116. } else if (command.equals("/start")) {
  117. message = start(name);
  118. } else if (command.equals("/stop")) {
  119. message = stop(name);
  120. } else {
  121. //Try GET
  122. doGet(request, response);
  123. }
  124. list(request, response, message);
  125. }
  126. /**
  127. * Add a host using the specified parameters.
  128. *
  129. * @param name host name
  130. */
  131. protected String add(HttpServletRequest request,String name) {
  132. StringWriter stringWriter = new StringWriter();
  133. PrintWriter printWriter = new PrintWriter(stringWriter);
  134. super.add(request,printWriter,name,true);
  135. return stringWriter.toString();
  136. }
  137. /**
  138. * Remove the specified host.
  139. *
  140. * @param name host name
  141. */
  142. protected String remove(String name) {
  143. StringWriter stringWriter = new StringWriter();
  144. PrintWriter printWriter = new PrintWriter(stringWriter);
  145. super.remove(printWriter, name);
  146. return stringWriter.toString();
  147. }
  148. /**
  149. * Start the host with the specified name.
  150. *
  151. * @param name Host name
  152. */
  153. protected String start(String name) {
  154. StringWriter stringWriter = new StringWriter();
  155. PrintWriter printWriter = new PrintWriter(stringWriter);
  156. super.start(printWriter, name);
  157. return stringWriter.toString();
  158. }
  159. /**
  160. * Stop the host with the specified name.
  161. *
  162. * @param name Host name
  163. */
  164. protected String stop(String name) {
  165. StringWriter stringWriter = new StringWriter();
  166. PrintWriter printWriter = new PrintWriter(stringWriter);
  167. super.stop(printWriter, name);
  168. return stringWriter.toString();
  169. }
  170. /**
  171. * Render a HTML list of the currently active Contexts in our virtual host,
  172. * and memory and server status information.
  173. *
  174. * @param request The request
  175. * @param response The response
  176. * @param message a message to display
  177. */
  178. public void list(HttpServletRequest request,
  179. HttpServletResponse response,
  180. String message) throws IOException {
  181. if (debug >= 1) {
  182. log(sm.getString("hostManagerServlet.list", engine.getName()));
  183. }
  184. PrintWriter writer = response.getWriter();
  185. // HTML Header Section
  186. writer.print(Constants.HTML_HEADER_SECTION);
  187. // Body Header Section
  188. Object[] args = new Object[2];
  189. args[0] = request.getContextPath();
  190. args[1] = sm.getString("htmlHostManagerServlet.title");
  191. writer.print(MessageFormat.format
  192. (Constants.BODY_HEADER_SECTION, args));
  193. // Message Section
  194. args = new Object[3];
  195. args[0] = sm.getString("htmlHostManagerServlet.messageLabel");
  196. if (message == null || message.length() == 0) {
  197. args[1] = "OK";
  198. } else {
  199. args[1] = RequestUtil.filter(message);
  200. }
  201. writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args));
  202. // Manager Section
  203. args = new Object[9];
  204. args[0] = sm.getString("htmlHostManagerServlet.manager");
  205. args[1] = response.encodeURL(request.getContextPath() + "/html/list");
  206. args[2] = sm.getString("htmlHostManagerServlet.list");
  207. args[3] = response.encodeURL
  208. (request.getContextPath() + "/" +
  209. sm.getString("htmlHostManagerServlet.helpHtmlManagerFile"));
  210. args[4] = sm.getString("htmlHostManagerServlet.helpHtmlManager");
  211. args[5] = response.encodeURL
  212. (request.getContextPath() + "/" +
  213. sm.getString("htmlHostManagerServlet.helpManagerFile"));
  214. args[6] = sm.getString("htmlHostManagerServlet.helpManager");
  215. args[7] = response.encodeURL("/manager/status");
  216. args[8] = sm.getString("statusServlet.title");
  217. writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args));
  218. // Hosts Header Section
  219. args = new Object[3];
  220. args[0] = sm.getString("htmlHostManagerServlet.hostName");
  221. args[1] = sm.getString("htmlHostManagerServlet.hostAliases");
  222. args[2] = sm.getString("htmlHostManagerServlet.hostTasks");
  223. writer.print(MessageFormat.format(HOSTS_HEADER_SECTION, args));
  224. // Hosts Row Section
  225. // Create sorted map of host names.
  226. Container[] children = engine.findChildren();
  227. String hostNames[] = new String[children.length];
  228. for (int i = 0; i < children.length; i++)
  229. hostNames[i] = children[i].getName();
  230. TreeMap<String,String> sortedHostNamesMap =
  231. new TreeMap<String,String>();
  232. for (int i = 0; i < hostNames.length; i++) {
  233. String displayPath = hostNames[i];
  234. sortedHostNamesMap.put(displayPath, hostNames[i]);
  235. }
  236. String hostsStart = sm.getString("htmlHostManagerServlet.hostsStart");
  237. String hostsStop = sm.getString("htmlHostManagerServlet.hostsStop");
  238. String hostsRemove = sm.getString("htmlHostManagerServlet.hostsRemove");
  239. Iterator<Map.Entry<String,String>> iterator =
  240. sortedHostNamesMap.entrySet().iterator();
  241. while (iterator.hasNext()) {
  242. Map.Entry<String,String> entry = iterator.next();
  243. String hostName = entry.getKey();
  244. Host host = (Host) engine.findChild(hostName);
  245. if (host != null ) {
  246. args = new Object[2];
  247. args[0] = RequestUtil.filter(hostName);
  248. String[] aliases = host.findAliases();
  249. StringBuilder buf = new StringBuilder();
  250. if (aliases.length > 0) {
  251. buf.append(aliases[0]);
  252. for (int j = 1; j < aliases.length; j++) {
  253. buf.append(", ").append(aliases[j]);
  254. }
  255. }
  256. if (buf.length() == 0) {
  257. buf.append("&nbsp;");
  258. args[1] = buf.toString();
  259. } else {
  260. args[1] = RequestUtil.filter(buf.toString());
  261. }
  262. writer.print
  263. (MessageFormat.format(HOSTS_ROW_DETAILS_SECTION, args));
  264. args = new Object[4];
  265. if (host.getState().isAvailable()) {
  266. args[0] = response.encodeURL
  267. (request.getContextPath() +
  268. "/html/stop?name=" +
  269. URLEncoder.encode(hostName, "UTF-8"));
  270. args[1] = hostsStop;
  271. } else {
  272. args[0] = response.encodeURL
  273. (request.getContextPath() +
  274. "/html/start?name=" +
  275. URLEncoder.encode(hostName, "UTF-8"));
  276. args[1] = hostsStart;
  277. }
  278. args[2] = response.encodeURL
  279. (request.getContextPath() +
  280. "/html/remove?name=" +
  281. URLEncoder.encode(hostName, "UTF-8"));
  282. args[3] = hostsRemove;
  283. if (host == this.installedHost) {
  284. writer.print(MessageFormat.format(
  285. MANAGER_HOST_ROW_BUTTON_SECTION, args));
  286. } else {
  287. writer.print(MessageFormat.format(
  288. HOSTS_ROW_BUTTON_SECTION, args));
  289. }
  290. }
  291. }
  292. // Add Section
  293. args = new Object[6];
  294. args[0] = sm.getString("htmlHostManagerServlet.addTitle");
  295. args[1] = sm.getString("htmlHostManagerServlet.addHost");
  296. args[2] = response.encodeURL(request.getContextPath() + "/html/add");
  297. args[3] = sm.getString("htmlHostManagerServlet.addName");
  298. args[4] = sm.getString("htmlHostManagerServlet.addAliases");
  299. args[5] = sm.getString("htmlHostManagerServlet.addAppBase");
  300. writer.print(MessageFormat.format(ADD_SECTION_START, args));
  301. args = new Object[3];
  302. args[0] = sm.getString("htmlHostManagerServlet.addAutoDeploy");
  303. args[1] = "autoDeploy";
  304. args[2] = "checked";
  305. writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
  306. args[0] = sm.getString("htmlHostManagerServlet.addDeployOnStartup");
  307. args[1] = "deployOnStartup";
  308. args[2] = "checked";
  309. writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
  310. args[0] = sm.getString("htmlHostManagerServlet.addDeployXML");
  311. args[1] = "deployXML";
  312. args[2] = "checked";
  313. writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
  314. args[0] = sm.getString("htmlHostManagerServlet.addUnpackWARs");
  315. args[1] = "unpackWARs";
  316. args[2] = "checked";
  317. writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
  318. args[0] = sm.getString("htmlHostManagerServlet.addXmlNamespaceAware");
  319. args[1] = "xmlNamespaceAware";
  320. args[2] = "";
  321. writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
  322. args[0] = sm.getString("htmlHostManagerServlet.addXmlValidation");
  323. args[1] = "xmlValidation";
  324. args[2] = "";
  325. writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
  326. args[0] = sm.getString("htmlHostManagerServlet.addManager");
  327. args[1] = "manager";
  328. args[2] = "checked";
  329. writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args));
  330. args = new Object[1];
  331. args[0] = sm.getString("htmlHostManagerServlet.addButton");
  332. writer.print(MessageFormat.format(ADD_SECTION_END, args));
  333. // Server Header Section
  334. args = new Object[7];
  335. args[0] = sm.getString("htmlHostManagerServlet.serverTitle");
  336. args[1] = sm.getString("htmlHostManagerServlet.serverVersion");
  337. args[2] = sm.getString("htmlHostManagerServlet.serverJVMVersion");
  338. args[3] = sm.getString("htmlHostManagerServlet.serverJVMVendor");
  339. args[4] = sm.getString("htmlHostManagerServlet.serverOSName");
  340. args[5] = sm.getString("htmlHostManagerServlet.serverOSVersion");
  341. args[6] = sm.getString("htmlHostManagerServlet.serverOSArch");
  342. writer.print(MessageFormat.format
  343. (Constants.SERVER_HEADER_SECTION, args));
  344. // Server Row Section
  345. args = new Object[6];
  346. args[0] = ServerInfo.getServerInfo();
  347. args[1] = System.getProperty("java.runtime.version");
  348. args[2] = System.getProperty("java.vm.vendor");
  349. args[3] = System.getProperty("os.name");
  350. args[4] = System.getProperty("os.version");
  351. args[5] = System.getProperty("os.arch");
  352. writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args));
  353. // HTML Tail Section
  354. writer.print(Constants.HTML_TAIL_SECTION);
  355. // Finish up the response
  356. writer.flush();
  357. writer.close();
  358. }
  359. // ------------------------------------------------------ Private Constants
  360. // These HTML sections are broken in relatively small sections, because of
  361. // limited number of substitutions MessageFormat can process
  362. // (maximum of 10).
  363. private static final String HOSTS_HEADER_SECTION =
  364. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" +
  365. "<tr>\n" +
  366. " <td colspan=\"5\" class=\"title\">{0}</td>\n" +
  367. "</tr>\n" +
  368. "<tr>\n" +
  369. " <td class=\"header-left\"><small>{0}</small></td>\n" +
  370. " <td class=\"header-center\"><small>{1}</small></td>\n" +
  371. " <td class=\"header-center\"><small>{2}</small></td>\n" +
  372. "</tr>\n";
  373. private static final String HOSTS_ROW_DETAILS_SECTION =
  374. "<tr>\n" +
  375. " <td class=\"row-left\"><small><a href=\"http://{0}\">{0}</a>" +
  376. "</small></td>\n" +
  377. " <td class=\"row-center\"><small>{1}</small></td>\n";
  378. private static final String MANAGER_HOST_ROW_BUTTON_SECTION =
  379. " <td class=\"row-left\">\n" +
  380. " <small>\n" +
  381. sm.getString("htmlHostManagerServlet.hostThis") +
  382. " </small>\n" +
  383. " </td>\n" +
  384. "</tr>\n";
  385. private static final String HOSTS_ROW_BUTTON_SECTION =
  386. " <td class=\"row-left\" NOWRAP>\n" +
  387. " <form class=\"inline\" method=\"POST\" action=\"{0}\">" +
  388. " <small><input type=\"submit\" value=\"{1}\"></small>" +
  389. " </form>\n" +
  390. " <form class=\"inline\" method=\"POST\" action=\"{2}\">" +
  391. " <small><input type=\"submit\" value=\"{3}\"></small>" +
  392. " </form>\n" +
  393. " </td>\n" +
  394. "</tr>\n";
  395. private static final String ADD_SECTION_START =
  396. "</table>\n" +
  397. "<br>\n" +
  398. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" +
  399. "<tr>\n" +
  400. " <td colspan=\"2\" class=\"title\">{0}</td>\n" +
  401. "</tr>\n" +
  402. "<tr>\n" +
  403. " <td colspan=\"2\" class=\"header-left\"><small>{1}</small></td>\n" +
  404. "</tr>\n" +
  405. "<tr>\n" +
  406. " <td colspan=\"2\">\n" +
  407. "<form method=\"post\" action=\"{2}\">\n" +
  408. "<table cellspacing=\"0\" cellpadding=\"3\">\n" +
  409. "<tr>\n" +
  410. " <td class=\"row-right\">\n" +
  411. " <small>{3}</small>\n" +
  412. " </td>\n" +
  413. " <td class=\"row-left\">\n" +
  414. " <input type=\"text\" name=\"name\" size=\"20\">\n" +
  415. " </td>\n" +
  416. "</tr>\n" +
  417. "<tr>\n" +
  418. " <td class=\"row-right\">\n" +
  419. " <small>{4}</small>\n" +
  420. " </td>\n" +
  421. " <td class=\"row-left\">\n" +
  422. " <input type=\"text\" name=\"aliases\" size=\"64\">\n" +
  423. " </td>\n" +
  424. "</tr>\n" +
  425. "<tr>\n" +
  426. " <td class=\"row-right\">\n" +
  427. " <small>{5}</small>\n" +
  428. " </td>\n" +
  429. " <td class=\"row-left\">\n" +
  430. " <input type=\"text\" name=\"appBase\" size=\"64\">\n" +
  431. " </td>\n" +
  432. "</tr>\n" ;
  433. private static final String ADD_SECTION_BOOLEAN =
  434. "<tr>\n" +
  435. " <td class=\"row-right\">\n" +
  436. " <small>{0}</small>\n" +
  437. " </td>\n" +
  438. " <td class=\"row-left\">\n" +
  439. " <input type=\"checkbox\" name=\"{1}\" {2}>\n" +
  440. " </td>\n" +
  441. "</tr>\n" ;
  442. private static final String ADD_SECTION_END =
  443. "<tr>\n" +
  444. " <td class=\"row-right\">\n" +
  445. " &nbsp;\n" +
  446. " </td>\n" +
  447. " <td class=\"row-left\">\n" +
  448. " <input type=\"submit\" value=\"{0}\">\n" +
  449. " </td>\n" +
  450. "</tr>\n" +
  451. "</table>\n" +
  452. "</form>\n" +
  453. "</td>\n" +
  454. "</tr>\n" +
  455. "</table>\n" +
  456. "<br>\n" +
  457. "\n";
  458. }