HADOOP-8922. Provide alternate JSONP output for JMXJsonServlet to allow javascript in browser (Damien Hardy via bobby)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1399001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4b27351435
commit
dc154bd209
|
@ -51,6 +51,9 @@ Release 2.0.3-alpha - Unreleased
|
||||||
|
|
||||||
HADOOP-8929. Add toString, other improvements for SampleQuantiles (todd)
|
HADOOP-8929. Add toString, other improvements for SampleQuantiles (todd)
|
||||||
|
|
||||||
|
HADOOP-8922. Provide alternate JSONP output for JMXJsonServlet to allow
|
||||||
|
javascript in browser (Damien Hardy via bobby)
|
||||||
|
|
||||||
OPTIMIZATIONS
|
OPTIMIZATIONS
|
||||||
|
|
||||||
HADOOP-8866. SampleQuantiles#query is O(N^2) instead of O(N). (Andrew Wang
|
HADOOP-8866. SampleQuantiles#query is O(N^2) instead of O(N). (Andrew Wang
|
||||||
|
|
|
@ -113,6 +113,9 @@ import org.codehaus.jackson.JsonGenerator;
|
||||||
* All other objects will be converted to a string and output as such.
|
* All other objects will be converted to a string and output as such.
|
||||||
*
|
*
|
||||||
* The bean's name and modelerType will be returned for all beans.
|
* The bean's name and modelerType will be returned for all beans.
|
||||||
|
*
|
||||||
|
* Optional paramater "callback" should be used to deliver JSONP response.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public class JMXJsonServlet extends HttpServlet {
|
public class JMXJsonServlet extends HttpServlet {
|
||||||
private static final Log LOG = LogFactory.getLog(JMXJsonServlet.class);
|
private static final Log LOG = LogFactory.getLog(JMXJsonServlet.class);
|
||||||
|
@ -120,6 +123,8 @@ public class JMXJsonServlet extends HttpServlet {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
// ----------------------------------------------------- Instance Variables
|
// ----------------------------------------------------- Instance Variables
|
||||||
|
private static final String CALLBACK_PARAM = "callback";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MBean server.
|
* MBean server.
|
||||||
*/
|
*/
|
||||||
|
@ -145,20 +150,33 @@ public class JMXJsonServlet extends HttpServlet {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void doGet(HttpServletRequest request, HttpServletResponse response) {
|
public void doGet(HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
String jsonpcb = null;
|
||||||
|
PrintWriter writer = null;
|
||||||
try {
|
try {
|
||||||
if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(),
|
if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(),
|
||||||
request, response)) {
|
request, response)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonGenerator jg = null;
|
||||||
|
|
||||||
response.setContentType("application/json; charset=utf8");
|
writer = response.getWriter();
|
||||||
|
|
||||||
PrintWriter writer = response.getWriter();
|
// "callback" parameter implies JSONP outpout
|
||||||
|
jsonpcb = request.getParameter(CALLBACK_PARAM);
|
||||||
|
if (jsonpcb != null) {
|
||||||
|
response.setContentType("application/javascript; charset=utf8");
|
||||||
|
writer.write(jsonpcb + "(");
|
||||||
|
} else {
|
||||||
|
response.setContentType("application/json; charset=utf8");
|
||||||
|
}
|
||||||
|
|
||||||
JsonFactory jsonFactory = new JsonFactory();
|
JsonFactory jsonFactory = new JsonFactory();
|
||||||
JsonGenerator jg = jsonFactory.createJsonGenerator(writer);
|
jg = jsonFactory.createJsonGenerator(writer);
|
||||||
|
jg.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
|
||||||
jg.useDefaultPrettyPrinter();
|
jg.useDefaultPrettyPrinter();
|
||||||
jg.writeStartObject();
|
jg.writeStartObject();
|
||||||
|
|
||||||
if (mBeanServer == null) {
|
if (mBeanServer == null) {
|
||||||
jg.writeStringField("result", "ERROR");
|
jg.writeStringField("result", "ERROR");
|
||||||
jg.writeStringField("message", "No MBeanServer could be found");
|
jg.writeStringField("message", "No MBeanServer could be found");
|
||||||
|
@ -183,6 +201,7 @@ public class JMXJsonServlet extends HttpServlet {
|
||||||
response);
|
response);
|
||||||
jg.close();
|
jg.close();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// query per mbean
|
// query per mbean
|
||||||
|
@ -199,6 +218,13 @@ public class JMXJsonServlet extends HttpServlet {
|
||||||
} catch ( MalformedObjectNameException e ) {
|
} catch ( MalformedObjectNameException e ) {
|
||||||
LOG.error("Caught an exception while processing JMX request", e);
|
LOG.error("Caught an exception while processing JMX request", e);
|
||||||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||||
|
} finally {
|
||||||
|
if (jsonpcb != null) {
|
||||||
|
writer.write(");");
|
||||||
|
}
|
||||||
|
if (writer != null) {
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,5 +78,29 @@ public class TestJMXJsonServlet extends HttpServerFunctionalTest {
|
||||||
"/jmx?get=java.lang:type=Memory::"));
|
"/jmx?get=java.lang:type=Memory::"));
|
||||||
LOG.info("/jmx RESULT: "+result);
|
LOG.info("/jmx RESULT: "+result);
|
||||||
assertReFind("\"ERROR\"", result);
|
assertReFind("\"ERROR\"", result);
|
||||||
|
|
||||||
|
// test to get JSONP result
|
||||||
|
result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory&callback=mycallback1"));
|
||||||
|
LOG.info("/jmx?qry=java.lang:type=Memory&callback=mycallback RESULT: "+result);
|
||||||
|
assertReFind("^mycallback1\\(\\{", result);
|
||||||
|
assertReFind("\\}\\);$", result);
|
||||||
|
|
||||||
|
// negative test to get an attribute of a mbean as JSONP
|
||||||
|
result = readOutput(new URL(baseUrl,
|
||||||
|
"/jmx?get=java.lang:type=Memory::&callback=mycallback2"));
|
||||||
|
LOG.info("/jmx RESULT: "+result);
|
||||||
|
assertReFind("^mycallback2\\(\\{", result);
|
||||||
|
assertReFind("\"ERROR\"", result);
|
||||||
|
assertReFind("\\}\\);$", result);
|
||||||
|
|
||||||
|
// test to get an attribute of a mbean as JSONP
|
||||||
|
result = readOutput(new URL(baseUrl,
|
||||||
|
"/jmx?get=java.lang:type=Memory::HeapMemoryUsage&callback=mycallback3"));
|
||||||
|
LOG.info("/jmx RESULT: "+result);
|
||||||
|
assertReFind("^mycallback3\\(\\{", result);
|
||||||
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
||||||
|
assertReFind("\"committed\"\\s*:", result);
|
||||||
|
assertReFind("\\}\\);$", result);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue