Fix MediaType lookup for ResourceHttpRequestHandler

As of 4.3 ResourceHttpRequestHandler delegates to the configured
ContentNegotiationManager, or one created internally, to look up
the media type for are resource.

This commit ensures the internally created ContentNegotiationManager is
correctly injected with the ServletContext through which it can perform
lookups as before.

Also the ServletPathContentNegotiationStrategy now checks the
ServletContext first and then delegates to its parent the
PathContentNegotiationStrategy and not vice versa. This is
consistent with how handleNoMatch (also in the same class) works
and also matches how ResourceHttpRequestHandler worked before 4.3.

Issue: SPR-14368
This commit is contained in:
Rossen Stoyanchev
2016-06-27 14:56:55 -04:00
parent a30ab30e4e
commit e38623df87
3 changed files with 45 additions and 17 deletions

View File

@@ -92,15 +92,18 @@ public class ServletPathExtensionContentNegotiationStrategy extends PathExtensio
* @since 4.3
*/
public MediaType getMediaTypeForResource(Resource resource) {
MediaType mediaType = super.getMediaTypeForResource(resource);
if (mediaType == null) {
MediaType mediaType = null;
if (this.servletContext != null) {
String mimeType = this.servletContext.getMimeType(resource.getFilename());
if (StringUtils.hasText(mimeType)) {
mediaType = MediaType.parseMediaType(mimeType);
}
}
if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
mediaType = null;
if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
MediaType superMediaType = super.getMediaTypeForResource(resource);
if (superMediaType != null) {
mediaType = superMediaType;
}
}
return mediaType;
}