1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
47
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
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 }