Avoid HandlerMapping attribute in ResourceUrlProvider
The use of the HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE in ResourceUrlProvider (as a way of saving lookup path determination) leads to incorrect results. For example when the request is forwarded the current requestUri may no longer be compariable to the value of the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE. Also where the request is mapped using a pattern, the value of PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE is not the same as the lookup path. This change removes the use of the attribute from ResourceUrlProvider and instead always determines the lookup path when getForRequestUrl is called. Issue: SPR-12332
This commit is contained in:
@@ -172,24 +172,17 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Getting resource URL for requestURL=" + requestUrl);
|
||||
}
|
||||
|
||||
String pathWithinMapping = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
||||
if (pathWithinMapping == null) {
|
||||
logger.trace("Request attribute with lookup path not found, calculating instead.");
|
||||
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);
|
||||
|
||||
int index = getLookupPathIndex(request);
|
||||
String prefix = requestUrl.substring(0, index);
|
||||
String lookupPath = requestUrl.substring(index);
|
||||
String resolvedPath = getForLookupPath(lookupPath);
|
||||
String resolvedLookupPath = getForLookupPath(lookupPath);
|
||||
return (resolvedLookupPath != null) ? prefix + resolvedLookupPath : null;
|
||||
}
|
||||
|
||||
return (resolvedPath != null) ? prefix + resolvedPath : null;
|
||||
private int getLookupPathIndex(HttpServletRequest request) {
|
||||
String requestUri = getPathHelper().getRequestUri(request);
|
||||
String lookupPath = getPathHelper().getLookupPathForRequest(request);
|
||||
return requestUri.indexOf(lookupPath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user