Work around Servlet dependency in content negotiation

Before this change the PathExtensionContentNegotiationStrategy accessed
the ServletContext via request.getServletContext, which is Servlet 3
specific. To work around it, there is now a Servlet-specific sub-class
that accepts a ServletContext as a constructor argument.

The ContentNegotiationManagerFactoryBean is now ServletContextAware and
if it has a ServletContext it creates the Servlet-specific sub-class
of PathExtensionContentNegotiationStrategy.

The ContentNegotiationManagerFactoryBean is now also used in several
places internally -- MVC namespace, MVC Java config, and the
ContentNegotiatingViewResolver -- to reduce duplication.

Issue: SPR-9826
This commit is contained in:
Rossen Stoyanchev
2012-09-26 09:16:08 -04:00
parent 2bb0104556
commit 7b30ffd522
11 changed files with 227 additions and 187 deletions

View File

@@ -16,10 +16,8 @@
package org.springframework.web.servlet.config;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
@@ -49,8 +47,7 @@ import org.springframework.util.xml.DomUtils;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
@@ -289,34 +286,32 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
contentNegotiationManagerRef = new RuntimeBeanReference(element.getAttribute("content-negotiation-manager"));
}
else {
RootBeanDefinition managerDef = new RootBeanDefinition(ContentNegotiationManager.class);
managerDef.setSource(source);
managerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
PathExtensionContentNegotiationStrategy strategy1 = new PathExtensionContentNegotiationStrategy(getDefaultMediaTypes());
HeaderContentNegotiationStrategy strategy2 = new HeaderContentNegotiationStrategy();
managerDef.getConstructorArgumentValues().addIndexedArgumentValue(0, Arrays.asList(strategy1,strategy2));
RootBeanDefinition factoryBeanDef = new RootBeanDefinition(ContentNegotiationManagerFactoryBean.class);
factoryBeanDef.setSource(source);
factoryBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
factoryBeanDef.getPropertyValues().add("mediaTypes", getDefaultMediaTypes());
String beanName = "mvcContentNegotiationManager";
parserContext.getReaderContext().getRegistry().registerBeanDefinition(beanName , managerDef);
parserContext.registerComponent(new BeanComponentDefinition(managerDef, beanName));
parserContext.getReaderContext().getRegistry().registerBeanDefinition(beanName , factoryBeanDef);
parserContext.registerComponent(new BeanComponentDefinition(factoryBeanDef, beanName));
contentNegotiationManagerRef = new RuntimeBeanReference(beanName);
}
return contentNegotiationManagerRef;
}
private Map<String, MediaType> getDefaultMediaTypes() {
Map<String, MediaType> map = new HashMap<String, MediaType>();
private Properties getDefaultMediaTypes() {
Properties props = new Properties();
if (romePresent) {
map.put("atom", MediaType.APPLICATION_ATOM_XML);
map.put("rss", MediaType.valueOf("application/rss+xml"));
props.put("atom", MediaType.APPLICATION_ATOM_XML_VALUE);
props.put("rss", "application/rss+xml");
}
if (jackson2Present || jacksonPresent) {
map.put("json", MediaType.APPLICATION_JSON);
props.put("json", MediaType.APPLICATION_JSON_VALUE);
}
if (jaxb2Present) {
map.put("xml", MediaType.APPLICATION_XML);
props.put("xml", MediaType.APPLICATION_XML_VALUE);
}
return map;
return props;
}
private RuntimeBeanReference getMessageCodesResolver(Element element, Object source, ParserContext parserContext) {

View File

@@ -15,22 +15,13 @@
*/
package org.springframework.web.servlet.config.annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationStrategy;
import org.springframework.web.accept.FixedContentNegotiationStrategy;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
/**
* Helps with configuring a {@link ContentNegotiationManager}.
@@ -45,19 +36,15 @@ import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
*/
public class ContentNegotiationConfigurer {
private boolean favorPathExtension = true;
private ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
private boolean favorParameter = false;
private boolean ignoreAcceptHeader = false;
private Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>();
private Boolean useJaf;
private String parameterName;
private MediaType defaultContentType;
/**
* Class constructor with {@link javax.servlet.ServletContext}.
*/
public ContentNegotiationConfigurer(ServletContext servletContext) {
this.factoryBean.setServletContext(servletContext);
}
/**
* Indicate whether the extension of the request path should be used to determine
@@ -66,32 +53,29 @@ public class ContentNegotiationConfigurer {
* for {@code /hotels.pdf} will be interpreted as a request for
* {@code "application/pdf"} regardless of the {@code Accept} header.
*/
public ContentNegotiationConfigurer setFavorPathExtension(boolean favorPathExtension) {
this.favorPathExtension = favorPathExtension;
public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension) {
this.factoryBean.setFavorPathExtension(favorPathExtension);
return this;
}
/**
* Add mappings from file extensions to media types.
* <p>If this property is not set, the Java Action Framework, if available, may
* still be used in conjunction with {@link #setFavorPathExtension(boolean)}.
* still be used in conjunction with {@link #favorPathExtension(boolean)}.
*/
public ContentNegotiationConfigurer addMediaType(String extension, MediaType mediaType) {
this.mediaTypes.put(extension, mediaType);
public ContentNegotiationConfigurer mediaType(String extension, MediaType mediaType) {
this.factoryBean.getMediaTypes().put(extension, mediaType);
return this;
}
/**
* Add mappings from file extensions to media types.
* <p>If this property is not set, the Java Action Framework, if available, may
* still be used in conjunction with {@link #setFavorPathExtension(boolean)}.
* still be used in conjunction with {@link #favorPathExtension(boolean)}.
*/
public ContentNegotiationConfigurer addMediaTypes(Map<String, MediaType> mediaTypes) {
if (!CollectionUtils.isEmpty(mediaTypes)) {
for (Map.Entry<String, MediaType> entry : mediaTypes.entrySet()) {
String extension = entry.getKey().toLowerCase(Locale.ENGLISH);
this.mediaTypes.put(extension, entry.getValue());
}
public ContentNegotiationConfigurer mediaTypes(Map<String, MediaType> mediaTypes) {
if (mediaTypes != null) {
this.factoryBean.getMediaTypes().putAll(mediaTypes);
}
return this;
}
@@ -99,29 +83,24 @@ public class ContentNegotiationConfigurer {
/**
* Add mappings from file extensions to media types replacing any previous mappings.
* <p>If this property is not set, the Java Action Framework, if available, may
* still be used in conjunction with {@link #setFavorPathExtension(boolean)}.
* still be used in conjunction with {@link #favorPathExtension(boolean)}.
*/
public ContentNegotiationConfigurer replaceMediaTypes(Map<String, MediaType> mediaTypes) {
this.mediaTypes.clear();
if (!CollectionUtils.isEmpty(mediaTypes)) {
for (Map.Entry<String, MediaType> entry : mediaTypes.entrySet()) {
String extension = entry.getKey().toLowerCase(Locale.ENGLISH);
this.mediaTypes.put(extension, entry.getValue());
}
}
this.factoryBean.getMediaTypes().clear();
mediaTypes(mediaTypes);
return this;
}
/**
* Indicate whether to use the Java Activation Framework as a fallback option
* to map from file extensions to media types. This is used only when
* {@link #setFavorPathExtension(boolean)} is set to {@code true}.
* {@link #favorPathExtension(boolean)} is set to {@code true}.
* <p>The default value is {@code true}.
* @see #parameterName
* @see #setMediaTypes(Map)
*/
public ContentNegotiationConfigurer setUseJaf(boolean useJaf) {
this.useJaf = useJaf;
public ContentNegotiationConfigurer useJaf(boolean useJaf) {
this.factoryBean.setUseJaf(useJaf);
return this;
}
@@ -134,10 +113,10 @@ public class ContentNegotiationConfigurer {
* {@code "application/pdf"} regardless of the {@code Accept} header.
* <p>To use this option effectively you must also configure the MediaType
* type mappings via {@link #setMediaTypes(Map)}.
* @see #setParameterName(String)
* @see #parameterName(String)
*/
public ContentNegotiationConfigurer setFavorParameter(boolean favorParameter) {
this.favorParameter = favorParameter;
public ContentNegotiationConfigurer favorParameter(boolean favorParameter) {
this.factoryBean.setFavorParameter(favorParameter);
return this;
}
@@ -146,8 +125,8 @@ public class ContentNegotiationConfigurer {
* if the {@link #setFavorParameter} property is {@code true}.
* <p>The default parameter name is {@code "format"}.
*/
public ContentNegotiationConfigurer setParameterName(String parameterName) {
this.parameterName = parameterName;
public ContentNegotiationConfigurer parameterName(String parameterName) {
this.factoryBean.setParameterName(parameterName);
return this;
}
@@ -158,8 +137,8 @@ public class ContentNegotiationConfigurer {
* possibly a request parameter if configured.
* <p>By default this value is set to {@code false}.
*/
public ContentNegotiationConfigurer setIgnoreAcceptHeader(boolean ignoreAcceptHeader) {
this.ignoreAcceptHeader = ignoreAcceptHeader;
public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader) {
this.factoryBean.setIgnoreAcceptHeader(ignoreAcceptHeader);
return this;
}
@@ -169,36 +148,17 @@ public class ContentNegotiationConfigurer {
* nor a request parameter, nor the {@code Accept} header could help determine
* the requested content type.
*/
public ContentNegotiationConfigurer setDefaultContentType(MediaType defaultContentType) {
this.defaultContentType = defaultContentType;
public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentType) {
this.factoryBean.setDefaultContentType(defaultContentType);
return this;
}
/**
* @return the configured {@link ContentNegotiationManager} instance
* Return the configured {@link ContentNegotiationManager} instance
*/
protected ContentNegotiationManager getContentNegotiationManager() {
List<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>();
if (this.favorPathExtension) {
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
if (this.useJaf != null) {
strategy.setUseJaf(this.useJaf);
}
strategies.add(strategy);
}
if (this.favorParameter) {
ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes);
strategy.setParameterName(this.parameterName);
strategies.add(strategy);
}
if (!this.ignoreAcceptHeader) {
strategies.add(new HeaderContentNegotiationStrategy());
}
if (this.defaultContentType != null) {
strategies.add(new FixedContentNegotiationStrategy(this.defaultContentType));
}
ContentNegotiationStrategy[] array = strategies.toArray(new ContentNegotiationStrategy[strategies.size()]);
return new ContentNegotiationManager(array);
protected ContentNegotiationManager getContentNegotiationManager() throws Exception {
this.factoryBean.afterPropertiesSet();
return this.factoryBean.getObject();
}
}

View File

@@ -167,10 +167,18 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
private List<HttpMessageConverter<?>> messageConverters;
/**
* Set the {@link javax.servlet.ServletContext}, e.g. for resource handling,
* looking up file extensions, etc.
*/
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/**
* Set the Spring {@link ApplicationContext}, e.g. for resource loading.
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@@ -219,10 +227,15 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
@Bean
public ContentNegotiationManager mvcContentNegotiationManager() {
if (this.contentNegotiationManager == null) {
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer();
configurer.addMediaTypes(getDefaultMediaTypes());
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext);
configurer.mediaTypes(getDefaultMediaTypes());
configureContentNegotiation(configurer);
this.contentNegotiationManager = configurer.getContentNegotiationManager();
try {
this.contentNegotiationManager = configurer.getContentNegotiationManager();
}
catch (Exception e) {
throw new BeanInitializationException("Could not create ContentNegotiationManager", e);
}
}
return this.contentNegotiationManager;
}
@@ -398,9 +411,11 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
try {
String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean";
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
} catch (ClassNotFoundException e) {
}
catch (ClassNotFoundException e) {
throw new BeanInitializationException("Could not find default validator", e);
} catch (LinkageError e) {
}
catch (LinkageError e) {
throw new BeanInitializationException("Could not find default validator", e);
}
validator = (Validator) BeanUtils.instantiate(clazz);

View File

@@ -19,7 +19,6 @@ package org.springframework.web.servlet.view;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
@@ -43,11 +42,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.FixedContentNegotiationStrategy;
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.accept.ContentNegotiationStrategy;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -99,13 +94,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
private ContentNegotiationManager contentNegotiationManager;
private boolean favorPathExtension = true;
private boolean favorParameter = false;
private boolean ignoreAcceptHeader = false;
private Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>();
private Boolean useJaf;
private String parameterName;
private MediaType defaultContentType;
private ContentNegotiationManagerFactoryBean cnManagerFactoryBean = new ContentNegotiationManagerFactoryBean();
private boolean useNotAcceptableStatusCode = false;
@@ -144,7 +133,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* @deprecated use {@link #setContentNegotiationManager(ContentNegotiationManager)}
*/
public void setFavorPathExtension(boolean favorPathExtension) {
this.favorPathExtension = favorPathExtension;
this.cnManagerFactoryBean.setFavorParameter(favorPathExtension);
}
/**
@@ -154,7 +143,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* @deprecated use {@link #setContentNegotiationManager(ContentNegotiationManager)}
*/
public void setUseJaf(boolean useJaf) {
this.useJaf = useJaf;
this.cnManagerFactoryBean.setUseJaf(useJaf);
}
/**
@@ -167,7 +156,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* @deprecated use {@link #setContentNegotiationManager(ContentNegotiationManager)}
*/
public void setFavorParameter(boolean favorParameter) {
this.favorParameter = favorParameter;
this.cnManagerFactoryBean.setFavorParameter(favorParameter);
}
/**
@@ -177,7 +166,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* @deprecated use {@link #setContentNegotiationManager(ContentNegotiationManager)}
*/
public void setParameterName(String parameterName) {
this.parameterName = parameterName;
this.cnManagerFactoryBean.setParameterName(parameterName);
}
/**
@@ -189,7 +178,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* @deprecated use {@link #setContentNegotiationManager(ContentNegotiationManager)}
*/
public void setIgnoreAcceptHeader(boolean ignoreAcceptHeader) {
this.ignoreAcceptHeader = ignoreAcceptHeader;
this.cnManagerFactoryBean.setIgnoreAcceptHeader(ignoreAcceptHeader);
}
/**
@@ -201,11 +190,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
*/
public void setMediaTypes(Map<String, String> mediaTypes) {
if (mediaTypes != null) {
for (Map.Entry<String, String> entry : mediaTypes.entrySet()) {
String extension = entry.getKey().toLowerCase(Locale.ENGLISH);
MediaType mediaType = MediaType.parseMediaType(entry.getValue());
this.mediaTypes.put(extension, mediaType);
}
this.cnManagerFactoryBean.getMediaTypes().putAll(mediaTypes);
}
}
@@ -217,7 +202,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* @deprecated use {@link #setContentNegotiationManager(ContentNegotiationManager)}
*/
public void setDefaultContentType(MediaType defaultContentType) {
this.defaultContentType = defaultContentType;
this.cnManagerFactoryBean.setDefaultContentType(defaultContentType);
}
/**
@@ -277,31 +262,13 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
"'viewResolvers' property on the ContentNegotiatingViewResolver");
}
OrderComparator.sort(this.viewResolvers);
this.cnManagerFactoryBean.setServletContext(servletContext);
}
public void afterPropertiesSet() throws Exception {
if (this.contentNegotiationManager == null) {
List<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>();
if (this.favorPathExtension) {
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
if (this.useJaf != null) {
strategy.setUseJaf(this.useJaf);
}
strategies.add(strategy);
}
if (this.favorParameter) {
ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes);
strategy.setParameterName(this.parameterName);
strategies.add(strategy);
}
if (!this.ignoreAcceptHeader) {
strategies.add(new HeaderContentNegotiationStrategy());
}
if (this.defaultContentType != null) {
strategies.add(new FixedContentNegotiationStrategy(this.defaultContentType));
}
ContentNegotiationStrategy[] array = strategies.toArray(new ContentNegotiationStrategy[strategies.size()]);
this.contentNegotiationManager = new ContentNegotiationManager(array);
this.cnManagerFactoryBean.afterPropertiesSet();
this.contentNegotiationManager = this.cnManagerFactoryBean.getObject();
}
}