SPR-7081 - Add ignoreJafMediaTypes to ContentNegotiatingViewResolver
This commit is contained in:
parent
385298b808
commit
e6b0a1d4d7
|
|
@ -72,7 +72,8 @@ import org.springframework.web.util.WebUtils;
|
||||||
* media type. The default name of the parameter is <code>format</code> and it can be configured using the
|
* media type. The default name of the parameter is <code>format</code> and it can be configured using the
|
||||||
* {@link #setParameterName(String) parameterName} property.</li>
|
* {@link #setParameterName(String) parameterName} property.</li>
|
||||||
* <li>If there is no match in the {@link #setMediaTypes(Map) mediaTypes} property and if the Java Activation
|
* <li>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.</li>
|
* Framework (JAF) is both {@linkplain #setUseJaf(boolean) enabled} and present on the class path,
|
||||||
|
* {@link FileTypeMap#getContentType(String)} is used instead.</li>
|
||||||
* <li>If the previous steps did not result in a media type, and
|
* <li>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
|
* {@link #setIgnoreAcceptHeader(boolean) ignoreAcceptHeader} is {@code false}, the request {@code Accept} header is
|
||||||
* used.</li>
|
* used.</li>
|
||||||
|
|
@ -123,6 +124,8 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||||
|
|
||||||
private boolean ignoreAcceptHeader = false;
|
private boolean ignoreAcceptHeader = false;
|
||||||
|
|
||||||
|
private boolean useJaf = true;
|
||||||
|
|
||||||
private ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<String, MediaType>();
|
private ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<String, MediaType>();
|
||||||
|
|
||||||
private List<View> defaultViews;
|
private List<View> defaultViews;
|
||||||
|
|
@ -224,6 +227,14 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||||
this.defaultContentType = defaultContentType;
|
this.defaultContentType = defaultContentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether to use the Java Activation Framework to map from file extensions to media types.
|
||||||
|
* <p>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.
|
* Sets the view resolvers to be wrapped by this view resolver.
|
||||||
* <p>If this property is not set, view resolvers will be detected automatically.
|
* <p>If this property is not set, view resolvers will be detected automatically.
|
||||||
|
|
@ -232,6 +243,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||||
this.viewResolvers = viewResolvers;
|
this.viewResolvers = viewResolvers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initServletContext(ServletContext servletContext) {
|
protected void initServletContext(ServletContext servletContext) {
|
||||||
if (this.viewResolvers == null) {
|
if (this.viewResolvers == null) {
|
||||||
|
|
@ -324,7 +336,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||||
}
|
}
|
||||||
extension = extension.toLowerCase(Locale.ENGLISH);
|
extension = extension.toLowerCase(Locale.ENGLISH);
|
||||||
MediaType mediaType = this.mediaTypes.get(extension);
|
MediaType mediaType = this.mediaTypes.get(extension);
|
||||||
if (mediaType == null && jafPresent) {
|
if (mediaType == null && useJaf && jafPresent) {
|
||||||
mediaType = ActivationMediaTypeFactory.getMediaType(filename);
|
mediaType = ActivationMediaTypeFactory.getMediaType(filename);
|
||||||
if (mediaType != null) {
|
if (mediaType != null) {
|
||||||
this.mediaTypes.putIfAbsent(extension, mediaType);
|
this.mediaTypes.putIfAbsent(extension, mediaType);
|
||||||
|
|
|
||||||
|
|
@ -49,14 +49,24 @@ public class ContentNegotiatingViewResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getMediaTypeFromFilename() {
|
public void getMediaTypeFromFilenameMediaTypes() {
|
||||||
assertEquals("Invalid content type", new MediaType("text", "html"),
|
|
||||||
viewResolver.getMediaTypeFromFilename("test.html"));
|
|
||||||
viewResolver.setMediaTypes(Collections.singletonMap("HTML", "application/xhtml+xml"));
|
viewResolver.setMediaTypes(Collections.singletonMap("HTML", "application/xhtml+xml"));
|
||||||
assertEquals("Invalid content type", new MediaType("application", "xhtml+xml"),
|
assertEquals("Invalid content type", new MediaType("application", "xhtml+xml"),
|
||||||
viewResolver.getMediaTypeFromFilename("test.html"));
|
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
|
@Test
|
||||||
public void getMediaTypeFilename() {
|
public void getMediaTypeFilename() {
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.html?foo=bar");
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test.html?foo=bar");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue