Skip to content

MediaTypeFactory should come with its own complete mime.types file - #1356

Closed
poutsma wants to merge 4 commits into
spring-projects:masterfrom
poutsma:SPR-14908
Closed

poutsma wants to merge 4 commits into
spring-projects:masterfrom
poutsma:SPR-14908

Conversation

@poutsma

@poutsma poutsma commented Mar 14, 2017

Copy link
Copy Markdown
Contributor

For your consideration, @jhoeller and @rstoyanchev, here is my PR for resolving https://jira.spring.io/browse/SPR-14908. I've opted to split the PR in three commits: the first showing the MediaTypeFactory changes, the second fixes the main code base to conform to these changes, and the third does the same for the test code base.

As per discussion with Juergen, I've dropped all JAF usage in favour of the new MediaTypeFactory, including that in pre 5.0 code, and deprecate the various useJaf properties we had in that code, with no direct replacement.

poutsma added 3 commits March 14, 2017 14:58
This commit drops the Java Activation Framework dependency from the
MediaTypeFactory, in favor of parsing our own `mime.types` file, which
was obtained from Apache HTTPD.

Issue: SPR-14908
This commit updates the main code base to conform to the dropped JAF
dependency in MediaTypeFactory. Specifically, it

 - Removes JAF detection (JAF_PRESENT constants)
 - Deprecated useJaf properties, with no direct replacement.
 - Updated docs to remove JAF references, in favor of MediaTypeFactory.

Issue: SPR-14908
This commit updates the test code base to conform to changes in media
types returned by the MediaTypeFactory.

Issue: SPR-14908
@sbrannen

Copy link
Copy Markdown
Member

What about the use of javax.activation.FileTypeMap in our MockServletContext implementations?

@poutsma

poutsma commented Mar 14, 2017 •

Copy link
Copy Markdown
Contributor Author

What about the use of javax.activation.FileTypeMap in our MockServletContext implementations?

I decided to keep that as is, since the MediaTypeFactory does not have a manual override mechanism, and the Javadoc comment on getMimeType seems to suggest that overriding is necessary.

@sbrannen

Copy link
Copy Markdown
Member

MockServletContext.getMimeType() doesn't mention anything about overriding the method.

In any case, it can't be overridden if the Spring TestContext Framework or parts of Spring MVC Test are creating the mock for the user transparently within a test. So, in such situations, if JAF is not available (e.g., on Java 9), then the tests would no longer work.

Or am I missing something?

@poutsma

poutsma commented Mar 14, 2017

Copy link
Copy Markdown
Contributor Author

I wasn't talking about method overriding; I was talking about overriding or customising the mime types returned by MockServletContext.getMimeType. JAF has such a customisation mechanism, and as you know there is a use case for it.

Except for replacing the mime.types file on the classpath, the new MediaTypeFactory does not have such a customisation mechanism. Its goal is to provide default mimes types, when none are provided through PathExtensionContentNegotiationStrategy or similar mechanisms (which are customisable).

So, in summary: JAF has a customisation mechanism; the new MediaTypeFactory does not. MockServletRequest.getMimeType needs such a mechanism, so it does not use MediaTypeFactory.

@sbrannen

Copy link
Copy Markdown
Member

OK, I now understand where you're coming from.

So... I guess we are then going to make JAF a requirement for the testing frameworks. I can live with that; we'll just have to document it.

My real concern was that production code and test code would demonstrate different behavior with regard to available mime types out of the box.

Do you not foresee any issues with that?

@sbrannen

Copy link
Copy Markdown
Member

By the way, nice investigative work on finding that use case. 😉

This commit changes the `MockServletContext.getMimeType` method to use
`MediaTypeFactory` instead of JAF. It also adds a `addMimeType(String,
MediaType)` method to customize the mime types returned from said
method.

Issue: SPR-14908
@poutsma

poutsma commented Mar 15, 2017

Copy link
Copy Markdown
Contributor Author

My real concern was that production code and test code would demonstrate different behavior with regard to available mime types out of the box.

Do you not foresee any issues with that?

I do, but to be fair: ServletContext.getMimeType() can pretty much return whatever the Servlet container wants: there is no guarantee that it uses JAF, or that it returns a sensible mime type at all. It might as well return foo/bar or even null for any given string, and that would be fine according to the spec.

If anything, I think we should make it easier for users to let MockServletContext.getMimeType() return whatever their servlet container returns. Easier than doing the JAF-override-dance, I mean. I've added a fourth commit to this PR that does just that, and also drops the JAF dependency in MockServletContext. For your consideration, @sbrannen.

@sbrannen

Copy link
Copy Markdown
Member

The changes in commit 689f0d7 look much more robust than what we had before.

Thanks, @poutsma!

rstoyanchev added a commit that referenced this pull request Mar 21, 2017
@rstoyanchev

Copy link
Copy Markdown
Contributor

I've processed the PR to avoid further merge conflicts.

That said we do need an alternative to the deprecated usefJaf property starting with ContentNegotiationManagerFactoryBean and RequestedContentTypeResolverBuilder and then all the places it was propagated to. This property serves an important purpose in allowing automatic lookups of extensions to be suppressed in favor of using only explicitly registered extensions. This can be a source of security issues.

We should also consider some improvement to MediaTypeFactory#getMediaType(Resource) which in all places where it's used it's then followed up with fallback on application/octet-stream. Perhaps getMediaTypeOrDefault or return an Optional.

@poutsma

poutsma commented Mar 23, 2017 •

Copy link
Copy Markdown
Contributor Author

getMediaType now returns an Optional<MediaType>, see fd1db57

@poutsma
poutsma deleted the SPR-14908 branch March 23, 2017 09:35
@poutsma

poutsma commented Mar 23, 2017 •

Copy link
Copy Markdown
Contributor Author

I've pushed a branch here, the main commit being this one, that introduces a useDefaults property, taking the role of the useJaf property. Let me know what you think, @rstoyanchev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants