Top Description Fields Constructors Methods
org.springframework.web.servlet.resource

public Class ResourceUrlProvider

extends Object
implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationListener, java.util.EventListener
Imports
java.util.ArrayList, .Comparator, .LinkedHashMap, .List, .Map, jakarta.servlet.http.HttpServletRequest, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeansException, org.springframework.context.ApplicationContext, .ApplicationContextAware, .ApplicationListener, org.springframework.context.event.ContextRefreshedEvent, org.springframework.lang.Nullable, org.springframework.util.AntPathMatcher, .PathMatcher, .StringUtils, org.springframework.web.servlet.HandlerMapping, org.springframework.web.servlet.handler.AbstractUrlHandlerMapping, org.springframework.web.util.UrlPathHelper

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 ResourceResolver chains of the configured ResourceHttpRequestHandlers to make its decisions.

Authors
Rossen Stoyanchev, Brian Clozel
Since
4.1

Field Summary

Modifier and TypeField and Description
private ApplicationContext
private boolean
private final Map<String, ResourceHttpRequestHandler>
protected final Log
private PathMatcher
private UrlPathHelper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
private int
getEndPathIndex(String lookupPath)

public final String

Returns:

the resolved public URL path, or null if unresolved
getForLookupPath
(String
the lookup path to check
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.

public final String

Returns:

the resolved public URL path, or null if unresolved
getForRequestUrl
(HttpServletRequest
the current request
request
,
String
the request URL path to resolve
requestUrl
)

A variation on 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.

public Map<String, ResourceHttpRequestHandler>
getHandlerMap()

Return the resource mappings, either manually configured or auto-detected when the Spring ApplicationContext is refreshed.

private int
public PathMatcher
getPathMatcher()

Return the configured PathMatcher.

public UrlPathHelper
getUrlPathHelper()

Return the configured UrlPathHelper.

public boolean
isAutodetect()

Return false if resource mappings were manually configured, true otherwise.

public void
onApplicationEvent(ContextRefreshedEvent
the event to respond to
event
)

Implements org.springframework.context.ApplicationListener.onApplicationEvent.

Handle an application event.

public void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
applicationContext
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

public void
setHandlerMap(Map<String, ResourceHttpRequestHandler> handlerMap)

Manually configure the resource mappings.

public void
setPathMatcher(PathMatcher pathMatcher)

Configure a PathMatcher to use when comparing target lookup path against resource mappings.

public void
setUrlPathHelper(UrlPathHelper urlPathHelper)

Configure a UrlPathHelper to use in getForRequestUrl(jakarta.servlet.http.HttpServletRequest, String) in order to derive the lookup path for a target request URL path.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext
Annotations
@Nullable
autodetectback to summary
private boolean autodetect
handlerMapback to summary
private final Map<String, ResourceHttpRequestHandler> handlerMap
loggerback to summary
protected final Log logger
pathMatcherback to summary
private PathMatcher pathMatcher
urlPathHelperback to summary
private UrlPathHelper urlPathHelper

Constructor Detail

ResourceUrlProviderback to summary
public ResourceUrlProvider()

Method Detail

detectResourceHandlersback to summary
protected void detectResourceHandlers(ApplicationContext appContext)
getEndPathIndexback to summary
private int getEndPathIndex(String lookupPath)
getForLookupPathback to summary
public final 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.

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:String

the lookup path to check

Returns:String

the resolved public URL path, or null if unresolved

Annotations
@Nullable
getForRequestUrlback to summary
public final String getForRequestUrl(HttpServletRequest request, String requestUrl)

A variation on 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.

Parameters
request:HttpServletRequest

the current request

requestUrl:String

the request URL path to resolve

Returns:String

the resolved public URL path, or null if unresolved

Annotations
@Nullable
getHandlerMapback to summary
public Map<String, ResourceHttpRequestHandler> getHandlerMap()

Return the resource mappings, either manually configured or auto-detected when the Spring ApplicationContext is refreshed.

getLookupPathIndexback to summary
private int getLookupPathIndex(HttpServletRequest request)
getPathMatcherback to summary
public PathMatcher getPathMatcher()

Return the configured PathMatcher.

getUrlPathHelperback to summary
public UrlPathHelper getUrlPathHelper()

Return the configured UrlPathHelper.

Since
4.2.8
isAutodetectback to summary
public boolean isAutodetect()

Return false if resource mappings were manually configured, true otherwise.

onApplicationEventback to summary
public void onApplicationEvent(ContextRefreshedEvent event)

Implements org.springframework.context.ApplicationListener.onApplicationEvent.

Doc from org.springframework.context.ApplicationListener.onApplicationEvent.

Handle an application event.

Parameters
event:ContextRefreshedEvent

the event to respond to

Annotations
@Override
setApplicationContextback to summary
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

Invoked after population of normal bean properties but before an init callback such as org.springframework.beans.factory.InitializingBean#afterPropertiesSet() or a custom init-method. Invoked after ResourceLoaderAware#setResourceLoader, ApplicationEventPublisherAware#setApplicationEventPublisher and MessageSourceAware, if applicable.

Parameters
applicationContext:ApplicationContext

the ApplicationContext object to be used by this object

Annotations
@Override
Exceptions
BeansException:
if thrown by application context methods
setHandlerMapback to summary
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.

setPathMatcherback to summary
public void setPathMatcher(PathMatcher pathMatcher)

Configure a PathMatcher to use when comparing target lookup path against resource mappings.

setUrlPathHelperback to summary
public void setUrlPathHelper(UrlPathHelper urlPathHelper)

Configure a UrlPathHelper to use in getForRequestUrl(jakarta.servlet.http.HttpServletRequest, String) in order to derive the lookup path for a target request URL path.