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 */
018package org.apache.hadoop.conf;
019
020import java.io.IOException;
021import java.io.Writer;
022
023import javax.servlet.ServletContext;
024import javax.servlet.ServletException;
025import javax.servlet.http.HttpServlet;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.apache.hadoop.classification.InterfaceAudience;
030import org.apache.hadoop.classification.InterfaceStability;
031import org.apache.hadoop.http.HttpServer2;
032
033/**
034 * A servlet to print out the running configuration data.
035 */
036@InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
037@InterfaceStability.Unstable
038public class ConfServlet extends HttpServlet {
039  private static final long serialVersionUID = 1L;
040
041  private static final String FORMAT_JSON = "json";
042  private static final String FORMAT_XML = "xml";
043  private static final String FORMAT_PARAM = "format";
044
045  /**
046   * Return the Configuration of the daemon hosting this servlet.
047   * This is populated when the HttpServer starts.
048   */
049  private Configuration getConfFromContext() {
050    Configuration conf = (Configuration)getServletContext().getAttribute(
051        HttpServer2.CONF_CONTEXT_ATTRIBUTE);
052    assert conf != null;
053    return conf;
054  }
055
056  @Override
057  public void doGet(HttpServletRequest request, HttpServletResponse response)
058      throws ServletException, IOException {
059
060    // If user is a static user and auth Type is null, that means
061    // there is a non-security environment and no need authorization,
062    // otherwise, do the authorization.
063    final ServletContext servletContext = getServletContext();
064    if (!HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) &&
065        !HttpServer2.isInstrumentationAccessAllowed(servletContext,
066                                                   request, response)) {
067      return;
068    }
069
070    String format = request.getParameter(FORMAT_PARAM);
071    if (null == format) {
072      format = FORMAT_XML;
073    }
074
075    if (FORMAT_XML.equals(format)) {
076      response.setContentType("text/xml; charset=utf-8");
077    } else if (FORMAT_JSON.equals(format)) {
078      response.setContentType("application/json; charset=utf-8");
079    }
080
081    Writer out = response.getWriter();
082    try {
083      writeResponse(getConfFromContext(), out, format);
084    } catch (BadFormatException bfe) {
085      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
086    }
087    out.close();
088  }
089
090  /**
091   * Guts of the servlet - extracted for easy testing.
092   */
093  static void writeResponse(Configuration conf, Writer out, String format)
094    throws IOException, BadFormatException {
095    if (FORMAT_JSON.equals(format)) {
096      Configuration.dumpConfiguration(conf, out);
097    } else if (FORMAT_XML.equals(format)) {
098      conf.writeXml(out);
099    } else {
100      throw new BadFormatException("Bad format: " + format);
101    }
102  }
103
104  public static class BadFormatException extends Exception {
105    private static final long serialVersionUID = 1L;
106
107    public BadFormatException(String msg) {
108      super(msg);
109    }
110  }
111
112}