001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.hadoop.conf;
019
020 import java.io.IOException;
021 import java.io.Writer;
022
023 import javax.servlet.ServletException;
024 import javax.servlet.http.HttpServlet;
025 import javax.servlet.http.HttpServletRequest;
026 import javax.servlet.http.HttpServletResponse;
027
028 import org.apache.hadoop.classification.InterfaceAudience;
029 import org.apache.hadoop.classification.InterfaceStability;
030 import org.apache.hadoop.http.HttpServer2;
031
032 /**
033 * A servlet to print out the running configuration data.
034 */
035 @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
036 @InterfaceStability.Unstable
037 public class ConfServlet extends HttpServlet {
038 private static final long serialVersionUID = 1L;
039
040 private static final String FORMAT_JSON = "json";
041 private static final String FORMAT_XML = "xml";
042 private static final String FORMAT_PARAM = "format";
043
044 /**
045 * Return the Configuration of the daemon hosting this servlet.
046 * This is populated when the HttpServer starts.
047 */
048 private Configuration getConfFromContext() {
049 Configuration conf = (Configuration)getServletContext().getAttribute(
050 HttpServer2.CONF_CONTEXT_ATTRIBUTE);
051 assert conf != null;
052 return conf;
053 }
054
055 @Override
056 public void doGet(HttpServletRequest request, HttpServletResponse response)
057 throws ServletException, IOException {
058
059 if (!HttpServer2.isInstrumentationAccessAllowed(getServletContext(),
060 request, response)) {
061 return;
062 }
063
064 String format = request.getParameter(FORMAT_PARAM);
065 if (null == format) {
066 format = FORMAT_XML;
067 }
068
069 if (FORMAT_XML.equals(format)) {
070 response.setContentType("text/xml; charset=utf-8");
071 } else if (FORMAT_JSON.equals(format)) {
072 response.setContentType("application/json; charset=utf-8");
073 }
074
075 Writer out = response.getWriter();
076 try {
077 writeResponse(getConfFromContext(), out, format);
078 } catch (BadFormatException bfe) {
079 response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
080 }
081 out.close();
082 }
083
084 /**
085 * Guts of the servlet - extracted for easy testing.
086 */
087 static void writeResponse(Configuration conf, Writer out, String format)
088 throws IOException, BadFormatException {
089 if (FORMAT_JSON.equals(format)) {
090 Configuration.dumpConfiguration(conf, out);
091 } else if (FORMAT_XML.equals(format)) {
092 conf.writeXml(out);
093 } else {
094 throw new BadFormatException("Bad format: " + format);
095 }
096 }
097
098 public static class BadFormatException extends Exception {
099 private static final long serialVersionUID = 1L;
100
101 public BadFormatException(String msg) {
102 super(msg);
103 }
104 }
105
106 }