提交 4fa4886e 编写于 作者: J Juergen Hoeller

Consistent trace log messages and general polishing

(cherry picked from commit 56263848)
上级 46016d09
/*
* 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");
* 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) {
if (logger.isTraceEnabled()) {
logger.trace("Resolving resource: requestPath=\"" + requestPath + "\"");
logger.trace("Resolving resource for request path \"" + requestPath + "\"");
}
return resolveResourceInternal(request, requestPath, locations, chain);
}
protected abstract Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain);
@Override
public String resolveUrlPath(String resourceUrlPath, List<? extends Resource> locations,
ResourceResolverChain chain) {
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);
}
protected abstract Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain);
protected abstract String resolveUrlPathInternal(String resourceUrlPath,
List<? extends Resource> locations, ResourceResolverChain chain);
......
/*
* 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");
* 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.StringWriter;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
......@@ -97,9 +97,7 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport {
throws IOException {
resource = transformerChain.transform(request, resource);
String filename = resource.getFilename();
if (!this.fileExtension.equals(StringUtils.getFilenameExtension(filename))) {
if (!this.fileExtension.equals(StringUtils.getFilenameExtension(resource.getFilename()))) {
return resource;
}
......@@ -138,7 +136,7 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport {
String hash = hashBuilder.build();
contentWriter.write("\n" + "# Hash: " + hash);
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));
......@@ -170,7 +168,7 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport {
private class CacheSection implements SectionTransformer {
private final String COMMENT_DIRECTIVE = "#";
private static final String COMMENT_DIRECTIVE = "#";
@Override
public String transform(String line, HashBuilder builder, Resource resource,
......@@ -178,7 +176,8 @@ public class AppCacheManifestTransformer extends ResourceTransformerSupport {
if (isLink(line) && !hasScheme(line)) {
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);
builder.appendResource(appCacheResource);
if (logger.isTraceEnabled()) {
......
......@@ -42,15 +42,17 @@ public class CachingResourceResolver extends AbstractResourceResolver {
private final Cache cache;
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
this(cacheManager.getCache(cacheName));
}
public CachingResourceResolver(Cache cache) {
Assert.notNull(cache, "'cache' is required");
Assert.notNull(cache, "Cache is required");
this.cache = cache;
}
/**
* Return the configured {@code Cache}.
*/
......@@ -58,6 +60,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
return this.cache;
}
@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain) {
......@@ -67,7 +70,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
if (resource != null) {
if (logger.isTraceEnabled()) {
logger.trace("Found match");
logger.trace("Found match: " + resource);
}
return resource;
}
......@@ -75,7 +78,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
resource = chain.resolveResource(request, requestPath, locations);
if (resource != null) {
if (logger.isTraceEnabled()) {
logger.trace("Putting resolved resource in cache");
logger.trace("Putting resolved resource in cache: " + resource);
}
this.cache.put(key, resource);
}
......@@ -104,7 +107,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
if (resolvedUrlPath != null) {
if (logger.isTraceEnabled()) {
logger.trace("Found match");
logger.trace("Found match: \"" + resolvedUrlPath + "\"");
}
return resolvedUrlPath;
}
......@@ -112,7 +115,7 @@ public class CachingResourceResolver extends AbstractResourceResolver {
resolvedUrlPath = chain.resolveUrlPath(resourceUrlPath, locations);
if (resolvedUrlPath != null) {
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);
}
......
......@@ -42,12 +42,13 @@ public class CachingResourceTransformer implements ResourceTransformer {
private final Cache cache;
public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
this(cacheManager.getCache(cacheName));
}
public CachingResourceTransformer(Cache cache) {
Assert.notNull(cache, "'cache' is required");
Assert.notNull(cache, "Cache is required");
this.cache = cache;
}
......@@ -59,6 +60,7 @@ public class CachingResourceTransformer implements ResourceTransformer {
return this.cache;
}
@Override
public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain)
throws IOException {
......@@ -66,7 +68,7 @@ public class CachingResourceTransformer implements ResourceTransformer {
Resource transformed = this.cache.get(resource, Resource.class);
if (transformed != null) {
if (logger.isTraceEnabled()) {
logger.trace("Found match");
logger.trace("Found match: " + transformed);
}
return transformed;
}
......@@ -74,7 +76,7 @@ public class CachingResourceTransformer implements ResourceTransformer {
transformed = transformerChain.transform(request, resource);
if (logger.isTraceEnabled()) {
logger.trace("Putting transformed resource in cache");
logger.trace("Putting transformed resource in cache: " + transformed);
}
this.cache.put(resource, transformed);
......
/*
* 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");
* 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
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import java.io.IOException;
......@@ -33,12 +34,10 @@ import org.springframework.util.FileCopyUtils;
*/
public class ContentVersionStrategy extends AbstractVersionStrategy {
public ContentVersionStrategy() {
super(new FileNameVersionPathStrategy());
}
@Override
public String getResourceVersion(Resource resource) {
try {
......@@ -46,7 +45,7 @@ public class ContentVersionStrategy extends AbstractVersionStrategy {
return DigestUtils.md5DigestAsHex(content);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to calculate hash for resource [" + resource + "]", ex);
throw new IllegalStateException("Failed to calculate hash for " + resource, ex);
}
}
......
/*
* 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");
* 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.util.Assert;
/**
* A default implementation of {@link ResourceResolverChain} for invoking a list
* of {@link ResourceResolver}s.
......@@ -53,6 +52,7 @@ class DefaultResourceResolverChain implements ResourceResolverChain {
if (resolver == null) {
return null;
}
try {
return resolver.resolveResource(request, requestPath, locations, this);
}
......@@ -67,6 +67,7 @@ class DefaultResourceResolverChain implements ResourceResolverChain {
if (resolver == null) {
return null;
}
try {
return resolver.resolveUrlPath(resourcePath, locations, this);
}
......@@ -76,7 +77,6 @@ class DefaultResourceResolverChain implements ResourceResolverChain {
}
private ResourceResolver getNext() {
Assert.state(this.index <= this.resolvers.size(),
"Current index exceeds the number of configured ResourceResolver's");
......
/*
* 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");
* 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;
/**
* A default implementation of {@link ResourceTransformerChain} for invoking a list
* of {@link ResourceTransformer}s.
* A default implementation of {@link ResourceTransformerChain} for invoking
* a list of {@link ResourceTransformer}s.
*
* @author Rossen Stoyanchev
* @since 4.1
......@@ -39,10 +39,11 @@ class DefaultResourceTransformerChain implements ResourceTransformerChain {
private int index = -1;
public DefaultResourceTransformerChain(ResourceResolverChain resolverChain,
List<ResourceTransformer> transformers) {
Assert.notNull(resolverChain, "'resolverChain' is required");
Assert.notNull(resolverChain, "ResourceResolverChain is required");
this.resolverChain = resolverChain;
if (transformers != null) {
this.transformers.addAll(transformers);
......@@ -54,12 +55,14 @@ class DefaultResourceTransformerChain implements ResourceTransformerChain {
return this.resolverChain;
}
@Override
public Resource transform(HttpServletRequest request, Resource resource) throws IOException {
ResourceTransformer transformer = getNext();
if (transformer == null) {
return resource;
}
try {
return transformer.transform(request, resource, this);
}
......@@ -69,7 +72,6 @@ class DefaultResourceTransformerChain implements ResourceTransformerChain {
}
private ResourceTransformer getNext() {
Assert.state(this.index <= this.transformers.size(),
"Current index exceeds the number of configured ResourceTransformer's");
......
/*
* 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");
* 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);
if (rd == null) {
throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" +
this.defaultServletName +"'");
this.defaultServletName + "'");
}
rd.forward(request, response);
}
......
/*
* 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");
* 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;
/**
* Interface for a resource descriptor that describes the encoding
* 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
* and Content, section 3.1.2.1</a>
*/
public String getContentEncoding();
String getContentEncoding();
}
/*
* 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");
* 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
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import org.springframework.core.io.Resource;
......@@ -34,6 +35,7 @@ public class FixedVersionStrategy extends AbstractVersionStrategy {
private final String version;
/**
* Create a new FixedVersionStrategy with the given version string.
* @param version the fixed version string to use
......
......@@ -27,7 +27,6 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.Resource;
/**
* A {@code ResourceResolver} that delegates to the chain to locate a resource
* 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 {
@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain) {
......@@ -58,8 +56,8 @@ public class GzipResourceResolver extends AbstractResourceResolver {
return gzipped;
}
}
catch (IOException e) {
logger.trace("No gzipped resource for [" + resource.getFilename() + "]", e);
catch (IOException ex) {
logger.trace("No gzipped resource for [" + resource.getFilename() + "]", ex);
}
return resource;
......@@ -67,7 +65,7 @@ public class GzipResourceResolver extends AbstractResourceResolver {
private boolean isGzipAccepted(HttpServletRequest request) {
String value = request.getHeader("Accept-Encoding");
return ((value != null) && value.toLowerCase().contains("gzip"));
return (value != null && value.toLowerCase().contains("gzip"));
}
@Override
......@@ -84,13 +82,11 @@ public class GzipResourceResolver extends AbstractResourceResolver {
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();
}
......
/*
* 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");
* 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.
*
* <p>Provides mechanisms for resolving an incoming request to an actual
* {@link org.springframework.core.io.Resource} and for obtaining the public
* URL path that clients should use when requesting the resource.
* {@link org.springframework.core.io.Resource} and for obtaining the
* public URL path that clients should use when requesting the resource.
*
* @author Jeremy Grelle
* @author Rossen Stoyanchev
......@@ -39,7 +39,6 @@ public interface ResourceResolver {
/**
* Resolve the supplied request and request path to a {@link Resource} that
* exists under one of the given resource locations.
*
* @param request the current request
* @param requestPath the portion of the request path to use
* @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
* to access the resource that is located at the given <em>internal</em>
* resource path.
*
* <p>This is useful when rendering URL links to clients.
*
* @param resourcePath the internal resource path
* @param locations the locations to search in when looking up resources
* @param chain the chain of resolvers to delegate to
......
/*
* 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");
* 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
* exists under one of the given resource locations.
*
* @param request the current request
* @param requestPath the portion of the request path to use
* @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
* to access the resource that is located at the given <em>internal</em>
* resource path.
*
* <p>This is useful when rendering URL links to clients.
*
* @param resourcePath the internal resource path
* @param locations the locations to search in when looking up resources
* @return the resolved public URL path or {@code null} if unresolved
......
/*
* 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");
* 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
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
package org.springframework.web.servlet.resource;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
......@@ -32,12 +32,10 @@ public interface ResourceTransformer {
/**
* Transform the given resource.
*
* @param request the current request
* @param resource the resource to transform
* @param transformerChain the chain of remaining transformers to delegate to
* @return the transformed resource, never {@code null}
*
* @throws IOException if the transformation fails
*/
Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain)
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -39,12 +39,10 @@ public interface ResourceTransformerChain {
/**
* Transform the given resource.
*
* @param request the current request
* @param resource the candidate resource to transform
* @return the transformed or the same resource, never {@code null}
*
* @throws java.io.IOException if transformation fails
* @throws IOException if transformation fails
*/
Resource transform(HttpServletRequest request, Resource resource) throws IOException;
......
/*
* 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");
* 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
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import java.util.Arrays;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
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).
* This is required only for links expressed as full paths, i.e. including
* context and servlet path, and not for relative links.
*
* <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} 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
* public facing link as determined by the resource resolver chain (e.g. the
* public URL may have a version inserted).
*
* @param resourcePath the path to a resource that needs to be re-written
* @param request the current request
* @param resource the resource being transformed
......@@ -82,7 +81,8 @@ public abstract class ResourceTransformerSupport implements ResourceTransformer
}
else {
// try resolving as relative path
return transformerChain.getResolverChain().resolveUrlPath(resourcePath, Arrays.asList(resource));
return transformerChain.getResolverChain().resolveUrlPath(
resourcePath, Collections.singletonList(resource));
}
}
......
/*
* 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");
* 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;
/**
* An extension of {@link org.springframework.core.io.ByteArrayResource} that a
* {@link ResourceTransformer} can use to represent an original resource
* preserving all other information except the content.
* An extension of {@link org.springframework.core.io.ByteArrayResource}
* that a {@link ResourceTransformer} can use to represent an original
* resource preserving all other information except the content.
*
* @author Jeremy Grelle
* @author Rossen Stoyanchev
......@@ -43,9 +43,9 @@ public class TransformedResource extends ByteArrayResource {
try {
this.lastModified = original.lastModified();
}
catch (IOException e) {
catch (IOException ex) {
// should never happen
throw new IllegalArgumentException(e);
throw new IllegalArgumentException(ex);
}
}
......
/*
* 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");
* 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
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
/**
......
/*
* 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");
* 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
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import org.springframework.core.io.Resource;
/**
* An extension of {@link VersionPathStrategy} that adds a method to determine the
* actual version of a {@link org.springframework.core.io.Resource Resource}.
* An extension of {@link VersionPathStrategy} that adds a method
* to determine the actual version of a {@link Resource}.
*
* @author Brian Clozel
* @author Rossen Stoyanchev
......@@ -29,9 +30,9 @@ import org.springframework.core.io.Resource;
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
* @return the version, never {@code null}
* @return the version (never {@code null})
*/
String getResourceVersion(Resource resource);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册