1 package org.apache.velocity.tools.view;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.http.HttpSession;
28 import javax.servlet.ServletContext;
29 import org.apache.velocity.app.VelocityEngine;
30 import org.apache.velocity.context.Context;
31 import org.apache.velocity.tools.ToolContext;
32 import org.apache.velocity.tools.Toolbox;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class ViewToolContext extends ToolContext implements ViewContext
74 {
75 private final HttpServletRequest request;
76 private final HttpServletResponse response;
77 private final ServletContext application;
78 private final VelocityEngine velocity;
79 private String toolboxKey = DEFAULT_TOOLBOX_KEY;
80
81 public ViewToolContext(VelocityEngine velocity,
82 HttpServletRequest request,
83 HttpServletResponse response,
84 ServletContext application)
85 {
86 super(velocity);
87
88 this.velocity = velocity;
89 this.request = request;
90 this.response = response;
91 this.application = application;
92
93
94 putToolProperties();
95 }
96
97 protected void setToolboxKey(String key)
98 {
99 this.toolboxKey = key;
100 }
101
102 protected void putToolProperties()
103 {
104 putToolProperty(REQUEST, getRequest());
105 if (getRequest() != null)
106 {
107 putToolProperty(LOCALE_KEY, getRequest().getLocale());
108 }
109 putToolProperty(RESPONSE, getResponse());
110 putToolProperty(SESSION, getSession());
111 putToolProperty(SERVLET_CONTEXT_KEY, getServletContext());
112 putToolProperty(PATH_KEY, ServletUtils.getPath(getRequest()));
113 }
114
115 protected List<Toolbox> getToolboxes()
116 {
117
118
119
120
121
122
123 if (super.getToolboxes().isEmpty())
124 {
125 addToolboxesUnderKey(this.toolboxKey);
126 }
127 return super.getToolboxes();
128 }
129
130 protected void addToolboxesUnderKey(String toolboxKey)
131 {
132 Toolbox reqTools = (Toolbox)getRequest().getAttribute(toolboxKey);
133 if (reqTools != null)
134 {
135 addToolbox(reqTools);
136 }
137
138 if (getSession() != null)
139 {
140 Toolbox sessTools = (Toolbox)getSession().getAttribute(toolboxKey);
141 if (sessTools != null)
142 {
143 addToolbox(sessTools);
144 }
145 }
146
147 Toolbox appTools = (Toolbox)getServletContext().getAttribute(toolboxKey);
148 if (appTools != null)
149 {
150 addToolbox(appTools);
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164 @Override
165 public Object get(String key)
166 {
167 boolean overwrite = getUserCanOverwriteTools();
168 Object o = overwrite ? getUserVar(key) : getToolVar(key);
169 if (o == null)
170 {
171 o = overwrite ? getToolVar(key) : getUserVar(key);
172 }
173 return o;
174 }
175
176
177
178
179
180
181
182 protected Object getUserVar(String key)
183 {
184 Object o = internalGet(key);
185 if (o != null)
186 {
187 return o;
188 }
189 return getAttribute(key);
190 }
191
192
193
194
195
196
197
198 protected Object getToolVar(String key)
199 {
200 Object o = findTool(key);
201 if (o != null)
202 {
203 return o;
204 }
205 return getServletApi(key);
206 }
207
208
209
210
211
212
213 protected Object getServletApi(String key)
214 {
215 if (key.equals(REQUEST))
216 {
217 return request;
218 }
219 else if(key.equals(RESPONSE))
220 {
221 return response;
222 }
223 else if (key.equals(SESSION))
224 {
225 return getSession();
226 }
227 else if (key.equals(APPLICATION))
228 {
229 return application;
230 }
231 return null;
232 }
233
234
235
236
237
238
239
240
241
242 public Object getAttribute(String key)
243 {
244 Object o = request.getAttribute(key);
245 if (o == null)
246 {
247 if (getSession() != null)
248 {
249 try
250 {
251 o = getSession().getAttribute(key);
252 }
253 catch (IllegalStateException ise)
254 {
255
256 o = null;
257 }
258 }
259
260 if (o == null)
261 {
262 o = application.getAttribute(key);
263 }
264 }
265 return o;
266 }
267
268
269
270
271
272 public HttpServletRequest getRequest()
273 {
274 return request;
275 }
276
277
278
279
280 public HttpServletResponse getResponse()
281 {
282 return response;
283 }
284
285
286
287
288 public HttpSession getSession()
289 {
290 return getRequest().getSession(false);
291 }
292
293
294
295
296 public ServletContext getServletContext()
297 {
298 return application;
299 }
300
301
302
303
304 public Context getVelocityContext()
305 {
306 return this;
307 }
308
309
310
311
312 public VelocityEngine getVelocityEngine()
313 {
314 return velocity;
315 }
316
317
318
319
320
321
322
323 public boolean containsKey(String key)
324 {
325 return super.containsKey(key)
326 || getAttribute(key) != null
327 || key.equals(REQUEST) && request != null
328 || key.equals(RESPONSE) && response != null
329 || key.equals(SESSION) && getSession() != null
330 || key.equals(APPLICATION) && application != null;
331 }
332
333 }