001/*
002 * Copyright 2002-2008 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.context.support;
018
019import java.util.HashSet;
020import java.util.Set;
021import javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpServletRequestWrapper;
023
024import org.springframework.util.Assert;
025import org.springframework.web.context.WebApplicationContext;
026
027/**
028 * HttpServletRequest decorator that makes all Spring beans in a
029 * given WebApplicationContext accessible as request attributes,
030 * through lazy checking once an attribute gets accessed.
031 *
032 * @author Juergen Hoeller
033 * @since 2.5
034 */
035public class ContextExposingHttpServletRequest extends HttpServletRequestWrapper {
036
037        private final WebApplicationContext webApplicationContext;
038
039        private final Set<String> exposedContextBeanNames;
040
041        private Set<String> explicitAttributes;
042
043
044        /**
045         * Create a new ContextExposingHttpServletRequest for the given request.
046         * @param originalRequest the original HttpServletRequest
047         * @param context the WebApplicationContext that this request runs in
048         */
049        public ContextExposingHttpServletRequest(HttpServletRequest originalRequest, WebApplicationContext context) {
050                this(originalRequest, context, null);
051        }
052
053        /**
054         * Create a new ContextExposingHttpServletRequest for the given request.
055         * @param originalRequest the original HttpServletRequest
056         * @param context the WebApplicationContext that this request runs in
057         * @param exposedContextBeanNames the names of beans in the context which
058         * are supposed to be exposed (if this is non-null, only the beans in this
059         * Set are eligible for exposure as attributes)
060         */
061        public ContextExposingHttpServletRequest(
062                        HttpServletRequest originalRequest, WebApplicationContext context, Set<String> exposedContextBeanNames) {
063
064                super(originalRequest);
065                Assert.notNull(context, "WebApplicationContext must not be null");
066                this.webApplicationContext = context;
067                this.exposedContextBeanNames = exposedContextBeanNames;
068        }
069
070
071        /**
072         * Return the WebApplicationContext that this request runs in.
073         */
074        public final WebApplicationContext getWebApplicationContext() {
075                return this.webApplicationContext;
076        }
077
078
079        @Override
080        public Object getAttribute(String name) {
081                if ((this.explicitAttributes == null || !this.explicitAttributes.contains(name)) &&
082                                (this.exposedContextBeanNames == null || this.exposedContextBeanNames.contains(name)) &&
083                                this.webApplicationContext.containsBean(name)) {
084                        return this.webApplicationContext.getBean(name);
085                }
086                else {
087                        return super.getAttribute(name);
088                }
089        }
090
091        @Override
092        public void setAttribute(String name, Object value) {
093                super.setAttribute(name, value);
094                if (this.explicitAttributes == null) {
095                        this.explicitAttributes = new HashSet<String>(8);
096                }
097                this.explicitAttributes.add(name);
098        }
099
100}