Prepend leading slash in ResourceUrlProvider

The getForRequestUrl method of ResourceUrlProvider uses the
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE attribute to
determine the relevant portion of the resource URL path.

However there are cases when that attribute may not have a leading
(e.g. when the current URL was matched to a prefix-based pattern
and hence extracted via PathMatcher#extractPathWithinPattern), which
interferes with the matching of resource URL paths to patterns.

This change ensures a leading slash is present

Issue: SPR-12281
This commit is contained in:
Rossen Stoyanchev
2014-10-14 14:18:04 -04:00
parent fa4ba2a82b
commit 24d77f3272
4 changed files with 28 additions and 12 deletions

View File

@@ -179,6 +179,9 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
pathWithinMapping = getPathHelper().getLookupPathForRequest(request);
}
// When extracted with PathMatcher, pathWithinMapping won't have leading slash
pathWithinMapping = (pathWithinMapping.charAt(0) == '/' ? pathWithinMapping : "/" + pathWithinMapping);
int index = getPathHelper().getRequestUri(request).indexOf(pathWithinMapping);
Assert.state(index != -1, "Failed to determine lookup path: " + requestUrl);