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;
022
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServlet;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.yetus.audience.InterfaceStability;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.http.HttpServer;
032
033/**
034 * A servlet to print out the running configuration data.
035 */
036@InterfaceAudience.LimitedPrivate({"HBase"})
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        HttpServer.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 (!HttpServer.isInstrumentationAccessAllowed(getServletContext(),
061                                                   request, response)) {
062      return;
063    }
064
065    String format = request.getParameter(FORMAT_PARAM);
066    if (null == format) {
067      format = FORMAT_XML;
068    }
069
070    if (FORMAT_XML.equals(format)) {
071      response.setContentType("text/xml; charset=utf-8");
072    } else if (FORMAT_JSON.equals(format)) {
073      response.setContentType("application/json; charset=utf-8");
074    }
075
076    Writer out = response.getWriter();
077    try {
078      writeResponse(getConfFromContext(), out, format);
079    } catch (BadFormatException bfe) {
080      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
081    }
082    out.close();
083  }
084
085  /**
086   * Guts of the servlet - extracted for easy testing.
087   */
088  static void writeResponse(Configuration conf, Writer out, String format)
089    throws IOException, BadFormatException {
090    if (FORMAT_JSON.equals(format)) {
091      Configuration.dumpConfiguration(conf, out);
092    } else if (FORMAT_XML.equals(format)) {
093      conf.writeXml(out);
094    } else {
095      throw new BadFormatException("Bad format: " + format);
096    }
097  }
098
099  public static class BadFormatException extends Exception {
100    private static final long serialVersionUID = 1L;
101
102    public BadFormatException(String msg) {
103      super(msg);
104    }
105  }
106
107}