Add abstractions for content negotiation
Introduced ContentNeogtiationStrategy for resolving the requested media types from an incoming request. The available implementations are based on path extension, request parameter, 'Accept' header, and a fixed default content type. The logic for these implementations is based on equivalent options, previously available only in the ContentNegotiatingViewResolver. Also in this commit is ContentNegotiationManager, the central class to use when configuring content negotiation options. It accepts one or more ContentNeogtiationStrategy instances and delegates to them. The ContentNeogiationManager can now be used to configure the following classes: - RequestMappingHandlerMappingm - RequestMappingHandlerAdapter - ExceptionHandlerExceptionResolver - ContentNegotiatingViewResolver Issue: SPR-8410, SPR-8417, SPR-8418,SPR-8416, SPR-8419,SPR-7722
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
|
||||
/**
|
||||
* A test fixture with a test sub-class of AbstractMappingContentNegotiationStrategy.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.2
|
||||
*/
|
||||
public class AbstractMappingContentNegotiationStrategyTests {
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypes() {
|
||||
Map<String, String> mapping = Collections.singletonMap("json", "application/json");
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("json", mapping);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
|
||||
|
||||
assertEquals(1, mediaTypes.size());
|
||||
assertEquals("application/json", mediaTypes.get(0).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypesNoMatch() {
|
||||
Map<String, String> mapping = null;
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("blah", mapping);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
|
||||
|
||||
assertEquals(0, mediaTypes.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypesNoKey() {
|
||||
Map<String, String> mapping = Collections.singletonMap("json", "application/json");
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy(null, mapping);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
|
||||
|
||||
assertEquals(0, mediaTypes.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypesHandleNoMatch() {
|
||||
Map<String, String> mapping = null;
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("xml", mapping);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
|
||||
|
||||
assertEquals(1, mediaTypes.size());
|
||||
assertEquals("application/xml", mediaTypes.get(0).toString());
|
||||
}
|
||||
|
||||
|
||||
private static class TestMappingContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
|
||||
|
||||
private final String extension;
|
||||
|
||||
public TestMappingContentNegotiationStrategy(String extension, Map<String, String> mapping) {
|
||||
super(mapping);
|
||||
this.extension = extension;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMediaTypeKey(NativeWebRequest request) {
|
||||
return this.extension;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MediaType handleNoMatch(NativeWebRequest request, String mappingKey) {
|
||||
return "xml".equals(mappingKey) ? MediaType.APPLICATION_XML : null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
/**
|
||||
* Test fixture for HeaderContentNegotiationStrategy tests.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class HeaderContentNegotiationStrategyTests {
|
||||
|
||||
private HeaderContentNegotiationStrategy strategy;
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
|
||||
private MockHttpServletRequest servletRequest;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.strategy = new HeaderContentNegotiationStrategy();
|
||||
this.servletRequest = new MockHttpServletRequest();
|
||||
this.webRequest = new ServletWebRequest(servletRequest );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypes() throws Exception {
|
||||
this.servletRequest.addHeader("Accept", "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c");
|
||||
List<MediaType> mediaTypes = this.strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
assertEquals(4, mediaTypes.size());
|
||||
assertEquals("text/html", mediaTypes.get(0).toString());
|
||||
assertEquals("text/x-c", mediaTypes.get(1).toString());
|
||||
assertEquals("text/x-dvi;q=0.8", mediaTypes.get(2).toString());
|
||||
assertEquals("text/plain;q=0.5", mediaTypes.get(3).toString());
|
||||
}
|
||||
|
||||
@Test(expected=HttpMediaTypeNotAcceptableException.class)
|
||||
public void resolveMediaTypesParseError() throws Exception {
|
||||
this.servletRequest.addHeader("Accept", "textplain; q=0.5");
|
||||
this.strategy.resolveMediaTypes(this.webRequest);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* Test fixture for MappingMediaTypeExtensionsResolver.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MappingMediaTypeExtensionsResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveExtensions() {
|
||||
Map<String, String> mapping = Collections.singletonMap("json", "application/json");
|
||||
MappingMediaTypeExtensionsResolver resolver = new MappingMediaTypeExtensionsResolver(mapping);
|
||||
List<String> extensions = resolver.resolveExtensions(MediaType.APPLICATION_JSON);
|
||||
|
||||
assertEquals(1, extensions.size());
|
||||
assertEquals("json", extensions.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
/**
|
||||
* A test fixture for PathExtensionContentNegotiationStrategy.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.2
|
||||
*/
|
||||
public class PathExtensionContentNegotiationStrategyTests {
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
|
||||
private MockHttpServletRequest servletRequest;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.servletRequest = new MockHttpServletRequest();
|
||||
this.webRequest = new ServletWebRequest(servletRequest );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypesFromMapping() {
|
||||
this.servletRequest.setRequestURI("test.html");
|
||||
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy();
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
assertEquals(Arrays.asList(new MediaType("text", "html")), mediaTypes);
|
||||
|
||||
strategy = new PathExtensionContentNegotiationStrategy(Collections.singletonMap("HTML", "application/xhtml+xml"));
|
||||
mediaTypes = strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
assertEquals(Arrays.asList(new MediaType("application", "xhtml+xml")), mediaTypes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypesFromJaf() {
|
||||
this.servletRequest.setRequestURI("test.xls");
|
||||
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy();
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
assertEquals(Arrays.asList(new MediaType("application", "vnd.ms-excel")), mediaTypes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMediaTypeFromFilenameNoJaf() {
|
||||
this.servletRequest.setRequestURI("test.xls");
|
||||
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy();
|
||||
strategy.setUseJaf(false);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM), mediaTypes);
|
||||
}
|
||||
|
||||
// SPR-8678
|
||||
|
||||
@Test
|
||||
public void getMediaTypeFilenameWithContextPath() {
|
||||
this.servletRequest.setContextPath("/project-1.0.0.M3");
|
||||
this.servletRequest.setRequestURI("/project-1.0.0.M3/");
|
||||
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy();
|
||||
|
||||
assertTrue("Context path should be excluded", strategy.resolveMediaTypes(webRequest).isEmpty());
|
||||
|
||||
this.servletRequest.setRequestURI("/project-1.0.0.M3");
|
||||
|
||||
assertTrue("Context path should be excluded", strategy.resolveMediaTypes(webRequest).isEmpty());
|
||||
}
|
||||
|
||||
// SPR-9390
|
||||
|
||||
@Test
|
||||
public void getMediaTypeFilenameWithEncodedURI() {
|
||||
this.servletRequest.setRequestURI("/quo%20vadis%3f.html");
|
||||
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy();
|
||||
|
||||
List<MediaType> result = strategy.resolveMediaTypes(webRequest);
|
||||
|
||||
assertEquals("Invalid content type", Collections.singletonList(new MediaType("text", "html")), result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user