Skip to content

Commit b1158aa

Browse files
committed
Polish logging in resource handling
1 parent 9cb5f48 commit b1158aa

File tree

8 files changed

+159
-88
lines changed

8 files changed

+159
-88
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.resource;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
import org.springframework.core.io.Resource;
22+
23+
import javax.servlet.http.HttpServletRequest;
24+
import java.util.List;
25+
26+
/**
27+
* Base class for {@link org.springframework.web.servlet.resource.ResourceResolver}
28+
* implementations.
29+
*
30+
* @author Rossen Stoyanchev
31+
* @since 4.1
32+
*/
33+
public abstract class AbstractResourceResolver implements ResourceResolver {
34+
35+
protected final Log logger = LogFactory.getLog(getClass());
36+
37+
38+
@Override
39+
public Resource resolveResource(HttpServletRequest request, String requestPath,
40+
List<? extends Resource> locations, ResourceResolverChain chain) {
41+
42+
if (logger.isTraceEnabled()) {
43+
logger.trace("Resolving resource: requestPath=\"" + requestPath + "\"");
44+
}
45+
return resolveResourceInternal(request, requestPath, locations, chain);
46+
}
47+
48+
protected abstract Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
49+
List<? extends Resource> locations, ResourceResolverChain chain);
50+
51+
@Override
52+
public String resolvePublicUrlPath(String resourceUrlPath, List<? extends Resource> locations,
53+
ResourceResolverChain chain) {
54+
55+
if (logger.isTraceEnabled()) {
56+
logger.trace("Resolving public URL for path=\"" + resourceUrlPath + "\"");
57+
}
58+
59+
return resolvePublicUrlPathInternal(resourceUrlPath, locations, chain);
60+
}
61+
62+
protected abstract String resolvePublicUrlPathInternal(String resourceUrlPath,
63+
List<? extends Resource> locations, ResourceResolverChain chain);
64+
65+
}

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultResourceResolverChain.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
*/
3939
class DefaultResourceResolverChain implements ResourceResolverChain {
4040

41-
private static final Log logger = LogFactory.getLog(DefaultResourceResolverChain.class);
42-
4341
private final List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
4442

4543
private int index = -1;
@@ -59,10 +57,7 @@ public Resource resolveResource(HttpServletRequest request, String requestPath,
5957
return null;
6058
}
6159
try {
62-
logBefore(resolver);
63-
Resource resource = resolver.resolveResource(request, requestPath, locations, this);
64-
logAfter(resolver, resource);
65-
return resource;
60+
return resolver.resolveResource(request, requestPath, locations, this);
6661
}
6762
finally {
6863
this.index--;
@@ -76,10 +71,7 @@ public String resolvePublicUrlPath(String resourcePath, List<? extends Resource>
7671
return null;
7772
}
7873
try {
79-
logBefore(resolver);
80-
String urlPath = resolver.resolvePublicUrlPath(resourcePath, locations, this);
81-
logAfter(resolver, urlPath);
82-
return urlPath;
74+
return resolver.resolvePublicUrlPath(resourcePath, locations, this);
8375
}
8476
finally {
8577
this.index--;
@@ -92,26 +84,11 @@ private ResourceResolver getNextResolver() {
9284
"Current index exceeds the number of configured ResourceResolver's");
9385

9486
if (this.index == (this.resolvers.size() - 1)) {
95-
if (logger.isTraceEnabled()) {
96-
logger.trace("No more ResourceResolver's to delegate to, returning null");
97-
}
9887
return null;
9988
}
10089

10190
this.index++;
10291
return this.resolvers.get(this.index);
10392
}
10493

105-
private void logBefore(ResourceResolver resolver) {
106-
if (logger.isTraceEnabled()) {
107-
logger.trace("Calling " + resolver.getClass().getSimpleName() + " index=" + this.index);
108-
}
109-
}
110-
111-
private void logAfter(ResourceResolver resolver, Object result) {
112-
if (logger.isTraceEnabled()) {
113-
logger.trace(resolver.getClass().getSimpleName() + " returned " + result);
114-
}
115-
}
116-
11794
}

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/FingerprintResourceResolver.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@
5050
* @author Sam Brannen
5151
* @since 4.1
5252
*/
53-
public class FingerprintResourceResolver implements ResourceResolver {
54-
55-
private static final Log logger = LogFactory.getLog(FingerprintResourceResolver.class);
53+
public class FingerprintResourceResolver extends AbstractResourceResolver {
5654

5755
private static final Pattern pattern = Pattern.compile("-(\\S*)\\.");
5856

5957

6058
@Override
61-
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations,
62-
ResourceResolverChain chain) {
59+
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
60+
List<? extends Resource> locations, ResourceResolverChain chain) {
6361

6462
Resource resolved = chain.resolveResource(request, requestPath, locations);
6563
if (resolved != null) {
@@ -68,25 +66,58 @@ public Resource resolveResource(HttpServletRequest request, String requestPath,
6866

6967
String hash = extractHash(requestPath);
7068
if (StringUtils.isEmpty(hash)) {
69+
if (logger.isTraceEnabled()) {
70+
logger.trace("No hash found");
71+
}
7172
return null;
7273
}
7374

7475
String simplePath = StringUtils.delete(requestPath, "-" + hash);
76+
77+
if (logger.isTraceEnabled()) {
78+
logger.trace("Extracted hash from path, re-resolving without hash, path=\"" + simplePath + "\"");
79+
}
80+
7581
Resource baseResource = chain.resolveResource(request, simplePath, locations);
7682
if (baseResource == null) {
7783
return null;
7884
}
7985

8086
String candidateHash = calculateHash(baseResource);
8187
if (candidateHash.equals(hash)) {
88+
if (logger.isTraceEnabled()) {
89+
logger.trace("Calculated hash matches extracted hash");
90+
}
8291
return baseResource;
8392
}
8493
else {
85-
logger.debug("Potential resource found for [" + requestPath + "], but fingerprint doesn't match.");
94+
logger.trace("Potential resource found for [" + requestPath + "], but fingerprint doesn't match.");
8695
return null;
8796
}
8897
}
8998

99+
@Override
100+
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
101+
ResourceResolverChain chain) {
102+
103+
String baseUrl = chain.resolvePublicUrlPath(resourceUrlPath, locations);
104+
if (StringUtils.hasText(baseUrl)) {
105+
if (logger.isTraceEnabled()) {
106+
logger.trace("Getting the original resource to calculate hash");
107+
}
108+
Resource original = chain.resolveResource(null, resourceUrlPath, locations);
109+
String hash = calculateHash(original);
110+
if (logger.isTraceEnabled()) {
111+
logger.trace("Calculated hash=" + hash);
112+
}
113+
String baseFilename = StringUtils.stripFilenameExtension(baseUrl);
114+
String extension = StringUtils.getFilenameExtension(baseUrl);
115+
return baseFilename + "-" + hash + "." + extension;
116+
}
117+
return baseUrl;
118+
}
119+
120+
90121
private String extractHash(String path) {
91122
Matcher matcher = pattern.matcher(path);
92123
if (matcher.find()) {
@@ -109,17 +140,4 @@ private String calculateHash(Resource resource) {
109140
}
110141
}
111142

112-
@Override
113-
public String resolvePublicUrlPath(String resourceUrlPath, List<? extends Resource> locations,
114-
ResourceResolverChain chain) {
115-
String baseUrl = chain.resolvePublicUrlPath(resourceUrlPath, locations);
116-
if (StringUtils.hasText(baseUrl)) {
117-
Resource original = chain.resolveResource(null, resourceUrlPath, locations);
118-
String hash = calculateHash(original);
119-
return StringUtils.stripFilenameExtension(baseUrl) + "-" + hash + "."
120-
+ StringUtils.getFilenameExtension(baseUrl);
121-
}
122-
return baseUrl;
123-
}
124-
125143
}

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/GzipResourceResolver.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@
4343
* @author Sam Brannen
4444
* @since 4.1
4545
*/
46-
public class GzipResourceResolver implements ResourceResolver {
47-
48-
private static final Log logger = LogFactory.getLog(GzipResourceResolver.class);
46+
public class GzipResourceResolver extends AbstractResourceResolver {
4947

5048

5149
@Override
52-
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations,
53-
ResourceResolverChain chain) {
50+
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
51+
List<? extends Resource> locations, ResourceResolverChain chain) {
5452

5553
Resource resource = chain.resolveResource(request, requestPath, locations);
5654
if ((resource == null) || !isGzipAccepted(request)) {
@@ -76,8 +74,9 @@ private boolean isGzipAccepted(HttpServletRequest request) {
7674
}
7775

7876
@Override
79-
public String resolvePublicUrlPath(String resourceUrlPath, List<? extends Resource> locations,
77+
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
8078
ResourceResolverChain chain) {
79+
8180
return chain.resolvePublicUrlPath(resourceUrlPath, locations);
8281
}
8382

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,42 @@
3737
* @author Sam Brannen
3838
* @since 4.1
3939
*/
40-
public class PathResourceResolver implements ResourceResolver {
41-
42-
private static final Log logger = LogFactory.getLog(PathResourceResolver.class);
40+
public class PathResourceResolver extends AbstractResourceResolver {
4341

4442

4543
@Override
46-
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations,
47-
ResourceResolverChain chain) {
44+
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
45+
List<? extends Resource> locations, ResourceResolverChain chain) {
46+
4847
return getResource(requestPath, locations);
4948
}
5049

5150
@Override
52-
public String resolvePublicUrlPath(String resourceUrlPath, List<? extends Resource> locations,
51+
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
5352
ResourceResolverChain chain) {
54-
return (getResource(resourceUrlPath, locations) != null) ? resourceUrlPath : null;
53+
54+
return (getResource(resourceUrlPath, locations) != null ? resourceUrlPath : null);
5555
}
5656

5757
private Resource getResource(String path, List<? extends Resource> locations) {
5858
for (Resource location : locations) {
5959
try {
60-
if (logger.isDebugEnabled()) {
61-
logger.debug("Looking for \"" + path + "\" under " + location);
60+
if (logger.isTraceEnabled()) {
61+
logger.trace("Checking location=[" + location + "]");
6262
}
6363
Resource resource = location.createRelative(path);
6464
if (resource.exists() && resource.isReadable()) {
65-
if (logger.isDebugEnabled()) {
66-
logger.debug("Resource exists and is readable");
65+
if (logger.isTraceEnabled()) {
66+
logger.trace("Found match");
6767
}
6868
return resource;
6969
}
7070
else if (logger.isTraceEnabled()) {
71-
logger.trace("Relative resource doesn't exist or isn't readable: " + resource);
71+
logger.trace("No match");
7272
}
7373
}
7474
catch (IOException ex) {
75-
logger.debug("Failed to create relative resource - trying next resource location", ex);
75+
logger.trace("Failure checking for relative resource. Trying next location.", ex);
7676
}
7777
}
7878
return null;

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PrefixResourceResolver.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.web.servlet.resource;
1818

19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
1921
import org.springframework.core.io.Resource;
2022
import org.springframework.util.Assert;
2123

@@ -39,7 +41,9 @@
3941
* @author Sam Brannen
4042
* @since 4.1
4143
*/
42-
public class PrefixResourceResolver implements ResourceResolver {
44+
public class PrefixResourceResolver extends AbstractResourceResolver {
45+
46+
private static final Log logger = LogFactory.getLog(PathResourceResolver.class);
4347

4448
private final String prefix;
4549

@@ -50,9 +54,12 @@ public PrefixResourceResolver(String prefix) {
5054
}
5155

5256
@Override
53-
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations,
54-
ResourceResolverChain chain) {
57+
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
58+
List<? extends Resource> locations, ResourceResolverChain chain) {
5559

60+
if (logger.isTraceEnabled()) {
61+
logger.trace("Resolving resource: requestPath=\"" + requestPath + "\"");
62+
}
5663
if (requestPath.startsWith(this.prefix)) {
5764
requestPath = requestPath.substring(this.prefix.length());
5865
}
@@ -61,8 +68,9 @@ public Resource resolveResource(HttpServletRequest request, String requestPath,
6168
}
6269

6370
@Override
64-
public String resolvePublicUrlPath(String resourceUrlPath, List<? extends Resource> locations,
71+
protected String resolvePublicUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations,
6572
ResourceResolverChain chain) {
73+
6674
String baseUrl = chain.resolvePublicUrlPath(resourceUrlPath, locations);
6775
return this.prefix + (baseUrl.startsWith("/") ? baseUrl : "/" + baseUrl);
6876
}

0 commit comments

Comments
 (0)