Skip to content

Commit

Permalink
Added ContentNegotiationManager(Collection<ContentNegotiationStrategy…
Browse files Browse the repository at this point in the history
…>) constructor
  • Loading branch information
jhoeller authored and unknown committed Feb 6, 2013
1 parent 6b82d29 commit ab3aa6c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -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
Expand All @@ -72,12 +74,29 @@ public ContentNegotiationManager(ContentNegotiationStrategy... strategies) {
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -22,7 +23,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.FactoryBean;
Expand All @@ -39,13 +39,13 @@
* <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;

Expand All @@ -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>.
Expand All @@ -81,7 +82,6 @@ public void setFavorPathExtension(boolean favorPathExtension) {
* <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)
*/
Expand Down Expand Up @@ -121,9 +121,8 @@ public void addMediaTypes(Map<String, MediaType> mediaTypes) {
* 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;
Expand All @@ -138,8 +137,7 @@ public void setUseJaf(boolean useJaf) {
* {@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;
Expand Down Expand Up @@ -180,7 +178,8 @@ public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}

public void afterPropertiesSet() throws Exception {

public void afterPropertiesSet() {
List<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>();

if (this.favorPathExtension) {
Expand Down Expand Up @@ -210,8 +209,12 @@ public void afterPropertiesSet() throws Exception {
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() {
Expand All @@ -222,8 +225,4 @@ public boolean isSingleton() {
return true;
}

public ContentNegotiationManager getObject() throws Exception {
return this.contentNegotiationManager;
}

}

0 comments on commit ab3aa6c

Please sign in to comment.