Search in sources :

Example 1 with PathExtensionContentNegotiationStrategy

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

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

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

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

the class AbstractMessageConverterMethodProcessor method initPathStrategy.

private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) {
    Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class;
    PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz);
    return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy());
}
Also used : PathExtensionContentNegotiationStrategy(org.springframework.web.accept.PathExtensionContentNegotiationStrategy)

Aggregations

PathExtensionContentNegotiationStrategy (org.springframework.web.accept.PathExtensionContentNegotiationStrategy)4 Test (org.junit.Test)3 MediaType (org.springframework.http.MediaType)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1 View (org.springframework.web.servlet.View)1 ViewResolver (org.springframework.web.servlet.ViewResolver)1