Expand static resource handling mechanism
An initial commit with expanded support for static resource handling: - Add ResourceResolver strategy for resolving a request to a Resource along with a few implementations. - Add PublicResourceUrlProvider to get URLs for client-side use. - Add ResourceUrlEncodingFilter and PublicResourceUrlProviderExposingInterceptor along with initial MVC Java config support. Issue: SPR-10933
This commit is contained in:
@@ -16,8 +16,12 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
@@ -25,6 +29,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
||||
import org.springframework.web.servlet.resource.ResourceResolver;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -89,6 +94,17 @@ public class ResourceHandlerRegistryTests {
|
||||
assertFalse(registry.hasMappingForPattern("/whatever"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourceResolversAndTransformers() {
|
||||
ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
|
||||
List<ResourceResolver> resolvers = Arrays.<ResourceResolver>asList(resolver);
|
||||
registry.setResourceResolvers(resolvers);
|
||||
|
||||
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) registry.getHandlerMapping();
|
||||
ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) hm.getUrlMap().values().iterator().next();
|
||||
|
||||
assertEquals(resolvers, handler.getResourceResolvers());
|
||||
}
|
||||
|
||||
private ResourceHttpRequestHandler getHandler(String pathPattern) {
|
||||
SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) registry.getHandlerMapping();
|
||||
|
||||
@@ -62,6 +62,7 @@ import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
import org.springframework.web.servlet.resource.PublicResourceUrlProviderExposingInterceptor;
|
||||
|
||||
/**
|
||||
* A test fixture with a sub-class of {@link WebMvcConfigurationSupport} that
|
||||
@@ -94,9 +95,10 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
rmHandlerMapping.afterPropertiesSet();
|
||||
HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
|
||||
assertNotNull(chain.getInterceptors());
|
||||
assertEquals(2, chain.getInterceptors().length);
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertEquals(LocaleChangeInterceptor.class, chain.getInterceptors()[0].getClass());
|
||||
assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
|
||||
assertEquals(PublicResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
|
||||
|
||||
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) webConfig.viewControllerHandlerMapping();
|
||||
handlerMapping.setApplicationContext(webAppContext);
|
||||
|
||||
@@ -55,6 +55,7 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.resource.PublicResourceUrlProviderExposingInterceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -116,8 +117,9 @@ public class WebMvcConfigurationSupportTests {
|
||||
HandlerExecutionChain chain = handlerMapping.getHandler(request);
|
||||
|
||||
assertNotNull(chain.getInterceptors());
|
||||
assertEquals(2, chain.getInterceptors().length);
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
|
||||
assertEquals(PublicResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
h2 { color:white; }
|
||||
@@ -1 +0,0 @@
|
||||
h1 { color:red; }
|
||||
@@ -1 +0,0 @@
|
||||
function foo() { console.log("hello bar"); }
|
||||
@@ -1 +0,0 @@
|
||||
function foo() { console.log("hello world"); }
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class FingerprintResourceResolverTests {
|
||||
|
||||
private ResourceResolverChain chain;
|
||||
|
||||
private FingerprintResourceResolver resolver = new FingerprintResourceResolver();
|
||||
|
||||
private List<Resource> locations;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(resolver);
|
||||
resolvers.add(new PathResourceResolver());
|
||||
chain = new DefaultResourceResolverChain(resolvers);
|
||||
locations = new ArrayList<Resource>();
|
||||
locations.add(new ClassPathResource("test/", getClass()));
|
||||
locations.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveWithoutHash() throws Exception {
|
||||
String file = "bar.css";
|
||||
Resource expected = new ClassPathResource("test/" + file, getClass());
|
||||
Resource actual = chain.resolveResource(null, file, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveWithHashNoMatch() throws Exception {
|
||||
String file = "bogus-e36d2e05253c6c7085a91522ce43a0b4.css";
|
||||
assertNull(chain.resolveResource(null, file, locations));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveStaticFingerprintedResource() throws Exception {
|
||||
String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
|
||||
Resource expected = new ClassPathResource("test/"+file, getClass());
|
||||
Resource actual = chain.resolveResource(null, file, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveDynamicFingerprintedResource() throws Exception {
|
||||
Resource expected = new ClassPathResource("test/bar.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream()));
|
||||
String path = "/bar-" + hash + ".css";
|
||||
Resource actual = chain.resolveResource(null, path, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveWithMultipleExtensions() throws Exception {
|
||||
Resource expected = new ClassPathResource("test/bar.min.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream()));
|
||||
String path = "/bar.min-" + hash + ".css";
|
||||
Resource actual = chain.resolveResource(null, path, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveWithMultipleHyphens() throws Exception {
|
||||
Resource expected = new ClassPathResource("test/foo-bar/foo-bar.css", getClass());
|
||||
String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream()));
|
||||
String path = "/foo-bar/foo-bar-" + hash + ".css";
|
||||
Resource actual = chain.resolveResource(null, path, locations);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extractHash() throws Exception {
|
||||
String hash = "7fbe76cdac6093784895bb4989203e5a";
|
||||
String path = "font-awesome/css/font-awesome.min-" + hash + ".css";
|
||||
|
||||
Method method = ReflectionUtils.findMethod(resolver.getClass(), "extractHash", String.class);
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
String result = (String) ReflectionUtils.invokeMethod(method, resolver, path);
|
||||
|
||||
assertEquals(hash, result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class GzipResourceResolverTests {
|
||||
|
||||
private ResourceResolverChain resolver;
|
||||
|
||||
private List<Resource> locations;
|
||||
|
||||
@BeforeClass
|
||||
public static void createGzippedResources() throws IOException {
|
||||
Resource location = new ClassPathResource("test/", GzipResourceResolverTests.class);
|
||||
Resource jsFile = new FileSystemResource(location.createRelative("/js/foo.js").getFile());
|
||||
Resource gzJsFile = jsFile.createRelative("foo.js.gz");
|
||||
Resource fingerPrintedFile = new FileSystemResource(location.createRelative("foo-e36d2e05253c6c7085a91522ce43a0b4.css").getFile());
|
||||
Resource gzFingerPrintedFile = fingerPrintedFile.createRelative("foo-e36d2e05253c6c7085a91522ce43a0b4.css.gz");
|
||||
|
||||
if (gzJsFile.getFile().createNewFile()) {
|
||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzJsFile.getFile()));
|
||||
FileCopyUtils.copy(jsFile.getInputStream(), out);
|
||||
}
|
||||
|
||||
if (gzFingerPrintedFile.getFile().createNewFile()) {
|
||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFingerPrintedFile.getFile()));
|
||||
FileCopyUtils.copy(fingerPrintedFile.getInputStream(), out);
|
||||
}
|
||||
|
||||
assertTrue(gzJsFile.exists());
|
||||
assertTrue(gzFingerPrintedFile.exists());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(new GzipResourceResolver());
|
||||
resolvers.add(new FingerprintResourceResolver());
|
||||
resolvers.add(new PathResourceResolver());
|
||||
resolver = new DefaultResourceResolverChain(resolvers);
|
||||
locations = new ArrayList<Resource>();
|
||||
locations.add(new ClassPathResource("test/", getClass()));
|
||||
locations.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveGzippedFile() throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept-Encoding", "gzip");
|
||||
String file = "js/foo.js";
|
||||
String gzFile = file+".gz";
|
||||
Resource resource = new ClassPathResource("test/"+gzFile, getClass());
|
||||
Resource resolved = resolver.resolveResource(request, file, locations);
|
||||
|
||||
assertEquals(resource.getDescription(), resolved.getDescription());
|
||||
assertEquals(new ClassPathResource("test/"+file).getFilename(), resolved.getFilename());
|
||||
assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class,
|
||||
resolved instanceof EncodedResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveFingerprintedGzippedFile() throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept-Encoding", "gzip");
|
||||
String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
|
||||
String gzFile = file+".gz";
|
||||
Resource resource = new ClassPathResource("test/"+gzFile, getClass());
|
||||
Resource resolved = resolver.resolveResource(request, file, locations);
|
||||
|
||||
assertEquals(resource.getDescription(), resolved.getDescription());
|
||||
assertEquals(new ClassPathResource("test/"+file).getFilename(), resolved.getFilename());
|
||||
assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class,
|
||||
resolved instanceof EncodedResource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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 org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link PrefixResourceResolver}
|
||||
*
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class PrefixResourceResolverTests {
|
||||
|
||||
private ResourceResolverChain resolver;
|
||||
|
||||
private List<Resource> locations;
|
||||
|
||||
private final String shaPrefix = "1df341f";
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(new PrefixResourceResolver(this.shaPrefix));
|
||||
resolvers.add(new PathResourceResolver());
|
||||
this.resolver = new DefaultResourceResolverChain(resolvers);
|
||||
this.locations = new ArrayList<Resource>();
|
||||
this.locations.add(new ClassPathResource("test/", getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveResource() {
|
||||
String resourceId = "foo.css";
|
||||
Resource expected = new ClassPathResource("test/foo.css", getClass());
|
||||
Resource actual = this.resolver.resolveResource(null, "/" + this.shaPrefix + "/" + resourceId, this.locations);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveUrlPath() {
|
||||
String resourceId = "/foo.css";
|
||||
String url = "/" + this.shaPrefix + resourceId;
|
||||
assertEquals(url, resolver.resolveUrlPath(resourceId, locations));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testFailPrefixResolverConstructor() {
|
||||
PrefixResourceResolver resolver = new PrefixResourceResolver("");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.test.MockFilterChain;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.config.annotation.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Integration tests using {@link ResourceUrlEncodingFilter} and
|
||||
* {@link PublicResourceUrlProvider} with the latter configured in Spring MVC Java config.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class PublicResourceUrlProviderJavaConfigTests {
|
||||
|
||||
private MockFilterChain filterChain;
|
||||
|
||||
private TestServlet servlet;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
|
||||
this.servlet = new TestServlet();
|
||||
this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter());
|
||||
|
||||
AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
|
||||
cxt.setServletContext(new MockServletContext());
|
||||
cxt.register(WebConfig.class);
|
||||
cxt.refresh();
|
||||
|
||||
PublicResourceUrlProvider urlProvider = cxt.getBean(PublicResourceUrlProvider.class);
|
||||
|
||||
this.request = new MockHttpServletRequest("GET", "/");
|
||||
request.setAttribute(PublicResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void rootServletMapping() throws Exception {
|
||||
|
||||
this.request.setRequestURI("/");
|
||||
this.request.setMethod("GET");
|
||||
this.request.setRequestURI("/myapp/index.html");
|
||||
this.request.setContextPath("/myapp");
|
||||
this.request.setServletPath("/index.html");
|
||||
this.filterChain.doFilter(request, new MockHttpServletResponse());
|
||||
|
||||
String actual = this.servlet.response.encodeURL("/myapp/resources/foo.css");
|
||||
assertEquals("/myapp/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefixServletMapping() throws Exception {
|
||||
|
||||
this.request.setRequestURI("/myapp/myservlet/index.html");
|
||||
this.request.setContextPath("/myapp");
|
||||
this.request.setServletPath("/myservlet");
|
||||
this.filterChain.doFilter(request, new MockHttpServletResponse());
|
||||
|
||||
String actual = this.servlet.response.encodeURL("/myapp/myservlet/resources/foo.css");
|
||||
assertEquals("/myapp/myservlet/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionServletMapping() throws Exception {
|
||||
|
||||
this.request.setRequestURI("/myapp/index.html");
|
||||
this.request.setContextPath("/myapp");
|
||||
this.request.setServletPath("/index.html");
|
||||
this.filterChain.doFilter(request, new MockHttpServletResponse());
|
||||
|
||||
String actual = this.servlet.response.encodeURL("/myapp/resources/foo.css");
|
||||
assertEquals("/myapp/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", actual);
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class WebConfig extends WebMvcConfigurationSupport {
|
||||
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
|
||||
List<ResourceResolver> resourceResolvers = new ArrayList<>();
|
||||
resourceResolvers.add(new FingerprintResourceResolver());
|
||||
resourceResolvers.add(new PathResourceResolver());
|
||||
|
||||
registry.addResourceHandler("/resources/**")
|
||||
.addResourceLocations("classpath:org/springframework/web/servlet/resource/test/")
|
||||
.setResourceResolvers(resourceResolvers);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestServlet extends HttpServlet {
|
||||
|
||||
private HttpServletResponse response;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
|
||||
this.response = response;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.web.servlet.resource.PublicResourceUrlProvider}.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class PublicResourceUrlProviderTests {
|
||||
|
||||
private PublicResourceUrlProvider translator;
|
||||
|
||||
private ResourceHttpRequestHandler handler;
|
||||
|
||||
private Map<String, ResourceHttpRequestHandler> handlerMap;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
List<Resource> locations = new ArrayList<Resource>();
|
||||
locations.add(new ClassPathResource("test/", getClass()));
|
||||
locations.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
|
||||
this.handler = new ResourceHttpRequestHandler();
|
||||
this.handler.setLocations(locations);
|
||||
|
||||
this.handlerMap = new HashMap<String, ResourceHttpRequestHandler>();
|
||||
this.handlerMap.put("/resources/**", this.handler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStaticResourceUrl() {
|
||||
initTranslator();
|
||||
|
||||
String url = this.translator.getForLookupPath("/resources/foo.css");
|
||||
assertEquals("/resources/foo.css", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFingerprintedResourceUrl() {
|
||||
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
resolvers.add(new FingerprintResourceResolver());
|
||||
resolvers.add(new PathResourceResolver());
|
||||
this.handler.setResourceResolvers(resolvers);
|
||||
initTranslator();
|
||||
|
||||
String url = this.translator.getForLookupPath("/resources/foo.css");
|
||||
assertEquals("/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", url);
|
||||
}
|
||||
|
||||
private void initTranslator() {
|
||||
this.translator = new PublicResourceUrlProvider();
|
||||
this.translator.setHandlerMap(this.handlerMap);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public class ResourceHttpRequestHandlerTests {
|
||||
private ResourceHttpRequestHandler handler;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws Exception {
|
||||
List<Resource> resourcePaths = new ArrayList<Resource>();
|
||||
resourcePaths.add(new ClassPathResource("test/", getClass()));
|
||||
resourcePaths.add(new ClassPathResource("testalternatepath/", getClass()));
|
||||
@@ -51,6 +51,7 @@ public class ResourceHttpRequestHandlerTests {
|
||||
handler.setLocations(resourcePaths);
|
||||
handler.setCacheSeconds(3600);
|
||||
handler.setServletContext(new TestServletContext());
|
||||
handler.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user