001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.resource;
018
019import java.io.IOException;
020
021import javax.servlet.http.HttpServletRequest;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025
026import org.springframework.cache.Cache;
027import org.springframework.cache.CacheManager;
028import org.springframework.core.io.Resource;
029import org.springframework.util.Assert;
030
031/**
032 * A {@link org.springframework.web.servlet.resource.ResourceTransformer} that checks a
033 * {@link org.springframework.cache.Cache} to see if a previously transformed resource
034 * exists in the cache and returns it if found, and otherwise delegates to the resolver
035 * chain and saves the result in the cache.
036 *
037 * @author Rossen Stoyanchev
038 * @since 4.1
039 */
040public class CachingResourceTransformer implements ResourceTransformer {
041
042        private static final Log logger = LogFactory.getLog(CachingResourceTransformer.class);
043
044        private final Cache cache;
045
046
047        public CachingResourceTransformer(Cache cache) {
048                Assert.notNull(cache, "Cache is required");
049                this.cache = cache;
050        }
051
052        public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
053                Cache cache = cacheManager.getCache(cacheName);
054                if (cache == null) {
055                        throw new IllegalArgumentException("Cache '" + cacheName + "' not found");
056                }
057                this.cache = cache;
058        }
059
060
061        /**
062         * Return the configured {@code Cache}.
063         */
064        public Cache getCache() {
065                return this.cache;
066        }
067
068
069        @Override
070        public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain)
071                        throws IOException {
072
073                Resource transformed = this.cache.get(resource, Resource.class);
074                if (transformed != null) {
075                        logger.trace("Resource resolved from cache");
076                        return transformed;
077                }
078
079                transformed = transformerChain.transform(request, resource);
080                this.cache.put(resource, transformed);
081
082                return transformed;
083        }
084
085}