Consistent trace log messages and general polishing

This commit is contained in:
Juergen Hoeller
2015-12-16 15:59:02 +01:00
parent 3be35c053d
commit 5626384812
22 changed files with 102 additions and 102 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -41,11 +41,10 @@ import org.springframework.util.CollectionUtils;
* If the path doesn't begin with a slash, one is prepended. * If the path doesn't begin with a slash, one is prepended.
* *
* <p>Supports direct matches (given "/test" -> registered "/test") and "*" * <p>Supports direct matches (given "/test" -> registered "/test") and "*"
* matches (given "/test" -> registered "/t*"). Note that the default is * pattern matches (given "/test" -> registered "/t*"). Note that the default
* to map within the current servlet mapping if applicable; see the * is to map within the current servlet mapping if applicable; see the
* {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details. * {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property. For details on the
* For details on the pattern options, see the * pattern options, see the {@link org.springframework.util.AntPathMatcher} javadoc.
* {@link org.springframework.util.AntPathMatcher} javadoc.
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -41,25 +41,26 @@ public abstract class AbstractResourceResolver implements ResourceResolver {
List<? extends Resource> locations, ResourceResolverChain chain) { List<? extends Resource> locations, ResourceResolverChain chain) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Resolving resource: requestPath=\"" + requestPath + "\""); logger.trace("Resolving resource for request path \"" + requestPath + "\"");
} }
return resolveResourceInternal(request, requestPath, locations, chain); return resolveResourceInternal(request, requestPath, locations, chain);
} }
protected abstract Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain);
@Override @Override
public String resolveUrlPath(String resourceUrlPath, List<? extends Resource> locations, public String resolveUrlPath(String resourceUrlPath, List<? extends Resource> locations,
ResourceResolverChain chain) { ResourceResolverChain chain) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Resolving public URL for path=\"" + resourceUrlPath + "\""); logger.trace("Resolving public URL for resource path \"" + resourceUrlPath + "\"");
} }
return resolveUrlPathInternal(resourceUrlPath, locations, chain); return resolveUrlPathInternal(resourceUrlPath, locations, chain);
} }
protected abstract Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain);
protected abstract String resolveUrlPathInternal(String resourceUrlPath, protected abstract String resolveUrlPathInternal(String resourceUrlPath,
List<? extends Resource> locations, ResourceResolverChain chain); List<? extends Resource> locations, ResourceResolverChain chain);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
@@ -97,9 +97,7 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport {
throws IOException { throws IOException {
resource = transformerChain.transform(request, resource); resource = transformerChain.transform(request, resource);
if (!this.fileExtension.equals(StringUtils.getFilenameExtension(resource.getFilename()))) {
String filename = resource.getFilename();
if (!this.fileExtension.equals(StringUtils.getFilenameExtension(filename))) {
return resource; return resource;
} }
@@ -138,7 +136,7 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport {
String hash = hashBuilder.build(); String hash = hashBuilder.build();
contentWriter.write("\n" + "# Hash: " + hash); contentWriter.write("\n" + "# Hash: " + hash);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("AppCache file: [" + resource.getFilename()+ "] Hash: [" + hash + "]"); logger.trace("AppCache file: [" + resource.getFilename()+ "] hash: [" + hash + "]");
} }
return new TransformedResource(resource, contentWriter.toString().getBytes(DEFAULT_CHARSET)); return new TransformedResource(resource, contentWriter.toString().getBytes(DEFAULT_CHARSET));
@@ -178,7 +176,8 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport {
if (isLink(line) && !hasScheme(line)) { if (isLink(line) && !hasScheme(line)) {
ResourceResolverChain resolverChain = transformerChain.getResolverChain(); ResourceResolverChain resolverChain = transformerChain.getResolverChain();
Resource appCacheResource = resolverChain.resolveResource(null, line, Arrays.asList(resource)); Resource appCacheResource =
resolverChain.resolveResource(null, line, Collections.singletonList(resource));
String path = resolveUrlPath(line, request, resource, transformerChain); String path = resolveUrlPath(line, request, resource, transformerChain);
builder.appendResource(appCacheResource); builder.appendResource(appCacheResource);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {

View File

@@ -42,15 +42,17 @@ public class CachingResourceResolver extends AbstractResourceResolver {
private final Cache cache; private final Cache cache;
public CachingResourceResolver(CacheManager cacheManager, String cacheName) { public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
this(cacheManager.getCache(cacheName)); this(cacheManager.getCache(cacheName));
} }
public CachingResourceResolver(Cache cache) { public CachingResourceResolver(Cache cache) {
Assert.notNull(cache, "'cache' is required"); Assert.notNull(cache, "Cache is required");
this.cache = cache; this.cache = cache;
} }
/** /**
* Return the configured {@code Cache}. * Return the configured {@code Cache}.
*/ */
@@ -58,6 +60,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
return this.cache; return this.cache;
} }
@Override @Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain) { List<? extends Resource> locations, ResourceResolverChain chain) {
@@ -67,7 +70,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
if (resource != null) { if (resource != null) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Found match"); logger.trace("Found match: " + resource);
} }
return resource; return resource;
} }
@@ -75,7 +78,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
resource = chain.resolveResource(request, requestPath, locations); resource = chain.resolveResource(request, requestPath, locations);
if (resource != null) { if (resource != null) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Putting resolved resource in cache"); logger.trace("Putting resolved resource in cache: " + resource);
} }
this.cache.put(key, resource); this.cache.put(key, resource);
} }
@@ -104,7 +107,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
if (resolvedUrlPath != null) { if (resolvedUrlPath != null) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Found match"); logger.trace("Found match: \"" + resolvedUrlPath + "\"");
} }
return resolvedUrlPath; return resolvedUrlPath;
} }
@@ -112,7 +115,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
resolvedUrlPath = chain.resolveUrlPath(resourceUrlPath, locations); resolvedUrlPath = chain.resolveUrlPath(resourceUrlPath, locations);
if (resolvedUrlPath != null) { if (resolvedUrlPath != null) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Putting resolved resource URL path in cache"); logger.trace("Putting resolved resource URL path in cache: \"" + resolvedUrlPath + "\"");
} }
this.cache.put(key, resolvedUrlPath); this.cache.put(key, resolvedUrlPath);
} }

View File

@@ -42,12 +42,13 @@ public class CachingResourceTransformer implements ResourceTransformer {
private final Cache cache; private final Cache cache;
public CachingResourceTransformer(CacheManager cacheManager, String cacheName) { public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
this(cacheManager.getCache(cacheName)); this(cacheManager.getCache(cacheName));
} }
public CachingResourceTransformer(Cache cache) { public CachingResourceTransformer(Cache cache) {
Assert.notNull(cache, "'cache' is required"); Assert.notNull(cache, "Cache is required");
this.cache = cache; this.cache = cache;
} }
@@ -59,6 +60,7 @@ public class CachingResourceTransformer implements ResourceTransformer {
return this.cache; return this.cache;
} }
@Override @Override
public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain)
throws IOException { throws IOException {
@@ -66,7 +68,7 @@ public class CachingResourceTransformer implements ResourceTransformer {
Resource transformed = this.cache.get(resource, Resource.class); Resource transformed = this.cache.get(resource, Resource.class);
if (transformed != null) { if (transformed != null) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Found match"); logger.trace("Found match: " + transformed);
} }
return transformed; return transformed;
} }
@@ -74,7 +76,7 @@ public class CachingResourceTransformer implements ResourceTransformer {
transformed = transformerChain.transform(request, resource); transformed = transformerChain.transform(request, resource);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Putting transformed resource in cache"); logger.trace("Putting transformed resource in cache: " + transformed);
} }
this.cache.put(resource, transformed); this.cache.put(resource, transformed);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.servlet.resource; package org.springframework.web.servlet.resource;
import java.io.IOException; import java.io.IOException;
@@ -33,12 +34,10 @@ import org.springframework.util.FileCopyUtils;
*/ */
public class ContentVersionStrategy extends AbstractVersionStrategy { public class ContentVersionStrategy extends AbstractVersionStrategy {
public ContentVersionStrategy() { public ContentVersionStrategy() {
super(new FileNameVersionPathStrategy()); super(new FileNameVersionPathStrategy());
} }
@Override @Override
public String getResourceVersion(Resource resource) { public String getResourceVersion(Resource resource) {
try { try {
@@ -46,7 +45,7 @@ public class ContentVersionStrategy extends AbstractVersionStrategy {
return DigestUtils.md5DigestAsHex(content); return DigestUtils.md5DigestAsHex(content);
} }
catch (IOException ex) { catch (IOException ex) {
throw new IllegalStateException("Failed to calculate hash for resource [" + resource + "]", ex); throw new IllegalStateException("Failed to calculate hash for " + resource, ex);
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* A default implementation of {@link ResourceResolverChain} for invoking a list * A default implementation of {@link ResourceResolverChain} for invoking a list
* of {@link ResourceResolver}s. * of {@link ResourceResolver}s.
@@ -53,6 +52,7 @@ class DefaultResourceResolverChain implements ResourceResolverChain {
if (resolver == null) { if (resolver == null) {
return null; return null;
} }
try { try {
return resolver.resolveResource(request, requestPath, locations, this); return resolver.resolveResource(request, requestPath, locations, this);
} }
@@ -67,6 +67,7 @@ class DefaultResourceResolverChain implements ResourceResolverChain {
if (resolver == null) { if (resolver == null) {
return null; return null;
} }
try { try {
return resolver.resolveUrlPath(resourcePath, locations, this); return resolver.resolveUrlPath(resourcePath, locations, this);
} }
@@ -76,7 +77,6 @@ class DefaultResourceResolverChain implements ResourceResolverChain {
} }
private ResourceResolver getNext() { private ResourceResolver getNext() {
Assert.state(this.index <= this.resolvers.size(), Assert.state(this.index <= this.resolvers.size(),
"Current index exceeds the number of configured ResourceResolver's"); "Current index exceeds the number of configured ResourceResolver's");

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -25,8 +25,8 @@ import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* A default implementation of {@link ResourceTransformerChain} for invoking a list * A default implementation of {@link ResourceTransformerChain} for invoking
* of {@link ResourceTransformer}s. * a list of {@link ResourceTransformer}s.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.1 * @since 4.1
@@ -39,10 +39,11 @@ class DefaultResourceTransformerChain implements ResourceTransformerChain {
private int index = -1; private int index = -1;
public DefaultResourceTransformerChain(ResourceResolverChain resolverChain, public DefaultResourceTransformerChain(ResourceResolverChain resolverChain,
List<ResourceTransformer> transformers) { List<ResourceTransformer> transformers) {
Assert.notNull(resolverChain, "'resolverChain' is required"); Assert.notNull(resolverChain, "ResourceResolverChain is required");
this.resolverChain = resolverChain; this.resolverChain = resolverChain;
if (transformers != null) { if (transformers != null) {
this.transformers.addAll(transformers); this.transformers.addAll(transformers);
@@ -54,12 +55,14 @@ class DefaultResourceTransformerChain implements ResourceTransformerChain {
return this.resolverChain; return this.resolverChain;
} }
@Override @Override
public Resource transform(HttpServletRequest request, Resource resource) throws IOException { public Resource transform(HttpServletRequest request, Resource resource) throws IOException {
ResourceTransformer transformer = getNext(); ResourceTransformer transformer = getNext();
if (transformer == null) { if (transformer == null) {
return resource; return resource;
} }
try { try {
return transformer.transform(request, resource, this); return transformer.transform(request, resource, this);
} }
@@ -69,7 +72,6 @@ class DefaultResourceTransformerChain implements ResourceTransformerChain {
} }
private ResourceTransformer getNext() { private ResourceTransformer getNext() {
Assert.state(this.index <= this.transformers.size(), Assert.state(this.index <= this.transformers.size(),
"Current index exceeds the number of configured ResourceTransformer's"); "Current index exceeds the number of configured ResourceTransformer's");

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -117,7 +117,7 @@ public class DefaultServletHttpRequestHandler implements HttpRequestHandler, Ser
RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName); RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName);
if (rd == null) { if (rd == null) {
throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" + throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" +
this.defaultServletName +"'"); this.defaultServletName + "'");
} }
rd.forward(request, response); rd.forward(request, response);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package org.springframework.web.servlet.resource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
/** /**
* Interface for a resource descriptor that describes the encoding * Interface for a resource descriptor that describes the encoding
* applied to the entire resource content. * applied to the entire resource content.
@@ -39,6 +38,6 @@ public interface EncodedResource extends Resource {
* @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.2.1">HTTP/1.1: Semantics * @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.2.1">HTTP/1.1: Semantics
* and Content, section 3.1.2.1</a> * and Content, section 3.1.2.1</a>
*/ */
public String getContentEncoding(); String getContentEncoding();
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.servlet.resource; package org.springframework.web.servlet.resource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@@ -34,6 +35,7 @@ public class FixedVersionStrategy extends AbstractVersionStrategy {
private final String version; private final String version;
/** /**
* Create a new FixedVersionStrategy with the given version string. * Create a new FixedVersionStrategy with the given version string.
* @param version the fixed version string to use * @param version the fixed version string to use

View File

@@ -27,7 +27,6 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.AbstractResource; import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
/** /**
* A {@code ResourceResolver} that delegates to the chain to locate a resource * A {@code ResourceResolver} that delegates to the chain to locate a resource
* and then attempts to find a variation with the ".gz" extension. * and then attempts to find a variation with the ".gz" extension.
@@ -42,7 +41,6 @@ import org.springframework.core.io.Resource;
*/ */
public class GzipResourceResolver extends AbstractResourceResolver { public class GzipResourceResolver extends AbstractResourceResolver {
@Override @Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain) { List<? extends Resource> locations, ResourceResolverChain chain) {
@@ -58,8 +56,8 @@ public class GzipResourceResolver extends AbstractResourceResolver {
return gzipped; return gzipped;
} }
} }
catch (IOException e) { catch (IOException ex) {
logger.trace("No gzipped resource for [" + resource.getFilename() + "]", e); logger.trace("No gzipped resource for [" + resource.getFilename() + "]", ex);
} }
return resource; return resource;
@@ -67,7 +65,7 @@ public class GzipResourceResolver extends AbstractResourceResolver {
private boolean isGzipAccepted(HttpServletRequest request) { private boolean isGzipAccepted(HttpServletRequest request) {
String value = request.getHeader("Accept-Encoding"); String value = request.getHeader("Accept-Encoding");
return ((value != null) && value.toLowerCase().contains("gzip")); return (value != null && value.toLowerCase().contains("gzip"));
} }
@Override @Override
@@ -84,13 +82,11 @@ public class GzipResourceResolver extends AbstractResourceResolver {
private final Resource gzipped; private final Resource gzipped;
public GzippedResource(Resource original) throws IOException { public GzippedResource(Resource original) throws IOException {
this.original = original; this.original = original;
this.gzipped = original.createRelative(original.getFilename() + ".gz"); this.gzipped = original.createRelative(original.getFilename() + ".gz");
} }
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
return this.gzipped.getInputStream(); return this.gzipped.getInputStream();
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -25,8 +25,8 @@ import org.springframework.core.io.Resource;
* A strategy for resolving a request to a server-side resource. * A strategy for resolving a request to a server-side resource.
* *
* <p>Provides mechanisms for resolving an incoming request to an actual * <p>Provides mechanisms for resolving an incoming request to an actual
* {@link org.springframework.core.io.Resource} and for obtaining the public * {@link org.springframework.core.io.Resource} and for obtaining the
* URL path that clients should use when requesting the resource. * public URL path that clients should use when requesting the resource.
* *
* @author Jeremy Grelle * @author Jeremy Grelle
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@@ -39,7 +39,6 @@ public interface ResourceResolver {
/** /**
* Resolve the supplied request and request path to a {@link Resource} that * Resolve the supplied request and request path to a {@link Resource} that
* exists under one of the given resource locations. * exists under one of the given resource locations.
*
* @param request the current request * @param request the current request
* @param requestPath the portion of the request path to use * @param requestPath the portion of the request path to use
* @param locations the locations to search in when looking up resources * @param locations the locations to search in when looking up resources
@@ -53,9 +52,7 @@ public interface ResourceResolver {
* Resolve the externally facing <em>public</em> URL path for clients to use * Resolve the externally facing <em>public</em> URL path for clients to use
* to access the resource that is located at the given <em>internal</em> * to access the resource that is located at the given <em>internal</em>
* resource path. * resource path.
*
* <p>This is useful when rendering URL links to clients. * <p>This is useful when rendering URL links to clients.
*
* @param resourcePath the internal resource path * @param resourcePath the internal resource path
* @param locations the locations to search in when looking up resources * @param locations the locations to search in when looking up resources
* @param chain the chain of resolvers to delegate to * @param chain the chain of resolvers to delegate to

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -35,7 +35,6 @@ public interface ResourceResolverChain {
/** /**
* Resolve the supplied request and request path to a {@link Resource} that * Resolve the supplied request and request path to a {@link Resource} that
* exists under one of the given resource locations. * exists under one of the given resource locations.
*
* @param request the current request * @param request the current request
* @param requestPath the portion of the request path to use * @param requestPath the portion of the request path to use
* @param locations the locations to search in when looking up resources * @param locations the locations to search in when looking up resources
@@ -47,9 +46,7 @@ public interface ResourceResolverChain {
* Resolve the externally facing <em>public</em> URL path for clients to use * Resolve the externally facing <em>public</em> URL path for clients to use
* to access the resource that is located at the given <em>internal</em> * to access the resource that is located at the given <em>internal</em>
* resource path. * resource path.
*
* <p>This is useful when rendering URL links to clients. * <p>This is useful when rendering URL links to clients.
*
* @param resourcePath the internal resource path * @param resourcePath the internal resource path
* @param locations the locations to search in when looking up resources * @param locations the locations to search in when looking up resources
* @return the resolved public URL path or {@code null} if unresolved * @return the resolved public URL path or {@code null} if unresolved

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.servlet.resource;
package org.springframework.web.servlet.resource;
import java.io.IOException; import java.io.IOException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -32,12 +32,10 @@ public interface ResourceTransformer {
/** /**
* Transform the given resource. * Transform the given resource.
*
* @param request the current request * @param request the current request
* @param resource the resource to transform * @param resource the resource to transform
* @param transformerChain the chain of remaining transformers to delegate to * @param transformerChain the chain of remaining transformers to delegate to
* @return the transformed resource, never {@code null} * @return the transformed resource, never {@code null}
*
* @throws IOException if the transformation fails * @throws IOException if the transformation fails
*/ */
Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain)

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -39,12 +39,10 @@ public interface ResourceTransformerChain {
/** /**
* Transform the given resource. * Transform the given resource.
*
* @param request the current request * @param request the current request
* @param resource the candidate resource to transform * @param resource the candidate resource to transform
* @return the transformed or the same resource, never {@code null} * @return the transformed or the same resource, never {@code null}
* * @throws IOException if transformation fails
* @throws java.io.IOException if transformation fails
*/ */
Resource transform(HttpServletRequest request, Resource resource) throws IOException; Resource transform(HttpServletRequest request, Resource resource) throws IOException;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.servlet.resource; package org.springframework.web.servlet.resource;
import java.util.Arrays; import java.util.Collections;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@@ -38,7 +39,6 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer
* URL of links in a transformed resource (e.g. import links in a CSS file). * URL of links in a transformed resource (e.g. import links in a CSS file).
* This is required only for links expressed as full paths, i.e. including * This is required only for links expressed as full paths, i.e. including
* context and servlet path, and not for relative links. * context and servlet path, and not for relative links.
*
* <p>By default this property is not set. In that case if a * <p>By default this property is not set. In that case if a
* {@code ResourceUrlProvider} is needed an attempt is made to find the * {@code ResourceUrlProvider} is needed an attempt is made to find the
* {@code ResourceUrlProvider} exposed through the * {@code ResourceUrlProvider} exposed through the
@@ -65,7 +65,6 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer
* contains links to other resources. Such links need to be replaced with the * contains links to other resources. Such links need to be replaced with the
* public facing link as determined by the resource resolver chain (e.g. the * public facing link as determined by the resource resolver chain (e.g. the
* public URL may have a version inserted). * public URL may have a version inserted).
*
* @param resourcePath the path to a resource that needs to be re-written * @param resourcePath the path to a resource that needs to be re-written
* @param request the current request * @param request the current request
* @param resource the resource being transformed * @param resource the resource being transformed
@@ -82,7 +81,8 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer
} }
else { else {
// try resolving as relative path // try resolving as relative path
return transformerChain.getResolverChain().resolveUrlPath(resourcePath, Arrays.asList(resource)); return transformerChain.getResolverChain().resolveUrlPath(
resourcePath, Collections.singletonList(resource));
} }
} }

View File

@@ -169,7 +169,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
*/ */
public final String getForRequestUrl(HttpServletRequest request, String requestUrl) { public final String getForRequestUrl(HttpServletRequest request, String requestUrl) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Getting resource URL for requestURL=" + requestUrl); logger.trace("Getting resource URL for request URL \"" + requestUrl + "\"");
} }
int prefixIndex = getLookupPathIndex(request); int prefixIndex = getLookupPathIndex(request);
int suffixIndex = getQueryParamsIndex(requestUrl); int suffixIndex = getQueryParamsIndex(requestUrl);
@@ -205,14 +205,16 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
*/ */
public final String getForLookupPath(String lookupPath) { public final String getForLookupPath(String lookupPath) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Getting resource URL for lookupPath=" + lookupPath); logger.trace("Getting resource URL for lookup path \"" + lookupPath + "\"");
} }
List<String> matchingPatterns = new ArrayList<String>(); List<String> matchingPatterns = new ArrayList<String>();
for (String pattern : this.handlerMap.keySet()) { for (String pattern : this.handlerMap.keySet()) {
if (getPathMatcher().match(pattern, lookupPath)) { if (getPathMatcher().match(pattern, lookupPath)) {
matchingPatterns.add(pattern); matchingPatterns.add(pattern);
} }
} }
if (!matchingPatterns.isEmpty()) { if (!matchingPatterns.isEmpty()) {
Comparator<String> patternComparator = getPathMatcher().getPatternComparator(lookupPath); Comparator<String> patternComparator = getPathMatcher().getPatternComparator(lookupPath);
Collections.sort(matchingPatterns, patternComparator); Collections.sort(matchingPatterns, patternComparator);
@@ -220,7 +222,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
String pathWithinMapping = getPathMatcher().extractPathWithinPattern(pattern, lookupPath); String pathWithinMapping = getPathMatcher().extractPathWithinPattern(pattern, lookupPath);
String pathMapping = lookupPath.substring(0, lookupPath.indexOf(pathWithinMapping)); String pathMapping = lookupPath.substring(0, lookupPath.indexOf(pathWithinMapping));
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Invoking ResourceResolverChain for URL pattern=\"" + pattern + "\""); logger.trace("Invoking ResourceResolverChain for URL pattern \"" + pattern + "\"");
} }
ResourceHttpRequestHandler handler = this.handlerMap.get(pattern); ResourceHttpRequestHandler handler = this.handlerMap.get(pattern);
ResourceResolverChain chain = new DefaultResourceResolverChain(handler.getResourceResolvers()); ResourceResolverChain chain = new DefaultResourceResolverChain(handler.getResourceResolvers());
@@ -229,12 +231,15 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
continue; continue;
} }
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Resolved public resource URL path=\"" + resolved + "\""); logger.trace("Resolved public resource URL path \"" + resolved + "\"");
} }
return pathMapping + resolved; return pathMapping + resolved;
} }
} }
logger.debug("No matching resource mapping");
if (logger.isDebugEnabled()) {
logger.debug("No matching resource mapping for lookup path \"" + lookupPath + "\"");
}
return null; return null;
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -22,9 +22,9 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
/** /**
* An extension of {@link org.springframework.core.io.ByteArrayResource} that a * An extension of {@link org.springframework.core.io.ByteArrayResource}
* {@link ResourceTransformer} can use to represent an original resource * that a {@link ResourceTransformer} can use to represent an original
* preserving all other information except the content. * resource preserving all other information except the content.
* *
* @author Jeremy Grelle * @author Jeremy Grelle
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@@ -43,9 +43,9 @@ public class TransformedResource extends ByteArrayResource {
try { try {
this.lastModified = original.lastModified(); this.lastModified = original.lastModified();
} }
catch (IOException e) { catch (IOException ex) {
// should never happen // should never happen
throw new IllegalArgumentException(e); throw new IllegalArgumentException(ex);
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.servlet.resource; package org.springframework.web.servlet.resource;
/** /**

View File

@@ -22,7 +22,6 @@ import java.util.Comparator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@@ -56,7 +55,9 @@ import org.springframework.util.StringUtils;
*/ */
public class VersionResourceResolver extends AbstractResourceResolver { public class VersionResourceResolver extends AbstractResourceResolver {
public static final String RESOURCE_VERSION_ATTRIBUTE = VersionResourceResolver.class.getName() + ".resourceVersion"; public static final String RESOURCE_VERSION_ATTRIBUTE =
VersionResourceResolver.class.getName() + ".resourceVersion";
private AntPathMatcher pathMatcher = new AntPathMatcher(); private AntPathMatcher pathMatcher = new AntPathMatcher();
@@ -149,14 +150,14 @@ public class VersionResourceResolver extends AbstractResourceResolver {
String candidateVersion = versionStrategy.extractVersion(requestPath); String candidateVersion = versionStrategy.extractVersion(requestPath);
if (StringUtils.isEmpty(candidateVersion)) { if (StringUtils.isEmpty(candidateVersion)) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("No version found in path=\"" + requestPath + "\""); logger.trace("No version found in path \"" + requestPath + "\"");
} }
return null; return null;
} }
String simplePath = versionStrategy.removeVersion(requestPath, candidateVersion); String simplePath = versionStrategy.removeVersion(requestPath, candidateVersion);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Extracted version from path, re-resolving without version, path=\"" + simplePath + "\""); logger.trace("Extracted version from path, re-resolving without version: \"" + simplePath + "\"");
} }
Resource baseResource = chain.resolveResource(request, simplePath, locations); Resource baseResource = chain.resolveResource(request, simplePath, locations);
@@ -167,17 +168,17 @@ public class VersionResourceResolver extends AbstractResourceResolver {
String actualVersion = versionStrategy.getResourceVersion(baseResource); String actualVersion = versionStrategy.getResourceVersion(baseResource);
if (candidateVersion.equals(actualVersion)) { if (candidateVersion.equals(actualVersion)) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("resource matches extracted version"); logger.trace("Resource matches extracted version: "+ candidateVersion);
} }
if (request != null) { if (request != null) {
request.setAttribute(VersionResourceResolver.RESOURCE_VERSION_ATTRIBUTE, candidateVersion); request.setAttribute(RESOURCE_VERSION_ATTRIBUTE, candidateVersion);
} }
return baseResource; return baseResource;
} }
else { else {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Potential resource found for [" + requestPath + "], but version [" + logger.trace("Potential resource found for \"" + requestPath + "\", but version [" +
candidateVersion + "] doesn't match."); candidateVersion + "] does not match");
} }
return null; return null;
} }
@@ -192,12 +193,12 @@ public class VersionResourceResolver extends AbstractResourceResolver {
return null; return null;
} }
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Getting the original resource to determine version"); logger.trace("Getting the original resource to determine version for path \"" + resourceUrlPath + "\"");
} }
Resource resource = chain.resolveResource(null, baseUrl, locations); Resource resource = chain.resolveResource(null, baseUrl, locations);
String version = versionStrategy.getResourceVersion(resource); String version = versionStrategy.getResourceVersion(resource);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Version=" + version); logger.trace("Determined version [" + version + "] for " + resource);
} }
return versionStrategy.addVersion(baseUrl, version); return versionStrategy.addVersion(baseUrl, version);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.servlet.resource; package org.springframework.web.servlet.resource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
/** /**
* An extension of {@link VersionPathStrategy} that adds a method to determine the * An extension of {@link VersionPathStrategy} that adds a method
* actual version of a {@link org.springframework.core.io.Resource Resource}. * to determine the actual version of a {@link Resource}.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@@ -29,9 +30,9 @@ import org.springframework.core.io.Resource;
public interface VersionStrategy extends VersionPathStrategy { public interface VersionStrategy extends VersionPathStrategy {
/** /**
* Get the version for the given resource. * Determine the version for the given resource.
* @param resource the resource to check * @param resource the resource to check
* @return the version, never {@code null} * @return the version (never {@code null})
*/ */
String getResourceVersion(Resource resource); String getResourceVersion(Resource resource);