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:
parent
f353a28ff4
commit
3d96c883d1
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ public class ResourceUrlProviderJavaConfigTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void resolvePathWithServletMappingByPrefix() throws Exception {
|
||||
public void resolvePathWithServletMappedByPrefix() throws Exception {
|
||||
this.request.setRequestURI("/myapp/myservlet/index");
|
||||
this.request.setServletPath("/myservlet");
|
||||
this.filterChain.doFilter(this.request, this.response);
|
||||
|
@ -88,6 +88,16 @@ public class ResourceUrlProviderJavaConfigTests {
|
|||
resolvePublicResourceUrlPath("/myapp/myservlet/resources/foo.css"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePathNoMatch() throws Exception {
|
||||
this.request.setRequestURI("/myapp/myservlet/index");
|
||||
this.request.setServletPath("/myservlet");
|
||||
this.filterChain.doFilter(this.request, this.response);
|
||||
|
||||
assertEquals("/myapp/myservlet/index", resolvePublicResourceUrlPath("/myapp/myservlet/index"));
|
||||
}
|
||||
|
||||
|
||||
private String resolvePublicResourceUrlPath(String path) {
|
||||
return this.servlet.wrappedResponse.encodeURL(path);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue