diff --git a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java index 961ffdcc72..0024eef27d 100644 --- a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java +++ b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java @@ -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 fileExtensionResolvers = new LinkedHashSet(); + /** * Create an instance with the given ContentNegotiationStrategy instances. *

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. + *

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 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. *

Note that some {@link ContentNegotiationStrategy} implementations also diff --git a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java index 8cf79f927d..a5d0357d91 100644 --- a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java +++ b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java @@ -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; *

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, InitializingBean, ServletContextAware { + implements FactoryBean, 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 highest priority. @@ -81,7 +82,6 @@ public class ContentNegotiationManagerFactoryBean *

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}. *

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. *

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 strategies = new ArrayList(); 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; - } - }