ResourceUrlEncodingFilter.java 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.servlet.resource;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

30
import org.springframework.web.filter.OncePerRequestFilter;
31 32 33 34

/**
 * A filter that wraps the {@link HttpServletResponse} and overrides its
 * {@link HttpServletResponse#encodeURL(String) encodeURL} method in order to
35 36
 * translate internal resource request URLs into public URL paths for external
 * use.
37 38 39
 *
 * @author Jeremy Grelle
 * @author Rossen Stoyanchev
40
 * @author Sam Brannen
41 42 43 44 45 46 47 48
 * @since 4.1
 */
public class ResourceUrlEncodingFilter extends OncePerRequestFilter {

	private static Log logger = LogFactory.getLog(ResourceUrlEncodingFilter.class);


	@Override
49 50
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
51 52 53 54

		filterChain.doFilter(request, new ResourceUrlEncodingResponseWrapper(request, response));
	}

55 56

	private static class ResourceUrlEncodingResponseWrapper extends HttpServletResponseWrapper {
57 58 59

		private HttpServletRequest request;

60

61 62 63 64 65 66 67 68
		private ResourceUrlEncodingResponseWrapper(HttpServletRequest request, HttpServletResponse wrapped) {
			super(wrapped);
			this.request = request;
		}

		@Override
		public String encodeURL(String url) {
			String name = PublicResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR;
69 70 71
			PublicResourceUrlProvider urlProvider = (PublicResourceUrlProvider) this.request.getAttribute(name);
			if (urlProvider != null) {
				String translatedUrl = urlProvider.getForRequestUrl(this.request, url);
72 73 74 75 76
				if (translatedUrl != null) {
					return super.encodeURL(translatedUrl);
				}
			}
			else {
77
				logger.debug("Request attribute exposing PublicResourceUrlProvider not found under name: " + name);
78 79 80 81 82 83
			}
			return super.encodeURL(url);
		}
	}

}