1 package org.apache.turbine.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.commons.lang3.exception.ExceptionUtils;
24 import org.apache.turbine.TurbineConstants;
25 import org.apache.turbine.annotation.TurbineConfiguration;
26 import org.apache.turbine.annotation.TurbineService;
27 import org.apache.turbine.pipeline.PipelineData;
28 import org.apache.turbine.services.template.TemplateService;
29 import org.apache.turbine.services.velocity.VelocityService;
30 import org.apache.turbine.util.RunData;
31 import org.apache.velocity.context.Context;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class VelocityScreen
49 extends TemplateScreen
50 {
51
52 protected static final String prefix = PREFIX + "/";
53
54
55 @TurbineService
56 protected VelocityService velocity;
57
58
59 @TurbineService
60 protected TemplateService templateService;
61
62 @TurbineConfiguration( TurbineConstants.TEMPLATE_ERROR_KEY )
63 protected String templateError = TurbineConstants.TEMPLATE_ERROR_VM;
64
65
66
67
68
69
70
71
72
73
74 protected void doBuildTemplate(PipelineData pipelineData,
75 Context context)
76 throws Exception
77 {
78
79 }
80
81
82
83
84
85
86
87
88
89 @Override
90 protected void doBuildTemplate(PipelineData pipelineData)
91 throws Exception
92 {
93 doBuildTemplate(pipelineData, velocity.getContext(pipelineData));
94 }
95
96
97
98
99
100
101
102
103 @Override
104 public String buildTemplate(PipelineData pipelineData)
105 throws Exception
106 {
107 RunData data = pipelineData.getRunData();
108 String screenData = null;
109
110 Context context = velocity.getContext(pipelineData);
111
112 String screenTemplate = data.getTemplateInfo().getScreenTemplate();
113 String templateName
114 = templateService.getScreenTemplateName(screenTemplate);
115
116
117 if (StringUtils.isEmpty(templateName))
118 {
119 log.error("Screen " + screenTemplate + " not found!");
120 throw new Exception("Could not find screen for " + screenTemplate);
121 }
122
123 try
124 {
125
126
127 if (getLayout(pipelineData) == null)
128 {
129 velocity.handleRequest(context,
130 prefix + templateName,
131 data.getResponse().getOutputStream());
132 }
133 else
134 {
135 screenData =
136 velocity.handleRequest(context, prefix + templateName);
137 }
138 }
139 catch (Exception e)
140 {
141
142
143
144 context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
145 context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
146
147 screenData = velocity.handleRequest(context, prefix + templateError);
148 }
149
150 return screenData;
151 }
152 }