HADOOP-13588. ConfServlet should respect Accept request header. Contributed by Weiwei Yang
This commit is contained in:
parent
8a93f45a80
commit
59d59667a8
@ -24,11 +24,14 @@
|
|||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
|
||||||
import org.apache.hadoop.classification.InterfaceAudience;
|
import org.apache.hadoop.classification.InterfaceAudience;
|
||||||
import org.apache.hadoop.classification.InterfaceStability;
|
import org.apache.hadoop.classification.InterfaceStability;
|
||||||
import org.apache.hadoop.http.HttpServer2;
|
import org.apache.hadoop.http.HttpServer2;
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A servlet to print out the running configuration data.
|
* A servlet to print out the running configuration data.
|
||||||
*/
|
*/
|
||||||
@ -37,9 +40,8 @@
|
|||||||
public class ConfServlet extends HttpServlet {
|
public class ConfServlet extends HttpServlet {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static final String FORMAT_JSON = "json";
|
protected static final String FORMAT_JSON = "json";
|
||||||
private static final String FORMAT_XML = "xml";
|
protected static final String FORMAT_XML = "xml";
|
||||||
private static final String FORMAT_PARAM = "format";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the Configuration of the daemon hosting this servlet.
|
* Return the Configuration of the daemon hosting this servlet.
|
||||||
@ -61,11 +63,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String format = request.getParameter(FORMAT_PARAM);
|
String format = parseAccecptHeader(request);
|
||||||
if (null == format) {
|
|
||||||
format = FORMAT_XML;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FORMAT_XML.equals(format)) {
|
if (FORMAT_XML.equals(format)) {
|
||||||
response.setContentType("text/xml; charset=utf-8");
|
response.setContentType("text/xml; charset=utf-8");
|
||||||
} else if (FORMAT_JSON.equals(format)) {
|
} else if (FORMAT_JSON.equals(format)) {
|
||||||
@ -81,6 +79,13 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
|
|||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static String parseAccecptHeader(HttpServletRequest request) {
|
||||||
|
String format = request.getHeader(HttpHeaders.ACCEPT);
|
||||||
|
return format != null && format.contains(FORMAT_JSON) ?
|
||||||
|
FORMAT_JSON : FORMAT_XML;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Guts of the servlet - extracted for easy testing.
|
* Guts of the servlet - extracted for easy testing.
|
||||||
*/
|
*/
|
||||||
|
@ -19,7 +19,11 @@
|
|||||||
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
@ -32,6 +36,7 @@
|
|||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic test case that the ConfServlet can write configuration
|
* Basic test case that the ConfServlet can write configuration
|
||||||
@ -47,6 +52,25 @@ private Configuration getTestConf() {
|
|||||||
return testConf;
|
return testConf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseHeaders() throws Exception {
|
||||||
|
HashMap<String, String> verifyMap = new HashMap<String, String>();
|
||||||
|
verifyMap.put("text/plain", ConfServlet.FORMAT_XML);
|
||||||
|
verifyMap.put(null, ConfServlet.FORMAT_XML);
|
||||||
|
verifyMap.put("text/xml", ConfServlet.FORMAT_XML);
|
||||||
|
verifyMap.put("application/xml", ConfServlet.FORMAT_XML);
|
||||||
|
verifyMap.put("application/json", ConfServlet.FORMAT_JSON);
|
||||||
|
|
||||||
|
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
|
||||||
|
for(String contentTypeExpected : verifyMap.keySet()) {
|
||||||
|
String contenTypeActual = verifyMap.get(contentTypeExpected);
|
||||||
|
Mockito.when(request.getHeader(HttpHeaders.ACCEPT))
|
||||||
|
.thenReturn(contentTypeExpected);
|
||||||
|
assertEquals(contenTypeActual,
|
||||||
|
ConfServlet.parseAccecptHeader(request));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void testWriteJson() throws Exception {
|
public void testWriteJson() throws Exception {
|
||||||
|
Loading…
Reference in New Issue
Block a user