01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.web.context.support;
18:
19: import java.util.HashSet;
20: import java.util.Set;
21:
22: import javax.servlet.http.HttpServletRequest;
23: import javax.servlet.http.HttpServletRequestWrapper;
24:
25: import org.springframework.web.context.WebApplicationContext;
26:
27: /**
28: * HttpServletRequest decorator that makes all Spring beans in a
29: * given WebApplicationContext accessible as request attributes,
30: * through lazy checking once an attribute gets accessed.
31: *
32: * @author Juergen Hoeller
33: * @since 2.5
34: */
35: public class ContextExposingHttpServletRequest extends
36: HttpServletRequestWrapper {
37:
38: private final WebApplicationContext webApplicationContext;
39:
40: private Set explicitAttributes;
41:
42: /**
43: * Create a new ContextExposingHttpServletRequest for the given request.
44: * @param originalRequest the original HttpServletRequest
45: * @param context the WebApplicationContext that this request runs in
46: */
47: public ContextExposingHttpServletRequest(
48: HttpServletRequest originalRequest,
49: WebApplicationContext context) {
50: super (originalRequest);
51: this .webApplicationContext = context;
52: }
53:
54: /**
55: * Return the WebApplicationContext that this request runs in.
56: */
57: public final WebApplicationContext getWebApplicationContext() {
58: return this .webApplicationContext;
59: }
60:
61: public Object getAttribute(String name) {
62: if ((this .explicitAttributes == null || !this .explicitAttributes
63: .contains(name))
64: && this .webApplicationContext.containsBean(name)) {
65: return this .webApplicationContext.getBean(name);
66: } else {
67: return super .getAttribute(name);
68: }
69: }
70:
71: public void setAttribute(String name, Object value) {
72: super .setAttribute(name, value);
73: if (this .explicitAttributes == null) {
74: this .explicitAttributes = new HashSet(8);
75: }
76: this.explicitAttributes.add(name);
77: }
78:
79: }
|