Search in sources :

Example 6 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class ContentNegotiationConfigurerTests method defaultSettings.

@Test
public void defaultSettings() throws Exception {
    ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
    this.servletRequest.setRequestURI("/flower.gif");
    assertEquals("Should be able to resolve file extensions by default", Arrays.asList(MediaType.IMAGE_GIF), manager.resolveMediaTypes(this.webRequest));
    this.servletRequest.setRequestURI("/flower?format=gif");
    this.servletRequest.addParameter("format", "gif");
    assertEquals("Should not resolve request parameters by default", Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
    this.servletRequest.setRequestURI("/flower");
    this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
    assertEquals("Should resolve Accept header by default", Arrays.asList(MediaType.IMAGE_GIF), manager.resolveMediaTypes(this.webRequest));
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) Test(org.junit.Test)

Example 7 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class ContentNegotiationConfigurerTests method ignoreAcceptHeader.

@Test
public void ignoreAcceptHeader() throws Exception {
    this.configurer.ignoreAcceptHeader(true);
    ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
    this.servletRequest.setRequestURI("/flower");
    this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
    assertEquals(Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) Test(org.junit.Test)

Example 8 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class MvcNamespaceTests method testContentNegotiationManager.

@Test
public void testContentNegotiationManager() throws Exception {
    loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 15);
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
    NativeWebRequest webRequest = new ServletWebRequest(request);
    assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));
    ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
    assertNotNull(compositeResolver);
    assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());
    ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
    assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
    ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
    assertSame(manager, cnvr.getContentNegotiationManager());
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) ViewResolver(org.springframework.web.servlet.ViewResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) TilesViewResolver(org.springframework.web.servlet.view.tiles3.TilesViewResolver) Test(org.junit.Test)

Example 9 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class RequestMappingHandlerMappingTests method useRegisteredSuffixPatternMatch.

@Test
public void useRegisteredSuffixPatternMatch() {
    assertTrue(this.handlerMapping.useSuffixPatternMatch());
    assertFalse(this.handlerMapping.useRegisteredSuffixPatternMatch());
    Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
    PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
    ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
    this.handlerMapping.setContentNegotiationManager(manager);
    this.handlerMapping.setUseRegisteredSuffixPatternMatch(true);
    this.handlerMapping.afterPropertiesSet();
    assertTrue(this.handlerMapping.useSuffixPatternMatch());
    assertTrue(this.handlerMapping.useRegisteredSuffixPatternMatch());
    assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MediaType(org.springframework.http.MediaType) PathExtensionContentNegotiationStrategy(org.springframework.web.accept.PathExtensionContentNegotiationStrategy) Test(org.junit.Test)

Example 10 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.setFavorPathExtension(false);
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(new MockServletContext());
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.addHeader("Accept", "application/json,text/plain,*/*");
    this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
    handler.handleRequest(this.request, this.response);
    assertEquals("text/html", this.response.getContentType());
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)24 Test (org.junit.Test)23 MediaType (org.springframework.http.MediaType)8 ViewResolver (org.springframework.web.servlet.ViewResolver)8 Locale (java.util.Locale)7 View (org.springframework.web.servlet.View)7 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 MockServletContext (org.springframework.mock.web.test.MockServletContext)4 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)4 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)4 ClassPathResource (org.springframework.core.io.ClassPathResource)3 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)3 PathExtensionContentNegotiationStrategy (org.springframework.web.accept.PathExtensionContentNegotiationStrategy)3 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)3 BeanNameViewResolver (org.springframework.web.servlet.view.BeanNameViewResolver)3 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Method (java.lang.reflect.Method)2