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.ServletConfig;
29 import javax.servlet.http.HttpServletRequest;
30
31 import org.apache.commons.configuration.Configuration;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.turbine.Turbine;
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 Log log = LogFactory.getLog(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
100
101 @Deprecated
102 public void init(ServletConfig config)
103 throws InitializationException
104 {
105 init();
106 }
107
108
109
110
111
112
113
114 public void addDefaultObjects(RunData data)
115 {
116 HttpServletRequest req = data.getRequest();
117
118
119
120
121
122
123 ApplicationTool templateLink = new TemplateLink();
124 templateLink.init(data);
125
126 req.setAttribute(LINK, templateLink);
127 req.setAttribute(RUNDATA, data);
128 }
129
130
131
132
133
134
135 public int getDefaultBufferSize()
136 {
137 return bufferSize;
138 }
139
140
141
142
143
144
145
146
147
148 public void handleRequest(RunData data, String templateName)
149 throws TurbineException
150 {
151 handleRequest(data, templateName, false);
152 }
153
154
155
156
157
158
159
160
161
162
163 public void handleRequest(RunData data, String templateName, boolean isForward)
164 throws TurbineException
165 {
166
167 String relativeTemplateName = getRelativeTemplateName(templateName);
168
169 if (StringUtils.isEmpty(relativeTemplateName))
170 {
171 throw new TurbineException(
172 "Template " + templateName + " not found in template paths");
173 }
174
175
176 RequestDispatcher dispatcher = data.getServletContext()
177 .getRequestDispatcher(relativeTemplateName);
178
179 try
180 {
181 if (isForward)
182 {
183
184 dispatcher.forward(data.getRequest(), data.getResponse());
185 }
186 else
187 {
188 data.getResponse().getWriter().flush();
189
190 dispatcher.include(data.getRequest(), data.getResponse());
191 }
192 }
193 catch (Exception e)
194 {
195
196
197 try
198 {
199 data.getResponse().getWriter().print("Error encountered processing a template: "
200 + templateName);
201 e.printStackTrace(data.getResponse().getWriter());
202 }
203 catch (IOException ignored)
204 {
205
206 }
207
208
209
210 throw new TurbineException(
211 "Error encountered processing a template: " + templateName, e);
212 }
213 }
214
215
216
217
218 private void initJsp()
219 throws Exception
220 {
221 Configuration config = getConfiguration();
222
223
224
225 relativeTemplatePaths = config.getStringArray(TEMPLATE_PATH_KEY);
226
227
228 templatePaths = new String [relativeTemplatePaths.length];
229 for (int i=0; i < relativeTemplatePaths.length; i++)
230 {
231 relativeTemplatePaths[i] = warnAbsolute(relativeTemplatePaths[i]);
232
233 templatePaths[i] = Turbine.getRealPath(relativeTemplatePaths[i]);
234 }
235
236 bufferSize = config.getInt(JspService.BUFFER_SIZE_KEY,
237 JspService.BUFFER_SIZE_DEFAULT);
238 }
239
240
241
242
243
244
245
246
247 @Override
248 public boolean templateExists(String template)
249 {
250 for (int i = 0; i < templatePaths.length; i++)
251 {
252 if (templateExists(templatePaths[i], template))
253 {
254 return true;
255 }
256 }
257 return false;
258 }
259
260
261
262
263
264
265
266
267
268
269 private boolean templateExists(String path, String template)
270 {
271 return new File(path, template).exists();
272 }
273
274
275
276
277
278
279
280
281
282
283 public String getRelativeTemplateName(String template)
284 {
285 template = warnAbsolute(template);
286
287
288
289
290 for (int i = 0; i < templatePaths.length; i++)
291 {
292 if (templateExists(templatePaths[i], template))
293 {
294 return relativeTemplatePaths[i] + "/" + template;
295 }
296 }
297 return null;
298 }
299
300
301
302
303
304
305
306 private String warnAbsolute(String template)
307 {
308 if (template.startsWith("/"))
309 {
310 log.warn("Template " + template
311 + " has a leading /, which is wrong!");
312 return template.substring(1);
313 }
314 return template;
315 }
316 }