org.springframework.web.accept.ContentNegotiationManagerFactoryBean Maven / Gradle / Ivy
/*
* Copyright 2002-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.accept;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.ServletContextAware;
/**
* Factory to create a {@code ContentNegotiationManager} and configure it with
* one or more {@link ContentNegotiationStrategy} instances via simple setters.
* The following table shows setters, resulting strategy instances, and if in
* use by default:
*
*
*
* Property Setter
* Underlying Strategy
* Default Setting
*
*
* {@link #setFavorPathExtension}
* {@link PathExtensionContentNegotiationStrategy Path Extension strategy}
* On
*
*
* {@link #setFavorParameter favorParameter}
* {@link ParameterContentNegotiationStrategy Parameter strategy}
* Off
*
*
* {@link #setIgnoreAcceptHeader ignoreAcceptHeader}
* {@link HeaderContentNegotiationStrategy Header strategy}
* On
*
*
* {@link #setDefaultContentType defaultContentType}
* {@link FixedContentNegotiationStrategy Fixed content strategy}
* Not set
*
*
* {@link #setDefaultContentTypeStrategy defaultContentTypeStrategy}
* {@link ContentNegotiationStrategy}
* Not set
*
*
*
* The order in which strategies are configured is fixed. Setters may only
* turn individual strategies on or off. If you need a custom order for any
* reason simply instantiate {@code ContentNegotiationManager} directly.
*
*
For the path extension and parameter strategies you may explicitly add
* {@link #setMediaTypes MediaType mappings}. This will be used to resolve path
* extensions or a parameter value such as "json" to a media type such as
* "application/json".
*
*
The path extension strategy will also use {@link ServletContext#getMimeType}
* and the Java Activation framework (JAF), if available, to resolve a path
* extension to a MediaType. You may {@link #setUseJaf suppress} the use of JAF.
*
* @author Rossen Stoyanchev
* @author Brian Clozel
* @since 3.2
*/
public class ContentNegotiationManagerFactoryBean
implements FactoryBean, ServletContextAware, InitializingBean {
private boolean favorPathExtension = true;
private boolean favorParameter = false;
private boolean ignoreAcceptHeader = false;
private Map mediaTypes = new HashMap();
private boolean ignoreUnknownPathExtensions = true;
private Boolean useJaf;
private String parameterName = "format";
private ContentNegotiationStrategy defaultNegotiationStrategy;
private ContentNegotiationManager contentNegotiationManager;
private ServletContext servletContext;
/**
* Whether the path extension in the URL path should be used to determine
* the requested media type.
* By default this is set to {@code true} in which case a request
* for {@code /hotels.pdf} will be interpreted as a request for
* {@code "application/pdf"} regardless of the 'Accept' header.
*/
public void setFavorPathExtension(boolean favorPathExtension) {
this.favorPathExtension = favorPathExtension;
}
/**
* Add a mapping from a key, extracted from a path extension or a query
* parameter, to a MediaType. This is required in order for the parameter
* strategy to work. Any extensions explicitly registered here are also
* whitelisted for the purpose of Reflected File Download attack detection
* (see Spring Framework reference documentation for more details on RFD
* attack protection).
*
The path extension strategy will also try to use
* {@link ServletContext#getMimeType} and JAF (if present) to resolve path
* extensions. To change this behavior see the {@link #useJaf} property.
* @param mediaTypes media type mappings
* @see #addMediaType(String, MediaType)
* @see #addMediaTypes(Map)
*/
public void setMediaTypes(Properties mediaTypes) {
if (!CollectionUtils.isEmpty(mediaTypes)) {
for (Map.Entry