Remove "Public" from the ResourceUrlProvider name
Also respect HandlerMapping order in ResourceUrlProvider
This commit is contained in:
@@ -77,8 +77,8 @@ import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExc
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.resource.PublicResourceUrlProvider;
|
||||
import org.springframework.web.servlet.resource.PublicResourceUrlProviderExposingInterceptor;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProvider;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
@@ -244,7 +244,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
InterceptorRegistry registry = new InterceptorRegistry();
|
||||
addInterceptors(registry);
|
||||
registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
|
||||
registry.addInterceptor(new PublicResourceUrlProviderExposingInterceptor(resourceUrlPathTranslator()));
|
||||
registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(resourceUrlPathTranslator()));
|
||||
this.interceptors = registry.getInterceptors();
|
||||
}
|
||||
return this.interceptors.toArray();
|
||||
@@ -358,8 +358,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PublicResourceUrlProvider resourceUrlPathTranslator() {
|
||||
PublicResourceUrlProvider translator = new PublicResourceUrlProvider();
|
||||
public ResourceUrlProvider resourceUrlPathTranslator() {
|
||||
ResourceUrlProvider translator = new ResourceUrlProvider();
|
||||
UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
|
||||
if (pathHelper != null) {
|
||||
translator.setUrlPathHelper(pathHelper);
|
||||
|
||||
@@ -49,17 +49,17 @@ public abstract class AbstractResourceResolver implements ResourceResolver {
|
||||
List<? extends Resource> locations, ResourceResolverChain chain);
|
||||
|
||||
@Override
|
||||
public String resolvePublicUrlPath(String resourceUrlPath, List<? extends Resource> locations,
|
||||
public String resolveUrlPath(String resourceUrlPath, List<? extends Resource> locations,
|
||||
ResourceResolverChain chain) {
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Resolving public URL for path=\"" + resourceUrlPath + "\"");
|
||||
}
|
||||
|
||||
return resolvePublicUrlPathInternal(resourceUrlPath, locations, chain);
|
||||
return resolveUrlPathInternal(resourceUrlPath, locations, chain);
|
||||
}
|
||||
|
||||
protected abstract String resolvePublicUrlPathInternal(String resourceUrlPath,
|
||||
protected abstract String resolveUrlPathInternal(String resourceUrlPath,
|
||||
List<? extends Resource> locations, ResourceResolverChain chain);
|
||||
|
||||
}
|
||||
|
||||
@@ -21,8 +21,6 @@ import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -65,13 +63,13 @@ class DefaultResourceResolverChain implements ResourceResolverChain {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolvePublicUrlPath(String resourcePath, List<? extends Resource> locations) {
|
||||
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations) {
|
||||
ResourceResolver resolver = getNextResolver();
|
||||
if (resolver == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return resolver.resolvePublicUrlPath(resourcePath, locations, this);
|
||||
return resolver.resolveUrlPath(resourcePath, locations, this);
|
||||
}
|
||||
finally {
|
||||
this.index--;
|
||||
|
||||
@@ -23,8 +23,6 @@ import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
@@ -97,10 +95,10 @@ public class FingerprintResourceResolver extends AbstractResourceResolver {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
ResourceResolverChain chain) {
|
||||
|
||||
String baseUrl = chain.resolvePublicUrlPath(resourceUrlPath, locations);
|
||||
String baseUrl = chain.resolveUrlPath(resourceUrlPath, locations);
|
||||
if (StringUtils.hasText(baseUrl)) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Getting the original resource to calculate hash");
|
||||
|
||||
@@ -25,8 +25,6 @@ import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
@@ -74,10 +72,10 @@ public class GzipResourceResolver extends AbstractResourceResolver {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
ResourceResolverChain chain) {
|
||||
|
||||
return chain.resolvePublicUrlPath(resourceUrlPath, locations);
|
||||
return chain.resolveUrlPath(resourceUrlPath, locations);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public class PathResourceResolver extends AbstractResourceResolver {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
ResourceResolverChain chain) {
|
||||
|
||||
return (getResource(resourceUrlPath, locations) != null ? resourceUrlPath : null);
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.resource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -63,10 +61,10 @@ public class PrefixResourceResolver extends AbstractResourceResolver {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
|
||||
ResourceResolverChain chain) {
|
||||
|
||||
String baseUrl = chain.resolvePublicUrlPath(resourceUrlPath, locations);
|
||||
String baseUrl = chain.resolveUrlPath(resourceUrlPath, locations);
|
||||
if (StringUtils.hasText(baseUrl)) {
|
||||
return this.prefix + (baseUrl.startsWith("/") ? baseUrl : "/" + baseUrl);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,6 @@ public interface ResourceResolver {
|
||||
* @param chain the chain of resolvers to delegate to
|
||||
* @return the resolved public URL path or {@code null} if unresolved
|
||||
*/
|
||||
String resolvePublicUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain);
|
||||
String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain);
|
||||
|
||||
}
|
||||
|
||||
@@ -56,6 +56,6 @@ public interface ResourceResolverChain {
|
||||
* @param locations the locations to search in when looking up resources
|
||||
* @return the resolved public URL path or {@code null} if unresolved
|
||||
*/
|
||||
String resolvePublicUrlPath(String resourcePath, List<? extends Resource> locations);
|
||||
String resolveUrlPath(String resourcePath, List<? extends Resource> locations);
|
||||
|
||||
}
|
||||
|
||||
@@ -65,8 +65,8 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
|
||||
|
||||
@Override
|
||||
public String encodeURL(String url) {
|
||||
String name = PublicResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR;
|
||||
PublicResourceUrlProvider urlProvider = (PublicResourceUrlProvider) this.request.getAttribute(name);
|
||||
String name = ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR;
|
||||
ResourceUrlProvider urlProvider = (ResourceUrlProvider) this.request.getAttribute(name);
|
||||
if (urlProvider != null) {
|
||||
String translatedUrl = urlProvider.getForRequestUrl(this.request, url);
|
||||
if (translatedUrl != null) {
|
||||
@@ -74,7 +74,7 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.debug("Request attribute exposing PublicResourceUrlProvider not found under name: " + name);
|
||||
logger.debug("Request attribute exposing ResourceUrlProvider not found under name: " + name);
|
||||
}
|
||||
return super.encodeURL(url);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.core.OrderComparator;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.PathMatcher;
|
||||
@@ -29,7 +30,10 @@ import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -44,7 +48,7 @@ import java.util.Map;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class PublicResourceUrlProvider implements ApplicationListener<ContextRefreshedEvent> {
|
||||
public class ResourceUrlProvider implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -130,10 +134,15 @@ public class PublicResourceUrlProvider implements ApplicationListener<ContextRef
|
||||
}
|
||||
}
|
||||
|
||||
protected void detectResourceHandlers(ApplicationContext applicationContext) {
|
||||
protected void detectResourceHandlers(ApplicationContext appContext) {
|
||||
|
||||
logger.debug("Looking for resource handler mappings");
|
||||
Map<String, SimpleUrlHandlerMapping> beans = applicationContext.getBeansOfType(SimpleUrlHandlerMapping.class);
|
||||
for (SimpleUrlHandlerMapping hm : beans.values()) {
|
||||
|
||||
Map<String, SimpleUrlHandlerMapping> map = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
|
||||
List<SimpleUrlHandlerMapping> handlerMappings = new ArrayList<SimpleUrlHandlerMapping>(map.values());
|
||||
Collections.sort(handlerMappings, new OrderComparator());
|
||||
|
||||
for (SimpleUrlHandlerMapping hm : handlerMappings) {
|
||||
for (String pattern : hm.getUrlMap().keySet()) {
|
||||
Object handler = hm.getUrlMap().get(pattern);
|
||||
if (handler instanceof ResourceHttpRequestHandler) {
|
||||
@@ -207,7 +216,7 @@ public class PublicResourceUrlProvider implements ApplicationListener<ContextRef
|
||||
}
|
||||
ResourceHttpRequestHandler handler = this.handlerMap.get(pattern);
|
||||
ResourceResolverChain chain = handler.createResourceResolverChain();
|
||||
String resolved = chain.resolvePublicUrlPath(pathWithinMapping, handler.getLocations());
|
||||
String resolved = chain.resolveUrlPath(pathWithinMapping, handler.getLocations());
|
||||
if (resolved == null) {
|
||||
throw new IllegalStateException("Failed to get public resource URL path for " + pathWithinMapping);
|
||||
}
|
||||
@@ -23,24 +23,24 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* An interceptor that exposes the {@link PublicResourceUrlProvider} instance it
|
||||
* An interceptor that exposes the {@link ResourceUrlProvider} instance it
|
||||
* is configured with as a request attribute.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class PublicResourceUrlProviderExposingInterceptor extends HandlerInterceptorAdapter {
|
||||
public class ResourceUrlProviderExposingInterceptor extends HandlerInterceptorAdapter {
|
||||
|
||||
/**
|
||||
* Name of the request attribute that holds the {@link PublicResourceUrlProvider}.
|
||||
* Name of the request attribute that holds the {@link ResourceUrlProvider}.
|
||||
*/
|
||||
public static final String RESOURCE_URL_PROVIDER_ATTR = PublicResourceUrlProvider.class.getName().toString();
|
||||
public static final String RESOURCE_URL_PROVIDER_ATTR = ResourceUrlProvider.class.getName().toString();
|
||||
|
||||
|
||||
private final PublicResourceUrlProvider resourceUrlProvider;
|
||||
private final ResourceUrlProvider resourceUrlProvider;
|
||||
|
||||
|
||||
public PublicResourceUrlProviderExposingInterceptor(PublicResourceUrlProvider resourceUrlProvider) {
|
||||
public ResourceUrlProviderExposingInterceptor(ResourceUrlProvider resourceUrlProvider) {
|
||||
Assert.notNull(resourceUrlProvider, "'resourceUrlProvider' is required");
|
||||
this.resourceUrlProvider = resourceUrlProvider;
|
||||
}
|
||||
Reference in New Issue
Block a user