Class ResourceUrlProvider

java.lang.Object
org.springframework.web.servlet.resource.ResourceUrlProvider
All Implemented Interfaces:
EventListener, Aware, ApplicationContextAware, ApplicationListener<ContextRefreshedEvent>

public class ResourceUrlProvider extends Object implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware
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.

Since:
4.1
Author:
Rossen Stoyanchev, Brian Clozel
  • Field Details

    • logger

      protected final org.apache.commons.logging.Log logger
  • Constructor Details

    • ResourceUrlProvider

      public ResourceUrlProvider()
  • Method Details

    • setApplicationContext

      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
      Description copied from interface: ApplicationContextAware
      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 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.

      Specified by:
      setApplicationContext in interface ApplicationContextAware
      Parameters:
      applicationContext - the ApplicationContext object to be used by this object
      Throws:
      BeansException - if thrown by application context methods
      See Also:
    • setUrlPathHelper

      @Deprecated(since="7.0", forRemoval=true) public void setUrlPathHelper(UrlPathHelper urlPathHelper)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use of PathMatcher and UrlPathHelper is deprecated for use at runtime in web modules. After the deprecation phase, it will no longer be possible to set a customized PathMatcher instance.
      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.
    • getUrlPathHelper

      @Deprecated(since="7.0", forRemoval=true) public UrlPathHelper getUrlPathHelper()
      Deprecated, for removal: This API element is subject to removal in a future version.
      use of PathMatcher and UrlPathHelper is deprecated for use at runtime in web modules. After the deprecation phase, it will no longer be possible to set a customized PathMatcher instance.
      Return the configured UrlPathHelper.
      Since:
      4.2.8
    • setPathMatcher

      @Deprecated(since="7.0", forRemoval=true) public void setPathMatcher(PathMatcher pathMatcher)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use of PathMatcher and UrlPathHelper is deprecated for use at runtime in web modules. After the deprecation phase, it will no longer be possible to set a customized PathMatcher instance.
      Configure a PathMatcher to use when comparing target lookup path against resource mappings.
    • getPathMatcher

      @Deprecated(since="7.0", forRemoval=true) public PathMatcher getPathMatcher()
      Deprecated, for removal: This API element is subject to removal in a future version.
      use of PathMatcher and UrlPathHelper is deprecated for use at runtime in web modules. After the deprecation phase, it will no longer be possible to set a customized PathMatcher instance.
      Return the configured PathMatcher.
    • setHandlerMap

      public void setHandlerMap(@Nullable 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 Spring ApplicationContext is refreshed.
    • isAutodetect

      public boolean isAutodetect()
      Return false if resource mappings were manually configured, true otherwise.
    • onApplicationEvent

      public void onApplicationEvent(ContextRefreshedEvent event)
      Description copied from interface: ApplicationListener
      Handle an application event.
      Specified by:
      onApplicationEvent in interface ApplicationListener<ContextRefreshedEvent>
      Parameters:
      event - the event to respond to
    • detectResourceHandlers

      protected void detectResourceHandlers(ApplicationContext appContext)
    • getForRequestUrl

      public final @Nullable 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 - the current request
      requestUrl - the request URL path to resolve
      Returns:
      the resolved public URL path, or null if unresolved
    • getForLookupPath

      public final @Nullable 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 - the lookup path to check
      Returns:
      the resolved public URL path, or null if unresolved