001/*
002 * Copyright 2002-2018 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.util.Collections;
020
021import javax.servlet.http.HttpServletRequest;
022
023import org.springframework.core.io.Resource;
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026import org.springframework.util.StringUtils;
027
028/**
029 * A base class for a {@code ResourceTransformer} with an optional helper method
030 * for resolving public links within a transformed resource.
031 *
032 * @author Brian Clozel
033 * @author Rossen Stoyanchev
034 * @author Juergen Hoeller
035 * @since 4.1
036 */
037public abstract class ResourceTransformerSupport implements ResourceTransformer {
038
039        @Nullable
040        private ResourceUrlProvider resourceUrlProvider;
041
042
043        /**
044         * Configure a {@link ResourceUrlProvider} to use when resolving the public
045         * URL of links in a transformed resource (e.g. import links in a CSS file).
046         * This is required only for links expressed as full paths and not for
047         * relative links.
048         */
049        public void setResourceUrlProvider(@Nullable ResourceUrlProvider resourceUrlProvider) {
050                this.resourceUrlProvider = resourceUrlProvider;
051        }
052
053        /**
054         * Return the configured {@code ResourceUrlProvider}.
055         */
056        @Nullable
057        public ResourceUrlProvider getResourceUrlProvider() {
058                return this.resourceUrlProvider;
059        }
060
061
062        /**
063         * A transformer can use this method when a resource being transformed
064         * contains links to other resources. Such links need to be replaced with the
065         * public facing link as determined by the resource resolver chain (e.g. the
066         * public URL may have a version inserted).
067         * @param resourcePath the path to a resource that needs to be re-written
068         * @param request the current request
069         * @param resource the resource being transformed
070         * @param transformerChain the transformer chain
071         * @return the resolved URL, or {@code} if not resolvable
072         */
073        @Nullable
074        protected String resolveUrlPath(String resourcePath, HttpServletRequest request,
075                        Resource resource, ResourceTransformerChain transformerChain) {
076
077                if (resourcePath.startsWith("/")) {
078                        // full resource path
079                        ResourceUrlProvider urlProvider = findResourceUrlProvider(request);
080                        return (urlProvider != null ? urlProvider.getForRequestUrl(request, resourcePath) : null);
081                }
082                else {
083                        // try resolving as relative path
084                        return transformerChain.getResolverChain().resolveUrlPath(
085                                        resourcePath, Collections.singletonList(resource));
086                }
087        }
088
089        /**
090         * Transform the given relative request path to an absolute path,
091         * taking the path of the given request as a point of reference.
092         * The resulting path is also cleaned from sequences like "path/..".
093         * @param path the relative path to transform
094         * @param request the referer request
095         * @return the absolute request path for the given resource path
096         */
097        protected String toAbsolutePath(String path, HttpServletRequest request) {
098                String absolutePath = path;
099                if (!path.startsWith("/")) {
100                        ResourceUrlProvider urlProvider = findResourceUrlProvider(request);
101                        Assert.state(urlProvider != null, "No ResourceUrlProvider");
102                        String requestPath = urlProvider.getUrlPathHelper().getRequestUri(request);
103                        absolutePath = StringUtils.applyRelativePath(requestPath, path);
104                }
105                return StringUtils.cleanPath(absolutePath);
106        }
107
108        @Nullable
109        private ResourceUrlProvider findResourceUrlProvider(HttpServletRequest request) {
110                if (this.resourceUrlProvider != null) {
111                        return this.resourceUrlProvider;
112                }
113                return (ResourceUrlProvider) request.getAttribute(
114                                ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR);
115        }
116
117}