Configurable UrlPathHelper in PathExtensionContentNegotiationStrategy

This commit also aligns ResourceUrlProvider's and RequestMappingInfo's UrlPathHelper setter/getter signatures.

Issue: SPR-14454
(cherry picked from commit 84afc60)
This commit is contained in:
Juergen Hoeller
2016-07-13 15:14:42 +02:00
parent be0b71ce31
commit 5c3c0f73c1
5 changed files with 83 additions and 38 deletions

View File

@@ -52,41 +52,45 @@ import org.springframework.web.util.WebUtils;
* @author Rossen Stoyanchev
* @since 3.2
*/
public class PathExtensionContentNegotiationStrategy
extends AbstractMappingContentNegotiationStrategy {
private static final Log logger = LogFactory.getLog(PathExtensionContentNegotiationStrategy.class);
public class PathExtensionContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
private static final boolean JAF_PRESENT = ClassUtils.isPresent("javax.activation.FileTypeMap",
PathExtensionContentNegotiationStrategy.class.getClassLoader());
private static final UrlPathHelper PATH_HELPER = new UrlPathHelper();
static {
PATH_HELPER.setUrlDecode(false);
}
private static final Log logger = LogFactory.getLog(PathExtensionContentNegotiationStrategy.class);
private UrlPathHelper urlPathHelper = new UrlPathHelper();
private boolean useJaf = true;
private boolean ignoreUnknownExtensions = true;
/**
* Create an instance with the given map of file extensions and media types.
*/
public PathExtensionContentNegotiationStrategy(Map<String, MediaType> mediaTypes) {
super(mediaTypes);
}
/**
* Create an instance without any mappings to start with. Mappings may be added
* later on if any extensions are resolved through the Java Activation framework.
*/
public PathExtensionContentNegotiationStrategy() {
super(null);
this(null);
}
/**
* Create an instance with the given map of file extensions and media types.
*/
public PathExtensionContentNegotiationStrategy(Map<String, MediaType> mediaTypes) {
super(mediaTypes);
this.urlPathHelper.setUrlDecode(false);
}
/**
* Configure a {@code UrlPathHelper} to use in {@link #getMediaTypeKey}
* in order to derive the lookup path for a target request URL path.
* @since 4.2.8
*/
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
this.urlPathHelper = urlPathHelper;
}
/**
* Whether to use the Java Activation Framework to look up file extensions.
@@ -113,7 +117,7 @@ public class PathExtensionContentNegotiationStrategy
logger.warn("An HttpServletRequest is required to determine the media type key");
return null;
}
String path = PATH_HELPER.getLookupPathForRequest(request);
String path = this.urlPathHelper.getLookupPathForRequest(request);
String filename = WebUtils.extractFullFilenameFromUrlPath(path);
String extension = StringUtils.getFilenameExtension(filename);
return (StringUtils.hasText(extension)) ? extension.toLowerCase(Locale.ENGLISH) : null;