|
Tomcat example source code file (HTMLHostManagerServlet.java)
The Tomcat HTMLHostManagerServlet.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.manager.host;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Container;
import org.apache.catalina.Host;
import org.apache.catalina.util.RequestUtil;
import org.apache.catalina.util.ServerInfo;
/**
* Servlet that enables remote management of the virtual hosts deployed
* on the server. Normally, this functionality will be protected by a security
* constraint in the web application deployment descriptor. However,
* this requirement can be relaxed during testing.
* <p>
* The difference between the <code>HostManagerServlet and this
* Servlet is that this Servlet prints out a HTML interface which
* makes it easier to administrate.
* <p>
* However if you use a software that parses the output of
* <code>HostManagerServlet you won't be able to upgrade
* to this Servlet since the output are not in the
* same format as from <code>HostManagerServlet
*
* @author Bip Thelin
* @author Malcolm Edgar
* @author Glenn L. Nielsen
* @author Peter Rossbach
* @version $Revision: 557454 $, $Date: 2007-07-19 04:19:10 +0200 (jeu., 19 juil. 2007) $
* @see ManagerServlet
*/
public final class HTMLHostManagerServlet extends HostManagerServlet {
// --------------------------------------------------------- Public Methods
/**
* Process a GET request for the specified resource.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet-specified error occurs
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Identify the request parameters that we need
String command = request.getPathInfo();
String name = request.getParameter("name");
// Prepare our output writer to generate the response message
response.setContentType("text/html; charset=" + Constants.CHARSET);
String message = "";
// Process the requested command
if (command == null) {
} else if (command.equals("/add")) {
message = add(request, name);
} else if (command.equals("/remove")) {
message = remove(name);
} else if (command.equals("/list")) {
} else if (command.equals("/start")) {
message = start(name);
} else if (command.equals("/stop")) {
message = stop(name);
} else {
message =
sm.getString("hostManagerServlet.unknownCommand", command);
}
list(request, response, message);
}
/**
* Add a host using the specified parameters.
*
* @param name host name
*/
protected String add(HttpServletRequest request,String name) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.add(request,printWriter,name,true);
return stringWriter.toString();
}
/**
* Remove the specified host.
*
* @param writer Writer to render results to
* @param name host name
*/
protected String remove(String name) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.remove(printWriter, name);
return stringWriter.toString();
}
/**
* Start the host with the specified name.
*
* @param name Host name
*/
protected String start(String name) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.start(printWriter, name);
return stringWriter.toString();
}
/**
* Stop the host with the specified name.
*
* @param name Host name
*/
protected String stop(String name) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.stop(printWriter, name);
return stringWriter.toString();
}
/**
* Render a HTML list of the currently active Contexts in our virtual host,
* and memory and server status information.
*
* @param request The request
* @param response The response
* @param message a message to display
*/
public void list(HttpServletRequest request,
HttpServletResponse response,
String message) throws IOException {
PrintWriter writer = response.getWriter();
// HTML Header Section
writer.print(Constants.HTML_HEADER_SECTION);
// Body Header Section
Object[] args = new Object[2];
args[0] = request.getContextPath();
args[1] = sm.getString("htmlHostManagerServlet.title");
writer.print(MessageFormat.format
(Constants.BODY_HEADER_SECTION, args));
// Message Section
args = new Object[3];
args[0] = sm.getString("htmlHostManagerServlet.messageLabel");
if (message == null || message.length() == 0) {
args[1] = "OK";
} else {
args[1] = RequestUtil.filter(message);
}
writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args));
// Manager Section
args = new Object[9];
args[0] = sm.getString("htmlHostManagerServlet.manager");
args[1] = response.encodeURL(request.getContextPath() + "/html/list");
args[2] = sm.getString("htmlHostManagerServlet.list");
args[3] = response.encodeURL
(request.getContextPath() + "/" +
sm.getString("htmlHostManagerServlet.helpHtmlManagerFile"));
args[4] = sm.getString("htmlHostManagerServlet.helpHtmlManager");
args[5] = response.encodeURL
(request.getContextPath() + "/" +
sm.getString("htmlHostManagerServlet.helpManagerFile"));
args[6] = sm.getString("htmlHostManagerServlet.helpManager");
args[7] = response.encodeURL("/manager/status");
args[8] = sm.getString("statusServlet.title");
writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args));
// Hosts Header Section
args = new Object[3];
args[0] = sm.getString("htmlHostManagerServlet.hostName");
args[1] = sm.getString("htmlHostManagerServlet.hostAliases");
args[2] = sm.getString("htmlHostManagerServlet.hostTasks");
writer.print(MessageFormat.format(HOSTS_HEADER_SECTION, args));
// Hosts Row Section
// Create sorted map of host names.
Container[] children = engine.findChildren();
String hostNames[] = new String[children.length];
for (int i = 0; i < children.length; i++)
hostNames[i] = children[i].getName();
TreeMap<String,String> sortedHostNamesMap =
new TreeMap<String,String>();
for (int i = 0; i < hostNames.length; i++) {
String displayPath = hostNames[i];
sortedHostNamesMap.put(displayPath, hostNames[i]);
}
String hostsStart = sm.getString("htmlHostManagerServlet.hostsStart");
String hostsStop = sm.getString("htmlHostManagerServlet.hostsStop");
String hostsRemove = sm.getString("htmlHostManagerServlet.hostsRemove");
Iterator<Map.Entry | \n" +
"</tr>\n" +
"<tr>\n" +
" <td class=\"header-left\">{0}\n" +
" <td class=\"header-center\">{1}\n" +
" <td class=\"header-center\">{2}\n" +
"</tr>\n";
private static final String HOSTS_ROW_DETAILS_SECTION =
"<tr>\n" +
" <td class=\"row-left\">{0}" +
"</small>\n" +
" <td class=\"row-center\">{1}\n";
private static final String MANAGER_HOST_ROW_BUTTON_SECTION =
" <td class=\"row-left\">\n" +
" <small>\n" +
" {1} \n" +
" {3} \n" +
" {5} \n" +
" </small>\n" +
" </td>\n" +
"</tr>\n";
private static final String HOSTS_ROW_BUTTON_SECTION =
" <td class=\"row-left\" NOWRAP>\n" +
" <small>\n" +
" <a href=\"{0}\" onclick=\"return(confirm(''{1} {6}\\n\\nAre you sure?''))\">{1} \n" +
" <a href=\"{2}\" onclick=\"return(confirm(''{3} {6}\\n\\nAre you sure?''))\">{3} \n" +
" <a href=\"{4}\" onclick=\"return(confirm(''{5} {6}\\n\\nAre you sure?''))\">{5} \n" +
" </small>\n" +
" </td>\n" +
"</tr>\n";
private static final String ADD_SECTION_START =
"</table>\n" +
"<br>\n" +
"<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n" +
"<tr>\n" +
" <td colspan=\"2\" class=\"title\">{0}\n" +
"</tr>\n" +
"<tr>\n" +
" <td colspan=\"2\" class=\"header-left\">{1}\n" +
"</tr>\n" +
"<tr>\n" +
" <td colspan=\"2\">\n" +
"<form method=\"get\" action=\"{2}\">\n" +
"<table cellspacing=\"0\" cellpadding=\"3\">\n" +
"<tr>\n" +
" <td class=\"row-right\">\n" +
" <small>{3}\n" +
" </td>\n" +
" <td class=\"row-left\">\n" +
" <input type=\"text\" name=\"name\" size=\"20\">\n" +
" </td>\n" +
"</tr>\n" +
"<tr>\n" +
" <td class=\"row-right\">\n" +
" <small>{4}\n" +
" </td>\n" +
" <td class=\"row-left\">\n" +
" <input type=\"text\" name=\"aliases\" size=\"64\">\n" +
" </td>\n" +
"</tr>\n" +
"<tr>\n" +
" <td class=\"row-right\">\n" +
" <small>{5}\n" +
" </td>\n" +
" <td class=\"row-left\">\n" +
" <input type=\"text\" name=\"appBase\" size=\"64\">\n" +
" </td>\n" +
"</tr>\n" ;
private static final String ADD_SECTION_BOOLEAN =
"<tr>\n" +
" <td class=\"row-right\">\n" +
" <small>{0}\n" +
" </td>\n" +
" <td class=\"row-left\">\n" +
" <input type=\"checkbox\" name=\"{1}\" {2}>\n" +
" </td>\n" +
"</tr>\n" ;
private static final String ADD_SECTION_END =
"<tr>\n" +
" <td class=\"row-right\">\n" +
" \n" +
" </td>\n" +
" <td class=\"row-left\">\n" +
" <input type=\"submit\" value=\"{0}\">\n" +
" </td>\n" +
"</tr>\n" +
"</table>\n" +
"</form>\n" +
"</td>\n" +
"</tr>\n" +
"</table>\n" +
"<br>\n" +
"\n";
}
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.