View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.http.conf;
19  
20  import java.io.IOException;
21  import java.io.Writer;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServlet;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.http.HttpServer;
32  
33  /**
34   * A servlet to print out the running configuration data.
35   */
36  @InterfaceAudience.LimitedPrivate({"HBase"})
37  @InterfaceStability.Unstable
38  public class ConfServlet extends HttpServlet {
39    private static final long serialVersionUID = 1L;
40  
41    private static final String FORMAT_JSON = "json";
42    private static final String FORMAT_XML = "xml";
43    private static final String FORMAT_PARAM = "format";
44  
45    /**
46     * Return the Configuration of the daemon hosting this servlet.
47     * This is populated when the HttpServer starts.
48     */
49    private Configuration getConfFromContext() {
50      Configuration conf = (Configuration)getServletContext().getAttribute(
51          HttpServer.CONF_CONTEXT_ATTRIBUTE);
52      assert conf != null;
53      return conf;
54    }
55  
56    @Override
57    public void doGet(HttpServletRequest request, HttpServletResponse response)
58        throws ServletException, IOException {
59  
60      if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(),
61                                                     request, response)) {
62        return;
63      }
64  
65      String format = request.getParameter(FORMAT_PARAM);
66      if (null == format) {
67        format = FORMAT_XML;
68      }
69  
70      if (FORMAT_XML.equals(format)) {
71        response.setContentType("text/xml; charset=utf-8");
72      } else if (FORMAT_JSON.equals(format)) {
73        response.setContentType("application/json; charset=utf-8");
74      }
75  
76      Writer out = response.getWriter();
77      try {
78        writeResponse(getConfFromContext(), out, format);
79      } catch (BadFormatException bfe) {
80        response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
81      }
82      out.close();
83    }
84  
85    /**
86     * Guts of the servlet - extracted for easy testing.
87     */
88    static void writeResponse(Configuration conf, Writer out, String format)
89      throws IOException, BadFormatException {
90      if (FORMAT_JSON.equals(format)) {
91        Configuration.dumpConfiguration(conf, out);
92      } else if (FORMAT_XML.equals(format)) {
93        conf.writeXml(out);
94      } else {
95        throw new BadFormatException("Bad format: " + format);
96      }
97    }
98  
99    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 }