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:
Rossen Stoyanchev
2012-09-26 09:16:08 -04:00
parent 2bb0104556
commit 7b30ffd522
11 changed files with 227 additions and 187 deletions

View File

@@ -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

View File

@@ -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);