1 package org.apache.turbine.services.jsp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.io.File;
25 import java.io.IOException;
26
27 import javax.servlet.RequestDispatcher;
28 import javax.servlet.http.HttpServletRequest;
29
30 import org.apache.commons.configuration2.Configuration;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.logging.log4j.LogManager;
33 import org.apache.logging.log4j.Logger;
34 import org.apache.turbine.Turbine;
35 import org.apache.turbine.pipeline.PipelineData;
36 import org.apache.turbine.services.InitializationException;
37 import org.apache.turbine.services.pull.ApplicationTool;
38 import org.apache.turbine.services.pull.tools.TemplateLink;
39 import org.apache.turbine.services.template.BaseTemplateEngineService;
40 import org.apache.turbine.util.RunData;
41 import org.apache.turbine.util.TurbineException;
42
43
44
45
46
47
48
49
50
51
52 public class TurbineJspService
53 extends BaseTemplateEngineService
54 implements JspService
55 {
56
57 private String[] templatePaths;
58
59
60 private String[] relativeTemplatePaths;
61
62
63 private int bufferSize;
64
65
66 private static Logger log = LogManager.getLogger(TurbineJspService.class);
67
68
69
70
71
72
73
74
75
76 @Override
77 public void init()
78 throws InitializationException
79 {
80 try
81 {
82 initJsp();
83 registerConfiguration(JspService.JSP_EXTENSION);
84 setInit(true);
85 }
86 catch (Exception e)
87 {
88 throw new InitializationException(
89 "TurbineJspService failed to initialize", e);
90 }
91 }
92
93
94
95
96
97
98
99 @Override
100 public void addDefaultObjects(PipelineData pipelineData)
101 {
102 HttpServletRequest req = pipelineData.get(Turbine.class, HttpServletRequest.class);
103
104
105
106
107
108
109 ApplicationTool templateLink = new TemplateLink();
110 templateLink.init(pipelineData);
111
112 req.setAttribute(LINK, templateLink);
113 req.setAttribute(PIPELINE_DATA, pipelineData);
114 }
115
116
117
118
119
120
121 @Override
122 public int getDefaultBufferSize()
123 {
124 return bufferSize;
125 }
126
127
128
129
130
131
132
133
134
135
136 @Override
137 public void handleRequest(PipelineData pipelineData, String templateName, boolean isForward)
138 throws TurbineException
139 {
140 if(!(pipelineData instanceof RunData))
141 {
142 throw new RuntimeException("Can't cast to rundata from pipeline data.");
143 }
144
145 RunData"../../../../../org/apache/turbine/util/RunData.html#RunData">RunData data = (RunData)pipelineData;
146
147
148 String relativeTemplateName = getRelativeTemplateName(templateName);
149
150 if (StringUtils.isEmpty(relativeTemplateName))
151 {
152 throw new TurbineException(
153 "Template " + templateName + " not found in template paths");
154 }
155
156
157 RequestDispatcher dispatcher = data.getServletContext()
158 .getRequestDispatcher(relativeTemplateName);
159
160 try
161 {
162 if (isForward)
163 {
164
165 dispatcher.forward(data.getRequest(), data.getResponse());
166 }
167 else
168 {
169 data.getResponse().getWriter().flush();
170
171 dispatcher.include(data.getRequest(), data.getResponse());
172 }
173 }
174 catch (Exception e)
175 {
176
177 try
178 {
179 data.getResponse().getWriter().print("Error encountered processing a template: "
180 + templateName);
181 e.printStackTrace(data.getResponse().getWriter());
182 }
183 catch (IOException ignored)
184 {
185
186 }
187
188
189
190 throw new TurbineException(
191 "Error encountered processing a template: " + templateName, e);
192 }
193 }
194
195
196
197
198
199
200
201
202
203 @Override
204 public void handleRequest(PipelineData pipelineData, String templateName)
205 throws TurbineException
206 {
207 handleRequest(pipelineData, templateName, false);
208 }
209
210
211
212
213 private void initJsp()
214 throws Exception
215 {
216 Configuration config = getConfiguration();
217
218
219
220 relativeTemplatePaths = config.getStringArray(TEMPLATE_PATH_KEY);
221
222
223 templatePaths = new String [relativeTemplatePaths.length];
224 for (int i=0; i < relativeTemplatePaths.length; i++)
225 {
226 relativeTemplatePaths[i] = warnAbsolute(relativeTemplatePaths[i]);
227
228 templatePaths[i] = Turbine.getRealPath(relativeTemplatePaths[i]);
229 }
230
231 bufferSize = config.getInt(JspService.BUFFER_SIZE_KEY,
232 JspService.BUFFER_SIZE_DEFAULT);
233 }
234
235
236
237
238
239
240
241
242 @Override
243 public boolean templateExists(String template)
244 {
245 for (int i = 0; i < templatePaths.length; i++)
246 {
247 if (templateExists(templatePaths[i], template))
248 {
249 return true;
250 }
251 }
252 return false;
253 }
254
255
256
257
258
259
260
261
262
263
264 private boolean templateExists(String path, String template)
265 {
266 return new File(path, template).exists();
267 }
268
269
270
271
272
273
274
275
276
277
278 @Override
279 public String getRelativeTemplateName(String template)
280 {
281 String relativeTemplate = warnAbsolute(template);
282
283
284
285
286 for (int i = 0; i < templatePaths.length; i++)
287 {
288 if (templateExists(templatePaths[i], relativeTemplate))
289 {
290 return relativeTemplatePaths[i] + "/" + relativeTemplate;
291 }
292 }
293 return null;
294 }
295
296
297
298
299
300
301
302 private String warnAbsolute(String template)
303 {
304 if (template.startsWith("/"))
305 {
306 log.warn("Template {} has a leading /, which is wrong!", template);
307 return template.substring(1);
308 }
309 return template;
310 }
311 }