spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.39k stars 38.05k forks source link

Allow defining custom file extension to media type mappings in ResourceHttpMessageConverter [SPR-14409] #18980

Open spring-projects-issues opened 8 years ago

spring-projects-issues commented 8 years ago

Oliver Drotbohm opened SPR-14409 and commented

ResourceHttpMessageConverter currently uses the mime.types mapping file contained in org/springframework/mail/javamail to control which file extension is mapped to which media type.

It would be cool if there was an easy way to register additional mappings on the converter itself that would take precedence over the automated lookup via JAF. That way, Spring Boot could expose those additional mappings through application.properties.


Affects: 4.3 GA

Issue Links:

spring-projects-issues commented 8 years ago

Rossen Stoyanchev commented

This sounds like a duplicate of #18233. In 4.3 already consult the ContentNegotiationManager with which you can register extensions. If using the MVC config, which is the case with Spring Boot, then the "central" ContentNegotiationManager bean should be getting injected into the ResourceHttpRequestHandler and so any explicit registrations should just work. Is that not the case?

spring-projects-issues commented 8 years ago

Oliver Drotbohm commented

Looking at the JavaDoc for ContentNegotiationManager I think we're talking about different use cases. The one I am describing doesn't necessarily expose the media type to be returned eventually from the original request (which is, what CNM seems to look up). Imagine a request with Accept: */* on /files/4711. The file identified by that URI will contain a file extension, so that if I wrap this into a ResponseEntity<Resource> in a controller, ResourceHttpMessageConverter and does its thing based on the resources filename. The mappings of the latter to a media type cannot be easily customized (i.e. I want to serve .foo with my custom media type application/x-foo).

I am not near a machine I can code on right now but I can give this a spin tomorrow.

spring-projects-issues commented 8 years ago

Rossen Stoyanchev commented

Oops, #18233 is about ResourceHttpRequestHandler where this one here is about ResourceHttpMessageConverter.

That said I think we can do exactly the same. See what we did in ResourceHttpRequestHandler#getMediaType. In particular the call to PathExtensionContentNegotiationStrategy#getMediaTypeForResource which checks the registered mappings and also encapsulates a check via JAF as well as the ServletContext.

spring-projects-issues commented 8 years ago

Oliver Drotbohm commented

Do you oversee what would be necessary for Boot to integrate with that? From what I can see, it currently only looks up a CNM in the ApplicationContext to hand it into the ContentNegotiatingViewResolver.