Search in sources :

Example 1 with HeaderContentNegotiationStrategy

use of org.springframework.web.accept.HeaderContentNegotiationStrategy 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 2 with HeaderContentNegotiationStrategy

use of org.springframework.web.accept.HeaderContentNegotiationStrategy in project spring-security by spring-projects.

the class RequestCacheConfigurer method createDefaultSavedRequestMatcher.

@SuppressWarnings("unchecked")
private RequestMatcher createDefaultSavedRequestMatcher(H http) {
    ContentNegotiationStrategy contentNegotiationStrategy = http.getSharedObject(ContentNegotiationStrategy.class);
    if (contentNegotiationStrategy == null) {
        contentNegotiationStrategy = new HeaderContentNegotiationStrategy();
    }
    RequestMatcher notFavIcon = new NegatedRequestMatcher(new AntPathRequestMatcher("/**/favicon.ico"));
    MediaTypeRequestMatcher jsonRequest = new MediaTypeRequestMatcher(contentNegotiationStrategy, MediaType.APPLICATION_JSON);
    jsonRequest.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
    RequestMatcher notJson = new NegatedRequestMatcher(jsonRequest);
    RequestMatcher notXRequestedWith = new NegatedRequestMatcher(new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
    boolean isCsrfEnabled = http.getConfigurer(CsrfConfigurer.class) != null;
    List<RequestMatcher> matchers = new ArrayList<RequestMatcher>();
    if (isCsrfEnabled) {
        RequestMatcher getRequests = new AntPathRequestMatcher("/**", "GET");
        matchers.add(0, getRequests);
    }
    matchers.add(notFavIcon);
    matchers.add(notJson);
    matchers.add(notXRequestedWith);
    return new AndRequestMatcher(matchers);
}
Also used : NegatedRequestMatcher(org.springframework.security.web.util.matcher.NegatedRequestMatcher) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher) NegatedRequestMatcher(org.springframework.security.web.util.matcher.NegatedRequestMatcher) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) RequestHeaderRequestMatcher(org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) ArrayList(java.util.ArrayList) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) ContentNegotiationStrategy(org.springframework.web.accept.ContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) RequestHeaderRequestMatcher(org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher) AndRequestMatcher(org.springframework.security.web.util.matcher.AndRequestMatcher)

Example 3 with HeaderContentNegotiationStrategy

use of org.springframework.web.accept.HeaderContentNegotiationStrategy in project coffeenet-starter by coffeenet.

the class IntegrationCoffeeNetWebSecurityConfigurerAdapter method mediaTypeRequestMatcher.

private static MediaTypeRequestMatcher mediaTypeRequestMatcher(final ContentNegotiationStrategy contentNegotiationStrategy) {
    ContentNegotiationStrategy negotiationStrategy = contentNegotiationStrategy;
    if (negotiationStrategy == null) {
        negotiationStrategy = new HeaderContentNegotiationStrategy();
    }
    MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, APPLICATION_XHTML_XML, new MediaType("image", "*"), TEXT_HTML, TEXT_PLAIN);
    matcher.setIgnoredMediaTypes(singleton(ALL));
    return matcher;
}
Also used : MediaType(org.springframework.http.MediaType) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MediaTypeRequestMatcher(org.springframework.security.web.util.matcher.MediaTypeRequestMatcher) ContentNegotiationStrategy(org.springframework.web.accept.ContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy)

Example 4 with HeaderContentNegotiationStrategy

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

the class ContentNegotiatingViewResolverTests method resolveViewNameWithAcceptHeader.

@Test
public void resolveViewNameWithAcceptHeader() throws Exception {
    request.addHeader("Accept", "application/vnd.ms-excel");
    Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
    MappingMediaTypeFileExtensionResolver extensionsResolver = new MappingMediaTypeFileExtensionResolver(mapping);
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy());
    manager.addFileExtensionResolvers(extensionsResolver);
    viewResolver.setContentNegotiationManager(manager);
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    View viewMock = mock(View.class, "application_xls");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MappingMediaTypeFileExtensionResolver(org.springframework.web.accept.MappingMediaTypeFileExtensionResolver) MediaType(org.springframework.http.MediaType) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 5 with HeaderContentNegotiationStrategy

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

the class ContentNegotiatingViewResolverTests method resolveViewNameAcceptHeaderSortByQuality.

// SPR-9160
@Test
public void resolveViewNameAcceptHeaderSortByQuality() throws Exception {
    request.addHeader("Accept", "text/plain;q=0.5, application/json");
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(new HeaderContentNegotiationStrategy()));
    ViewResolver htmlViewResolver = mock(ViewResolver.class);
    ViewResolver jsonViewResolver = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(htmlViewResolver, jsonViewResolver));
    View htmlView = mock(View.class, "text_html");
    View jsonViewMock = mock(View.class, "application_json");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(htmlViewResolver.resolveViewName(viewName, locale)).willReturn(htmlView);
    given(jsonViewResolver.resolveViewName(viewName, locale)).willReturn(jsonViewMock);
    given(htmlView.getContentType()).willReturn("text/html");
    given(jsonViewMock.getContentType()).willReturn("application/json");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", jsonViewMock, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Aggregations

HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)11 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)7 ContentNegotiationStrategy (org.springframework.web.accept.ContentNegotiationStrategy)7 MediaType (org.springframework.http.MediaType)4 RequestHeaderRequestMatcher (org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher)4 Test (org.junit.Test)3 HttpSecurity (org.springframework.security.config.annotation.web.builders.HttpSecurity)3 AndRequestMatcher (org.springframework.security.web.util.matcher.AndRequestMatcher)3 NegatedRequestMatcher (org.springframework.security.web.util.matcher.NegatedRequestMatcher)3 RequestMatcher (org.springframework.security.web.util.matcher.RequestMatcher)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 View (org.springframework.web.servlet.View)3 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 ViewResolver (org.springframework.web.servlet.ViewResolver)2 Before (org.junit.Before)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)1 HttpStatusEntryPoint (org.springframework.security.web.authentication.HttpStatusEntryPoint)1 LoginUrlAuthenticationEntryPoint (org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint)1