View Javadoc
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 java.util.List;
20  import javax.servlet.http.HttpServletRequest;
21  
22  import org.springframework.cache.Cache;
23  import org.springframework.cache.CacheManager;
24  import org.springframework.core.io.Resource;
25  import org.springframework.util.Assert;
26  
27  /**
28   * A {@link org.springframework.web.servlet.resource.ResourceResolver} that
29   * resolves resources from a {@link org.springframework.cache.Cache} or otherwise
30   * delegates to the resolver chain and saves the result in the cache.
31   *
32   * @author Rossen Stoyanchev
33   * @since 4.1
34   */
35  public class CachingResourceResolver extends AbstractResourceResolver {
36  
37  	public static final String RESOLVED_RESOURCE_CACHE_KEY_PREFIX = "resolvedResource:";
38  
39  	public static final String RESOLVED_URL_PATH_CACHE_KEY_PREFIX = "resolvedUrlPath:";
40  
41  
42  	private final Cache cache;
43  
44  	public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
45  		this(cacheManager.getCache(cacheName));
46  	}
47  
48  	public CachingResourceResolver(Cache cache) {
49  		Assert.notNull(cache, "'cache' is required");
50  		this.cache = cache;
51  	}
52  
53  	/**
54  	 * Return the configured {@code Cache}.
55  	 */
56  	public Cache getCache() {
57  		return this.cache;
58  	}
59  
60  	@Override
61  	protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
62  			List<? extends Resource> locations, ResourceResolverChain chain) {
63  
64  		String key = RESOLVED_RESOURCE_CACHE_KEY_PREFIX + requestPath;
65  		Resource resource = this.cache.get(key, Resource.class);
66  
67  		if (resource != null) {
68  			if (logger.isTraceEnabled()) {
69  				logger.trace("Found match");
70  			}
71  			return resource;
72  		}
73  
74  		resource = chain.resolveResource(request, requestPath, locations);
75  		if (resource != null) {
76  			if (logger.isTraceEnabled()) {
77  				logger.trace("Putting resolved resource in cache");
78  			}
79  			this.cache.put(key, resource);
80  		}
81  
82  		return resource;
83  	}
84  
85  	@Override
86  	protected String resolveUrlPathInternal(String resourceUrlPath,
87  			List<? extends Resource> locations, ResourceResolverChain chain) {
88  
89  		String key = RESOLVED_URL_PATH_CACHE_KEY_PREFIX + resourceUrlPath;
90  		String resolvedUrlPath = this.cache.get(key, String.class);
91  
92  		if (resolvedUrlPath != null) {
93  			if (logger.isTraceEnabled()) {
94  				logger.trace("Found match");
95  			}
96  			return resolvedUrlPath;
97  		}
98  
99  		resolvedUrlPath = chain.resolveUrlPath(resourceUrlPath, locations);
100 		if (resolvedUrlPath != null) {
101 			if (logger.isTraceEnabled()) {
102 				logger.trace("Putting resolved resource URL path in cache");
103 			}
104 			this.cache.put(key, resolvedUrlPath);
105 		}
106 
107 		return resolvedUrlPath;
108 	}
109 
110 }