SPR-7081 - Add ignoreJafMediaTypes to ContentNegotiatingViewResolver

This commit is contained in:
Arjen Poutsma 2010-04-13 08:37:59 +00:00
parent 385298b808
commit e6b0a1d4d7
2 changed files with 27 additions and 5 deletions

View File

@ -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
* {@link #setParameterName(String) parameterName} property.</li>
* <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
* {@link #setIgnoreAcceptHeader(boolean) ignoreAcceptHeader} is {@code false}, the request {@code Accept} header is
* used.</li>
@ -123,6 +124,8 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
private boolean ignoreAcceptHeader = false;
private boolean useJaf = true;
private ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<String, MediaType>();
private List<View> 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.
* <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.
* <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;
}
@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);

View File

@ -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");