From: Teryk Bellahsene Date: Mon, 21 Sep 2015 15:59:46 +0000 (+0200) Subject: SONAR-6865 Read 'Accept' HTTP header instead of 'Content-Type' X-Git-Tag: 5.2-RC1~170 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=efa592c16deb8f5477d4086406f51bb41d0cdd37;p=sonarqube.git SONAR-6865 Read 'Accept' HTTP header instead of 'Content-Type' --- 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 @@ -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); 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.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.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); }