/* * 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.net.URLEncoder; import java.text.MessageFormat; 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.ServerInfo; import org.apache.tomcat.util.res.StringManager; import org.apache.tomcat.util.security.Escape; /** * 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. *
* The difference between the HostManagerServlet and this
* Servlet is that this Servlet prints out a HTML interface which
* makes it easier to administrate.
*
* However if you use a software that parses the output of
* HostManagerServlet you won't be able to upgrade
* to this Servlet since the output are not in the
* same format as from HostManagerServlet
*
* @author Bip Thelin
* @author Malcolm Edgar
* @author Glenn L. Nielsen
* @author Peter Rossbach
* @see org.apache.catalina.manager.ManagerServlet
*/
public final class HTMLHostManagerServlet extends HostManagerServlet {
private static final long serialVersionUID = 1L;
// --------------------------------------------------------- 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
*/
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
StringManager smClient = StringManager.getManager(
Constants.Package, request.getLocales());
// Identify the request parameters that we need
String command = request.getPathInfo();
// 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) {
// No command == list
} else if (command.equals("/list")) {
// Nothing to do - always generate list
} else if (command.equals("/add") || command.equals("/remove") ||
command.equals("/start") || command.equals("/stop") ||
command.equals("/persist")) {
message = smClient.getString(
"hostManagerServlet.postCommand", command);
} else {
message = smClient.getString(
"hostManagerServlet.unknownCommand", command);
}
list(request, response, message, smClient);
}
/**
* Process a POST 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
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringManager smClient = StringManager.getManager(
Constants.Package, request.getLocales());
// 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) {
// No command == list
} else if (command.equals("/add")) {
message = add(request, name, smClient);
} else if (command.equals("/remove")) {
message = remove(name, smClient);
} else if (command.equals("/start")) {
message = start(name, smClient);
} else if (command.equals("/stop")) {
message = stop(name, smClient);
} else if (command.equals("/persist")) {
message = persist(smClient);
} else {
//Try GET
doGet(request, response);
}
list(request, response, message, smClient);
}
/**
* Add a host using the specified parameters.
*
* @param request The Servlet request
* @param name Host name
* @param smClient StringManager for the client's locale
* @return output
*/
protected String add(HttpServletRequest request,String name,
StringManager smClient) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.add(request,printWriter,name,true, smClient);
return stringWriter.toString();
}
/**
* Remove the specified host.
*
* @param name Host name
* @param smClient StringManager for the client's locale
* @return output
*/
protected String remove(String name, StringManager smClient) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.remove(printWriter, name, smClient);
return stringWriter.toString();
}
/**
* Start the host with the specified name.
*
* @param name Host name
* @param smClient StringManager for the client's locale
* @return output
*/
protected String start(String name, StringManager smClient) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.start(printWriter, name, smClient);
return stringWriter.toString();
}
/**
* Stop the host with the specified name.
*
* @param name Host name
* @param smClient StringManager for the client's locale
* @return output
*/
protected String stop(String name, StringManager smClient) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.stop(printWriter, name, smClient);
return stringWriter.toString();
}
/**
* Persist the current configuration to server.xml.
*
* @param smClient i18n resources localized for the client
* @return output
*/
protected String persist(StringManager smClient) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
super.persist(printWriter, smClient);
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
* @param smClient StringManager for the client's locale
* @throws IOException An IO error occurred
*/
public void list(HttpServletRequest request,
HttpServletResponse response,
String message,
StringManager smClient) throws IOException {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.list", engine.getName()));
}
PrintWriter writer = response.getWriter();
// HTML Header Section
writer.print(org.apache.catalina.manager.Constants.HTML_HEADER_SECTION);
// Body Header Section
Object[] args = new Object[2];
args[0] = request.getContextPath();
args[1] = smClient.getString("htmlHostManagerServlet.title");
writer.print(MessageFormat.format(
org.apache.catalina.manager.Constants.BODY_HEADER_SECTION, args));
// Message Section
args = new Object[3];
args[0] = smClient.getString("htmlHostManagerServlet.messageLabel");
if (message == null || message.length() == 0) {
args[1] = "OK";
} else {
args[1] = Escape.htmlElementContent(message);
}
writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args));
// Manager Section
args = new Object[9];
args[0] = smClient.getString("htmlHostManagerServlet.manager");
args[1] = response.encodeURL(request.getContextPath() + "/html/list");
args[2] = smClient.getString("htmlHostManagerServlet.list");
args[3] = // External link
(request.getContextPath() + "/" +
smClient.getString("htmlHostManagerServlet.helpHtmlManagerFile"));
args[4] = smClient.getString("htmlHostManagerServlet.helpHtmlManager");
args[5] = // External link
(request.getContextPath() + "/" +
smClient.getString("htmlHostManagerServlet.helpManagerFile"));
args[6] = smClient.getString("htmlHostManagerServlet.helpManager");
args[7] = response.encodeURL("/manager/status");
args[8] = smClient.getString("statusServlet.title");
writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args));
// Hosts Header Section
args = new Object[3];
args[0] = smClient.getString("htmlHostManagerServlet.hostName");
args[1] = smClient.getString("htmlHostManagerServlet.hostAliases");
args[2] = smClient.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\n" +
"
\n" +
"\n" +
" \n" +
"{0} \n" +
"\n" +
" \n";
private static final String HOSTS_ROW_DETAILS_SECTION =
"{0} \n" +
" {1} \n" +
" {2} \n" +
"\n" +
" \n";
private static final String HOSTS_ROW_BUTTON_SECTION =
" {0}" +
" \n" +
" {1} \n";
private static final String MANAGER_HOST_ROW_BUTTON_SECTION =
" \n" +
" {4}\n" +
" \n" +
"\n" +
" \n" +
" \n" +
" \n" +
"\n";
private static final String ADD_SECTION_START =
"
\n" +
"\n" +
"
\n" +
"\n" +
" \n" +
"{0} \n" +
"\n" +
" \n" +
"{1} \n" +
"\n" +
" \n" +
"\n" +
"\n" +
" \n" +
"
\n" +
"\n";
private static final String PERSIST_SECTION =
"\n" +
"
\n" +
"\n" +
" \n" +
"{0} \n" +
"\n" +
" \n" +
"\n" +
" {3}\n" +
" \n" +
"
\n" +
"\n";
}