diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java index 510b12f3bda..72facd21e9b 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java @@ -72,7 +72,8 @@ import org.springframework.web.util.WebUtils; * media type. The default name of the parameter is format and it can be configured using the * {@link #setParameterName(String) parameterName} property. *
  • If there is no match in the {@link #setMediaTypes(Map) mediaTypes} property and if the Java Activation - * Framework (JAF) is present on the class path, {@link FileTypeMap#getContentType(String)} is used instead.
  • + * Framework (JAF) is both {@linkplain #setUseJaf(boolean) enabled} and present on the class path, + * {@link FileTypeMap#getContentType(String)} is used instead. *
  • If the previous steps did not result in a media type, and * {@link #setIgnoreAcceptHeader(boolean) ignoreAcceptHeader} is {@code false}, the request {@code Accept} header is * used.
  • @@ -123,6 +124,8 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport private boolean ignoreAcceptHeader = false; + private boolean useJaf = true; + private ConcurrentMap mediaTypes = new ConcurrentHashMap(); private List defaultViews; @@ -224,6 +227,14 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport this.defaultContentType = defaultContentType; } + /** + * Indicates whether to use the Java Activation Framework to map from file extensions to media types. + *

    Default is {@code true}, i.e. the Java Activation Framework is used. + */ + public void setUseJaf(boolean useJaf) { + this.useJaf = useJaf; + } + /** * Sets the view resolvers to be wrapped by this view resolver. *

    If this property is not set, view resolvers will be detected automatically. @@ -232,6 +243,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport this.viewResolvers = viewResolvers; } + @Override protected void initServletContext(ServletContext servletContext) { if (this.viewResolvers == null) { @@ -324,7 +336,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport } extension = extension.toLowerCase(Locale.ENGLISH); MediaType mediaType = this.mediaTypes.get(extension); - if (mediaType == null && jafPresent) { + if (mediaType == null && useJaf && jafPresent) { mediaType = ActivationMediaTypeFactory.getMediaType(filename); if (mediaType != null) { this.mediaTypes.putIfAbsent(extension, mediaType); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java index e73a7ab2cbe..c9895bd8167 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java @@ -49,14 +49,24 @@ public class ContentNegotiatingViewResolverTests { } @Test - public void getMediaTypeFromFilename() { - assertEquals("Invalid content type", new MediaType("text", "html"), - viewResolver.getMediaTypeFromFilename("test.html")); + public void getMediaTypeFromFilenameMediaTypes() { viewResolver.setMediaTypes(Collections.singletonMap("HTML", "application/xhtml+xml")); assertEquals("Invalid content type", new MediaType("application", "xhtml+xml"), viewResolver.getMediaTypeFromFilename("test.html")); } + @Test + public void getMediaTypeFromFilenameJaf() { + assertEquals("Invalid content type", new MediaType("text", "html"), + viewResolver.getMediaTypeFromFilename("test.html")); + } + + @Test + public void getMediaTypeFromFilenameNoJaf() { + viewResolver.setUseJaf(false); + assertNull("Invalid content type", viewResolver.getMediaTypeFromFilename("test.html")); + } + @Test public void getMediaTypeFilename() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.html?foo=bar");