Support for multiple default content types
Enhance FixedContentNegotiationStrategy and places where it is exposed to also accept a list of media types. Issue: SPR-15367
This commit is contained in:
committed by
Rossen Stoyanchev
parent
95e78b16f7
commit
4a890226ea
@@ -233,8 +233,8 @@ public class ContentNegotiationManagerFactoryBean
|
||||
* <p>By default this is not set.
|
||||
* @see #setDefaultContentTypeStrategy
|
||||
*/
|
||||
public void setDefaultContentType(MediaType contentType) {
|
||||
this.defaultNegotiationStrategy = new FixedContentNegotiationStrategy(contentType);
|
||||
public void setDefaultContentType(List<MediaType> contentTypes) {
|
||||
this.defaultNegotiationStrategy = new FixedContentNegotiationStrategy(contentTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,12 +16,11 @@
|
||||
|
||||
package org.springframework.web.accept;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
|
||||
@@ -35,23 +34,36 @@ public class FixedContentNegotiationStrategy implements ContentNegotiationStrate
|
||||
|
||||
private static final Log logger = LogFactory.getLog(FixedContentNegotiationStrategy.class);
|
||||
|
||||
private final List<MediaType> contentType;
|
||||
private final List<MediaType> contentTypes;
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance with the given content type.
|
||||
*/
|
||||
public FixedContentNegotiationStrategy(MediaType contentType) {
|
||||
this.contentType = Collections.singletonList(contentType);
|
||||
public FixedContentNegotiationStrategy(MediaType... contentTypes) {
|
||||
this.contentTypes = Arrays.asList(contentTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance with the given content type.
|
||||
*
|
||||
* <p>
|
||||
* List is ordered in the same manner as a "quality" parameter on incoming requests.
|
||||
* If destinations which do not support any of the media types provided are present,
|
||||
* end the list with {@link MediaType#ALL} to allow standard media type determination
|
||||
*/
|
||||
public FixedContentNegotiationStrategy(List<MediaType> contentTypes) {
|
||||
this.contentTypes = contentTypes;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MediaType> resolveMediaTypes(NativeWebRequest request) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Requested media types: " + this.contentType);
|
||||
logger.debug("Requested media types: " + this.contentTypes);
|
||||
}
|
||||
return this.contentType;
|
||||
|
||||
return this.contentTypes;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,13 +16,13 @@
|
||||
|
||||
package org.springframework.web.accept;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
@@ -161,10 +161,10 @@ public class ContentNegotiationManagerFactoryBeanTests {
|
||||
|
||||
assertEquals(Collections.<MediaType>emptyList(), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setDefaultContentType() throws Exception {
|
||||
this.factoryBean.setDefaultContentType(MediaType.APPLICATION_JSON);
|
||||
this.factoryBean.setDefaultContentType(Arrays.asList(MediaType.APPLICATION_JSON));
|
||||
this.factoryBean.afterPropertiesSet();
|
||||
ContentNegotiationManager manager = this.factoryBean.getObject();
|
||||
|
||||
@@ -176,6 +176,21 @@ public class ContentNegotiationManagerFactoryBeanTests {
|
||||
assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON),
|
||||
manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setMultipleDefaultContentTypess() throws Exception {
|
||||
this.factoryBean.setDefaultContentType(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL));
|
||||
this.factoryBean.afterPropertiesSet();
|
||||
ContentNegotiationManager manager = this.factoryBean.getObject();
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL),
|
||||
manager.resolveMediaTypes(this.webRequest));
|
||||
|
||||
// SPR-15367
|
||||
this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL),
|
||||
manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
@Test // SPR-12286
|
||||
public void setDefaultContentTypeWithStrategy() throws Exception {
|
||||
|
||||
@@ -15,8 +15,10 @@
|
||||
*/
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -218,11 +220,18 @@ public class ContentNegotiationConfigurer {
|
||||
|
||||
/**
|
||||
* Set the default content type to use when no content type is requested.
|
||||
* <p>By default this is not set.
|
||||
* <p>
|
||||
* Media types are ordered in the same manner as a "quality" parameter on incoming
|
||||
* requests. If destinations which do not support any of the media types provided are
|
||||
* present, end the list with {@link MediaType#ALL} to allow standard media type
|
||||
* determination
|
||||
* <p>
|
||||
* By default this is not set.
|
||||
*
|
||||
* @see #defaultContentTypeStrategy
|
||||
*/
|
||||
public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentType) {
|
||||
this.factory.setDefaultContentType(defaultContentType);
|
||||
public ContentNegotiationConfigurer defaultContentType(MediaType... defaultContentTypes) {
|
||||
this.factory.setDefaultContentType(Arrays.asList(defaultContentTypes));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -20,7 +20,6 @@ import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
@@ -111,6 +110,14 @@ public class ContentNegotiationConfigurerTests {
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setMultipleDefaultContentTypes() throws Exception {
|
||||
this.configurer.defaultContentType(MediaType.APPLICATION_JSON, MediaType.ALL);
|
||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultContentTypeStrategy() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user