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