Resolve ResourceUrlProvider from current request

This commit changes `ResourceTransformerSupport` to look for the
`ResourceUrlProvider` in the current request if none is configured on
the resource transformer itself.

Issue: SPR-15369
This commit is contained in:
Brian Clozel
2017-03-24 16:09:14 +01:00
parent fdd503152d
commit 2baceac5ff
4 changed files with 12 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,15 +38,8 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer
/**
* Configure a {@link ResourceUrlProvider} to use when resolving the public
* URL of links in a transformed resource (e.g. import links in a CSS file).
* This is required only for links expressed as full paths, i.e. including
* context and servlet path, and not for relative links.
* <p>By default this property is not set. In that case if a
* {@code ResourceUrlProvider} is needed an attempt is made to find the
* {@code ResourceUrlProvider} exposed through the
* {@link org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor
* ResourceUrlProviderExposingInterceptor} (configured by default in the MVC
* Java config and XML namespace). Therefore explicitly configuring this
* property should not be needed in most cases.
* This is required only for links expressed as full paths and not for
* relative links.
* @param resourceUrlProvider the URL provider to use
*/
public void setResourceUrlProvider(ResourceUrlProvider resourceUrlProvider) {
@@ -96,7 +89,7 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer
* @return the absolute request path for the given resource path
*/
protected String toAbsolutePath(String path, HttpServletRequest request) {
String requestPath = this.getResourceUrlProvider().getUrlPathHelper().getRequestUri(request);
String requestPath = this.findResourceUrlProvider(request).getUrlPathHelper().getRequestUri(request);
String absolutePath = StringUtils.applyRelativePath(requestPath, path);
return StringUtils.cleanPath(absolutePath);
}