1 16 17 18 package org.apache.catalina.hostmanager; 19 20 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import java.text.MessageFormat ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.TreeMap ; 27 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.catalina.Container; 33 import org.apache.catalina.Host; 34 import org.apache.catalina.util.ServerInfo; 35 36 58 59 public final class HTMLHostManagerServlet extends HostManagerServlet { 60 61 63 72 public void doGet(HttpServletRequest request, 73 HttpServletResponse response) 74 throws IOException , ServletException { 75 76 String command = request.getPathInfo(); 78 79 String name = request.getParameter("name"); 80 81 response.setContentType("text/html; charset=" + Constants.CHARSET); 83 84 String message = ""; 85 if (command == null) { 87 } else if (command.equals("/add")) { 88 message = add(request, name); 89 } else if (command.equals("/remove")) { 90 message = remove(name); 91 } else if (command.equals("/list")) { 92 } else if (command.equals("/start")) { 93 message = start(name); 94 } else if (command.equals("/stop")) { 95 message = stop(name); 96 } else { 97 message = 98 sm.getString("hostManagerServlet.unknownCommand", command); 99 } 100 101 list(request, response, message); 102 } 103 104 105 110 protected String add(HttpServletRequest request,String name) { 111 112 StringWriter stringWriter = new StringWriter (); 113 PrintWriter printWriter = new PrintWriter (stringWriter); 114 115 super.add(request,printWriter,name,true); 116 117 return stringWriter.toString(); 118 } 119 120 121 127 protected String remove(String name) { 128 129 StringWriter stringWriter = new StringWriter (); 130 PrintWriter printWriter = new PrintWriter (stringWriter); 131 132 super.remove(printWriter, name); 133 134 return stringWriter.toString(); 135 } 136 137 138 143 protected String start(String name) { 144 145 StringWriter stringWriter = new StringWriter (); 146 PrintWriter printWriter = new PrintWriter (stringWriter); 147 148 super.start(printWriter, name); 149 150 return stringWriter.toString(); 151 } 152 153 154 159 protected String stop(String name) { 160 161 StringWriter stringWriter = new StringWriter (); 162 PrintWriter printWriter = new PrintWriter (stringWriter); 163 164 super.stop(printWriter, name); 165 166 return stringWriter.toString(); 167 } 168 169 170 178 public void list(HttpServletRequest request, 179 HttpServletResponse response, 180 String message) throws IOException { 181 182 PrintWriter writer = response.getWriter(); 183 184 writer.print(Constants.HTML_HEADER_SECTION); 186 187 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 194 args = new Object [3]; 196 args[0] = sm.getString("htmlHostManagerServlet.messageLabel"); 197 args[1] = (message == null || message.length() == 0) ? "OK" : message; 198 writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args)); 199 200 args = new Object [9]; 202 args[0] = sm.getString("htmlHostManagerServlet.manager"); 203 args[1] = response.encodeURL(request.getContextPath() + "/html/list"); 204 args[2] = sm.getString("htmlHostManagerServlet.list"); 205 args[3] = response.encodeURL 206 (request.getContextPath() + "/" + 207 sm.getString("htmlHostManagerServlet.helpHtmlManagerFile")); 208 args[4] = sm.getString("htmlHostManagerServlet.helpHtmlManager"); 209 args[5] = response.encodeURL 210 (request.getContextPath() + "/" + 211 sm.getString("htmlHostManagerServlet.helpManagerFile")); 212 args[6] = sm.getString("htmlHostManagerServlet.helpManager"); 213 args[7] = response.encodeURL("/manager/status"); 214 args[8] = sm.getString("statusServlet.title"); 215 writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args)); 216 217 args = new Object [3]; 219 args[0] = sm.getString("htmlHostManagerServlet.hostName"); 220 args[1] = sm.getString("htmlHostManagerServlet.hostAliases"); 221 args[2] = sm.getString("htmlHostManagerServlet.hostTasks"); 222 writer.print(MessageFormat.format(HOSTS_HEADER_SECTION, args)); 223 224 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 231 TreeMap sortedHostNamesMap = new TreeMap (); 232 233 for (int i = 0; i < hostNames.length; i++) { 234 String displayPath = hostNames[i]; 235 sortedHostNamesMap.put(displayPath, hostNames[i]); 236 } 237 238 String hostsStart = sm.getString("htmlHostManagerServlet.hostsStart"); 239 String hostsStop = sm.getString("htmlHostManagerServlet.hostsStop"); 240 String hostsRemove = sm.getString("htmlHostManagerServlet.hostsRemove"); 241 242 Iterator iterator = sortedHostNamesMap.entrySet().iterator(); 243 while (iterator.hasNext()) { 244 Map.Entry entry = (Map.Entry ) iterator.next(); 245 String hostName = (String ) entry.getKey(); 246 Host host = (Host) engine.findChild(hostName); 247 248 if (host != null ) { 249 args = new Object [2]; 250 args[0] = hostName; 251 String [] aliases = host.findAliases(); 252 StringBuffer buf = new StringBuffer (); 253 if (aliases.length > 0) { 254 buf.append(aliases[0]); 255 for (int j = 1; j < aliases.length; j++) { 256 buf.append(", ").append(aliases[j]); 257 } 258 } 259 args[1] = buf.toString(); 260 writer.print 261 (MessageFormat.format(HOSTS_ROW_DETAILS_SECTION, args)); 262 263 args = new Object [6]; 264 args[0] = response.encodeURL 265 (request.getContextPath() + 266 "/html/start?name=" + hostName); 267 args[1] = hostsStart; 268 args[2] = response.encodeURL 269 (request.getContextPath() + 270 "/html/stop?name=" + hostName); 271 args[3] = hostsStop; 272 args[4] = response.encodeURL 273 (request.getContextPath() + 274 "/html/remove?name=" + hostName); 275 args[5] = hostsRemove; 276 if (host == this.host) { 277 writer.print(MessageFormat.format( 278 MANAGER_HOST_ROW_BUTTON_SECTION, args)); 279 } else { 280 writer.print(MessageFormat.format( 281 HOSTS_ROW_BUTTON_SECTION, args)); 282 } 283 284 } 285 } 286 287 args = new Object [6]; 289 args[0] = sm.getString("htmlHostManagerServlet.addTitle"); 290 args[1] = sm.getString("htmlHostManagerServlet.addHost"); 291 args[2] = response.encodeURL(request.getContextPath() + "/html/add"); 292 args[3] = sm.getString("htmlHostManagerServlet.addName"); 293 args[4] = sm.getString("htmlHostManagerServlet.addAliases"); 294 args[5] = sm.getString("htmlHostManagerServlet.addAppBase"); 295 writer.print(MessageFormat.format(ADD_SECTION_START, args)); 296 297 args = new Object [3]; 298 args[0] = sm.getString("htmlHostManagerServlet.addAutoDeploy"); 299 args[1] = "autoDeploy"; 300 args[2] = "checked"; 301 writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args)); 302 args[0] = sm.getString("htmlHostManagerServlet.addDeployOnStartup"); 303 args[1] = "deployOnStartup"; 304 args[2] = "checked"; 305 writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args)); 306 args[0] = sm.getString("htmlHostManagerServlet.addDeployXML"); 307 args[1] = "deployXML"; 308 args[2] = "checked"; 309 writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args)); 310 args[0] = sm.getString("htmlHostManagerServlet.addUnpackWARs"); 311 args[1] = "unpackWARs"; 312 args[2] = "checked"; 313 writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args)); 314 args[0] = sm.getString("htmlHostManagerServlet.addXmlNamespaceAware"); 315 args[1] = "xmlNamespaceAware"; 316 args[2] = ""; 317 writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args)); 318 args[0] = sm.getString("htmlHostManagerServlet.addXmlValidation"); 319 args[1] = "xmlValidation"; 320 args[2] = ""; 321 writer.print(MessageFormat.format(ADD_SECTION_BOOLEAN, args)); 322 323 324 args = new Object [1]; 325 args[0] = sm.getString("htmlHostManagerServlet.addButton"); 326 writer.print(MessageFormat.format(ADD_SECTION_END, args)); 327 328 args = new Object [7]; 330 args[0] = sm.getString("htmlHostManagerServlet.serverTitle"); 331 args[1] = sm.getString("htmlHostManagerServlet.serverVersion"); 332 args[2] = sm.getString("htmlHostManagerServlet.serverJVMVersion"); 333 args[3] = sm.getString("htmlHostManagerServlet.serverJVMVendor"); 334 args[4] = sm.getString("htmlHostManagerServlet.serverOSName"); 335 args[5] = sm.getString("htmlHostManagerServlet.serverOSVersion"); 336 args[6] = sm.getString("htmlHostManagerServlet.serverOSArch"); 337 writer.print(MessageFormat.format 338 (Constants.SERVER_HEADER_SECTION, args)); 339 340 args = new Object [6]; 342 args[0] = ServerInfo.getServerInfo(); 343 args[1] = System.getProperty("java.runtime.version"); 344 args[2] = System.getProperty("java.vm.vendor"); 345 args[3] = System.getProperty("os.name"); 346 args[4] = System.getProperty("os.version"); 347 args[5] = System.getProperty("os.arch"); 348 writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args)); 349 350 writer.print(Constants.HTML_TAIL_SECTION); 352 353 writer.flush(); 355 writer.close(); 356 } 357 358 359 361 365 private static final String HOSTS_HEADER_SECTION = 366 "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" + 367 "<tr>\n" + 368 " <td colspan=\"5\" class=\"title\">{0}</td>\n" + 369 "</tr>\n" + 370 "<tr>\n" + 371 " <td class=\"header-left\"><small>{0}</small></td>\n" + 372 " <td class=\"header-center\"><small>{1}</small></td>\n" + 373 " <td class=\"header-center\"><small>{2}</small></td>\n" + 374 "</tr>\n"; 375 376 private static final String HOSTS_ROW_DETAILS_SECTION = 377 "<tr>\n" + 378 " <td class=\"row-left\"><small><a HREF=\"{0}\">{0}</a>" + 379 "</small></td>\n" + 380 " <td class=\"row-center\"><small>{1}</small></td>\n"; 381 382 private static final String MANAGER_HOST_ROW_BUTTON_SECTION = 383 " <td class=\"row-left\">\n" + 384 " <small>\n" + 385 " {1} \n" + 386 " {3} \n" + 387 " {5} \n" + 388 " </small>\n" + 389 " </td>\n" + 390 "</tr>\n"; 391 392 private static final String HOSTS_ROW_BUTTON_SECTION = 393 " <td class=\"row-left\">\n" + 394 " <small>\n" + 395 " <a HREF=\"{0}\" onclick=\"return(confirm('Are you sure?'))\">{1}</a> \n" + 396 " <a HREF=\"{2}\" onclick=\"return(confirm('Are you sure?'))\">{3}</a> \n" + 397 " <a HREF=\"{4}\" onclick=\"return(confirm('Are you sure?'))\">{5}</a> \n" + 398 " </small>\n" + 399 " </td>\n" + 400 "</tr>\n"; 401 402 private static final String ADD_SECTION_START = 403 "</table>\n" + 404 "<br>\n" + 405 "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" + 406 "<tr>\n" + 407 " <td colspan=\"2\" class=\"title\">{0}</td>\n" + 408 "</tr>\n" + 409 "<tr>\n" + 410 " <td colspan=\"2\" class=\"header-left\"><small>{1}</small></td>\n" + 411 "</tr>\n" + 412 "<tr>\n" + 413 " <td colspan=\"2\">\n" + 414 "<form method=\"get\" action=\"{2}\">\n" + 415 "<table cellspacing=\"0\" cellpadding=\"3\">\n" + 416 "<tr>\n" + 417 " <td class=\"row-right\">\n" + 418 " <small>{3}</small>\n" + 419 " </td>\n" + 420 " <td class=\"row-left\">\n" + 421 " <input type=\"text\" name=\"name\" size=\"20\">\n" + 422 " </td>\n" + 423 "</tr>\n" + 424 "<tr>\n" + 425 " <td class=\"row-right\">\n" + 426 " <small>{4}</small>\n" + 427 " </td>\n" + 428 " <td class=\"row-left\">\n" + 429 " <input type=\"text\" name=\"aliases\" size=\"64\">\n" + 430 " </td>\n" + 431 "</tr>\n" + 432 "<tr>\n" + 433 " <td class=\"row-right\">\n" + 434 " <small>{5}</small>\n" + 435 " </td>\n" + 436 " <td class=\"row-left\">\n" + 437 " <input type=\"text\" name=\"appBase\" size=\"64\">\n" + 438 " </td>\n" + 439 "</tr>\n" ; 440 441 private static final String ADD_SECTION_BOOLEAN = 442 "<tr>\n" + 443 " <td class=\"row-right\">\n" + 444 " <small>{0}</small>\n" + 445 " </td>\n" + 446 " <td class=\"row-left\">\n" + 447 " <input type=\"checkbox\" name=\"{1}\" {2}>\n" + 448 " </td>\n" + 449 "</tr>\n" ; 450 451 private static final String ADD_SECTION_END = 452 "<tr>\n" + 453 " <td class=\"row-right\">\n" + 454 " \n" + 455 " </td>\n" + 456 " <td class=\"row-left\">\n" + 457 " <input type=\"hidden\" name=\"manager\" value=\"true\">\n" + 458 " <input type=\"submit\" value=\"{0}\">\n" + 459 " </td>\n" + 460 "</tr>\n" + 461 "</table>\n" + 462 "</form>\n" + 463 "</td>\n" + 464 "</tr>\n" + 465 "</table>\n" + 466 "<br>\n" + 467 "\n"; 468 469 } 470 | Popular Tags |