/** * Build a {@link ContentNegotiationManager} based on this configurer's settings. * @since 4.3.12 * @see ContentNegotiationManagerFactoryBean#getObject() */ protected ContentNegotiationManager buildContentNegotiationManager() { this.factory.addMediaTypes(this.mediaTypes); return this.factory.build(); }
/** * Whether the path extension in the URL path should be used to determine * the requested media type. * <p>By default this is set to {@code true} in which case a request * for {@code /hotels.pdf} will be interpreted as a request for * {@code "application/pdf"} regardless of the 'Accept' header. */ public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension) { this.factory.setFavorPathExtension(favorPathExtension); return this; }
/** * When {@link #favorPathExtension favorPathExtension} is set, this * property determines whether to use only registered {@code MediaType} mappings * to resolve a path extension to a specific MediaType. * <p>By default this is not set in which case * {@code PathExtensionContentNegotiationStrategy} will use defaults if available. */ public ContentNegotiationConfigurer useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) { this.factory.setUseRegisteredExtensionsOnly(useRegisteredExtensionsOnly); return this; }
@Before public void setup() throws Exception { ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = factoryBean.getObject(); ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); this.handler = new ReactiveTypeHandler(adapterRegistry, new SyncTaskExecutor(), manager); resetRequest(); }
@Test(expected = HttpMediaTypeNotAcceptableException.class) // SPR-10170 public void favorPathWithIgnoreUnknownPathExtensionTurnedOff() throws Exception { this.factoryBean.setFavorPathExtension(true); this.factoryBean.setIgnoreUnknownPathExtensions(false); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); this.servletRequest.setRequestURI("/flower.foobarbaz"); this.servletRequest.addParameter("format", "json"); manager.resolveMediaTypes(this.webRequest); }
@Test public void favorParameter() throws Exception { this.factoryBean.setFavorParameter(true); Map<String, MediaType> mediaTypes = new HashMap<>(); mediaTypes.put("json", MediaType.APPLICATION_JSON); this.factoryBean.addMediaTypes(mediaTypes); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); this.servletRequest.setRequestURI("/flower"); this.servletRequest.addParameter("format", "json"); assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); }
@Test public void favorPath() throws Exception { this.factoryBean.setFavorPathExtension(true); this.factoryBean.addMediaTypes(Collections.singletonMap("bar", new MediaType("application", "bar"))); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); this.servletRequest.setRequestURI("/flower.foo"); assertEquals(Collections.singletonList(new MediaType("application", "foo")), manager.resolveMediaTypes(this.webRequest)); this.servletRequest.setRequestURI("/flower.bar"); assertEquals(Collections.singletonList(new MediaType("application", "bar")), manager.resolveMediaTypes(this.webRequest)); this.servletRequest.setRequestURI("/flower.gif"); assertEquals(Collections.singletonList(MediaType.IMAGE_GIF), manager.resolveMediaTypes(this.webRequest)); }
@Test // SPR-14577 public void getMediaTypeWithFavorPathExtensionOff() throws Exception { ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean(); factory.setFavorPathExtension(false); 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.addHeader("Accept", "application/json,text/plain,*/*"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html"); handler.handleRequest(this.request, this.response); assertEquals("text/html", this.response.getContentType()); }
@Test // SPR-13658 public void getResourceWithRegisteredMediaType() throws Exception { ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean(); factory.addMediaType("bar", 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.bar"); handler.handleRequest(this.request, this.response); assertEquals("foo/bar", this.response.getContentType()); assertEquals("h1 { color:red; }", this.response.getContentAsString()); }
@Override public void afterPropertiesSet() { build(); }
@Test(expected = HttpMediaTypeNotAcceptableException.class) // SPR-10170 public void favorParameterWithUnknownMediaType() throws HttpMediaTypeNotAcceptableException { this.factoryBean.setFavorParameter(true); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); this.servletRequest.setRequestURI("/flower"); this.servletRequest.setParameter("format", "invalid"); manager.resolveMediaTypes(this.webRequest); }
@Test public void ignoreAcceptHeader() throws Exception { this.factoryBean.setIgnoreAcceptHeader(true); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); this.servletRequest.setRequestURI("/flower"); this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE); assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest)); }
@Test // SPR-12286 public void setDefaultContentTypeWithStrategy() throws Exception { this.factoryBean.setDefaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)); this.factoryBean.afterPropertiesSet(); ContentNegotiationManager manager = this.factoryBean.getObject(); assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE); assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); }
@Before public void setup() { TestServletContext servletContext = new TestServletContext(); servletContext.getMimeTypes().put("foo", "application/foo"); this.servletRequest = new MockHttpServletRequest(servletContext); this.webRequest = new ServletWebRequest(this.servletRequest); this.factoryBean = new ContentNegotiationManagerFactoryBean(); this.factoryBean.setServletContext(this.servletRequest.getServletContext()); }
/** * Class constructor with {@link javax.servlet.ServletContext}. */ public ContentNegotiationConfigurer(@Nullable ServletContext servletContext) { if (servletContext != null) { this.factory.setServletContext(servletContext); } }
@Bean public ContentNegotiatingViewResolver contentViewResolver() { ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean(); contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON); InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/jsp/"); viewResolver.setSuffix(".jsp"); MappingJackson2JsonView defaultView = new MappingJackson2JsonView(); defaultView.setExtractValueFromSingleKeyModel(true); ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver(); contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject()); contentViewResolver.setViewResolvers(Arrays.<ViewResolver> asList(viewResolver)); contentViewResolver.setDefaultViews(Arrays.<View> asList(defaultView)); return contentViewResolver; }
/** * Whether to ignore requests with path extension that cannot be resolved * to any media type. Setting this to {@code false} will result in an * {@code HttpMediaTypeNotAcceptableException} if there is no match. * <p>By default this is set to {@code true}. */ public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore) { this.factory.setIgnoreUnknownPathExtensions(ignore); return this; }
/** * Whether a request parameter ("format" by default) should be used to * determine the requested media type. For this option to work you must * register {@link #mediaType(String, MediaType) media type mappings}. * <p>By default this is set to {@code false}. * @see #parameterName(String) */ public ContentNegotiationConfigurer favorParameter(boolean favorParameter) { this.factory.setFavorParameter(favorParameter); return this; }
/** * Whether to disable checking the 'Accept' request header. * <p>By default this value is set to {@code false}. */ public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader) { this.factory.setIgnoreAcceptHeader(ignoreAcceptHeader); return this; }
/** * Set a custom {@link ContentNegotiationStrategy} to use to determine * the content type to use when no content type is requested. * <p>By default this is not set. * @since 4.1.2 * @see #defaultContentType */ public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) { this.factory.setDefaultContentTypeStrategy(defaultStrategy); return this; }