001/*
002 * Copyright 2002-2015 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 javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import org.springframework.util.Assert;
023import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
024
025/**
026 * An interceptor that exposes the {@link ResourceUrlProvider} instance it
027 * is configured with as a request attribute.
028 *
029 * @author Rossen Stoyanchev
030 * @since 4.1
031 */
032public class ResourceUrlProviderExposingInterceptor extends HandlerInterceptorAdapter {
033
034        /**
035         * Name of the request attribute that holds the {@link ResourceUrlProvider}.
036         */
037        public static final String RESOURCE_URL_PROVIDER_ATTR = ResourceUrlProvider.class.getName();
038
039        private final ResourceUrlProvider resourceUrlProvider;
040
041
042        public ResourceUrlProviderExposingInterceptor(ResourceUrlProvider resourceUrlProvider) {
043                Assert.notNull(resourceUrlProvider, "ResourceUrlProvider is required");
044                this.resourceUrlProvider = resourceUrlProvider;
045        }
046
047        @Override
048        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
049                        throws Exception {
050
051                request.setAttribute(RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
052                return true;
053        }
054
055}