Class ResourceUrlProvider
- java.lang.Object
- org.springframework.web.servlet.resource.ResourceUrlProvider
- All Implemented Interfaces:
EventListener,ApplicationListener<ContextRefreshedEvent>
public class ResourceUrlProvider extends Object implements ApplicationListener<ContextRefreshedEvent>
A central component to use to obtain the public URL path that clients should use to access a static resource.This class is aware of Spring MVC handler mappings used to serve static resources and uses the
ResourceResolverchains of the configuredResourceHttpRequestHandlers to make its decisions.- Since:
- 4.1
- Author:
- Rossen Stoyanchev
Constructor Summary
Constructors Constructor Description ResourceUrlProvider()
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voiddetectResourceHandlers(ApplicationContext appContext)StringgetForLookupPath(String lookupPath)Compare the given path against configured resource handler mappings and if a match is found use theResourceResolverchain of the matchedResourceHttpRequestHandlerto resolve the URL path to expose for public use.StringgetForRequestUrl(HttpServletRequest request, String requestUrl)A variation ongetForLookupPath(String)that accepts a full request URL path (i.e.Map<String,ResourceHttpRequestHandler>getHandlerMap()Return the resource mappings, either manually configured or auto-detected when the SpringApplicationContextis refreshed.UrlPathHelpergetPathHelper()Deprecated.as of Spring 4.2.8, in favor ofgetUrlPathHelper()PathMatchergetPathMatcher()Return the configuredPathMatcher.UrlPathHelpergetUrlPathHelper()Return the configuredUrlPathHelper.booleanisAutodetect()Returnfalseif resource mappings were manually configured,trueotherwise.voidonApplicationEvent(ContextRefreshedEvent event)Handle an application event.voidsetHandlerMap(Map<String,ResourceHttpRequestHandler> handlerMap)Manually configure the resource mappings.voidsetPathMatcher(PathMatcher pathMatcher)Configure aPathMatcherto use when comparing target lookup path against resource mappings.voidsetUrlPathHelper(UrlPathHelper urlPathHelper)Configure aUrlPathHelperto use ingetForRequestUrl(javax.servlet.http.HttpServletRequest, String)in order to derive the lookup path for a target request URL path.
Constructor Detail
ResourceUrlProvider
public ResourceUrlProvider()
Method Detail
setUrlPathHelper
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
Configure aUrlPathHelperto use ingetForRequestUrl(javax.servlet.http.HttpServletRequest, String)in order to derive the lookup path for a target request URL path.
getUrlPathHelper
public UrlPathHelper getUrlPathHelper()
Return the configuredUrlPathHelper.- Since:
- 4.2.8
getPathHelper
@Deprecated public UrlPathHelper getPathHelper()
Deprecated.as of Spring 4.2.8, in favor ofgetUrlPathHelper()
setPathMatcher
public void setPathMatcher(PathMatcher pathMatcher)
Configure aPathMatcherto use when comparing target lookup path against resource mappings.
getPathMatcher
public PathMatcher getPathMatcher()
Return the configuredPathMatcher.
setHandlerMap
public void setHandlerMap(Map<String,ResourceHttpRequestHandler> handlerMap)
Manually configure the resource mappings.Note: by default resource mappings are auto-detected from the Spring
ApplicationContext. However if this property is used, the auto-detection is turned off.
getHandlerMap
public Map<String,ResourceHttpRequestHandler> getHandlerMap()
Return the resource mappings, either manually configured or auto-detected when the SpringApplicationContextis refreshed.
isAutodetect
public boolean isAutodetect()
Returnfalseif resource mappings were manually configured,trueotherwise.
onApplicationEvent
public void onApplicationEvent(ContextRefreshedEvent event)
Description copied from interface:ApplicationListenerHandle an application event.- Specified by:
onApplicationEventin interfaceApplicationListener<ContextRefreshedEvent>- Parameters:
event- the event to respond to
detectResourceHandlers
protected void detectResourceHandlers(ApplicationContext appContext)
getForRequestUrl
public final String getForRequestUrl(HttpServletRequest request, String requestUrl)
A variation ongetForLookupPath(String)that accepts a full request URL path (i.e. including context and servlet path) and returns the full request URL path to expose for public use.- Parameters:
request- the current requestrequestUrl- the request URL path to resolve- Returns:
- the resolved public URL path, or
nullif unresolved
getForLookupPath
public final String getForLookupPath(String lookupPath)
Compare the given path against configured resource handler mappings and if a match is found use theResourceResolverchain of the matchedResourceHttpRequestHandlerto resolve the URL path to expose for public use.It is expected that the given path is what Spring MVC would use for request mapping purposes, i.e. excluding context and servlet path portions.
If several handler mappings match, the handler used will be the one configured with the most specific pattern.
- Parameters:
lookupPath- the lookup path to check- Returns:
- the resolved public URL path, or
nullif unresolved