Introduce 'useRegisteredExtensionsOnly' property in classes using MediaTypeFactory

This commit introduces a `useRegisteredExtensionsOnly` property that
indicates whether classes that use the `MediaTypeFactory` for supplying
default media types can do so.

 - In classes that were introduced in Spring 5.0, the
 `useRegisteredExtensionsOnly` property takes the place of the
 `useJaf` property that was removed in 0aaa652
 - In classes that existed before Spring 5.0, the
 `useRegisteredExtensionsOnly` property is added in addition to the
 deprecated `useJaf`, the latter delegating to the former, but with
 flipped behavior.

Issue: SPR-14908
This commit is contained in:
Arjen Poutsma
2017-03-23 14:56:43 +01:00
parent e2aa880301
commit a287e67992
8 changed files with 101 additions and 22 deletions

View File

@@ -42,6 +42,8 @@ import org.springframework.web.util.UriUtils;
*/
public class PathExtensionContentTypeResolver extends AbstractMappingContentTypeResolver {
private boolean useRegisteredExtensionsOnly = false;
private boolean ignoreUnknownExtensions = true;
@@ -61,6 +63,15 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
}
/**
* Whether to only use the registered mappings to look up file extensions, or also refer to
* defaults.
* <p>By default this is set to {@code false}, meaning that defaults are used.
*/
public void setUseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) {
this.useRegisteredExtensionsOnly = useRegisteredExtensionsOnly;
}
/**
* Whether to ignore requests with unknown file extension. Setting this to
* {@code false} results in {@code HttpMediaTypeNotAcceptableException}.
@@ -80,14 +91,16 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
@Override
protected MediaType handleNoMatch(String key) throws NotAcceptableStatusException {
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType("file." + key);
if (mediaType.isPresent()) {
return mediaType.get();
if (!this.useRegisteredExtensionsOnly) {
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType("file." + key);
if (mediaType.isPresent()) {
return mediaType.get();
}
}
if (!this.ignoreUnknownExtensions) {
throw new NotAcceptableStatusException(getAllMediaTypes());
if (this.ignoreUnknownExtensions) {
return null;
}
return null;
throw new NotAcceptableStatusException(getAllMediaTypes());
}
/**

View File

@@ -93,6 +93,8 @@ public class RequestedContentTypeResolverBuilder {
private boolean ignoreUnknownPathExtensions = true;
private Boolean useRegisteredExtensionsOnly;
private String parameterName = "format";
private RequestedContentTypeResolver contentTypeResolver;
@@ -152,6 +154,18 @@ public class RequestedContentTypeResolverBuilder {
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 RequestedContentTypeResolverBuilder useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) {
this.useRegisteredExtensionsOnly = useRegisteredExtensionsOnly;
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
@@ -211,6 +225,9 @@ public class RequestedContentTypeResolverBuilder {
if (this.favorPathExtension) {
PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver(this.mediaTypes);
resolver.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions);
if (this.useRegisteredExtensionsOnly != null) {
resolver.setUseRegisteredExtensionsOnly(this.useRegisteredExtensionsOnly);
}
resolvers.add(resolver);
}