Work around Servlet dependency in content negotiation
Before this change the PathExtensionContentNegotiationStrategy accessed the ServletContext via request.getServletContext, which is Servlet 3 specific. To work around it, there is now a Servlet-specific sub-class that accepts a ServletContext as a constructor argument. The ContentNegotiationManagerFactoryBean is now ServletContextAware and if it has a ServletContext it creates the Servlet-specific sub-class of PathExtensionContentNegotiationStrategy. The ContentNegotiationManagerFactoryBean is now also used in several places internally -- MVC namespace, MVC Java config, and the ContentNegotiatingViewResolver -- to reduce duplication. Issue: SPR-9826
This commit is contained in:
@@ -42,9 +42,11 @@ public class ContentNegotiationManagerFactoryBeanTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.factoryBean = new ContentNegotiationManagerFactoryBean();
|
||||
this.servletRequest = new MockHttpServletRequest();
|
||||
this.webRequest = new ServletWebRequest(this.servletRequest);
|
||||
|
||||
this.factoryBean = new ContentNegotiationManagerFactoryBean();
|
||||
this.factoryBean.setServletContext(this.servletRequest.getServletContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -22,6 +22,8 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -44,7 +46,7 @@ public class PathExtensionContentNegotiationStrategyTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.servletRequest = new MockHttpServletRequest();
|
||||
this.webRequest = new ServletWebRequest(servletRequest );
|
||||
this.webRequest = new ServletWebRequest(servletRequest);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -74,8 +76,12 @@ public class PathExtensionContentNegotiationStrategyTests {
|
||||
|
||||
@Test
|
||||
public void getMediaTypeFromFilenameNoJaf() {
|
||||
|
||||
this.servletRequest.setRequestURI("test.xls");
|
||||
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy();
|
||||
|
||||
ServletContext servletContext = this.servletRequest.getServletContext();
|
||||
PathExtensionContentNegotiationStrategy strategy =
|
||||
new ServletPathExtensionContentNegotiationStrategy(servletContext);
|
||||
strategy.setUseJaf(false);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
Reference in New Issue
Block a user