private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) { Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class; PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz); return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy()); }
/** * Set the default content type to use when no content type is requested. * <p>By default this is not set. * @see #setDefaultContentTypeStrategy */ public void setDefaultContentType(MediaType contentType) { this.defaultNegotiationStrategy = new FixedContentNegotiationStrategy(contentType); }
@Override public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException { return resolveMediaTypeKey(webRequest, getMediaTypeKey(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)); }
/** * Create a default instance with a {@link HeaderContentNegotiationStrategy}. */ public ContentNegotiationManager() { this(new HeaderContentNegotiationStrategy()); }
@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(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); }
/** * 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(); }
@Override public List<MediaType> resolveMediaTypes(NativeWebRequest request) throws HttpMediaTypeNotAcceptableException { for (ContentNegotiationStrategy strategy : this.strategies) { List<MediaType> mediaTypes = strategy.resolveMediaTypes(request); if (mediaTypes.equals(MEDIA_TYPE_ALL_LIST)) { continue; } return mediaTypes; } return MEDIA_TYPE_ALL_LIST; }
/** * 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; }
/** * 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; }
@Override public void afterPropertiesSet() { build(); }
/** * 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; }
/** * 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; }
/** * Class constructor with {@link javax.servlet.ServletContext}. */ public ContentNegotiationConfigurer(@Nullable ServletContext servletContext) { if (servletContext != null) { this.factory.setServletContext(servletContext); } }
/** * Set the query parameter name to use when {@link #favorParameter} is on. * <p>The default parameter name is {@code "format"}. */ public ContentNegotiationConfigurer parameterName(String parameterName) { this.factory.setParameterName(parameterName); return this; }
/** * Set the default content types to use when no content type is requested. * <p>By default this is not set. * @since 5.0 * @see #setDefaultContentTypeStrategy */ public void setDefaultContentTypes(List<MediaType> contentTypes) { this.defaultNegotiationStrategy = new FixedContentNegotiationStrategy(contentTypes); }
/** * Indicate whether to use the Java Activation Framework as a fallback option * to map from file extensions to media types. * @deprecated as of 5.0, in favor of {@link #setUseRegisteredExtensionsOnly(boolean)}, which * has reverse behavior. */ @Deprecated public void setUseJaf(boolean useJaf) { setUseRegisteredExtensionsOnly(!useJaf); }