1 package org.apache.velocity.tools.struts;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Stack;
25 import javax.servlet.http.HttpSession;
26 import org.apache.struts.tiles.AttributeDefinition;
27 import org.apache.struts.tiles.ComponentContext;
28 import org.apache.struts.tiles.ComponentDefinition;
29 import org.apache.struts.tiles.Controller;
30 import org.apache.struts.tiles.DefinitionAttribute;
31 import org.apache.struts.tiles.DefinitionNameAttribute;
32 import org.apache.struts.tiles.DefinitionsFactoryException;
33 import org.apache.struts.tiles.DirectStringAttribute;
34 import org.apache.struts.tiles.TilesUtil;
35 import org.apache.velocity.context.Context;
36 import org.apache.velocity.tools.Scope;
37 import org.apache.velocity.tools.config.DefaultKey;
38 import org.apache.velocity.tools.config.ValidScope;
39 import org.apache.velocity.tools.view.ImportSupport;
40 import org.apache.velocity.tools.view.ViewContext;
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 @DefaultKey("tiles")
71 @ValidScope(Scope.REQUEST)
72 public class TilesTool extends ImportSupport
73 {
74 static final String PAGE_SCOPE = "page";
75 static final String REQUEST_SCOPE = "request";
76 static final String SESSION_SCOPE = "session";
77 static final String APPLICATION_SCOPE = "application";
78
79 protected Context velocityContext;
80
81
82
83
84
85 protected Stack contextStack;
86
87
88
89
90 protected boolean catchExceptions = true;
91
92
93
94 @Deprecated
95 public void init(Object obj)
96 {
97 if (obj instanceof ViewContext)
98 {
99 ViewContext ctx = (ViewContext)obj;
100 setVelocityContext(ctx.getVelocityContext());
101 setRequest(ctx.getRequest());
102 setResponse(ctx.getResponse());
103 setServletContext(ctx.getServletContext());
104 setLog(ctx.getVelocityEngine().getLog());
105 }
106 }
107
108
109
110
111
112
113
114 public void setVelocityContext(Context context)
115 {
116 if (context == null)
117 {
118 throw new NullPointerException("velocity context should not be null");
119 }
120 this.velocityContext = context;
121 }
122
123 public void setCatchExceptions(boolean catchExceptions)
124 {
125 this.catchExceptions = catchExceptions;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public String get(Object obj) throws Exception {
146 try
147 {
148 Object value = getCurrentContext().getAttribute(obj.toString());
149 if (value != null)
150 {
151 return processObjectValue(value);
152 }
153 return processAsDefinitionOrURL(obj.toString());
154 }
155 catch (Exception e)
156 {
157 LOG.error("TilesTool : Exeption while rendering Tile " + obj, e);
158
159
160 if (!this.catchExceptions) {
161 throw e;
162 }
163
164 return null;
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177 public Object getAttribute(String name)
178 {
179 Object value = getCurrentContext().getAttribute(name);
180 if (value == null)
181 {
182 LOG.warn("TilesTool : Tile attribute '" + name + "' wasn't found in context.");
183 }
184 return value;
185 }
186
187
188
189
190
191
192
193
194
195
196 public void importAttribute(String name)
197 {
198 this.importAttribute(name, PAGE_SCOPE);
199 }
200
201
202
203
204
205
206
207
208
209
210
211 public void importAttribute(String name, String scope)
212 {
213 Object value = getCurrentContext().getAttribute(name);
214 if (value == null)
215 {
216 LOG.warn("TilesTool : Tile attribute '" + name + "' wasn't found in context.");
217 }
218
219 if (scope.equals(PAGE_SCOPE))
220 {
221 velocityContext.put(name, value);
222 }
223 else if (scope.equals(REQUEST_SCOPE))
224 {
225 request.setAttribute(name, value);
226 }
227 else if (scope.equals(SESSION_SCOPE))
228 {
229 request.getSession().setAttribute(name, value);
230 }
231 else if (scope.equals(APPLICATION_SCOPE))
232 {
233 application.setAttribute(name, value);
234 }
235 }
236
237
238
239
240
241
242
243
244 public void importAttributes()
245 {
246 this.importAttributes(PAGE_SCOPE);
247 }
248
249
250
251
252
253
254
255
256
257
258 public void importAttributes(String scope)
259 {
260 ComponentContext context = getCurrentContext();
261 Iterator names = context.getAttributeNames();
262
263 if (scope.equals(PAGE_SCOPE))
264 {
265 while (names.hasNext())
266 {
267 String name = (String)names.next();
268 velocityContext.put(name, context.getAttribute(name));
269 }
270 }
271 else if (scope.equals(REQUEST_SCOPE))
272 {
273 while (names.hasNext())
274 {
275 String name = (String)names.next();
276 request.setAttribute(name, context.getAttribute(name));
277 }
278 }
279 else if (scope.equals(SESSION_SCOPE))
280 {
281 HttpSession session = request.getSession();
282 while (names.hasNext())
283 {
284 String name = (String)names.next();
285 session.setAttribute(name, context.getAttribute(name));
286 }
287 }
288 else if (scope.equals(APPLICATION_SCOPE))
289 {
290 while (names.hasNext())
291 {
292 String name = (String)names.next();
293 application.setAttribute(name, context.getAttribute(name));
294 }
295 }
296 }
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 protected String processObjectValue(Object value) throws Exception
312 {
313
314 if (value instanceof AttributeDefinition)
315 {
316
317 return processTypedAttribute((AttributeDefinition)value);
318 }
319 else if (value instanceof ComponentDefinition)
320 {
321 return processDefinition((ComponentDefinition)value);
322 }
323
324 return processAsDefinitionOrURL(value.toString());
325 }
326
327
328
329
330
331
332
333
334 protected String processTypedAttribute(AttributeDefinition value)
335 throws Exception
336 {
337 if (value instanceof DirectStringAttribute)
338 {
339 return (String)value.getValue();
340 }
341 else if (value instanceof DefinitionAttribute)
342 {
343 return processDefinition((ComponentDefinition)value.getValue());
344 }
345 else if (value instanceof DefinitionNameAttribute)
346 {
347 return processAsDefinitionOrURL((String)value.getValue());
348 }
349
350 return doInsert((String)value.getValue(), null, null);
351 }
352
353
354
355
356
357
358
359
360 protected String processAsDefinitionOrURL(String name) throws Exception
361 {
362 try
363 {
364 ComponentDefinition definition =
365 TilesUtil.getDefinition(name, this.request, this.application);
366 if (definition != null)
367 {
368 return processDefinition(definition);
369 }
370 }
371 catch (DefinitionsFactoryException ex)
372 {
373
374 }
375
376 return processUrl(name);
377 }
378
379
380
381
382
383
384
385
386 protected String processDefinition(ComponentDefinition definition) throws
387 Exception
388 {
389 Controller controller = null;
390 try
391 {
392 controller = definition.getOrCreateController();
393
394 String role = definition.getRole();
395 String page = definition.getTemplate();
396
397 return doInsert(definition.getAttributes(),
398 page,
399 role,
400 controller);
401 }
402 catch (InstantiationException ex)
403 {
404 throw new Exception(ex.getMessage());
405 }
406 }
407
408
409
410
411
412
413
414
415 protected String processUrl(String url) throws Exception
416 {
417 return doInsert(url, null, null);
418 }
419
420
421
422
423
424
425
426
427
428
429 protected String doInsert(String page, String role, Controller controller) throws
430 Exception
431 {
432 if (role != null && !this.request.isUserInRole(role))
433 {
434 return null;
435 }
436
437 ComponentContext subCompContext = new ComponentContext();
438 return doInsert(subCompContext, page, role, controller);
439 }
440
441
442
443
444
445
446
447
448
449
450
451 protected String doInsert(Map attributes,
452 String page,
453 String role,
454 Controller controller) throws Exception
455 {
456 if (role != null && !this.request.isUserInRole(role))
457 {
458 return null;
459 }
460
461 ComponentContext subCompContext = new ComponentContext(attributes);
462 return doInsert(subCompContext, page, role, controller);
463 }
464
465
466
467
468
469
470
471
472
473
474
475
476 protected String doInsert(ComponentContext subCompContext,
477 String page,
478 String role,
479 Controller controller) throws Exception
480 {
481 pushTilesContext();
482 try
483 {
484 ComponentContext.setContext(subCompContext, this.request);
485
486
487 if (controller != null)
488 {
489 controller.execute(subCompContext,
490 this.request,
491 this.response,
492 this.application);
493 }
494
495 return this.acquireString(page);
496 }
497 finally
498 {
499 popTilesContext();
500 }
501 }
502
503
504
505
506
507 protected ComponentContext getCurrentContext()
508 {
509 return ComponentContext.getContext(this.request);
510 }
511
512
513
514
515
516
517 protected void pushTilesContext()
518 {
519 if (this.contextStack == null)
520 {
521 this.contextStack = new Stack();
522 }
523 contextStack.push(getCurrentContext());
524 }
525
526
527
528
529
530 protected void popTilesContext()
531 {
532 ComponentContext context = (ComponentContext)this.contextStack.pop();
533 ComponentContext.setContext(context, this.request);
534 }
535
536 }