Revert "Add initial cut of expanded resource handling"

This reverts commit d4a0e628d5, reversing
changes made to 8abe949734.
This commit is contained in:
Rossen Stoyanchev
2013-10-07 21:18:41 -04:00
parent 80e5513918
commit 6a323292d8
31 changed files with 27 additions and 1812 deletions

View File

@@ -0,0 +1 @@
h2 { color:white; }

View File

@@ -0,0 +1 @@
h1 { color:red; }

View File

@@ -0,0 +1 @@
function foo() { console.log("hello bar"); }

View File

@@ -0,0 +1 @@
function foo() { console.log("hello world"); }

View File

@@ -1,65 +0,0 @@
/*
* 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 org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import static org.junit.Assert.*;
/**
*
* @author Jeremy Grelle
*/
public class ExtensionMappingResourceResolverTests {
private ResourceResolverChain resolver;
private List<Resource> locations;
@Before
public void setUp() {
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
resolvers.add(new PathExtensionResourceResolver());
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 resolveLessResource() throws Exception {
String resourceId = "zoo.css";
Resource resource = new ClassPathResource("test/" + resourceId + ".less", getClass());
Resource resolved = resolver.resolveResource(null, resourceId, locations);
assertEquals(resource, resolved);
}
@Test
public void resolveLessUrl() {
String resourceId = "zoo.css";
String url = "zoo.css";
assertEquals(url, resolver.resolveUrlPath(resourceId, locations));
}
}

View File

@@ -1,123 +0,0 @@
/*
* 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);
}
}

View File

@@ -1,110 +0,0 @@
/*
* 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);
}
}

View File

@@ -43,7 +43,7 @@ public class ResourceHttpRequestHandlerTests {
private ResourceHttpRequestHandler handler;
@Before
public void setUp() throws Exception {
public void setUp() {
List<Resource> resourcePaths = new ArrayList<Resource>();
resourcePaths.add(new ClassPathResource("test/", getClass()));
resourcePaths.add(new ClassPathResource("testalternatepath/", getClass()));
@@ -51,7 +51,6 @@ public class ResourceHttpRequestHandlerTests {
handler.setLocations(resourcePaths);
handler.setCacheSeconds(3600);
handler.setServletContext(new TestServletContext());
handler.afterPropertiesSet();
}
@Test

View File

@@ -1,154 +0,0 @@
/*
* 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.Collections;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
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.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import static org.junit.Assert.*;
/**
*
* @author Rossen Stoyanchev
*/
public class ResourceUrlFilterTests {
private MockFilterChain filterChain;
private TestServlet servlet;
@Test
public void rootServletMapping() throws Exception {
initFilterChain(WebConfig.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
request.setRequestURI("/myapp/index.html");
request.setContextPath("/myapp");
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 {
initFilterChain(WebConfig.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
request.setRequestURI("/myapp/myservlet/index.html");
request.setContextPath("/myapp");
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 {
initFilterChain(WebConfig.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
request.setRequestURI("/myapp/index.html");
request.setContextPath("/myapp");
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);
}
private void initFilterChain(Class<?> configClass) throws ServletException {
MockServletContext servletContext = new MockServletContext();
AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
cxt.setServletContext(servletContext);
cxt.register(configClass);
cxt.refresh();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, cxt);
ResourceUrlFilter filter = new ResourceUrlFilter();
filter.setServletContext(servletContext);
filter.initFilterBean();
this.servlet = new TestServlet();
this.filterChain = new MockFilterChain(servlet, filter);
}
@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);
}
@Bean
public ResourceUrlGenerator resourceUrlGenerator() {
ResourceUrlGenerator generator = new ResourceUrlGenerator();
SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) resourceHandlerMapping();
generator.setResourceHandlerMappings(Collections.singletonList(handlerMapping));
return generator;
}
}
private static class TestServlet extends HttpServlet {
private HttpServletResponse response;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
this.response = response;
}
}
}

View File

@@ -1,100 +0,0 @@
/*
* 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.Collections;
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 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import static org.junit.Assert.*;
/**
*
* @author Jeremy Grelle
* @author Rossen Stoyanchev
*/
public class ResourceUrlGeneratorTests {
ResourceHttpRequestHandler handler;
SimpleUrlHandlerMapping mapping;
ResourceUrlGenerator generator;
@Before
public void setUp() {
List<Resource> locations = new ArrayList<Resource>();
locations.add(new ClassPathResource("test/", getClass()));
locations.add(new ClassPathResource("testalternatepath/", getClass()));
Map<String, ResourceHttpRequestHandler> urlMap = new HashMap<String, ResourceHttpRequestHandler>();
handler = new ResourceHttpRequestHandler();
handler.setLocations(locations);
urlMap.put("/resources/**", handler);
mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(urlMap);
}
private void initGenerator() {
generator = new ResourceUrlGenerator();
generator.setResourceHandlerMappings(Collections.singletonList(this.mapping));
}
@Test
public void getStaticResourceUrl() {
initGenerator();
String url = generator.getResourceUrl("/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());
handler.setResourceResolvers(resolvers);
initGenerator();
String url = generator.getResourceUrl("/resources/foo.css");
assertEquals("/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", url);
}
@Test
public void getExtensionMappedResourceUrl() {
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
resolvers.add(new PathExtensionResourceResolver());
resolvers.add(new PathResourceResolver());
handler.setResourceResolvers(resolvers);
initGenerator();
String url = generator.getResourceUrl("/resources/zoo.css");
assertEquals("/resources/zoo.css", url);
}
}