Browse Source

Polish

pull/1011/head
Brian Clozel 9 years ago
parent
commit
e079a726f8
  1. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
  2. 22
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

5
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

@ -485,7 +485,10 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
if (mediaType == null) { if (mediaType == null) {
ServletWebRequest webRequest = new ServletWebRequest(request); ServletWebRequest webRequest = new ServletWebRequest(request);
try { try {
getContentNegotiationManager().resolveMediaTypes(webRequest); List<MediaType> mediaTypes = getContentNegotiationManager().resolveMediaTypes(webRequest);
if(!mediaTypes.isEmpty()) {
mediaType = mediaTypes.get(0);
}
} }
catch (HttpMediaTypeNotAcceptableException ex) { catch (HttpMediaTypeNotAcceptableException ex) {
// Ignore // Ignore

22
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

@ -54,6 +54,7 @@ import org.springframework.web.servlet.HandlerMapping;
* @author Keith Donald * @author Keith Donald
* @author Jeremy Grelle * @author Jeremy Grelle
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Brian Clozel
*/ */
public class ResourceHttpRequestHandlerTests { public class ResourceHttpRequestHandlerTests {
@ -244,6 +245,27 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("h1 { color:red; }", this.response.getContentAsString()); assertEquals("h1 { color:red; }", this.response.getContentAsString());
} }
@Test // SPR-13658
public void getResourceWithRegisteredMediaTypeDefaultStrategy() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.setFavorPathExtension(false);
factory.setDefaultContentType(new MediaType("foo", "bar"));
factory.afterPropertiesSet();
ContentNegotiationManager manager = factory.getObject();
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
this.handler = new ResourceHttpRequestHandler();
this.handler.setLocations(paths);
this.handler.setContentNegotiationManager(manager);
this.handler.afterPropertiesSet();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
this.handler.handleRequest(this.request, this.response);
assertEquals("foo/bar", this.response.getContentType());
assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
@Test @Test
public void invalidPath() throws Exception { public void invalidPath() throws Exception {
for (HttpMethod method : HttpMethod.values()) { for (HttpMethod method : HttpMethod.values()) {

Loading…
Cancel
Save