/** * Private constructor with already parsed media type expressions. */ private ProducesRequestCondition(Collection<ProduceMediaTypeExpression> expressions, @Nullable ContentNegotiationManager manager) { this.expressions = new ArrayList<>(expressions); Collections.sort(this.expressions); this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager()); }
/** * Same as {@link #ProducesRequestCondition(String[], String[])} but also * accepting a {@link ContentNegotiationManager}. * @param produces expressions with syntax defined by {@link RequestMapping#produces()} * @param headers expressions with syntax defined by {@link RequestMapping#headers()} * @param manager used to determine requested media types */ public ProducesRequestCondition(String[] produces, @Nullable String[] headers, @Nullable ContentNegotiationManager manager) { this.expressions = new ArrayList<>(parseExpressions(produces, headers)); Collections.sort(this.expressions); this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager()); }
/** * Constructor with list of converters and ContentNegotiationManager as well * as request/response body advice instances. */ protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager, @Nullable List<Object> requestResponseBodyAdvice) { super(converters, requestResponseBodyAdvice); this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager()); this.pathStrategy = initPathStrategy(this.contentNegotiationManager); this.safeExtensions.addAll(this.contentNegotiationManager.getAllFileExtensions()); this.safeExtensions.addAll(WHITELISTED_EXTENSIONS); }
/** * Private constructor with already parsed media type expressions. */ private ProducesRequestCondition(Collection<ProduceMediaTypeExpression> expressions, @Nullable ContentNegotiationManager manager) { this.expressions = new ArrayList<>(expressions); Collections.sort(this.expressions); this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager()); }
public ReactiveTypeHandler() { this(ReactiveAdapterRegistry.getSharedInstance(), new SyncTaskExecutor(), new ContentNegotiationManager()); }
/** * Constructor with list of converters and ContentNegotiationManager as well * as request/response body advice instances. */ protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager, @Nullable List<Object> requestResponseBodyAdvice) { super(converters, requestResponseBodyAdvice); this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager()); this.pathStrategy = initPathStrategy(this.contentNegotiationManager); this.safeExtensions.addAll(this.contentNegotiationManager.getAllFileExtensions()); this.safeExtensions.addAll(WHITELISTED_EXTENSIONS); }
public ReactiveTypeHandler() { this(ReactiveAdapterRegistry.getSharedInstance(), new SyncTaskExecutor(), new ContentNegotiationManager()); }
@Before public void setup() { StaticWebApplicationContext context = new StaticWebApplicationContext(); context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class); context.registerSingleton("tilesConfigurer", TilesConfigurer.class); context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class); context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class); this.registry = new ViewResolverRegistry(new ContentNegotiationManager(), context); }
@Before public void setUp() { GenericWebApplicationContext appContext = new GenericWebApplicationContext(); appContext.refresh(); this.registry = new ResourceHandlerRegistry(appContext, new MockServletContext(), new ContentNegotiationManager(), new UrlPathHelper()); this.registration = this.registry.addResourceHandler("/resources/**"); this.registration.addResourceLocations("classpath:org/springframework/web/servlet/config/annotation/"); this.response = new MockHttpServletResponse(); }
@Test public void wrapConcurrentResult_CollectedValuesList() throws Exception { List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter()); ResolvableType elementType = ResolvableType.forClass(List.class); ReactiveTypeHandler.CollectedValuesList result = new ReactiveTypeHandler.CollectedValuesList(elementType); result.add(Arrays.asList("foo1", "bar1")); result.add(Arrays.asList("foo2", "bar2")); ContentNegotiationManager manager = new ContentNegotiationManager(); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters, manager)); ServletInvocableHandlerMethod hm = getHandlerMethod(new MethodLevelResponseBodyHandler(), "handleFluxOfLists"); hm = hm.wrapConcurrentResult(result); hm.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("[[\"foo1\",\"bar1\"],[\"foo2\",\"bar2\"]]", this.response.getContentAsString()); }
@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); }
@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()); }
@Test public void nestedViewResolverIsNotSpringBean() throws Exception { StaticWebApplicationContext webAppContext = new StaticWebApplicationContext(); webAppContext.setServletContext(new MockServletContext()); webAppContext.refresh(); InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver(); nestedResolver.setApplicationContext(webAppContext); nestedResolver.setViewClass(InternalResourceView.class); viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver))); FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML); viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy)); viewResolver.afterPropertiesSet(); String viewName = "view"; Locale locale = Locale.ENGLISH; View result = viewResolver.resolveViewName(viewName, locale); assertNotNull("Invalid view", result); }
@Test // SPR-15478 public void wrapConcurrentResult_CollectedValuesListWithResponseEntity() throws Exception { List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter()); ResolvableType elementType = ResolvableType.forClass(Bar.class); ReactiveTypeHandler.CollectedValuesList result = new ReactiveTypeHandler.CollectedValuesList(elementType); result.add(new Bar("foo")); result.add(new Bar("bar")); ContentNegotiationManager manager = new ContentNegotiationManager(); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters, manager)); ServletInvocableHandlerMethod hm = getHandlerMethod(new ResponseEntityHandler(), "handleFlux"); hm = hm.wrapConcurrentResult(result); hm.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("[{\"value\":\"foo\"},{\"value\":\"bar\"}]", this.response.getContentAsString()); }
@Test public void resolveViewNameWithRequestParameter() throws Exception { request.addParameter("format", "xls"); Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel")); ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping); viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy)); ViewResolver viewResolverMock = mock(ViewResolver.class); viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock)); viewResolver.afterPropertiesSet(); 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); }
@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); }
@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); }
@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); }
@Test public void resolveViewNameWithDefaultContentType() throws Exception { request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); MediaType mediaType = new MediaType("application", "xml"); FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(mediaType); viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy)); ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1"); ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2"); viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2)); viewResolver.afterPropertiesSet(); View viewMock1 = mock(View.class, "application_xml"); View viewMock2 = mock(View.class, "text_html"); String viewName = "view"; Locale locale = Locale.ENGLISH; given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1); given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2); given(viewMock1.getContentType()).willReturn("application/xml"); given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1"); View result = viewResolver.resolveViewName(viewName, locale); assertSame("Invalid view", viewMock1, result); }
viewList.add(new MarshallingView(marshaller)); ContentNegotiationManager manager = new ContentNegotiationManager( new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));