1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.context.contexts;
17
18 import javax.servlet.http.HttpServletRequest;
19
20 import net.sf.morph.reflect.BeanReflector;
21 import net.sf.morph.reflect.reflectors.HttpSessionAttributeReflector;
22 import net.sf.morph.reflect.reflectors.ServletContextAttributeReflector;
23 import net.sf.morph.reflect.reflectors.ServletRequestAttributeReflector;
24 import net.sf.morph.reflect.reflectors.ServletRequestParameterReflector;
25
26 /**
27 * <p>
28 * Context for HTTP servlets that evaluates request parameters, request
29 * attributes, session attributes, and application attributes, in that order.
30 * Exposing request parameters in this manner is somewhat unusual, so
31 * {@link HttpServletContext#setReflectingRequestParameters(boolean)}can be
32 * called to turn this behavior off.
33 * </p>
34 *
35 * <p>
36 * This class is <em>not</em> threadsafe, but since usually only a single
37 * thread is accessing a request at any given time, this should be OK.
38 * </p>
39 *
40 * @author Matt Sgarlata
41 * @since Nov 29, 2004
42 */
43 public class HttpServletContext extends ReflectorHierarchicalContext {
44
45 private static final BeanReflector REQUEST_PARAMETER_REFLECTOR = new ServletRequestParameterReflector();
46 private static final BeanReflector REQUEST_ATTRIBUTE_REFLECTOR = new ServletRequestAttributeReflector();
47 private static final BeanReflector SESSION_ATTRIBUTE_REFLECTOR = new HttpSessionAttributeReflector();
48 private static final BeanReflector APPLICATION_ATTRIBUTE_REFLECTOR = new ServletContextAttributeReflector();
49
50 private ReflectorHierarchicalContext applicationContext;
51 private ReflectorHierarchicalContext sessionContext;
52 private ReflectorHierarchicalContext requestContext;
53
54 private boolean reflectingRequestParameters;
55
56 public HttpServletContext() {
57 super();
58 reflectingRequestParameters = true;
59
60 applicationContext = new ReflectorHierarchicalContext();
61 applicationContext.setBeanReflector(APPLICATION_ATTRIBUTE_REFLECTOR);
62
63 sessionContext = new ReflectorHierarchicalContext();
64 sessionContext.setBeanReflector(SESSION_ATTRIBUTE_REFLECTOR);
65 sessionContext.setParentContext(applicationContext);
66
67 requestContext = new ReflectorHierarchicalContext();
68 requestContext.setBeanReflector(REQUEST_ATTRIBUTE_REFLECTOR);
69 requestContext.setParentContext(sessionContext);
70
71 this.setBeanReflector(REQUEST_PARAMETER_REFLECTOR);
72 this.setParentContext(requestContext);
73 }
74
75 public HttpServletContext(HttpServletRequest request) {
76 this();
77 setRequest(request);
78 }
79
80 /**
81 * This method is overridden so that set calls are always invoked on the
82 * requestContext, since request parameters cannot be set but logically a
83 * set call is being sent to the request, so it should succeed.
84 */
85 protected void setHierarchicalImpl(String propertyName, Object propertyValue)
86 throws Exception {
87 requestContext.set(propertyName, propertyValue);
88 }
89
90 public HttpServletRequest getRequest() {
91 return (HttpServletRequest) getDelegate();
92 }
93 public void setRequest(HttpServletRequest request) {
94 applicationContext.setDelegate(request.getSession().getServletContext());
95 sessionContext.setDelegate(request.getSession());
96 requestContext.setDelegate(request);
97 this.setDelegate(request);
98 }
99 /**
100 * @return Returns the reflectingRequestParameters.
101 */
102 public boolean isReflectingRequestParameters() {
103 return reflectingRequestParameters;
104 }
105 /**
106 * @param reflectingRequestParameters The reflectingRequestParameters to set.
107 */
108 public void setReflectingRequestParameters(
109 boolean reflectingRequestParameters) {
110 this.reflectingRequestParameters = reflectingRequestParameters;
111 if (reflectingRequestParameters) {
112 this.setBeanReflector(REQUEST_PARAMETER_REFLECTOR);
113 this.setParentContext(requestContext);
114 }
115 else {
116 this.setBeanReflector(REQUEST_ATTRIBUTE_REFLECTOR);
117 this.setParentContext(sessionContext);
118 }
119 }
120 }