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.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.http.HttpServer; 030 031import org.apache.yetus.audience.InterfaceAudience; 032import org.apache.yetus.audience.InterfaceStability; 033 034/** 035 * A servlet to print out the running configuration data. 036 */ 037@InterfaceAudience.LimitedPrivate({"HBase"}) 038@InterfaceStability.Unstable 039public class ConfServlet extends HttpServlet { 040 private static final long serialVersionUID = 1L; 041 042 private static final String FORMAT_JSON = "json"; 043 private static final String FORMAT_XML = "xml"; 044 private static final String FORMAT_PARAM = "format"; 045 046 /** 047 * Return the Configuration of the daemon hosting this servlet. 048 * This is populated when the HttpServer starts. 049 */ 050 private Configuration getConfFromContext() { 051 Configuration conf = (Configuration)getServletContext().getAttribute( 052 HttpServer.CONF_CONTEXT_ATTRIBUTE); 053 assert conf != null; 054 return conf; 055 } 056 057 @Override 058 public void doGet(HttpServletRequest request, HttpServletResponse response) 059 throws ServletException, IOException { 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}