/** * 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()); }
@SuppressWarnings("unchecked") private Collection<MediaType> getMediaTypes(NativeWebRequest request) throws HttpMediaTypeNotAcceptableException { Collection<MediaType> mediaTypes = (Collection<MediaType>) request.getAttribute( HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); return CollectionUtils.isEmpty(mediaTypes) ? this.contentNegotiationManager.resolveMediaTypes(request) : mediaTypes; }
/** * Find a {@code ContentNegotiationStrategy} of the given type. * @param strategyType the strategy type * @return the first matching strategy, or {@code null} if none * @since 4.3 */ @SuppressWarnings("unchecked") @Nullable public <T extends ContentNegotiationStrategy> T getStrategy(Class<T> strategyType) { for (ContentNegotiationStrategy strategy : getStrategies()) { if (strategyType.isInstance(strategy)) { return (T) strategy; } } return null; }
/** * 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); }
@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 explicitStrategies() throws Exception { Map<String, MediaType> mediaTypes = Collections.singletonMap("bar", new MediaType("application", "bar")); ParameterContentNegotiationStrategy strategy1 = new ParameterContentNegotiationStrategy(mediaTypes); HeaderContentNegotiationStrategy strategy2 = new HeaderContentNegotiationStrategy(); List<ContentNegotiationStrategy> strategies = Arrays.asList(strategy1, strategy2); this.factoryBean.setStrategies(strategies); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); assertEquals(strategies, manager.getStrategies()); this.servletRequest.setRequestURI("/flower"); this.servletRequest.addParameter("format", "bar"); assertEquals(Collections.singletonList(new MediaType("application", "bar")), manager.resolveMediaTypes(this.webRequest)); }
/** * Return the file extensions to use for suffix pattern matching. If * {@code registeredSuffixPatternMatch=true}, the extensions are obtained * from the configured {@code contentNegotiationManager}. */ @Nullable public List<String> getFileExtensions() { if (useRegisteredSuffixPatternMatch() && this.contentNegotiationManager != null) { return this.contentNegotiationManager.getAllFileExtensions(); } return null; }
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) { Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class; PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz); return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy()); }
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception { List<View> candidateViews = new ArrayList<>(); if (this.viewResolvers != null) { Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set"); for (ViewResolver viewResolver : this.viewResolvers) { View view = viewResolver.resolveViewName(viewName, locale); if (view != null) { candidateViews.add(view); } for (MediaType requestedMediaType : requestedMediaTypes) { List<String> extensions = this.contentNegotiationManager.resolveFileExtensions(requestedMediaType); for (String extension : extensions) { String viewNameWithExtension = viewName + '.' + extension; view = viewResolver.resolveViewName(viewNameWithExtension, locale); if (view != null) { candidateViews.add(view); } } } } } if (!CollectionUtils.isEmpty(this.defaultViews)) { candidateViews.addAll(this.defaultViews); } return candidateViews; }
/** * 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); }
/** * Return the file extensions to use for suffix pattern matching. If * {@code registeredSuffixPatternMatch=true}, the extensions are obtained * from the configured {@code contentNegotiationManager}. */ @Nullable public List<String> getFileExtensions() { if (useRegisteredSuffixPatternMatch() && this.contentNegotiationManager != null) { return this.contentNegotiationManager.getAllFileExtensions(); } return null; }
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) { Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class; PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz); return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy()); }
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception { List<View> candidateViews = new ArrayList<>(); if (this.viewResolvers != null) { Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set"); for (ViewResolver viewResolver : this.viewResolvers) { View view = viewResolver.resolveViewName(viewName, locale); if (view != null) { candidateViews.add(view); } for (MediaType requestedMediaType : requestedMediaTypes) { List<String> extensions = this.contentNegotiationManager.resolveFileExtensions(requestedMediaType); for (String extension : extensions) { String viewNameWithExtension = viewName + '.' + extension; view = viewResolver.resolveViewName(viewNameWithExtension, locale); if (view != null) { candidateViews.add(view); } } } } } if (!CollectionUtils.isEmpty(this.defaultViews)) { candidateViews.addAll(this.defaultViews); } return candidateViews; }
/** * 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()); }
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException { return this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request)); }
/** * Constructor with list of converters and ContentNegotiationManager as well * as request/response body advice instances. */ protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters, ContentNegotiationManager manager, 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); }
/** * Find a {@code ContentNegotiationStrategy} of the given type. * @param strategyType the strategy type * @return the first matching strategy, or {@code null} if none * @since 4.3 */ @SuppressWarnings("unchecked") @Nullable public <T extends ContentNegotiationStrategy> T getStrategy(Class<T> strategyType) { for (ContentNegotiationStrategy strategy : getStrategies()) { if (strategyType.isInstance(strategy)) { return (T) strategy; } } return null; }
@Test public void testPathMatchingConfiguration() { loadBeanDefinitions("mvc-config-path-matching.xml"); RequestMappingHandlerMapping hm = this.appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(hm); assertTrue(hm.useSuffixPatternMatch()); assertFalse(hm.useTrailingSlashMatch()); assertTrue(hm.useRegisteredSuffixPatternMatch()); assertThat(hm.getUrlPathHelper(), Matchers.instanceOf(TestPathHelper.class)); assertThat(hm.getPathMatcher(), Matchers.instanceOf(TestPathMatcher.class)); List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions(); assertThat(fileExtensions, Matchers.contains("xml")); assertThat(fileExtensions, Matchers.hasSize(1)); }
/** * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager} * setup and the availability of a {@code ServletContext}. * @see ServletPathExtensionContentNegotiationStrategy * @see PathExtensionContentNegotiationStrategy */ protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() { Map<String, MediaType> mediaTypes = null; if (getContentNegotiationManager() != null) { PathExtensionContentNegotiationStrategy strategy = getContentNegotiationManager().getStrategy(PathExtensionContentNegotiationStrategy.class); if (strategy != null) { mediaTypes = new HashMap<>(strategy.getMediaTypes()); } } return (getServletContext() != null ? new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) : new PathExtensionContentNegotiationStrategy(mediaTypes)); }
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception { List<View> candidateViews = new ArrayList<View>(); for (ViewResolver viewResolver : this.viewResolvers) { View view = viewResolver.resolveViewName(viewName, locale); if (view != null) { candidateViews.add(view); } for (MediaType requestedMediaType : requestedMediaTypes) { List<String> extensions = this.contentNegotiationManager.resolveFileExtensions(requestedMediaType); for (String extension : extensions) { String viewNameWithExtension = viewName + '.' + extension; view = viewResolver.resolveViewName(viewNameWithExtension, locale); if (view != null) { candidateViews.add(view); } } } } if (!CollectionUtils.isEmpty(this.defaultViews)) { candidateViews.addAll(this.defaultViews); } return candidateViews; }