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