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