Added ContentNegotiationManager(Collection<ContentNegotiationStrategy>) constructor

This commit is contained in:
Juergen Hoeller
2013-02-06 20:52:04 +01:00
committed by unknown
parent 6b82d293d5
commit ab3aa6c8c2
2 changed files with 37 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.web.accept;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
@@ -55,6 +56,7 @@ public class ContentNegotiationManager implements ContentNegotiationStrategy, Me
private final Set<MediaTypeFileExtensionResolver> fileExtensionResolvers =
new LinkedHashSet<MediaTypeFileExtensionResolver>();
/**
* Create an instance with the given ContentNegotiationStrategy instances.
* <p>Each instance is checked to see if it is also an implementation of
@@ -72,12 +74,29 @@ public class ContentNegotiationManager implements ContentNegotiationStrategy, Me
}
/**
* Create an instance with a {@link HeaderContentNegotiationStrategy}.
* Create an instance with the given ContentNegotiationStrategy instances.
* <p>Each instance is checked to see if it is also an implementation of
* MediaTypeFileExtensionResolver, and if so it is registered as such.
* @param strategies one more more ContentNegotiationStrategy instances
*/
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies) {
Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
this.contentNegotiationStrategies.addAll(strategies);
for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) {
if (strategy instanceof MediaTypeFileExtensionResolver) {
this.fileExtensionResolvers.add((MediaTypeFileExtensionResolver) strategy);
}
}
}
/**
* Create a default instance with a {@link HeaderContentNegotiationStrategy}.
*/
public ContentNegotiationManager() {
this(new HeaderContentNegotiationStrategy());
}
/**
* Add MediaTypeFileExtensionResolver instances.
* <p>Note that some {@link ContentNegotiationStrategy} implementations also

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.accept;
import java.util.ArrayList;
@@ -22,7 +23,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.FactoryBean;
@@ -39,13 +39,13 @@ import org.springframework.web.context.ServletContextAware;
* <p>By default strategies for checking the extension of the request path and
* the {@code Accept} header are registered. The path extension check will perform
* lookups through the {@link ServletContext} and the Java Activation Framework
* (if present) unless {@linkplain #setMediaTypes(Properties) media types} are configured.
* (if present) unless {@linkplain #setMediaTypes media types} are configured.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
public class ContentNegotiationManagerFactoryBean
implements FactoryBean<ContentNegotiationManager>, InitializingBean, ServletContextAware {
implements FactoryBean<ContentNegotiationManager>, ServletContextAware, InitializingBean {
private boolean favorPathExtension = true;
@@ -65,6 +65,7 @@ public class ContentNegotiationManagerFactoryBean
private ServletContext servletContext;
/**
* Indicate whether the extension of the request path should be used to determine
* the requested media type with the <em>highest priority</em>.
@@ -81,7 +82,6 @@ public class ContentNegotiationManagerFactoryBean
* <p>When this mapping is not set or when an extension is not found, the Java
* Action Framework, if available, may be used if enabled via
* {@link #setFavorPathExtension(boolean)}.
*
* @see #addMediaType(String, MediaType)
* @see #addMediaTypes(Map)
*/
@@ -121,9 +121,8 @@ public class ContentNegotiationManagerFactoryBean
* to map from file extensions to media types. This is used only when
* {@link #setFavorPathExtension(boolean)} is set to {@code true}.
* <p>The default value is {@code true}.
*
* @see #parameterName
* @see #setMediaTypes(Properties)
* @see #setParameterName
* @see #setMediaTypes
*/
public void setUseJaf(boolean useJaf) {
this.useJaf = useJaf;
@@ -138,8 +137,7 @@ public class ContentNegotiationManagerFactoryBean
* {@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(Properties)}.
*
* @see #setParameterName(String)
* @see #setParameterName
*/
public void setFavorParameter(boolean favorParameter) {
this.favorParameter = favorParameter;
@@ -180,7 +178,8 @@ public class ContentNegotiationManagerFactoryBean
this.servletContext = servletContext;
}
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
List<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>();
if (this.favorPathExtension) {
@@ -210,8 +209,12 @@ public class ContentNegotiationManagerFactoryBean
strategies.add(new FixedContentNegotiationStrategy(this.defaultContentType));
}
ContentNegotiationStrategy[] array = strategies.toArray(new ContentNegotiationStrategy[strategies.size()]);
this.contentNegotiationManager = new ContentNegotiationManager(array);
this.contentNegotiationManager = new ContentNegotiationManager(strategies);
}
public ContentNegotiationManager getObject() {
return this.contentNegotiationManager;
}
public Class<?> getObjectType() {
@@ -222,8 +225,4 @@ public class ContentNegotiationManagerFactoryBean
return true;
}
public ContentNegotiationManager getObject() throws Exception {
return this.contentNegotiationManager;
}
}