hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mi...@apache.org
Subject [27/37] hbase-site git commit: Published site at 7063562bf1c0bfcc5efa791b8536a298edab8fcb.
Date Tue, 16 Feb 2016 17:29:30 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/6a178a98/devapidocs/src-html/org/apache/hadoop/hbase/http/jmx/JMXJsonServlet.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/http/jmx/JMXJsonServlet.html b/devapidocs/src-html/org/apache/hadoop/hbase/http/jmx/JMXJsonServlet.html
index 919041c..8ee8072 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/http/jmx/JMXJsonServlet.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/http/jmx/JMXJsonServlet.html
@@ -43,192 +43,198 @@
 <span class="sourceLineNo">035</span>import org.apache.commons.logging.LogFactory;<a
name="line.35"></a>
 <span class="sourceLineNo">036</span>import org.apache.hadoop.hbase.http.HttpServer;<a
name="line.36"></a>
 <span class="sourceLineNo">037</span>import org.apache.hadoop.hbase.util.JSONBean;<a
name="line.37"></a>
-<span class="sourceLineNo">038</span><a name="line.38"></a>
-<span class="sourceLineNo">039</span>/*<a name="line.39"></a>
-<span class="sourceLineNo">040</span> * This servlet is based off of the JMXProxyServlet
from Tomcat 7.0.14. It has<a name="line.40"></a>
-<span class="sourceLineNo">041</span> * been rewritten to be read only and to
output in a JSON format so it is not<a name="line.41"></a>
-<span class="sourceLineNo">042</span> * really that close to the original.<a
name="line.42"></a>
-<span class="sourceLineNo">043</span> */<a name="line.43"></a>
-<span class="sourceLineNo">044</span>/**<a name="line.44"></a>
-<span class="sourceLineNo">045</span> * Provides Read only web access to JMX.<a
name="line.45"></a>
-<span class="sourceLineNo">046</span> * &lt;p&gt;<a name="line.46"></a>
-<span class="sourceLineNo">047</span> * This servlet generally will be placed
under the /jmx URL for each<a name="line.47"></a>
-<span class="sourceLineNo">048</span> * HttpServer.  It provides read only<a
name="line.48"></a>
-<span class="sourceLineNo">049</span> * access to JMX metrics.  The optional
&lt;code&gt;qry&lt;/code&gt; parameter<a name="line.49"></a>
-<span class="sourceLineNo">050</span> * may be used to query only a subset of
the JMX Beans.  This query<a name="line.50"></a>
-<span class="sourceLineNo">051</span> * functionality is provided through the<a
name="line.51"></a>
-<span class="sourceLineNo">052</span> * {@link MBeanServer#queryNames(ObjectName,
javax.management.QueryExp)}<a name="line.52"></a>
-<span class="sourceLineNo">053</span> * method.<a name="line.53"></a>
-<span class="sourceLineNo">054</span> * &lt;/p&gt;<a name="line.54"></a>
-<span class="sourceLineNo">055</span> * &lt;p&gt;<a name="line.55"></a>
-<span class="sourceLineNo">056</span> * For example &lt;code&gt;http://.../jmx?qry=Hadoop:*&lt;/code&gt;
will return<a name="line.56"></a>
-<span class="sourceLineNo">057</span> * all hadoop metrics exposed through JMX.<a
name="line.57"></a>
-<span class="sourceLineNo">058</span> * &lt;/p&gt;<a name="line.58"></a>
-<span class="sourceLineNo">059</span> * &lt;p&gt;<a name="line.59"></a>
-<span class="sourceLineNo">060</span> * The optional &lt;code&gt;get&lt;/code&gt;
parameter is used to query an specific <a name="line.60"></a>
-<span class="sourceLineNo">061</span> * attribute of a JMX bean.  The format
of the URL is<a name="line.61"></a>
-<span class="sourceLineNo">062</span> * &lt;code&gt;http://.../jmx?get=MXBeanName::AttributeName&lt;/code&gt;<a
name="line.62"></a>
-<span class="sourceLineNo">063</span> * &lt;/p&gt;<a name="line.63"></a>
-<span class="sourceLineNo">064</span> * &lt;p&gt;<a name="line.64"></a>
-<span class="sourceLineNo">065</span> * For example <a name="line.65"></a>
-<span class="sourceLineNo">066</span> * &lt;code&gt;<a name="line.66"></a>
-<span class="sourceLineNo">067</span> * http://../jmx?get=Hadoop:service=NameNode,name=NameNodeInfo::ClusterId<a
name="line.67"></a>
-<span class="sourceLineNo">068</span> * &lt;/code&gt; will return the
cluster id of the namenode mxbean.<a name="line.68"></a>
-<span class="sourceLineNo">069</span> * &lt;/p&gt;<a name="line.69"></a>
-<span class="sourceLineNo">070</span> * &lt;p&gt;<a name="line.70"></a>
-<span class="sourceLineNo">071</span> * If the &lt;code&gt;qry&lt;/code&gt;
or the &lt;code&gt;get&lt;/code&gt; parameter is not formatted <a name="line.71"></a>
-<span class="sourceLineNo">072</span> * correctly then a 400 BAD REQUEST http
response code will be returned. <a name="line.72"></a>
-<span class="sourceLineNo">073</span> * &lt;/p&gt;<a name="line.73"></a>
-<span class="sourceLineNo">074</span> * &lt;p&gt;<a name="line.74"></a>
-<span class="sourceLineNo">075</span> * If a resouce such as a mbean or attribute
can not be found, <a name="line.75"></a>
-<span class="sourceLineNo">076</span> * a 404 SC_NOT_FOUND http response code
will be returned. <a name="line.76"></a>
-<span class="sourceLineNo">077</span> * &lt;/p&gt;<a name="line.77"></a>
-<span class="sourceLineNo">078</span> * &lt;p&gt;<a name="line.78"></a>
-<span class="sourceLineNo">079</span> * The return format is JSON and in the
form<a name="line.79"></a>
-<span class="sourceLineNo">080</span> * &lt;/p&gt;<a name="line.80"></a>
-<span class="sourceLineNo">081</span> *  &lt;pre&gt;&lt;code&gt;<a
name="line.81"></a>
-<span class="sourceLineNo">082</span> *  {<a name="line.82"></a>
-<span class="sourceLineNo">083</span> *    "beans" : [<a name="line.83"></a>
-<span class="sourceLineNo">084</span> *      {<a name="line.84"></a>
-<span class="sourceLineNo">085</span> *        "name":"bean-name"<a name="line.85"></a>
-<span class="sourceLineNo">086</span> *        ...<a name="line.86"></a>
-<span class="sourceLineNo">087</span> *      }<a name="line.87"></a>
-<span class="sourceLineNo">088</span> *    ]<a name="line.88"></a>
-<span class="sourceLineNo">089</span> *  }<a name="line.89"></a>
-<span class="sourceLineNo">090</span> *  &lt;/code&gt;&lt;/pre&gt;<a
name="line.90"></a>
-<span class="sourceLineNo">091</span> *  &lt;p&gt;<a name="line.91"></a>
-<span class="sourceLineNo">092</span> *  The servlet attempts to convert the
the JMXBeans into JSON. Each<a name="line.92"></a>
-<span class="sourceLineNo">093</span> *  bean's attributes will be converted
to a JSON object member.<a name="line.93"></a>
-<span class="sourceLineNo">094</span> *  <a name="line.94"></a>
-<span class="sourceLineNo">095</span> *  If the attribute is a boolean, a number,
a string, or an array<a name="line.95"></a>
-<span class="sourceLineNo">096</span> *  it will be converted to the JSON equivalent.
<a name="line.96"></a>
-<span class="sourceLineNo">097</span> *  <a name="line.97"></a>
-<span class="sourceLineNo">098</span> *  If the value is a {@link CompositeData}
then it will be converted<a name="line.98"></a>
-<span class="sourceLineNo">099</span> *  to a JSON object with the keys as the
name of the JSON member and<a name="line.99"></a>
-<span class="sourceLineNo">100</span> *  the value is converted following these
same rules.<a name="line.100"></a>
-<span class="sourceLineNo">101</span> *  <a name="line.101"></a>
-<span class="sourceLineNo">102</span> *  If the value is a {@link TabularData}
then it will be converted<a name="line.102"></a>
-<span class="sourceLineNo">103</span> *  to an array of the {@link CompositeData}
elements that it contains.<a name="line.103"></a>
-<span class="sourceLineNo">104</span> *  <a name="line.104"></a>
-<span class="sourceLineNo">105</span> *  All other objects will be converted
to a string and output as such.<a name="line.105"></a>
-<span class="sourceLineNo">106</span> *  <a name="line.106"></a>
-<span class="sourceLineNo">107</span> *  The bean's name and modelerType will
be returned for all beans.<a name="line.107"></a>
-<span class="sourceLineNo">108</span> *<a name="line.108"></a>
-<span class="sourceLineNo">109</span> *  Optional paramater "callback" should
be used to deliver JSONP response.<a name="line.109"></a>
-<span class="sourceLineNo">110</span> * &lt;/p&gt;<a name="line.110"></a>
-<span class="sourceLineNo">111</span> *  <a name="line.111"></a>
-<span class="sourceLineNo">112</span> */<a name="line.112"></a>
-<span class="sourceLineNo">113</span>public class JMXJsonServlet extends HttpServlet
{<a name="line.113"></a>
-<span class="sourceLineNo">114</span>  private static final Log LOG = LogFactory.getLog(JMXJsonServlet.class);<a
name="line.114"></a>
-<span class="sourceLineNo">115</span><a name="line.115"></a>
-<span class="sourceLineNo">116</span>  private static final long serialVersionUID
= 1L;<a name="line.116"></a>
-<span class="sourceLineNo">117</span><a name="line.117"></a>
-<span class="sourceLineNo">118</span>  private static final String CALLBACK_PARAM
= "callback";<a name="line.118"></a>
-<span class="sourceLineNo">119</span>  /**<a name="line.119"></a>
-<span class="sourceLineNo">120</span>   * If query string includes 'description',
then we will emit bean and attribute descriptions to<a name="line.120"></a>
-<span class="sourceLineNo">121</span>   * output IFF they are not null and IFF
the description is not the same as the attribute name:<a name="line.121"></a>
-<span class="sourceLineNo">122</span>   * i.e. specify an URL like so: /jmx?description=true<a
name="line.122"></a>
-<span class="sourceLineNo">123</span>   */<a name="line.123"></a>
-<span class="sourceLineNo">124</span>  private static final String INCLUDE_DESCRIPTION
= "description";<a name="line.124"></a>
-<span class="sourceLineNo">125</span><a name="line.125"></a>
-<span class="sourceLineNo">126</span>  /**<a name="line.126"></a>
-<span class="sourceLineNo">127</span>   * MBean server.<a name="line.127"></a>
-<span class="sourceLineNo">128</span>   */<a name="line.128"></a>
-<span class="sourceLineNo">129</span>  protected transient MBeanServer mBeanServer;<a
name="line.129"></a>
-<span class="sourceLineNo">130</span><a name="line.130"></a>
-<span class="sourceLineNo">131</span>  protected transient JSONBean jsonBeanWriter;<a
name="line.131"></a>
-<span class="sourceLineNo">132</span><a name="line.132"></a>
-<span class="sourceLineNo">133</span>  /**<a name="line.133"></a>
-<span class="sourceLineNo">134</span>   * Initialize this servlet.<a name="line.134"></a>
-<span class="sourceLineNo">135</span>   */<a name="line.135"></a>
-<span class="sourceLineNo">136</span>  @Override<a name="line.136"></a>
-<span class="sourceLineNo">137</span>  public void init() throws ServletException
{<a name="line.137"></a>
-<span class="sourceLineNo">138</span>    // Retrieve the MBean server<a name="line.138"></a>
-<span class="sourceLineNo">139</span>    mBeanServer = ManagementFactory.getPlatformMBeanServer();<a
name="line.139"></a>
-<span class="sourceLineNo">140</span>    this.jsonBeanWriter = new JSONBean();<a
name="line.140"></a>
-<span class="sourceLineNo">141</span>  }<a name="line.141"></a>
-<span class="sourceLineNo">142</span><a name="line.142"></a>
-<span class="sourceLineNo">143</span>  /**<a name="line.143"></a>
-<span class="sourceLineNo">144</span>   * Process a GET request for the specified
resource.<a name="line.144"></a>
-<span class="sourceLineNo">145</span>   * <a name="line.145"></a>
-<span class="sourceLineNo">146</span>   * @param request<a name="line.146"></a>
-<span class="sourceLineNo">147</span>   *          The servlet request we are
processing<a name="line.147"></a>
-<span class="sourceLineNo">148</span>   * @param response<a name="line.148"></a>
-<span class="sourceLineNo">149</span>   *          The servlet response we are
creating<a name="line.149"></a>
-<span class="sourceLineNo">150</span>   */<a name="line.150"></a>
-<span class="sourceLineNo">151</span>  @Override<a name="line.151"></a>
-<span class="sourceLineNo">152</span>  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="XSS_REQUEST_PARAMETER_TO_SERVLET_WRITER",<a
name="line.152"></a>
-<span class="sourceLineNo">153</span>    justification="TODO: See HBASE-15122")<a
name="line.153"></a>
-<span class="sourceLineNo">154</span>  public void doGet(HttpServletRequest request,
HttpServletResponse response) {<a name="line.154"></a>
-<span class="sourceLineNo">155</span>    try {<a name="line.155"></a>
-<span class="sourceLineNo">156</span>      if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(),
request, response)) {<a name="line.156"></a>
-<span class="sourceLineNo">157</span>        return;<a name="line.157"></a>
-<span class="sourceLineNo">158</span>      }<a name="line.158"></a>
-<span class="sourceLineNo">159</span>      String jsonpcb = null;<a name="line.159"></a>
-<span class="sourceLineNo">160</span>      PrintWriter writer = null;<a name="line.160"></a>
-<span class="sourceLineNo">161</span>      JSONBean.Writer beanWriter = null;<a
name="line.161"></a>
-<span class="sourceLineNo">162</span>      try {<a name="line.162"></a>
-<span class="sourceLineNo">163</span>        writer = response.getWriter();<a
name="line.163"></a>
-<span class="sourceLineNo">164</span>        beanWriter = this.jsonBeanWriter.open(writer);<a
name="line.164"></a>
-<span class="sourceLineNo">165</span> <a name="line.165"></a>
-<span class="sourceLineNo">166</span>        // "callback" parameter implies
JSONP outpout<a name="line.166"></a>
-<span class="sourceLineNo">167</span>        jsonpcb = request.getParameter(CALLBACK_PARAM);<a
name="line.167"></a>
-<span class="sourceLineNo">168</span>        if (jsonpcb != null) {<a name="line.168"></a>
-<span class="sourceLineNo">169</span>          response.setContentType("application/javascript;
charset=utf8");<a name="line.169"></a>
-<span class="sourceLineNo">170</span>          writer.write(jsonpcb + "(");<a
name="line.170"></a>
-<span class="sourceLineNo">171</span>        } else {<a name="line.171"></a>
-<span class="sourceLineNo">172</span>          response.setContentType("application/json;
charset=utf8");<a name="line.172"></a>
-<span class="sourceLineNo">173</span>        }<a name="line.173"></a>
-<span class="sourceLineNo">174</span>        // Should we output description
on each attribute and bean?<a name="line.174"></a>
-<span class="sourceLineNo">175</span>        String tmpStr = request.getParameter(INCLUDE_DESCRIPTION);<a
name="line.175"></a>
-<span class="sourceLineNo">176</span>        boolean description = tmpStr !=
null &amp;&amp; tmpStr.length() &gt; 0;<a name="line.176"></a>
-<span class="sourceLineNo">177</span><a name="line.177"></a>
-<span class="sourceLineNo">178</span>        // query per mbean attribute<a
name="line.178"></a>
-<span class="sourceLineNo">179</span>        String getmethod = request.getParameter("get");<a
name="line.179"></a>
-<span class="sourceLineNo">180</span>        if (getmethod != null) {<a name="line.180"></a>
-<span class="sourceLineNo">181</span>          String[] splitStrings = getmethod.split("\\:\\:");<a
name="line.181"></a>
-<span class="sourceLineNo">182</span>          if (splitStrings.length != 2)
{<a name="line.182"></a>
-<span class="sourceLineNo">183</span>            beanWriter.write("result", "ERROR");<a
name="line.183"></a>
-<span class="sourceLineNo">184</span>            beanWriter.write("message",
"query format is not as expected.");<a name="line.184"></a>
-<span class="sourceLineNo">185</span>            beanWriter.flush();<a name="line.185"></a>
-<span class="sourceLineNo">186</span>            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.186"></a>
-<span class="sourceLineNo">187</span>            return;<a name="line.187"></a>
-<span class="sourceLineNo">188</span>          }<a name="line.188"></a>
-<span class="sourceLineNo">189</span>          if (beanWriter.write(this.mBeanServer,
new ObjectName(splitStrings[0]),<a name="line.189"></a>
-<span class="sourceLineNo">190</span>              splitStrings[1], description)
!= 0) {<a name="line.190"></a>
-<span class="sourceLineNo">191</span>            beanWriter.flush();<a name="line.191"></a>
-<span class="sourceLineNo">192</span>            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.192"></a>
-<span class="sourceLineNo">193</span>          }<a name="line.193"></a>
-<span class="sourceLineNo">194</span>          return;<a name="line.194"></a>
-<span class="sourceLineNo">195</span>        }<a name="line.195"></a>
-<span class="sourceLineNo">196</span><a name="line.196"></a>
-<span class="sourceLineNo">197</span>        // query per mbean<a name="line.197"></a>
-<span class="sourceLineNo">198</span>        String qry = request.getParameter("qry");<a
name="line.198"></a>
-<span class="sourceLineNo">199</span>        if (qry == null) {<a name="line.199"></a>
-<span class="sourceLineNo">200</span>          qry = "*:*";<a name="line.200"></a>
-<span class="sourceLineNo">201</span>        }<a name="line.201"></a>
-<span class="sourceLineNo">202</span>        if (beanWriter.write(this.mBeanServer,
new ObjectName(qry), null, description) != 0) {<a name="line.202"></a>
-<span class="sourceLineNo">203</span>          beanWriter.flush();<a name="line.203"></a>
-<span class="sourceLineNo">204</span>          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.204"></a>
-<span class="sourceLineNo">205</span>        }<a name="line.205"></a>
-<span class="sourceLineNo">206</span>      } finally {<a name="line.206"></a>
-<span class="sourceLineNo">207</span>        if (beanWriter != null) beanWriter.close();<a
name="line.207"></a>
-<span class="sourceLineNo">208</span>        if (jsonpcb != null) {<a name="line.208"></a>
-<span class="sourceLineNo">209</span>           writer.write(");");<a name="line.209"></a>
-<span class="sourceLineNo">210</span>        }<a name="line.210"></a>
-<span class="sourceLineNo">211</span>        if (writer != null) {<a name="line.211"></a>
-<span class="sourceLineNo">212</span>          writer.close();<a name="line.212"></a>
-<span class="sourceLineNo">213</span>        }<a name="line.213"></a>
-<span class="sourceLineNo">214</span>      }<a name="line.214"></a>
-<span class="sourceLineNo">215</span>    } catch (IOException e) {<a name="line.215"></a>
-<span class="sourceLineNo">216</span>      LOG.error("Caught an exception while
processing JMX request", e);<a name="line.216"></a>
-<span class="sourceLineNo">217</span>      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);<a
name="line.217"></a>
-<span class="sourceLineNo">218</span>    } catch (MalformedObjectNameException
e) {<a name="line.218"></a>
-<span class="sourceLineNo">219</span>      LOG.error("Caught an exception while
processing JMX request", e);<a name="line.219"></a>
-<span class="sourceLineNo">220</span>      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.220"></a>
-<span class="sourceLineNo">221</span>    }<a name="line.221"></a>
-<span class="sourceLineNo">222</span>  }<a name="line.222"></a>
-<span class="sourceLineNo">223</span>}<a name="line.223"></a>
+<span class="sourceLineNo">038</span>import org.owasp.esapi.ESAPI;<a name="line.38"></a>
+<span class="sourceLineNo">039</span><a name="line.39"></a>
+<span class="sourceLineNo">040</span>/*<a name="line.40"></a>
+<span class="sourceLineNo">041</span> * This servlet is based off of the JMXProxyServlet
from Tomcat 7.0.14. It has<a name="line.41"></a>
+<span class="sourceLineNo">042</span> * been rewritten to be read only and to
output in a JSON format so it is not<a name="line.42"></a>
+<span class="sourceLineNo">043</span> * really that close to the original.<a
name="line.43"></a>
+<span class="sourceLineNo">044</span> */<a name="line.44"></a>
+<span class="sourceLineNo">045</span>/**<a name="line.45"></a>
+<span class="sourceLineNo">046</span> * Provides Read only web access to JMX.<a
name="line.46"></a>
+<span class="sourceLineNo">047</span> * &lt;p&gt;<a name="line.47"></a>
+<span class="sourceLineNo">048</span> * This servlet generally will be placed
under the /jmx URL for each<a name="line.48"></a>
+<span class="sourceLineNo">049</span> * HttpServer.  It provides read only<a
name="line.49"></a>
+<span class="sourceLineNo">050</span> * access to JMX metrics.  The optional
&lt;code&gt;qry&lt;/code&gt; parameter<a name="line.50"></a>
+<span class="sourceLineNo">051</span> * may be used to query only a subset of
the JMX Beans.  This query<a name="line.51"></a>
+<span class="sourceLineNo">052</span> * functionality is provided through the<a
name="line.52"></a>
+<span class="sourceLineNo">053</span> * {@link MBeanServer#queryNames(ObjectName,
javax.management.QueryExp)}<a name="line.53"></a>
+<span class="sourceLineNo">054</span> * method.<a name="line.54"></a>
+<span class="sourceLineNo">055</span> * &lt;/p&gt;<a name="line.55"></a>
+<span class="sourceLineNo">056</span> * &lt;p&gt;<a name="line.56"></a>
+<span class="sourceLineNo">057</span> * For example &lt;code&gt;http://.../jmx?qry=Hadoop:*&lt;/code&gt;
will return<a name="line.57"></a>
+<span class="sourceLineNo">058</span> * all hadoop metrics exposed through JMX.<a
name="line.58"></a>
+<span class="sourceLineNo">059</span> * &lt;/p&gt;<a name="line.59"></a>
+<span class="sourceLineNo">060</span> * &lt;p&gt;<a name="line.60"></a>
+<span class="sourceLineNo">061</span> * The optional &lt;code&gt;get&lt;/code&gt;
parameter is used to query an specific <a name="line.61"></a>
+<span class="sourceLineNo">062</span> * attribute of a JMX bean.  The format
of the URL is<a name="line.62"></a>
+<span class="sourceLineNo">063</span> * &lt;code&gt;http://.../jmx?get=MXBeanName::AttributeName&lt;/code&gt;<a
name="line.63"></a>
+<span class="sourceLineNo">064</span> * &lt;/p&gt;<a name="line.64"></a>
+<span class="sourceLineNo">065</span> * &lt;p&gt;<a name="line.65"></a>
+<span class="sourceLineNo">066</span> * For example <a name="line.66"></a>
+<span class="sourceLineNo">067</span> * &lt;code&gt;<a name="line.67"></a>
+<span class="sourceLineNo">068</span> * http://../jmx?get=Hadoop:service=NameNode,name=NameNodeInfo::ClusterId<a
name="line.68"></a>
+<span class="sourceLineNo">069</span> * &lt;/code&gt; will return the
cluster id of the namenode mxbean.<a name="line.69"></a>
+<span class="sourceLineNo">070</span> * &lt;/p&gt;<a name="line.70"></a>
+<span class="sourceLineNo">071</span> * &lt;p&gt;<a name="line.71"></a>
+<span class="sourceLineNo">072</span> * If the &lt;code&gt;qry&lt;/code&gt;
or the &lt;code&gt;get&lt;/code&gt; parameter is not formatted <a name="line.72"></a>
+<span class="sourceLineNo">073</span> * correctly then a 400 BAD REQUEST http
response code will be returned. <a name="line.73"></a>
+<span class="sourceLineNo">074</span> * &lt;/p&gt;<a name="line.74"></a>
+<span class="sourceLineNo">075</span> * &lt;p&gt;<a name="line.75"></a>
+<span class="sourceLineNo">076</span> * If a resouce such as a mbean or attribute
can not be found, <a name="line.76"></a>
+<span class="sourceLineNo">077</span> * a 404 SC_NOT_FOUND http response code
will be returned. <a name="line.77"></a>
+<span class="sourceLineNo">078</span> * &lt;/p&gt;<a name="line.78"></a>
+<span class="sourceLineNo">079</span> * &lt;p&gt;<a name="line.79"></a>
+<span class="sourceLineNo">080</span> * The return format is JSON and in the
form<a name="line.80"></a>
+<span class="sourceLineNo">081</span> * &lt;/p&gt;<a name="line.81"></a>
+<span class="sourceLineNo">082</span> *  &lt;pre&gt;&lt;code&gt;<a
name="line.82"></a>
+<span class="sourceLineNo">083</span> *  {<a name="line.83"></a>
+<span class="sourceLineNo">084</span> *    "beans" : [<a name="line.84"></a>
+<span class="sourceLineNo">085</span> *      {<a name="line.85"></a>
+<span class="sourceLineNo">086</span> *        "name":"bean-name"<a name="line.86"></a>
+<span class="sourceLineNo">087</span> *        ...<a name="line.87"></a>
+<span class="sourceLineNo">088</span> *      }<a name="line.88"></a>
+<span class="sourceLineNo">089</span> *    ]<a name="line.89"></a>
+<span class="sourceLineNo">090</span> *  }<a name="line.90"></a>
+<span class="sourceLineNo">091</span> *  &lt;/code&gt;&lt;/pre&gt;<a
name="line.91"></a>
+<span class="sourceLineNo">092</span> *  &lt;p&gt;<a name="line.92"></a>
+<span class="sourceLineNo">093</span> *  The servlet attempts to convert the
the JMXBeans into JSON. Each<a name="line.93"></a>
+<span class="sourceLineNo">094</span> *  bean's attributes will be converted
to a JSON object member.<a name="line.94"></a>
+<span class="sourceLineNo">095</span> *  <a name="line.95"></a>
+<span class="sourceLineNo">096</span> *  If the attribute is a boolean, a number,
a string, or an array<a name="line.96"></a>
+<span class="sourceLineNo">097</span> *  it will be converted to the JSON equivalent.
<a name="line.97"></a>
+<span class="sourceLineNo">098</span> *  <a name="line.98"></a>
+<span class="sourceLineNo">099</span> *  If the value is a {@link CompositeData}
then it will be converted<a name="line.99"></a>
+<span class="sourceLineNo">100</span> *  to a JSON object with the keys as the
name of the JSON member and<a name="line.100"></a>
+<span class="sourceLineNo">101</span> *  the value is converted following these
same rules.<a name="line.101"></a>
+<span class="sourceLineNo">102</span> *  <a name="line.102"></a>
+<span class="sourceLineNo">103</span> *  If the value is a {@link TabularData}
then it will be converted<a name="line.103"></a>
+<span class="sourceLineNo">104</span> *  to an array of the {@link CompositeData}
elements that it contains.<a name="line.104"></a>
+<span class="sourceLineNo">105</span> *  <a name="line.105"></a>
+<span class="sourceLineNo">106</span> *  All other objects will be converted
to a string and output as such.<a name="line.106"></a>
+<span class="sourceLineNo">107</span> *  <a name="line.107"></a>
+<span class="sourceLineNo">108</span> *  The bean's name and modelerType will
be returned for all beans.<a name="line.108"></a>
+<span class="sourceLineNo">109</span> *<a name="line.109"></a>
+<span class="sourceLineNo">110</span> *  Optional paramater "callback" should
be used to deliver JSONP response.<a name="line.110"></a>
+<span class="sourceLineNo">111</span> * &lt;/p&gt;<a name="line.111"></a>
+<span class="sourceLineNo">112</span> *  <a name="line.112"></a>
+<span class="sourceLineNo">113</span> */<a name="line.113"></a>
+<span class="sourceLineNo">114</span>public class JMXJsonServlet extends HttpServlet
{<a name="line.114"></a>
+<span class="sourceLineNo">115</span>  private static final Log LOG = LogFactory.getLog(JMXJsonServlet.class);<a
name="line.115"></a>
+<span class="sourceLineNo">116</span><a name="line.116"></a>
+<span class="sourceLineNo">117</span>  private static final long serialVersionUID
= 1L;<a name="line.117"></a>
+<span class="sourceLineNo">118</span><a name="line.118"></a>
+<span class="sourceLineNo">119</span>  private static final String CALLBACK_PARAM
= "callback";<a name="line.119"></a>
+<span class="sourceLineNo">120</span>  /**<a name="line.120"></a>
+<span class="sourceLineNo">121</span>   * If query string includes 'description',
then we will emit bean and attribute descriptions to<a name="line.121"></a>
+<span class="sourceLineNo">122</span>   * output IFF they are not null and IFF
the description is not the same as the attribute name:<a name="line.122"></a>
+<span class="sourceLineNo">123</span>   * i.e. specify an URL like so: /jmx?description=true<a
name="line.123"></a>
+<span class="sourceLineNo">124</span>   */<a name="line.124"></a>
+<span class="sourceLineNo">125</span>  private static final String INCLUDE_DESCRIPTION
= "description";<a name="line.125"></a>
+<span class="sourceLineNo">126</span><a name="line.126"></a>
+<span class="sourceLineNo">127</span>  /**<a name="line.127"></a>
+<span class="sourceLineNo">128</span>   * MBean server.<a name="line.128"></a>
+<span class="sourceLineNo">129</span>   */<a name="line.129"></a>
+<span class="sourceLineNo">130</span>  protected transient MBeanServer mBeanServer;<a
name="line.130"></a>
+<span class="sourceLineNo">131</span><a name="line.131"></a>
+<span class="sourceLineNo">132</span>  protected transient JSONBean jsonBeanWriter;<a
name="line.132"></a>
+<span class="sourceLineNo">133</span><a name="line.133"></a>
+<span class="sourceLineNo">134</span>  /**<a name="line.134"></a>
+<span class="sourceLineNo">135</span>   * Initialize this servlet.<a name="line.135"></a>
+<span class="sourceLineNo">136</span>   */<a name="line.136"></a>
+<span class="sourceLineNo">137</span>  @Override<a name="line.137"></a>
+<span class="sourceLineNo">138</span>  public void init() throws ServletException
{<a name="line.138"></a>
+<span class="sourceLineNo">139</span>    // Retrieve the MBean server<a name="line.139"></a>
+<span class="sourceLineNo">140</span>    mBeanServer = ManagementFactory.getPlatformMBeanServer();<a
name="line.140"></a>
+<span class="sourceLineNo">141</span>    this.jsonBeanWriter = new JSONBean();<a
name="line.141"></a>
+<span class="sourceLineNo">142</span>  }<a name="line.142"></a>
+<span class="sourceLineNo">143</span><a name="line.143"></a>
+<span class="sourceLineNo">144</span>  /**<a name="line.144"></a>
+<span class="sourceLineNo">145</span>   * Process a GET request for the specified
resource.<a name="line.145"></a>
+<span class="sourceLineNo">146</span>   * <a name="line.146"></a>
+<span class="sourceLineNo">147</span>   * @param request<a name="line.147"></a>
+<span class="sourceLineNo">148</span>   *          The servlet request we are
processing<a name="line.148"></a>
+<span class="sourceLineNo">149</span>   * @param response<a name="line.149"></a>
+<span class="sourceLineNo">150</span>   *          The servlet response we are
creating<a name="line.150"></a>
+<span class="sourceLineNo">151</span>   */<a name="line.151"></a>
+<span class="sourceLineNo">152</span>  @Override<a name="line.152"></a>
+<span class="sourceLineNo">153</span>  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="XSS_REQUEST_PARAMETER_TO_SERVLET_WRITER",<a
name="line.153"></a>
+<span class="sourceLineNo">154</span>    justification="TODO: See HBASE-15122")<a
name="line.154"></a>
+<span class="sourceLineNo">155</span>  public void doGet(HttpServletRequest request,
HttpServletResponse response) {<a name="line.155"></a>
+<span class="sourceLineNo">156</span>    try {<a name="line.156"></a>
+<span class="sourceLineNo">157</span>      if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(),
request, response)) {<a name="line.157"></a>
+<span class="sourceLineNo">158</span>        return;<a name="line.158"></a>
+<span class="sourceLineNo">159</span>      }<a name="line.159"></a>
+<span class="sourceLineNo">160</span>      String jsonpcb = null;<a name="line.160"></a>
+<span class="sourceLineNo">161</span>      PrintWriter writer = null;<a name="line.161"></a>
+<span class="sourceLineNo">162</span>      JSONBean.Writer beanWriter = null;<a
name="line.162"></a>
+<span class="sourceLineNo">163</span>      try {<a name="line.163"></a>
+<span class="sourceLineNo">164</span>        writer = response.getWriter();<a
name="line.164"></a>
+<span class="sourceLineNo">165</span>        beanWriter = this.jsonBeanWriter.open(writer);<a
name="line.165"></a>
+<span class="sourceLineNo">166</span> <a name="line.166"></a>
+<span class="sourceLineNo">167</span>        // "callback" parameter implies
JSONP outpout<a name="line.167"></a>
+<span class="sourceLineNo">168</span>        jsonpcb = request.getParameter(CALLBACK_PARAM);<a
name="line.168"></a>
+<span class="sourceLineNo">169</span>        if (jsonpcb != null) {<a name="line.169"></a>
+<span class="sourceLineNo">170</span>          response.setContentType("application/javascript;
charset=utf8");<a name="line.170"></a>
+<span class="sourceLineNo">171</span>          writer.write(encodeJS(jsonpcb)
+ "(");<a name="line.171"></a>
+<span class="sourceLineNo">172</span>        } else {<a name="line.172"></a>
+<span class="sourceLineNo">173</span>          response.setContentType("application/json;
charset=utf8");<a name="line.173"></a>
+<span class="sourceLineNo">174</span>        }<a name="line.174"></a>
+<span class="sourceLineNo">175</span>        // Should we output description
on each attribute and bean?<a name="line.175"></a>
+<span class="sourceLineNo">176</span>        String tmpStr = request.getParameter(INCLUDE_DESCRIPTION);<a
name="line.176"></a>
+<span class="sourceLineNo">177</span>        boolean description = tmpStr !=
null &amp;&amp; tmpStr.length() &gt; 0;<a name="line.177"></a>
+<span class="sourceLineNo">178</span><a name="line.178"></a>
+<span class="sourceLineNo">179</span>        // query per mbean attribute<a
name="line.179"></a>
+<span class="sourceLineNo">180</span>        String getmethod = request.getParameter("get");<a
name="line.180"></a>
+<span class="sourceLineNo">181</span>        if (getmethod != null) {<a name="line.181"></a>
+<span class="sourceLineNo">182</span>          String[] splitStrings = getmethod.split("\\:\\:");<a
name="line.182"></a>
+<span class="sourceLineNo">183</span>          if (splitStrings.length != 2)
{<a name="line.183"></a>
+<span class="sourceLineNo">184</span>            beanWriter.write("result", "ERROR");<a
name="line.184"></a>
+<span class="sourceLineNo">185</span>            beanWriter.write("message",
"query format is not as expected.");<a name="line.185"></a>
+<span class="sourceLineNo">186</span>            beanWriter.flush();<a name="line.186"></a>
+<span class="sourceLineNo">187</span>            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.187"></a>
+<span class="sourceLineNo">188</span>            return;<a name="line.188"></a>
+<span class="sourceLineNo">189</span>          }<a name="line.189"></a>
+<span class="sourceLineNo">190</span>          if (beanWriter.write(this.mBeanServer,
new ObjectName(splitStrings[0]),<a name="line.190"></a>
+<span class="sourceLineNo">191</span>              splitStrings[1], description)
!= 0) {<a name="line.191"></a>
+<span class="sourceLineNo">192</span>            beanWriter.flush();<a name="line.192"></a>
+<span class="sourceLineNo">193</span>            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.193"></a>
+<span class="sourceLineNo">194</span>          }<a name="line.194"></a>
+<span class="sourceLineNo">195</span>          return;<a name="line.195"></a>
+<span class="sourceLineNo">196</span>        }<a name="line.196"></a>
+<span class="sourceLineNo">197</span><a name="line.197"></a>
+<span class="sourceLineNo">198</span>        // query per mbean<a name="line.198"></a>
+<span class="sourceLineNo">199</span>        String qry = request.getParameter("qry");<a
name="line.199"></a>
+<span class="sourceLineNo">200</span>        if (qry == null) {<a name="line.200"></a>
+<span class="sourceLineNo">201</span>          qry = "*:*";<a name="line.201"></a>
+<span class="sourceLineNo">202</span>        }<a name="line.202"></a>
+<span class="sourceLineNo">203</span>        if (beanWriter.write(this.mBeanServer,
new ObjectName(qry), null, description) != 0) {<a name="line.203"></a>
+<span class="sourceLineNo">204</span>          beanWriter.flush();<a name="line.204"></a>
+<span class="sourceLineNo">205</span>          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.205"></a>
+<span class="sourceLineNo">206</span>        }<a name="line.206"></a>
+<span class="sourceLineNo">207</span>      } finally {<a name="line.207"></a>
+<span class="sourceLineNo">208</span>        if (beanWriter != null) beanWriter.close();<a
name="line.208"></a>
+<span class="sourceLineNo">209</span>        if (jsonpcb != null) {<a name="line.209"></a>
+<span class="sourceLineNo">210</span>           writer.write(");");<a name="line.210"></a>
+<span class="sourceLineNo">211</span>        }<a name="line.211"></a>
+<span class="sourceLineNo">212</span>        if (writer != null) {<a name="line.212"></a>
+<span class="sourceLineNo">213</span>          writer.close();<a name="line.213"></a>
+<span class="sourceLineNo">214</span>        }<a name="line.214"></a>
+<span class="sourceLineNo">215</span>      }<a name="line.215"></a>
+<span class="sourceLineNo">216</span>    } catch (IOException e) {<a name="line.216"></a>
+<span class="sourceLineNo">217</span>      LOG.error("Caught an exception while
processing JMX request", e);<a name="line.217"></a>
+<span class="sourceLineNo">218</span>      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);<a
name="line.218"></a>
+<span class="sourceLineNo">219</span>    } catch (MalformedObjectNameException
e) {<a name="line.219"></a>
+<span class="sourceLineNo">220</span>      LOG.error("Caught an exception while
processing JMX request", e);<a name="line.220"></a>
+<span class="sourceLineNo">221</span>      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);<a
name="line.221"></a>
+<span class="sourceLineNo">222</span>    }<a name="line.222"></a>
+<span class="sourceLineNo">223</span>  }<a name="line.223"></a>
+<span class="sourceLineNo">224</span><a name="line.224"></a>
+<span class="sourceLineNo">225</span>  private String encodeJS(String inputStr)
{<a name="line.225"></a>
+<span class="sourceLineNo">226</span>    return ESAPI.encoder().encodeForJavaScript(inputStr);<a
name="line.226"></a>
+<span class="sourceLineNo">227</span>  }<a name="line.227"></a>
+<span class="sourceLineNo">228</span><a name="line.228"></a>
+<span class="sourceLineNo">229</span>}<a name="line.229"></a>
 
 
 


Mime
View raw message