aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-09-21 17:59:46 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-09-30 16:27:12 +0200
commitefa592c16deb8f5477d4086406f51bb41d0cdd37 (patch)
tree3cabe9018b4c6b050bcab5c5c144e82a73f7762f
parent604570330b3702c3c2df0463bd2610208e64a55b (diff)
downloadsonarqube-efa592c16deb8f5477d4086406f51bb41d0cdd37.tar.gz
sonarqube-efa592c16deb8f5477d4086406f51bb41d0cdd37.zip
SONAR-6865 Read 'Accept' HTTP header instead of 'Content-Type'
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java12
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java5
2 files changed, 13 insertions, 4 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java
index 6e637c2a6cf..931de1d6beb 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java
@@ -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
@@ -92,6 +95,11 @@ public class ServletRequest extends ValidatingRequest {
}
@CheckForNull
+ private String acceptedContentTypeInResponse() {
+ return source.getHeader(HttpHeaders.ACCEPT);
+ }
+
+ @CheckForNull
private static String mediaTypeFromUrl(String url) {
String mediaType = url.substring(url.lastIndexOf('.') + 1);
return SUPPORTED_FORMATS.get(mediaType.toUpperCase());
diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java b/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java
index 2df707593ff..a49fce8a92e 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java
@@ -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);
}