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

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

@ -54,6 +54,7 @@ import org.springframework.web.servlet.HandlerMapping; @@ -54,6 +54,7 @@ import org.springframework.web.servlet.HandlerMapping;
* @author Keith Donald
* @author Jeremy Grelle
* @author Rossen Stoyanchev
* @author Brian Clozel
*/
public class ResourceHttpRequestHandlerTests {
@ -244,6 +245,27 @@ public class ResourceHttpRequestHandlerTests { @@ -244,6 +245,27 @@ public class ResourceHttpRequestHandlerTests {
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
public void invalidPath() throws Exception {
for (HttpMethod method : HttpMethod.values()) {

Loading…
Cancel
Save