Repository: hadoop
Updated Branches:
  refs/heads/branch-2 9c6e8ece9 -> e208a4e7e


HADOOP-13588. ConfServlet should respect Accept request header. Contributed by 
Weiwei Yang

(cherry picked from commit 59d59667a8b1d3fb4a744a41774b2397fd91cbb3)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/9ba3268f
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/9ba3268f
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/9ba3268f

Branch: refs/heads/branch-2
Commit: 9ba3268fa3ad4fec5222361058a2086d2cb8c3e0
Parents: 9c6e8ec
Author: John Zhuge <jzh...@apache.org>
Authored: Fri Aug 11 10:54:25 2017 -0700
Committer: John Zhuge <jzh...@apache.org>
Committed: Fri Aug 11 10:54:25 2017 -0700

----------------------------------------------------------------------
 .../org/apache/hadoop/conf/ConfServlet.java     | 21 ++++++++++-------
 .../org/apache/hadoop/conf/TestConfServlet.java | 24 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/9ba3268f/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/ConfServlet.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/ConfServlet.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/ConfServlet.java
index d4b34e9..f9a2535 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/ConfServlet.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/ConfServlet.java
@@ -25,11 +25,14 @@ import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
 
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.http.HttpServer2;
 
+import com.google.common.annotations.VisibleForTesting;
+
 /**
  * A servlet to print out the running configuration data.
  */
@@ -38,9 +41,8 @@ import org.apache.hadoop.http.HttpServer2;
 public class ConfServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
 
-  private static final String FORMAT_JSON = "json";
-  private static final String FORMAT_XML = "xml";
-  private static final String FORMAT_PARAM = "format";
+  protected static final String FORMAT_JSON = "json";
+  protected static final String FORMAT_XML = "xml";
 
   /**
    * Return the Configuration of the daemon hosting this servlet.
@@ -67,11 +69,7 @@ public class ConfServlet extends HttpServlet {
       return;
     }
 
-    String format = request.getParameter(FORMAT_PARAM);
-    if (null == format) {
-      format = FORMAT_XML;
-    }
-
+    String format = parseAccecptHeader(request);
     if (FORMAT_XML.equals(format)) {
       response.setContentType("text/xml; charset=utf-8");
     } else if (FORMAT_JSON.equals(format)) {
@@ -87,6 +85,13 @@ public class ConfServlet extends HttpServlet {
     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.
    */

http://git-wip-us.apache.org/repos/asf/hadoop/blob/9ba3268f/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfServlet.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfServlet.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfServlet.java
index 1c22ee6..73d4992 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfServlet.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfServlet.java
@@ -19,7 +19,11 @@ package org.apache.hadoop.conf;
 
 import java.io.StringWriter;
 import java.io.StringReader;
+import java.util.HashMap;
 import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.core.HttpHeaders;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
@@ -32,6 +36,7 @@ import org.xml.sax.InputSource;
 
 import junit.framework.TestCase;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * Basic test case that the ConfServlet can write configuration
@@ -48,6 +53,25 @@ public class TestConfServlet extends TestCase {
   }
 
   @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
   @SuppressWarnings("unchecked")
   public void testWriteJson() throws Exception {
     StringWriter sw = new StringWriter();


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-commits-h...@hadoop.apache.org

Reply via email to