Revert "Add initial cut of expanded resource handling"
This reverts commitd4a0e628d5, reversing changes made to8abe949734.
This commit is contained in:
@@ -23,10 +23,7 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
||||
import org.springframework.web.servlet.resource.ResourceResolver;
|
||||
import org.springframework.web.servlet.resource.ResourceTransformer;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a resource handlers.
|
||||
@@ -46,11 +43,6 @@ public class ResourceHandlerRegistration {
|
||||
|
||||
private Integer cachePeriod;
|
||||
|
||||
private List<ResourceResolver> resourceResolvers;
|
||||
|
||||
private List<ResourceTransformer> resourceTransformers;
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@link ResourceHandlerRegistration} instance.
|
||||
* @param resourceLoader a resource loader for turning a String location into a {@link Resource}
|
||||
@@ -78,23 +70,6 @@ public class ResourceHandlerRegistration {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the list of {@link ResourceResolver}s to use.
|
||||
* <p>
|
||||
* By default {@link PathResourceResolver} is configured. If using this property, it
|
||||
* is recommended to add {@link PathResourceResolver} as the last resolver.
|
||||
*/
|
||||
public void setResourceResolvers(List<ResourceResolver> resourceResolvers) {
|
||||
this.resourceResolvers = resourceResolvers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the list of {@link ResourceTransformer}s to use.
|
||||
*/
|
||||
public void setResourceTransformers(List<ResourceTransformer> transformers) {
|
||||
this.resourceTransformers = transformers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the cache period for the resources served by the resource handler, in seconds. The default is to not
|
||||
* send any cache headers but to rely on last-modified timestamps only. Set to 0 in order to send cache headers
|
||||
@@ -120,15 +95,9 @@ public class ResourceHandlerRegistration {
|
||||
protected ResourceHttpRequestHandler getRequestHandler() {
|
||||
Assert.isTrue(!CollectionUtils.isEmpty(locations), "At least one location is required for resource handling.");
|
||||
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
|
||||
if (this.resourceResolvers != null) {
|
||||
requestHandler.setResourceResolvers(this.resourceResolvers);
|
||||
}
|
||||
if (this.resourceTransformers != null) {
|
||||
requestHandler.setResourceTransformers(this.resourceTransformers);
|
||||
}
|
||||
requestHandler.setLocations(this.locations);
|
||||
if (this.cachePeriod != null) {
|
||||
requestHandler.setCacheSeconds(this.cachePeriod);
|
||||
requestHandler.setLocations(locations);
|
||||
if (cachePeriod != null) {
|
||||
requestHandler.setCacheSeconds(cachePeriod);
|
||||
}
|
||||
return requestHandler;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
@@ -99,12 +98,6 @@ public class ResourceHandlerRegistry {
|
||||
ResourceHttpRequestHandler requestHandler = registration.getRequestHandler();
|
||||
requestHandler.setServletContext(servletContext);
|
||||
requestHandler.setApplicationContext(applicationContext);
|
||||
try {
|
||||
requestHandler.afterPropertiesSet();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", e);
|
||||
}
|
||||
urlMap.put(pathPattern, requestHandler);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,113 +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 javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
class DefaultResourceResolverChain implements ResourceResolverChain {
|
||||
|
||||
private static Log logger = LogFactory.getLog(DefaultResourceResolverChain.class);
|
||||
|
||||
private final List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
|
||||
|
||||
private int index = -1;
|
||||
|
||||
|
||||
public DefaultResourceResolverChain(List<ResourceResolver> resolvers) {
|
||||
this.resolvers.addAll((resolvers != null) ? resolvers : new ArrayList<ResourceResolver>());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Resource resolveResource(HttpServletRequest request, String requestPath, List<Resource> locations) {
|
||||
ResourceResolver resolver = getNextResolver();
|
||||
if (resolver == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
logBefore(resolver);
|
||||
Resource resource = resolver.resolveResource(request, requestPath, locations, this);
|
||||
logAfter(resolver, resource);
|
||||
return resource;
|
||||
}
|
||||
finally {
|
||||
this.index--;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveUrlPath(String resourcePath, List<Resource> locations) {
|
||||
ResourceResolver resolver = getNextResolver();
|
||||
if (resolver == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
logBefore(resolver);
|
||||
String urlPath = resolver.resolveUrlPath(resourcePath, locations, this);
|
||||
logAfter(resolver, urlPath);
|
||||
return urlPath;
|
||||
}
|
||||
finally {
|
||||
this.index--;
|
||||
}
|
||||
}
|
||||
|
||||
private ResourceResolver getNextResolver() {
|
||||
|
||||
Assert.state(this.index <= this.resolvers.size(),
|
||||
"Current index exceeds the number of configured ResourceResolver's");
|
||||
|
||||
if (this.index == (this.resolvers.size() - 1)) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("No more ResourceResolver's to delegate to, returning null");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
this.index++;
|
||||
return this.resolvers.get(this.index);
|
||||
}
|
||||
|
||||
private void logBefore(ResourceResolver resolver) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Calling " + resolver.getClass().getName() + " at index [" + this.index + "]");
|
||||
}
|
||||
}
|
||||
|
||||
private void logAfter(ResourceResolver resolver, Object result) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace(resolver.getClass().getName() + " returned " + result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +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 org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface EncodedResource extends Resource {
|
||||
|
||||
public String getContentEncoding();
|
||||
|
||||
}
|
||||
@@ -1,119 +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.IOException;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public class FingerprintResourceResolver implements ResourceResolver {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(FingerprintResourceResolver.class);
|
||||
|
||||
private Pattern pattern = Pattern.compile("-(\\S*)\\.");
|
||||
|
||||
|
||||
@Override
|
||||
public Resource resolveResource(HttpServletRequest request, String requestPath,
|
||||
List<Resource> locations, ResourceResolverChain chain) {
|
||||
|
||||
// First try the resolved full path, in case resource has been written that way to disk at build-time
|
||||
// or the resource is requested without fingerprint
|
||||
|
||||
Resource resolved = chain.resolveResource(request, requestPath, locations);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
// Now try extracting and matching the hash for dev mode
|
||||
String hash = extractHash(requestPath);
|
||||
if (StringUtils.isEmpty(hash)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String simplePath = StringUtils.delete(requestPath, "-" + hash);
|
||||
Resource baseResource = chain.resolveResource(request, simplePath, locations);
|
||||
if (baseResource == null) {
|
||||
logger.debug("Failed to find resource after removing fingerprint: " + simplePath);
|
||||
return null;
|
||||
}
|
||||
|
||||
String candidateHash = calculateHash(baseResource);
|
||||
if (candidateHash.equals(hash)) {
|
||||
logger.debug("Fingerprint match succeeded.");
|
||||
return baseResource;
|
||||
}
|
||||
else {
|
||||
logger.debug("Potential resource found, but fingerprint doesn't match.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String extractHash(String path) {
|
||||
Matcher matcher = this.pattern.matcher(path);
|
||||
if (matcher.find()) {
|
||||
logger.debug("Found fingerprint in path: " + matcher.group(1));
|
||||
String match = matcher.group(1);
|
||||
return match.contains("-") ? match.substring(match.lastIndexOf("-") + 1) : match;
|
||||
}
|
||||
else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private String calculateHash(Resource resource) {
|
||||
try {
|
||||
byte[] content = FileCopyUtils.copyToByteArray(resource.getInputStream());
|
||||
return DigestUtils.md5DigestAsHex(content);
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.error("Failed to calculate hash on resource " + resource.toString());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveUrlPath(String resourcePath, List<Resource> locations, ResourceResolverChain chain) {
|
||||
// TODO - Consider caching here for better efficiency
|
||||
String baseUrl = chain.resolveUrlPath(resourcePath, locations);
|
||||
if (StringUtils.hasText(baseUrl)) {
|
||||
Resource original = chain.resolveResource(null, resourcePath, locations);
|
||||
String hash = calculateHash(original);
|
||||
return StringUtils.stripFilenameExtension(baseUrl)
|
||||
+ "-" + hash + "." + StringUtils.getFilenameExtension(baseUrl);
|
||||
}
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,148 +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.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* A {@link ResourceResolver} that lets the next resolver in the chain locate a Resource
|
||||
* and then attempts to find a variation of that Resource with ".gz" extension. This
|
||||
* resolver will only get involved if the client has indicated it supports gzipped
|
||||
* responses through the "Accept-Encoding" header.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public class GzipResourceResolver implements ResourceResolver {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(GzipResourceResolver.class);
|
||||
|
||||
|
||||
@Override
|
||||
public Resource resolveResource(HttpServletRequest request, String requestPath,
|
||||
List<Resource> locations, ResourceResolverChain chain) {
|
||||
|
||||
Resource resource = chain.resolveResource(request, requestPath, locations);
|
||||
if ((resource == null) || !isGzipAccepted(request)) {
|
||||
return resource;
|
||||
}
|
||||
|
||||
try {
|
||||
Resource gzipped = new GzippedResource(resource);
|
||||
if (gzipped.exists()) {
|
||||
return gzipped;
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.trace("No gzipped resource for " + resource.getFilename(), e);
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
private boolean isGzipAccepted(HttpServletRequest request) {
|
||||
String value = request.getHeader("Accept-Encoding");
|
||||
return ((value != null) && value.toLowerCase().contains("gzip"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveUrlPath(String resourcePath, List<Resource> locations, ResourceResolverChain chain) {
|
||||
return chain.resolveUrlPath(resourcePath, locations);
|
||||
}
|
||||
|
||||
|
||||
private static final class GzippedResource extends AbstractResource implements EncodedResource {
|
||||
|
||||
private final Resource original;
|
||||
|
||||
private final Resource gzipped;
|
||||
|
||||
|
||||
public GzippedResource(Resource original) throws IOException {
|
||||
this.original = original;
|
||||
this.gzipped = original.createRelative(original.getFilename() + ".gz");
|
||||
}
|
||||
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return this.gzipped.getInputStream();
|
||||
}
|
||||
|
||||
public boolean exists() {
|
||||
return this.gzipped.exists();
|
||||
}
|
||||
|
||||
public boolean isReadable() {
|
||||
return this.gzipped.isReadable();
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return this.gzipped.isOpen();
|
||||
}
|
||||
|
||||
public URL getURL() throws IOException {
|
||||
return this.gzipped.getURL();
|
||||
}
|
||||
|
||||
public URI getURI() throws IOException {
|
||||
return this.gzipped.getURI();
|
||||
}
|
||||
|
||||
public File getFile() throws IOException {
|
||||
return this.gzipped.getFile();
|
||||
}
|
||||
|
||||
public long contentLength() throws IOException {
|
||||
return this.gzipped.contentLength();
|
||||
}
|
||||
|
||||
public long lastModified() throws IOException {
|
||||
return this.gzipped.lastModified();
|
||||
}
|
||||
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
return this.gzipped.createRelative(relativePath);
|
||||
}
|
||||
|
||||
public String getFilename() {
|
||||
return this.original.getFilename();
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this.gzipped.getDescription();
|
||||
}
|
||||
|
||||
public String getContentEncoding() {
|
||||
return "gzip";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +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.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.lesscss.LessCompiler;
|
||||
import org.lesscss.LessException;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @since 4.0
|
||||
*/
|
||||
public class LessResourceTransformer implements ResourceTransformer {
|
||||
|
||||
private static final String LESS_EXT = "less";
|
||||
|
||||
private final LessCompiler compiler = new LessCompiler();
|
||||
|
||||
|
||||
@Override
|
||||
public Resource transform(Resource original) throws IOException {
|
||||
TransformedResource transformed;
|
||||
try {
|
||||
String content = "";
|
||||
if (original instanceof TransformedResource) {
|
||||
content = ((TransformedResource) original).getContentAsString();
|
||||
}
|
||||
else {
|
||||
content = this.compiler.compile(original.getFile());
|
||||
}
|
||||
transformed = new TransformedResource(original.getFilename().replace(
|
||||
"." + LESS_EXT, ""), content.getBytes("UTF-8"), original.lastModified());
|
||||
}
|
||||
catch (LessException ex) {
|
||||
//TODO - Nicely print out the compilation error
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return transformed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean willTransform(HttpServletRequest request, Resource original) {
|
||||
return LESS_EXT.equals(StringUtils.getFilenameExtension(original.getFilename()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,127 +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.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public class PathExtensionResourceResolver implements ResourceResolver {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(PathExtensionResourceResolver.class);
|
||||
|
||||
private final boolean compareTimeStamp;
|
||||
|
||||
|
||||
public PathExtensionResourceResolver() {
|
||||
this.compareTimeStamp = false;
|
||||
}
|
||||
|
||||
public PathExtensionResourceResolver(boolean compareTimeStamp) {
|
||||
this.compareTimeStamp = compareTimeStamp;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Resource resolveResource(HttpServletRequest request, String requestPath,
|
||||
List<Resource> locations, ResourceResolverChain chain) {
|
||||
|
||||
Resource resource = chain.resolveResource(request, requestPath, locations);
|
||||
if ((resource != null) && !this.compareTimeStamp) {
|
||||
return resource;
|
||||
}
|
||||
|
||||
for (Resource location : locations) {
|
||||
String baseFilename = StringUtils.getFilename(requestPath);
|
||||
try {
|
||||
Resource basePath = location.createRelative(StringUtils.delete(requestPath, baseFilename));
|
||||
if (basePath.getFile().isDirectory()) {
|
||||
for (String fileName : basePath.getFile().list(new ExtensionFilenameFilter(baseFilename))) {
|
||||
//Always use the first match
|
||||
Resource matched = basePath.createRelative(fileName);
|
||||
if ((resource == null) || (matched.lastModified() > resource.lastModified())) {
|
||||
return matched;
|
||||
}
|
||||
else {
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.trace("Error occurred locating resource based on file extension mapping", e);
|
||||
}
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveUrlPath(String resourcePath, List<Resource> locations,
|
||||
ResourceResolverChain chain) {
|
||||
|
||||
String resolved = chain.resolveUrlPath(resourcePath, locations);
|
||||
if (StringUtils.hasText(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
Resource mappedResource = resolveResource(null, resourcePath, locations, chain);
|
||||
if (mappedResource != null) {
|
||||
return resourcePath;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static final class ExtensionFilenameFilter implements FilenameFilter {
|
||||
|
||||
private final String filename;
|
||||
|
||||
private final String extension;
|
||||
|
||||
private final int extensionLength;
|
||||
|
||||
|
||||
public ExtensionFilenameFilter(String filename) {
|
||||
this.filename = filename;
|
||||
this.extension = "." + StringUtils.getFilenameExtension(filename);
|
||||
this.extensionLength = this.extension.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(File directory, String name) {
|
||||
return (name.contains(this.extension)
|
||||
&& this.filename.equals(name.substring(0, name.lastIndexOf(this.extension) + this.extensionLength)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,78 +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.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* A simple path-based {@link ResourceResolver} that appends the request path to each
|
||||
* configured Resource location and checks if such a resource exists.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public class PathResourceResolver implements ResourceResolver {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(PathResourceResolver.class);
|
||||
|
||||
|
||||
@Override
|
||||
public Resource resolveResource(HttpServletRequest request,
|
||||
String requestPath, List<Resource> locations, ResourceResolverChain chain) {
|
||||
|
||||
return getResource(requestPath, locations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveUrlPath(String resourcePath, List<Resource> locations, ResourceResolverChain chain) {
|
||||
return (getResource(resourcePath, locations) != null) ? resourcePath : null;
|
||||
}
|
||||
|
||||
private Resource getResource(String path, List<Resource> locations) {
|
||||
for (Resource location : locations) {
|
||||
try {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Trying relative path [" + path + "] against base location: " + location);
|
||||
}
|
||||
Resource resource = location.createRelative(path);
|
||||
if (resource.exists() && resource.isReadable()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Found matching resource: " + resource);
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
else if (logger.isTraceEnabled()) {
|
||||
logger.trace("Relative resource doesn't exist or isn't readable: " + resource);
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
logger.debug("Failed to create relative resource - trying next resource location", ex);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,6 @@ package org.springframework.web.servlet.resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.activation.FileTypeMap;
|
||||
@@ -79,21 +78,13 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
|
||||
private static final boolean jafPresent =
|
||||
ClassUtils.isPresent("javax.activation.FileTypeMap", ResourceHttpRequestHandler.class.getClassLoader());
|
||||
|
||||
private static final String CONTENT_ENCODING = "Content-Encoding";
|
||||
|
||||
private List<Resource> locations;
|
||||
|
||||
private List<ResourceResolver> resourceResolvers = new ArrayList<ResourceResolver>();
|
||||
|
||||
private List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();
|
||||
|
||||
|
||||
public ResourceHttpRequestHandler() {
|
||||
super(METHOD_GET, METHOD_HEAD);
|
||||
this.resourceResolvers.add(new PathResourceResolver());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set a {@code List} of {@code Resource} paths to use as sources
|
||||
* for serving static resources.
|
||||
@@ -103,32 +94,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
|
||||
this.locations = locations;
|
||||
}
|
||||
|
||||
public List<Resource> getLocations() {
|
||||
return this.locations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the list of {@link ResourceResolver}s to use.
|
||||
* <p>
|
||||
* By default {@link PathResourceResolver} is configured. If using this property, it
|
||||
* is recommended to add {@link PathResourceResolver} as the last resolver.
|
||||
*/
|
||||
public void setResourceResolvers(List<ResourceResolver> resourceResolvers) {
|
||||
this.resourceResolvers = resourceResolvers;
|
||||
}
|
||||
|
||||
public List<ResourceResolver> getResourceResolvers() {
|
||||
return this.resourceResolvers;
|
||||
}
|
||||
|
||||
public void setResourceTransformers(List<ResourceTransformer> transformers) {
|
||||
this.resourceTransformers = (transformers != null) ? transformers : new ArrayList<ResourceTransformer>();
|
||||
}
|
||||
|
||||
public List<ResourceTransformer> getResourceTransformers() {
|
||||
return this.resourceTransformers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
|
||||
@@ -190,7 +155,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
|
||||
writeContent(response, resource);
|
||||
}
|
||||
|
||||
protected Resource getResource(HttpServletRequest request) throws IOException{
|
||||
protected Resource getResource(HttpServletRequest request) {
|
||||
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
||||
if (path == null) {
|
||||
throw new IllegalStateException("Required request attribute '" +
|
||||
@@ -204,19 +169,27 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
|
||||
return null;
|
||||
}
|
||||
|
||||
ResourceResolverChain chain = new DefaultResourceResolverChain(this.resourceResolvers);
|
||||
Resource resource = chain.resolveResource(request, path, this.locations);
|
||||
|
||||
return (resource != null) ? applyTransformers(request, resource) : null;
|
||||
}
|
||||
|
||||
private Resource applyTransformers(HttpServletRequest request, Resource resource) throws IOException {
|
||||
for (ResourceTransformer transformer : this.resourceTransformers) {
|
||||
if (transformer.willTransform(request, resource)) {
|
||||
return applyTransformers(request, transformer.transform(resource));
|
||||
for (Resource location : this.locations) {
|
||||
try {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Trying relative path [" + path + "] against base location: " + location);
|
||||
}
|
||||
Resource resource = location.createRelative(path);
|
||||
if (resource.exists() && resource.isReadable()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Found matching resource: " + resource);
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
else if (logger.isTraceEnabled()) {
|
||||
logger.trace("Relative resource doesn't exist or isn't readable: " + resource);
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
logger.debug("Failed to create relative resource - trying next resource location", ex);
|
||||
}
|
||||
}
|
||||
return resource;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,10 +241,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
|
||||
if (mediaType != null) {
|
||||
response.setContentType(mediaType.toString());
|
||||
}
|
||||
|
||||
if (resource instanceof EncodedResource) {
|
||||
response.setHeader(CONTENT_ENCODING, ((EncodedResource) resource).getContentEncoding());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -329,4 +298,4 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +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.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* A strategy for two way resolution of URL paths to actual {@link Resource}s located
|
||||
* from one or more configured locations.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface ResourceResolver {
|
||||
|
||||
/**
|
||||
* Resolve the URL path of an incoming request to an actual {@link Resource}.
|
||||
*
|
||||
* @param request the current request
|
||||
* @param requestPath the portion of the request path to use
|
||||
* @param locations the configured locations where to look up resources
|
||||
* @param chain the chain with remaining resolvers to delegate to
|
||||
*
|
||||
* @return the resolved {@link Resource} or {@code null} if this resolver could not
|
||||
* resolve the resource
|
||||
*/
|
||||
Resource resolveResource(HttpServletRequest request, String requestPath,
|
||||
List<Resource> locations, ResourceResolverChain chain);
|
||||
|
||||
/**
|
||||
* Resolve the given resource path to a URL path. This is useful when rendering URL
|
||||
* links to clients to determine the actual URL to use.
|
||||
*
|
||||
* @param resourcePath the resource path
|
||||
* @param locations the configured locations where to look up resources
|
||||
* @param chain the chain with remaining resolvers to delegate to
|
||||
*
|
||||
* @return the resolved URL path or {@code null} if this resolver could not resolve
|
||||
* the given resource path
|
||||
*/
|
||||
String resolveUrlPath(String resourcePath, List<Resource> locations, ResourceResolverChain chain);
|
||||
|
||||
}
|
||||
@@ -1,60 +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.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* A contract for invoking a chain of {@link ResourceResolver}s. Each resolver is passed a
|
||||
* reference to the chain allowing it delegate to the remaining resolvers.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface ResourceResolverChain {
|
||||
|
||||
/**
|
||||
* Resolve the URL path of an incoming request to an actual {@link Resource}.
|
||||
*
|
||||
* @param request the current request
|
||||
* @param requestPath the portion of the request path to use
|
||||
* @param locations the configured locations where to look up resources
|
||||
*
|
||||
* @return the resolved {@link Resource} or {@code null} if this resolver could not
|
||||
* resolve the resource
|
||||
*/
|
||||
Resource resolveResource(HttpServletRequest request, String requestPath, List<Resource> locations);
|
||||
|
||||
/**
|
||||
* Resolve the given resource path to a URL path. This is useful when rendering URL
|
||||
* links to clients to determine the actual URL to use.
|
||||
*
|
||||
* @param resourcePath the resource path
|
||||
* @param locations the configured locations where to look up resources
|
||||
*
|
||||
* @return the resolved URL path or {@code null} if this resolver could not resolve
|
||||
* the given resource path
|
||||
*/
|
||||
String resolveUrlPath(String resourcePath, List<Resource> locations);
|
||||
|
||||
}
|
||||
@@ -1,53 +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.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* A strategy for transforming a resource.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface ResourceTransformer {
|
||||
|
||||
/**
|
||||
* Whether this transformer can transform the given resource.
|
||||
*
|
||||
* @param request the context request
|
||||
* @param resource the candidate resource to transform
|
||||
*/
|
||||
boolean willTransform(HttpServletRequest request, Resource resource);
|
||||
|
||||
/**
|
||||
* Transform the given resource and return a new resource.
|
||||
*
|
||||
* @param resource the resource to transform
|
||||
* @return the transformed resource, never {@code null}
|
||||
*
|
||||
* @throws IOException if the transformation fails
|
||||
*/
|
||||
Resource transform(Resource resource) throws IOException;
|
||||
|
||||
}
|
||||
@@ -1,98 +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.IOException;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpServletResponseWrapper;
|
||||
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
|
||||
/**
|
||||
* A filter that wraps the {@link HttpServletResponse} and overrides its
|
||||
* {@link HttpServletResponse#encodeURL(String) encodeURL} method in order to generate
|
||||
* resource URL links via {@link ResourceUrlGenerator}.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public class ResourceUrlFilter extends OncePerRequestFilter {
|
||||
|
||||
private Set<ResourceUrlGenerator> resourceUrlGenerators;
|
||||
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
|
||||
filterChain.doFilter(request, new ResourceUrlResponseWrapper(request, response));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initFilterBean() throws ServletException {
|
||||
WebApplicationContext cxt = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
|
||||
Map<String, ResourceUrlGenerator> beans = cxt.getBeansOfType(ResourceUrlGenerator.class);
|
||||
this.resourceUrlGenerators = new LinkedHashSet<ResourceUrlGenerator>();
|
||||
this.resourceUrlGenerators.addAll(beans.values());
|
||||
}
|
||||
|
||||
|
||||
private class ResourceUrlResponseWrapper extends HttpServletResponseWrapper {
|
||||
|
||||
private final UrlPathHelper pathHelper = new UrlPathHelper();
|
||||
|
||||
private String pathPrefix;
|
||||
|
||||
|
||||
private ResourceUrlResponseWrapper(HttpServletRequest request, HttpServletResponse wrapped) {
|
||||
super(wrapped);
|
||||
String requestUri = this.pathHelper.getRequestUri(request);
|
||||
String lookupPath = this.pathHelper.getLookupPathForRequest(request);
|
||||
this.pathPrefix = requestUri.replace(lookupPath, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encodeURL(String url) {
|
||||
if(url.startsWith(this.pathPrefix)) {
|
||||
String relativeUrl = url.replaceFirst(this.pathPrefix, "");
|
||||
if (!relativeUrl.startsWith("/")) {
|
||||
relativeUrl = "/" + relativeUrl;
|
||||
}
|
||||
for (ResourceUrlGenerator generator : resourceUrlGenerators) {
|
||||
String resourceUrl = generator.getResourceUrl(relativeUrl);
|
||||
if (resourceUrl != null) {
|
||||
return super.encodeURL(this.pathPrefix + resourceUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.encodeURL(url);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,129 +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 java.util.Map.Entry;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
|
||||
|
||||
/**
|
||||
* A helper class for generating the URL for a resource. Given knowledge of all configured
|
||||
* resource handler mappings (see {@link #setResourceHandlerMappings(List)}), it can
|
||||
* determine whether a given path is a path to a resource, as well as what URL should be
|
||||
* sent to the client to access that resource. This is essentially the reverse of
|
||||
* resolving an incoming request URL to a resource.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public class ResourceUrlGenerator {
|
||||
|
||||
private final List<ResourceMapping> resourceMappings = new ArrayList<ResourceMapping>();
|
||||
|
||||
|
||||
/**
|
||||
* Configure this instance with the handler mappings used to serve resources. It is
|
||||
* expected that the handler mapping URL map contains handlers of type
|
||||
* {@link ResourceHttpRequestHandler}.
|
||||
*
|
||||
* @param handlerMappings resource handler mappings
|
||||
*/
|
||||
public void setResourceHandlerMappings(List<SimpleUrlHandlerMapping> handlerMappings) {
|
||||
this.resourceMappings.clear();
|
||||
if (handlerMappings == null) {
|
||||
return;
|
||||
}
|
||||
for (SimpleUrlHandlerMapping handlerMapping : handlerMappings) {
|
||||
PathMatcher pathMatcher = handlerMapping.getPathMatcher();
|
||||
|
||||
for(Entry<String, ?> entry : handlerMapping.getUrlMap().entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof ResourceHttpRequestHandler) {
|
||||
ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) value;
|
||||
|
||||
String pattern = entry.getKey();
|
||||
List<ResourceResolver> resolvers = handler.getResourceResolvers();
|
||||
List<Resource> locations = handler.getLocations();
|
||||
this.resourceMappings.add(new ResourceMapping(pattern, pathMatcher, resolvers, locations));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the given resource path to a URL path. This is useful when rendering URL
|
||||
* links to clients to determine the actual URL to use.
|
||||
*
|
||||
* @param candidatePath the resource path to resolve
|
||||
*
|
||||
* @return the resolved URL path or {@code null} if the given path does not match to
|
||||
* any resource or otherwise could not be resolved to a resource URL path
|
||||
*/
|
||||
public String getResourceUrl(String candidatePath) {
|
||||
for (ResourceMapping mapping : this.resourceMappings) {
|
||||
String url = mapping.getUrlForResource(candidatePath);
|
||||
if (url != null) {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static class ResourceMapping {
|
||||
|
||||
private final String pattern;
|
||||
|
||||
private final PathMatcher pathMatcher;
|
||||
|
||||
private final List<ResourceResolver> resolvers;
|
||||
|
||||
private final List<Resource> locations;
|
||||
|
||||
|
||||
public ResourceMapping(String pattern, PathMatcher pathMatcher,
|
||||
List<ResourceResolver> resolvers, List<Resource> locations) {
|
||||
|
||||
this.pattern = pattern;
|
||||
this.pathMatcher = pathMatcher;
|
||||
this.resolvers = resolvers;
|
||||
this.locations = locations;
|
||||
}
|
||||
|
||||
public String getUrlForResource(String candidatePath) {
|
||||
|
||||
if (this.pathMatcher.match(this.pattern, candidatePath)) {
|
||||
|
||||
String pathWithinMapping = this.pathMatcher.extractPathWithinPattern(this.pattern, candidatePath);
|
||||
String pathMapping = candidatePath.replace(pathWithinMapping, "");
|
||||
|
||||
DefaultResourceResolverChain chain = new DefaultResourceResolverChain(this.resolvers);
|
||||
String url = chain.resolveUrlPath(pathWithinMapping, this.locations);
|
||||
if (url != null) {
|
||||
return pathMapping + url;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +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.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
* @since 4.0
|
||||
*/
|
||||
public class TransformedResource extends ByteArrayResource {
|
||||
|
||||
private final String filename;
|
||||
private final long lastModified;
|
||||
|
||||
public TransformedResource(String filename, byte[] transformedContent) {
|
||||
super(transformedContent);
|
||||
this.filename = filename;
|
||||
this.lastModified = new Date().getTime();
|
||||
}
|
||||
|
||||
public TransformedResource(String filename, byte[] transformedContent, long lastModified) {
|
||||
super(transformedContent);
|
||||
this.filename = filename;
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return this.filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long lastModified() throws IOException {
|
||||
return this.lastModified;
|
||||
}
|
||||
|
||||
public String getContentAsString() {
|
||||
try {
|
||||
return new String(getByteArray(), "UTF-8");
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user