Search in sources :

Example 11 with ContentNegotiationManager

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

the class ContentNegotiatingViewResolverTests method resolveViewNameFilenameDefaultView.

@Test
public void resolveViewNameFilenameDefaultView() throws Exception {
    request.setRequestURI("/test.json");
    Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
    PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));
    ViewResolver viewResolverMock1 = mock(ViewResolver.class);
    ViewResolver viewResolverMock2 = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
    View viewMock1 = mock(View.class, "application_xml");
    View viewMock2 = mock(View.class, "text_html");
    View viewMock3 = mock(View.class, "application_json");
    List<View> defaultViews = new ArrayList<>();
    defaultViews.add(viewMock3);
    viewResolver.setDefaultViews(defaultViews);
    viewResolver.afterPropertiesSet();
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
    given(viewResolverMock1.resolveViewName(viewName + ".json", locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
    given(viewResolverMock2.resolveViewName(viewName + ".json", locale)).willReturn(null);
    given(viewMock1.getContentType()).willReturn("application/xml");
    given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
    given(viewMock3.getContentType()).willReturn("application/json");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock3, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) PathExtensionContentNegotiationStrategy(org.springframework.web.accept.PathExtensionContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 12 with ContentNegotiationManager

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

the class ViewResolutionTests method testContentNegotiation.

@Test
public void testContentNegotiation() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Person.class);
    List<View> viewList = new ArrayList<>();
    viewList.add(new MappingJackson2JsonView());
    viewList.add(new MarshallingView(marshaller));
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
    ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
    cnViewResolver.setDefaultViews(viewList);
    cnViewResolver.setContentNegotiationManager(manager);
    cnViewResolver.afterPropertiesSet();
    MockMvc mockMvc = standaloneSetup(new PersonController()).setViewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
    mockMvc.perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.person.name").value("Corea"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
Also used : ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) View(org.springframework.web.servlet.View) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MockMvc(org.springframework.test.web.servlet.MockMvc) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.Test)

Example 13 with ContentNegotiationManager

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

the class RequestMappingHandlerMappingTests method useRegisteredSuffixPatternMatchInitialization.

@Test
public void useRegisteredSuffixPatternMatchInitialization() {
    Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
    PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
    ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
    final Set<String> extensions = new HashSet<>();
    RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() {

        @Override
        protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
            extensions.addAll(getFileExtensions());
            return super.getMappingForMethod(method, handlerType);
        }
    };
    wac.registerSingleton("testController", ComposedAnnotationController.class);
    wac.refresh();
    hm.setContentNegotiationManager(manager);
    hm.setUseRegisteredSuffixPatternMatch(true);
    hm.setApplicationContext(wac);
    hm.afterPropertiesSet();
    assertEquals(Collections.singleton("json"), extensions);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MediaType(org.springframework.http.MediaType) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) PathExtensionContentNegotiationStrategy(org.springframework.web.accept.PathExtensionContentNegotiationStrategy) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with ContentNegotiationManager

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

the class ResourceHttpRequestHandlerTests method getResourceWithRegisteredMediaType.

// SPR-13658
@Test
public void getResourceWithRegisteredMediaType() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.addMediaType("css", new MediaType("foo", "bar"));
    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.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
    handler.handleRequest(this.request, this.response);
    assertEquals("foo/bar", this.response.getContentType());
    assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
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) MediaType(org.springframework.http.MediaType) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 15 with ContentNegotiationManager

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

the class ContentNegotiationConfigurerTests method setDefaultContentTypeStrategy.

@Test
public void setDefaultContentTypeStrategy() throws Exception {
    this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
    ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
    assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) 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