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;
@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
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);
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;
@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);
@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);
}