]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6865 Read 'Accept' HTTP header instead of 'Content-Type'
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 21 Sep 2015 15:59:46 +0000 (17:59 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 30 Sep 2015 14:27:12 +0000 (16:27 +0200)
server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java
server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java

index 6e637c2a6cf8ba8e7bef6b7ab809547d4f5255df..931de1d6beb84849fb4c77c99525f347e9543338 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.ws;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.net.HttpHeaders;
 import java.io.InputStream;
 import java.util.Map;
 import javax.annotation.CheckForNull;
@@ -51,8 +52,10 @@ public class ServletRequest extends ValidatingRequest {
 
   @Override
   public String getMediaType() {
-    String mediaTypeFromUrl = mediaTypeFromUrl(source.getRequestURI());
-    return firstNonNull(mediaTypeFromUrl, source.getContentType(), MimeTypes.DEFAULT);
+    return firstNonNull(
+      mediaTypeFromUrl(source.getRequestURI()),
+      acceptedContentTypeInResponse(),
+      MimeTypes.DEFAULT);
   }
 
   @Override
@@ -91,6 +94,11 @@ public class ServletRequest extends ValidatingRequest {
     return url.toString();
   }
 
+  @CheckForNull
+  private String acceptedContentTypeInResponse() {
+    return source.getHeader(HttpHeaders.ACCEPT);
+  }
+
   @CheckForNull
   private static String mediaTypeFromUrl(String url) {
     String mediaType = url.substring(url.lastIndexOf('.') + 1);
index 2df707593ff29905bc643607fac60d1679eef6ad..a49fce8a92e40afe542148649edcb6ed4cabbb99 100644 (file)
@@ -21,6 +21,7 @@
 package org.sonar.server.ws;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.net.HttpHeaders;
 import java.util.Collections;
 import javax.servlet.http.HttpServletRequest;
 import org.jruby.RubyFile;
@@ -45,7 +46,7 @@ public class ServletRequestTest {
 
   @Test
   public void getMediaType() throws Exception {
-    when(source.getContentType()).thenReturn(MimeTypes.JSON);
+    when(source.getHeader(HttpHeaders.ACCEPT)).thenReturn(MimeTypes.JSON);
     when(source.getRequestURI()).thenReturn("/path/to/resource/search");
     ServletRequest request = new ServletRequest(source, Collections.<String, Object>emptyMap());
     assertThat(request.getMediaType()).isEqualTo(MimeTypes.JSON);
@@ -61,7 +62,7 @@ public class ServletRequestTest {
   @Test
   public void media_type_taken_in_url_first() throws Exception {
     ServletRequest request = new ServletRequest(source, Collections.<String, Object>emptyMap());
-    when(source.getContentType()).thenReturn(MimeTypes.JSON);
+    when(source.getHeader(HttpHeaders.ACCEPT)).thenReturn(MimeTypes.JSON);
     when(source.getRequestURI()).thenReturn("/path/to/resource/search.protobuf");
     assertThat(request.getMediaType()).isEqualTo(MimeTypes.PROTOBUF);
   }