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  package org.springframework.web.servlet.resource;
17  
18  import java.util.Arrays;
19  import javax.servlet.http.HttpServletRequest;
20  
21  import org.springframework.core.io.Resource;
22  
23  /**
24   * A base class for a {@code ResourceTransformer} with an optional helper method
25   * for resolving public links within a transformed resource.
26   *
27   * @author Brian Clozel
28   * @author Rossen Stoyanchev
29   * @since 4.1
30   */
31  public abstract class ResourceTransformerSupport implements ResourceTransformer {
32  
33  	private ResourceUrlProvider resourceUrlProvider;
34  
35  
36  	/**
37  	 * Configure a {@link ResourceUrlProvider} to use when resolving the public
38  	 * URL of links in a transformed resource (e.g. import links in a CSS file).
39  	 * This is required only for links expressed as full paths, i.e. including
40  	 * context and servlet path, and not for relative links.
41  	 *
42  	 * <p>By default this property is not set. In that case if a
43  	 * {@code ResourceUrlProvider} is needed an attempt is made to find the
44  	 * {@code ResourceUrlProvider} exposed through the
45  	 * {@link org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor
46  	 * ResourceUrlProviderExposingInterceptor} (configured by default in the MVC
47  	 * Java config and XML namespace). Therefore explicitly configuring this
48  	 * property should not be needed in most cases.
49  	 * @param resourceUrlProvider the URL provider to use
50  	 */
51  	public void setResourceUrlProvider(ResourceUrlProvider resourceUrlProvider) {
52  		this.resourceUrlProvider = resourceUrlProvider;
53  	}
54  
55  	/**
56  	 * @return the configured {@code ResourceUrlProvider}.
57  	 */
58  	public ResourceUrlProvider getResourceUrlProvider() {
59  		return this.resourceUrlProvider;
60  	}
61  
62  
63  	/**
64  	 * A transformer can use this method when a resource being transformed
65  	 * contains links to other resources. Such links need to be replaced with the
66  	 * public facing link as determined by the resource resolver chain (e.g. the
67  	 * public URL may have a version inserted).
68  	 *
69  	 * @param resourcePath the path to a resource that needs to be re-written
70  	 * @param request the current request
71  	 * @param resource the resource being transformed
72  	 * @param transformerChain the transformer chain
73  	 * @return the resolved URL or null
74  	 */
75  	protected String resolveUrlPath(String resourcePath, HttpServletRequest request,
76  			Resource resource, ResourceTransformerChain transformerChain) {
77  
78  		if (resourcePath.startsWith("/")) {
79  			// full resource path
80  			ResourceUrlProvider urlProvider = findResourceUrlProvider(request);
81  			return (urlProvider != null ? urlProvider.getForRequestUrl(request, resourcePath) : null);
82  		}
83  		else {
84  			// try resolving as relative path
85  			return transformerChain.getResolverChain().resolveUrlPath(resourcePath, Arrays.asList(resource));
86  		}
87  	}
88  
89  	private ResourceUrlProvider findResourceUrlProvider(HttpServletRequest request) {
90  		if (this.resourceUrlProvider != null) {
91  			return this.resourceUrlProvider;
92  		}
93  		return (ResourceUrlProvider) request.getAttribute(
94  				ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR);
95  	}
96  
97  }