Cache lookup path in ResourceUrlEncodingFilter

Commit https://github.com/spring-projects/spring-framework/commit/2b97d6
introduced a change where the path within the DispatcherServlet is
determined with each call to ResourceUrlProvider.getForRequestUrl.

To avoid repeating that every time a URL is encoded through the
response, we now cache the result of the lookupPath determination in
ResourceUrlEncodingFilter.

Issue: SPR-12332
This commit is contained in:
Rossen Stoyanchev
2014-10-16 21:54:34 -04:00
parent f353a28ff4
commit 3d96c883d1
2 changed files with 26 additions and 3 deletions

View File

@@ -57,6 +57,9 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
private HttpServletRequest request;
/* Cache the index of the path within the DispatcherServlet mapping. */
private Integer indexLookupPath;
private ResourceUrlEncodingResponseWrapper(HttpServletRequest request, HttpServletResponse wrapped) {
super(wrapped);
@@ -70,8 +73,11 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
logger.debug("Request attribute exposing ResourceUrlProvider not found.");
return super.encodeURL(url);
}
String resolvedUrl = resourceUrlProvider.getForRequestUrl(this.request, url);
return (resolvedUrl != null ? super.encodeURL(resolvedUrl) : super.encodeURL(url));
initIndexLookupPath(resourceUrlProvider);
String prefix = url.substring(0, this.indexLookupPath);
String lookupPath = url.substring(this.indexLookupPath);
lookupPath = resourceUrlProvider.getForLookupPath(lookupPath);
return (lookupPath != null ? super.encodeURL(prefix + lookupPath) : super.encodeURL(url));
}
private ResourceUrlProvider getResourceUrlProvider() {
@@ -79,6 +85,13 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
return (ResourceUrlProvider) this.request.getAttribute(name);
}
private void initIndexLookupPath(ResourceUrlProvider urlProvider) {
if (this.indexLookupPath == null) {
String requestUri = urlProvider.getPathHelper().getRequestUri(this.request);
String lookupPath = urlProvider.getPathHelper().getLookupPathForRequest(this.request);
this.indexLookupPath = requestUri.indexOf(lookupPath);
}
}
}
}