1 17 18 19 package org.apache.catalina.manager; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.text.MessageFormat ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.TreeMap ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.catalina.Container; 35 import org.apache.catalina.Context; 36 import org.apache.catalina.util.RequestUtil; 37 import org.apache.catalina.util.ServerInfo; 38 import org.apache.tomcat.util.http.fileupload.DiskFileUpload; 39 import org.apache.tomcat.util.http.fileupload.FileItem; 40 41 63 64 public final class HTMLManagerServlet extends ManagerServlet { 65 66 68 77 public void doGet(HttpServletRequest request, 78 HttpServletResponse response) 79 throws IOException , ServletException { 80 81 String command = request.getPathInfo(); 83 84 String path = request.getParameter("path"); 85 String deployPath = request.getParameter("deployPath"); 86 String deployConfig = request.getParameter("deployConfig"); 87 String deployWar = request.getParameter("deployWar"); 88 89 response.setContentType("text/html; charset=" + Constants.CHARSET); 91 92 String message = ""; 93 if (command == null || command.equals("/")) { 95 } else if (command.equals("/deploy")) { 96 message = deployInternal(deployConfig, deployPath, deployWar); 97 } else if (command.equals("/list")) { 98 } else if (command.equals("/reload")) { 99 message = reload(path); 100 } else if (command.equals("/undeploy")) { 101 message = undeploy(path); 102 } else if (command.equals("/sessions")) { 103 message = sessions(path); 104 } else if (command.equals("/start")) { 105 message = start(path); 106 } else if (command.equals("/stop")) { 107 message = stop(path); 108 } else { 109 message = 110 sm.getString("managerServlet.unknownCommand", 111 RequestUtil.filter(command)); 112 } 113 114 list(request, response, message); 115 } 116 117 126 public void doPost(HttpServletRequest request, 127 HttpServletResponse response) 128 throws IOException , ServletException { 129 130 String command = request.getPathInfo(); 132 133 if (command == null || !command.equals("/upload")) { 134 doGet(request,response); 135 return; 136 } 137 138 response.setContentType("text/html; charset=" + Constants.CHARSET); 140 141 String message = ""; 142 143 DiskFileUpload upload = new DiskFileUpload(); 145 146 File tempdir = (File ) getServletContext().getAttribute 148 ("javax.servlet.context.tempdir"); 149 upload.setSizeMax(-1); 151 upload.setRepositoryPath(tempdir.getCanonicalPath()); 152 153 String basename = null; 155 String war = null; 156 FileItem warUpload = null; 157 try { 158 List items = upload.parseRequest(request); 159 160 Iterator iter = items.iterator(); 162 while (iter.hasNext()) { 163 FileItem item = (FileItem) iter.next(); 164 165 if (!item.isFormField()) { 166 if (item.getFieldName().equals("deployWar") && 167 warUpload == null) { 168 warUpload = item; 169 } else { 170 item.delete(); 171 } 172 } 173 } 174 while (true) { 175 if (warUpload == null) { 176 message = sm.getString 177 ("htmlManagerServlet.deployUploadNoFile"); 178 break; 179 } 180 war = warUpload.getName(); 181 if (!war.toLowerCase().endsWith(".war")) { 182 message = sm.getString 183 ("htmlManagerServlet.deployUploadNotWar",war); 184 break; 185 } 186 if (war.lastIndexOf('\\') >= 0) { 188 war = war.substring(war.lastIndexOf('\\') + 1); 189 } 190 if (war.lastIndexOf('/') >= 0) { 191 war = war.substring(war.lastIndexOf('/') + 1); 192 } 193 basename = war.substring(0, war.toLowerCase().indexOf(".war")); 196 File file = new File (getAppBase(), war); 197 if (file.exists()) { 198 message = sm.getString 199 ("htmlManagerServlet.deployUploadWarExists",war); 200 break; 201 } 202 String path = null; 203 if (basename.equals("ROOT")) { 204 path = ""; 205 } else { 206 path = "/" + basename; 207 } 208 209 if (!isServiced(path)) { 210 addServiced(path); 211 try { 212 warUpload.write(file); 213 check(path); 215 } finally { 216 removeServiced(path); 217 } 218 } 219 break; 220 } 221 } catch(Exception e) { 222 message = sm.getString 223 ("htmlManagerServlet.deployUploadFail", e.getMessage()); 224 log(message, e); 225 } finally { 226 if (warUpload != null) { 227 warUpload.delete(); 228 } 229 warUpload = null; 230 } 231 232 list(request, response, message); 233 } 234 235 244 protected String deployInternal(String config, String path, String war) { 245 246 StringWriter stringWriter = new StringWriter (); 247 PrintWriter printWriter = new PrintWriter (stringWriter); 248 249 super.deploy(printWriter, config, path, war, false); 250 251 return stringWriter.toString(); 252 } 253 254 262 public void list(HttpServletRequest request, 263 HttpServletResponse response, 264 String message) throws IOException { 265 266 if (debug >= 1) 267 log("list: Listing contexts for virtual host '" + 268 host.getName() + "'"); 269 270 PrintWriter writer = response.getWriter(); 271 272 writer.print(Constants.HTML_HEADER_SECTION); 274 275 Object [] args = new Object [2]; 277 args[0] = request.getContextPath(); 278 args[1] = sm.getString("htmlManagerServlet.title"); 279 writer.print(MessageFormat.format 280 (Constants.BODY_HEADER_SECTION, args)); 281 282 args = new Object [3]; 284 args[0] = sm.getString("htmlManagerServlet.messageLabel"); 285 args[1] = (message == null || message.length() == 0) ? "OK" : message; 286 writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args)); 287 288 args = new Object [9]; 290 args[0] = sm.getString("htmlManagerServlet.manager"); 291 args[1] = response.encodeURL(request.getContextPath() + "/html/list"); 292 args[2] = sm.getString("htmlManagerServlet.list"); 293 args[3] = response.encodeURL 294 (request.getContextPath() + "/" + 295 sm.getString("htmlManagerServlet.helpHtmlManagerFile")); 296 args[4] = sm.getString("htmlManagerServlet.helpHtmlManager"); 297 args[5] = response.encodeURL 298 (request.getContextPath() + "/" + 299 sm.getString("htmlManagerServlet.helpManagerFile")); 300 args[6] = sm.getString("htmlManagerServlet.helpManager"); 301 args[7] = response.encodeURL 302 (request.getContextPath() + "/status"); 303 args[8] = sm.getString("statusServlet.title"); 304 writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args)); 305 306 args = new Object [6]; 308 args[0] = sm.getString("htmlManagerServlet.appsTitle"); 309 args[1] = sm.getString("htmlManagerServlet.appsPath"); 310 args[2] = sm.getString("htmlManagerServlet.appsName"); 311 args[3] = sm.getString("htmlManagerServlet.appsAvailable"); 312 args[4] = sm.getString("htmlManagerServlet.appsSessions"); 313 args[5] = sm.getString("htmlManagerServlet.appsTasks"); 314 writer.print(MessageFormat.format(APPS_HEADER_SECTION, args)); 315 316 Container children[] = host.findChildren(); 319 String contextPaths[] = new String [children.length]; 320 for (int i = 0; i < children.length; i++) 321 contextPaths[i] = children[i].getName(); 322 323 TreeMap sortedContextPathsMap = new TreeMap (); 324 325 for (int i = 0; i < contextPaths.length; i++) { 326 String displayPath = contextPaths[i]; 327 sortedContextPathsMap.put(displayPath, contextPaths[i]); 328 } 329 330 String appsStart = sm.getString("htmlManagerServlet.appsStart"); 331 String appsStop = sm.getString("htmlManagerServlet.appsStop"); 332 String appsReload = sm.getString("htmlManagerServlet.appsReload"); 333 String appsUndeploy = sm.getString("htmlManagerServlet.appsUndeploy"); 334 335 Iterator iterator = sortedContextPathsMap.entrySet().iterator(); 336 boolean isHighlighted = true; 337 boolean isDeployed = true; 338 String highlightColor = null; 339 340 while (iterator.hasNext()) { 341 isHighlighted = !isHighlighted; 343 if(isHighlighted) { 344 highlightColor = "#C3F3C3"; 345 } else { 346 highlightColor = "#FFFFFF"; 347 } 348 349 Map.Entry entry = (Map.Entry ) iterator.next(); 350 String displayPath = (String ) entry.getKey(); 351 String contextPath = (String ) entry.getKey(); 352 Context context = (Context) host.findChild(contextPath); 353 if (displayPath.equals("")) { 354 displayPath = "/"; 355 } 356 357 if (context != null ) { 358 try { 359 isDeployed = isDeployed(contextPath); 360 } catch (Exception e) { 361 isDeployed = false; 363 } 364 365 args = new Object [6]; 366 args[0] = displayPath; 367 args[1] = context.getDisplayName(); 368 if (args[1] == null) { 369 args[1] = " "; 370 } 371 args[2] = new Boolean (context.getAvailable()); 372 args[3] = response.encodeURL 373 (request.getContextPath() + 374 "/html/sessions?path=" + displayPath); 375 if (context.getManager() != null) { 376 args[4] = new Integer 377 (context.getManager().getActiveSessions()); 378 } else { 379 args[4] = new Integer (0); 380 } 381 382 args[5] = highlightColor; 383 384 writer.print 385 (MessageFormat.format(APPS_ROW_DETAILS_SECTION, args)); 386 387 args = new Object [9]; 388 args[0] = response.encodeURL 389 (request.getContextPath() + 390 "/html/start?path=" + displayPath); 391 args[1] = appsStart; 392 args[2] = response.encodeURL 393 (request.getContextPath() + 394 "/html/stop?path=" + displayPath); 395 args[3] = appsStop; 396 args[4] = response.encodeURL 397 (request.getContextPath() + 398 "/html/reload?path=" + displayPath); 399 args[5] = appsReload; 400 args[6] = response.encodeURL 401 (request.getContextPath() + 402 "/html/undeploy?path=" + displayPath); 403 args[7] = appsUndeploy; 404 405 args[8] = highlightColor; 406 407 if (context.getPath().equals(this.context.getPath())) { 408 writer.print(MessageFormat.format( 409 MANAGER_APP_ROW_BUTTON_SECTION, args)); 410 } else if (context.getAvailable() && isDeployed) { 411 writer.print(MessageFormat.format( 412 STARTED_DEPLOYED_APPS_ROW_BUTTON_SECTION, args)); 413 } else if (context.getAvailable() && !isDeployed) { 414 writer.print(MessageFormat.format( 415 STARTED_NONDEPLOYED_APPS_ROW_BUTTON_SECTION, args)); 416 } else if (!context.getAvailable() && isDeployed) { 417 writer.print(MessageFormat.format( 418 STOPPED_DEPLOYED_APPS_ROW_BUTTON_SECTION, args)); 419 } else { 420 writer.print(MessageFormat.format( 421 STOPPED_NONDEPLOYED_APPS_ROW_BUTTON_SECTION, args)); 422 } 423 424 } 425 } 426 427 args = new Object [7]; 429 args[0] = sm.getString("htmlManagerServlet.deployTitle"); 430 args[1] = sm.getString("htmlManagerServlet.deployServer"); 431 args[2] = response.encodeURL(request.getContextPath() + "/html/deploy"); 432 args[3] = sm.getString("htmlManagerServlet.deployPath"); 433 args[4] = sm.getString("htmlManagerServlet.deployConfig"); 434 args[5] = sm.getString("htmlManagerServlet.deployWar"); 435 args[6] = sm.getString("htmlManagerServlet.deployButton"); 436 writer.print(MessageFormat.format(DEPLOY_SECTION, args)); 437 438 args = new Object [4]; 439 args[0] = sm.getString("htmlManagerServlet.deployUpload"); 440 args[1] = response.encodeURL(request.getContextPath() + "/html/upload"); 441 args[2] = sm.getString("htmlManagerServlet.deployUploadFile"); 442 args[3] = sm.getString("htmlManagerServlet.deployButton"); 443 writer.print(MessageFormat.format(UPLOAD_SECTION, args)); 444 445 args = new Object [7]; 447 args[0] = sm.getString("htmlManagerServlet.serverTitle"); 448 args[1] = sm.getString("htmlManagerServlet.serverVersion"); 449 args[2] = sm.getString("htmlManagerServlet.serverJVMVersion"); 450 args[3] = sm.getString("htmlManagerServlet.serverJVMVendor"); 451 args[4] = sm.getString("htmlManagerServlet.serverOSName"); 452 args[5] = sm.getString("htmlManagerServlet.serverOSVersion"); 453 args[6] = sm.getString("htmlManagerServlet.serverOSArch"); 454 writer.print(MessageFormat.format 455 (Constants.SERVER_HEADER_SECTION, args)); 456 457 args = new Object [6]; 459 args[0] = ServerInfo.getServerInfo(); 460 args[1] = System.getProperty("java.runtime.version"); 461 args[2] = System.getProperty("java.vm.vendor"); 462 args[3] = System.getProperty("os.name"); 463 args[4] = System.getProperty("os.version"); 464 args[5] = System.getProperty("os.arch"); 465 writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args)); 466 467 writer.print(Constants.HTML_TAIL_SECTION); 469 470 writer.flush(); 472 writer.close(); 473 } 474 475 483 protected String reload(String path) { 484 485 StringWriter stringWriter = new StringWriter (); 486 PrintWriter printWriter = new PrintWriter (stringWriter); 487 488 super.reload(printWriter, path); 489 490 return stringWriter.toString(); 491 } 492 493 501 protected String undeploy(String path) { 502 503 StringWriter stringWriter = new StringWriter (); 504 PrintWriter printWriter = new PrintWriter (stringWriter); 505 506 super.undeploy(printWriter, path); 507 508 return stringWriter.toString(); 509 } 510 511 519 public String sessions(String path) { 520 521 StringWriter stringWriter = new StringWriter (); 522 PrintWriter printWriter = new PrintWriter (stringWriter); 523 524 super.sessions(printWriter, path); 525 526 return stringWriter.toString(); 527 } 528 529 537 public String start(String path) { 538 539 StringWriter stringWriter = new StringWriter (); 540 PrintWriter printWriter = new PrintWriter (stringWriter); 541 542 super.start(printWriter, path); 543 544 return stringWriter.toString(); 545 } 546 547 555 protected String stop(String path) { 556 557 StringWriter stringWriter = new StringWriter (); 558 PrintWriter printWriter = new PrintWriter (stringWriter); 559 560 super.stop(printWriter, path); 561 562 return stringWriter.toString(); 563 } 564 565 567 571 private static final String APPS_HEADER_SECTION = 572 "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" + 573 "<tr>\n" + 574 " <td colspan=\"5\" class=\"title\">{0}</td>\n" + 575 "</tr>\n" + 576 "<tr>\n" + 577 " <td class=\"header-left\"><small>{1}</small></td>\n" + 578 " <td class=\"header-left\"><small>{2}</small></td>\n" + 579 " <td class=\"header-center\"><small>{3}</small></td>\n" + 580 " <td class=\"header-center\"><small>{4}</small></td>\n" + 581 " <td class=\"header-center\"><small>{5}</small></td>\n" + 582 "</tr>\n"; 583 584 private static final String APPS_ROW_DETAILS_SECTION = 585 "<tr>\n" + 586 " <td class=\"row-left\" bgcolor=\"{5}\"><small><a HREF=\"{0}\">{0}</a></small></td>\n" + 587 " <td class=\"row-left\" bgcolor=\"{5}\"><small>{1}</small></td>\n" + 588 " <td class=\"row-center\" bgcolor=\"{5}\"><small>{2}</small></td>\n" + 589 " <td class=\"row-center\" bgcolor=\"{5}\"><small><a HREF=\"{3}\">{4}</a></small></td>\n"; 590 591 private static final String MANAGER_APP_ROW_BUTTON_SECTION = 592 " <td class=\"row-left\" bgcolor=\"{8}\">\n" + 593 " <small>\n" + 594 " {1} \n" + 595 " {3} \n" + 596 " {5} \n" + 597 " {7} \n" + 598 " </small>\n" + 599 " </td>\n" + 600 "</tr>\n"; 601 602 private static final String STARTED_DEPLOYED_APPS_ROW_BUTTON_SECTION = 603 " <td class=\"row-left\" bgcolor=\"{8}\">\n" + 604 " <small>\n" + 605 " {1} \n" + 606 " <a HREF=\"{2}\" onclick=\"return(confirm('''Are you sure?'''))\">{3}</a> \n" + 607 " <a HREF=\"{4}\" onclick=\"return(confirm('''Are you sure?'''))\">{5}</a> \n" + 608 " <a HREF=\"{6}\" onclick=\"return(confirm('''Are you sure?'''))\">{7}</a> \n" + 609 " </small>\n" + 610 " </td>\n" + 611 "</tr>\n"; 612 613 private static final String STOPPED_DEPLOYED_APPS_ROW_BUTTON_SECTION = 614 " <td class=\"row-left\" bgcolor=\"{8}\">\n" + 615 " <small>\n" + 616 " <a HREF=\"{0}\" onclick=\"return(confirm('''Are you sure?'''))\">{1}</a> \n" + 617 " {3} \n" + 618 " {5} \n" + 619 " <a HREF=\"{6}\" onclick=\"return(confirm('''Are you sure? This will delete the application.'''))\">{7}</a> \n" + 620 " </small>\n" + 621 " </td>\n" + 622 "</tr>\n"; 623 624 private static final String STARTED_NONDEPLOYED_APPS_ROW_BUTTON_SECTION = 625 " <td class=\"row-left\" bgcolor=\"{8}\">\n" + 626 " <small>\n" + 627 " {1} \n" + 628 " <a HREF=\"{2}\" onclick=\"return(confirm('''Are you sure?'''))\">{3}</a> \n" + 629 " <a HREF=\"{4}\" onclick=\"return(confirm('''Are you sure?'''))\">{5}</a> \n" + 630 " {7} \n" + 631 " </small>\n" + 632 " </td>\n" + 633 "</tr>\n"; 634 635 private static final String STOPPED_NONDEPLOYED_APPS_ROW_BUTTON_SECTION = 636 " <td class=\"row-left\" bgcolor=\"{8}\">\n" + 637 " <small>\n" + 638 " <a HREF=\"{0}\" onclick=\"return(confirm('''Are you sure?'''))\">{1}</a> \n" + 639 " {3} \n" + 640 " {5} \n" + 641 " {7} \n" + 642 " </small>\n" + 643 " </td>\n" + 644 "</tr>\n"; 645 646 private static final String DEPLOY_SECTION = 647 "</table>\n" + 648 "<br>\n" + 649 "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" + 650 "<tr>\n" + 651 " <td colspan=\"2\" class=\"title\">{0}</td>\n" + 652 "</tr>\n" + 653 "<tr>\n" + 654 " <td colspan=\"2\" class=\"header-left\"><small>{1}</small></td>\n" + 655 "</tr>\n" + 656 "<tr>\n" + 657 " <td colspan=\"2\">\n" + 658 "<form method=\"get\" action=\"{2}\">\n" + 659 "<table cellspacing=\"0\" cellpadding=\"3\">\n" + 660 "<tr>\n" + 661 " <td class=\"row-right\">\n" + 662 " <small>{3}</small>\n" + 663 " </td>\n" + 664 " <td class=\"row-left\">\n" + 665 " <input type=\"text\" name=\"deployPath\" size=\"20\">\n" + 666 " </td>\n" + 667 "</tr>\n" + 668 "<tr>\n" + 669 " <td class=\"row-right\">\n" + 670 " <small>{4}</small>\n" + 671 " </td>\n" + 672 " <td class=\"row-left\">\n" + 673 " <input type=\"text\" name=\"deployConfig\" size=\"20\">\n" + 674 " </td>\n" + 675 "</tr>\n" + 676 "<tr>\n" + 677 " <td class=\"row-right\">\n" + 678 " <small>{5}</small>\n" + 679 " </td>\n" + 680 " <td class=\"row-left\">\n" + 681 " <input type=\"text\" name=\"deployWar\" size=\"40\">\n" + 682 " </td>\n" + 683 "</tr>\n" + 684 "<tr>\n" + 685 " <td class=\"row-right\">\n" + 686 " \n" + 687 " </td>\n" + 688 " <td class=\"row-left\">\n" + 689 " <input type=\"submit\" value=\"{6}\">\n" + 690 " </td>\n" + 691 "</tr>\n" + 692 "</table>\n" + 693 "</form>\n" + 694 "</td>\n" + 695 "</tr>\n"; 696 697 private static final String UPLOAD_SECTION = 698 "<tr>\n" + 699 " <td colspan=\"2\" class=\"header-left\"><small>{0}</small></td>\n" + 700 "</tr>\n" + 701 "<tr>\n" + 702 " <td colspan=\"2\">\n" + 703 "<form action=\"{1}\" method=\"post\" " + 704 "enctype=\"multipart/form-data\">\n" + 705 "<table cellspacing=\"0\" cellpadding=\"3\">\n" + 706 "<tr>\n" + 707 " <td class=\"row-right\">\n" + 708 " <small>{2}</small>\n" + 709 " </td>\n" + 710 " <td class=\"row-left\">\n" + 711 " <input type=\"file\" name=\"deployWar\" size=\"40\">\n" + 712 " </td>\n" + 713 "</tr>\n" + 714 "<tr>\n" + 715 " <td class=\"row-right\">\n" + 716 " \n" + 717 " </td>\n" + 718 " <td class=\"row-left\">\n" + 719 " <input type=\"submit\" value=\"{3}\">\n" + 720 " </td>\n" + 721 "</tr>\n" + 722 "</table>\n" + 723 "</form>\n" + 724 "</table>\n" + 725 "<br>\n" + 726 "\n"; 727 728 } 729 | Popular Tags |