From b3bfa95e2bff2a1e06b37c327a70aac54d727a1c Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 26 Nov 2014 14:49:43 +0100 Subject: [PATCH] Fix ResourceUrlEncodingFilter with context mapping Prior to this change, the ResourceUrlEncodingFilter would work well when the application is mapped to "/". But when mapped to a non-empty servlet context, this filter would not properly encode URLs and apply ResourceResolver URL resolution for resources. This commit makes sure that the lookup path is properly resolved in the request URI, taking into account the servlet context. Issue: SPR-12459 --- .../resource/ResourceUrlEncodingFilter.java | 2 +- .../ResourceUrlEncodingFilterTests.java | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java index b84c64d9f9..4a1b7a6aef 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java @@ -88,7 +88,7 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter { if (this.indexLookupPath == null) { String requestUri = urlProvider.getPathHelper().getRequestUri(this.request); String lookupPath = urlProvider.getPathHelper().getLookupPathForRequest(this.request); - this.indexLookupPath = requestUri.indexOf(lookupPath); + this.indexLookupPath = requestUri.lastIndexOf(lookupPath); } } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java new file mode 100644 index 0000000000..2f44b4480d --- /dev/null +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.web.servlet.resource; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.mock.web.test.MockHttpServletRequest; +import org.springframework.mock.web.test.MockHttpServletResponse; + +import static org.junit.Assert.*; + +/** + * Unit tests for {@code ResourceUrlEncodingFilter}. + * + * @author Brian Clozel + */ +public class ResourceUrlEncodingFilterTests { + + private ResourceUrlEncodingFilter filter; + + private ResourceUrlProvider resourceUrlProvider; + + @Before + public void createFilter() throws Exception { + VersionResourceResolver versionResolver = new VersionResourceResolver(); + versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy())); + PathResourceResolver pathResolver = new PathResourceResolver(); + pathResolver.setAllowedLocations(new ClassPathResource("test/", getClass())); + List resolvers = Arrays.asList(versionResolver, pathResolver); + + this.filter = new ResourceUrlEncodingFilter(); + this.resourceUrlProvider = createResourceUrlProvider(resolvers); + } + + @Test + public void encodeURL() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); + request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider); + MockHttpServletResponse response = new MockHttpServletResponse(); + + this.filter.doFilterInternal(request, response, new FilterChain() { + @Override + public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { + String result = ((HttpServletResponse)response).encodeURL("/resources/bar.css"); + assertEquals("/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result); + } + }); + } + + @Test + public void encodeURLWithContext() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/foo"); + request.setContextPath("/context"); + request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider); + MockHttpServletResponse response = new MockHttpServletResponse(); + + this.filter.doFilterInternal(request, response, new FilterChain() { + @Override + public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { + String result = ((HttpServletResponse)response).encodeURL("/context/resources/bar.css"); + assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result); + } + }); + } + + protected ResourceUrlProvider createResourceUrlProvider(List resolvers) { + ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); + handler.setLocations(Arrays.asList(new ClassPathResource("test/", getClass()))); + handler.setResourceResolvers(resolvers); + ResourceUrlProvider urlProvider = new ResourceUrlProvider(); + urlProvider.setHandlerMap(Collections.singletonMap("/resources/**", handler)); + return urlProvider; + } + +} -- GitLab