public class ResourceUrlProvider extends Object implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware
This class is aware of Spring MVC handler mappings used to serve static
resources and uses the ResourceResolver
chains of the configured
ResourceHttpRequestHandler
s to make its decisions.
Constructor and Description |
---|
ResourceUrlProvider() |
Modifier and Type | Method and Description |
---|---|
protected void |
detectResourceHandlers(ApplicationContext appContext) |
String |
getForLookupPath(String lookupPath)
Compare the given path against configured resource handler mappings and
if a match is found use the
ResourceResolver chain of the matched
ResourceHttpRequestHandler to resolve the URL path to expose for
public use. |
String |
getForRequestUrl(HttpServletRequest request,
String requestUrl)
A variation on
getForLookupPath(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 Spring
ApplicationContext is refreshed. |
PathMatcher |
getPathMatcher()
Return the configured
PathMatcher . |
UrlPathHelper |
getUrlPathHelper()
Return the configured
UrlPathHelper . |
boolean |
isAutodetect()
Return
false if resource mappings were manually configured,
true otherwise. |
void |
onApplicationEvent(ContextRefreshedEvent event)
Handle an application event.
|
void |
setApplicationContext(ApplicationContext applicationContext)
Set the ApplicationContext that this object runs in.
|
void |
setHandlerMap(Map<String,ResourceHttpRequestHandler> handlerMap)
Manually configure the resource mappings.
|
void |
setPathMatcher(PathMatcher pathMatcher)
Configure a
PathMatcher to use when comparing target lookup path
against resource mappings. |
void |
setUrlPathHelper(UrlPathHelper urlPathHelper)
Configure a
UrlPathHelper to use in
getForRequestUrl(javax.servlet.http.HttpServletRequest, String)
in order to derive the lookup path for a target request URL path. |
protected final Log logger
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
ApplicationContextAware
Invoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and
MessageSourceAware
, if applicable.
setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectApplicationContextException
- in case of context initialization errorsBeansException
- if thrown by application context methodsBeanInitializationException
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
UrlPathHelper
to use in
getForRequestUrl(javax.servlet.http.HttpServletRequest, String)
in order to derive the lookup path for a target request URL path.public UrlPathHelper getUrlPathHelper()
UrlPathHelper
.public void setPathMatcher(PathMatcher pathMatcher)
PathMatcher
to use when comparing target lookup path
against resource mappings.public PathMatcher getPathMatcher()
PathMatcher
.public void setHandlerMap(@Nullable Map<String,ResourceHttpRequestHandler> handlerMap)
Note: by default resource mappings are auto-detected
from the Spring ApplicationContext
. However if this property is
used, the auto-detection is turned off.
public Map<String,ResourceHttpRequestHandler> getHandlerMap()
ApplicationContext
is refreshed.public boolean isAutodetect()
false
if resource mappings were manually configured,
true
otherwise.public void onApplicationEvent(ContextRefreshedEvent event)
ApplicationListener
onApplicationEvent
in interface ApplicationListener<ContextRefreshedEvent>
event
- the event to respond toprotected void detectResourceHandlers(ApplicationContext appContext)
@Nullable public final String getForRequestUrl(HttpServletRequest request, String requestUrl)
getForLookupPath(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.request
- the current requestrequestUrl
- the request URL path to resolvenull
if unresolved@Nullable public final String getForLookupPath(String lookupPath)
ResourceResolver
chain of the matched
ResourceHttpRequestHandler
to 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.
lookupPath
- the lookup path to checknull
if unresolved