1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.tiles;
23
24 import java.io.IOException;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.action.ActionServlet;
33 import org.apache.struts.action.RequestProcessor;
34 import org.apache.struts.config.ForwardConfig;
35 import org.apache.struts.config.ModuleConfig;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class TilesRequestProcessor extends RequestProcessor {
56
57
58
59
60 protected DefinitionsFactory definitionsFactory = null;
61
62
63
64
65 protected static Log log = LogFactory.getLog(TilesRequestProcessor.class);
66
67
68
69
70
71
72
73
74 public void init(ActionServlet servlet, ModuleConfig moduleConfig)
75 throws ServletException {
76
77 super.init(servlet, moduleConfig);
78 this.initDefinitionsMapping();
79 }
80
81
82
83
84
85 protected void initDefinitionsMapping() throws ServletException {
86
87 definitionsFactory =
88 (
89 (TilesUtilStrutsImpl) TilesUtil
90 .getTilesUtil())
91 .getDefinitionsFactory(
92 getServletContext(),
93 moduleConfig);
94
95 if (definitionsFactory == null) {
96
97 log.info(
98 "Definition Factory not found for module '"
99 + moduleConfig.getPrefix()
100 + "'. "
101 + "Have you declared the appropriate plugin in struts-config.xml ?");
102
103 return;
104 }
105
106 log.info(
107 "Tiles definition factory found for request processor '"
108 + moduleConfig.getPrefix()
109 + "'.");
110
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 protected boolean processTilesDefinition(
132 String definitionName,
133 boolean contextRelative,
134 HttpServletRequest request,
135 HttpServletResponse response)
136 throws IOException, ServletException {
137
138 return processTilesDefinition(definitionName, request, response);
139
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 protected boolean processTilesDefinition(
156 String definitionName,
157 HttpServletRequest request,
158 HttpServletResponse response)
159 throws IOException, ServletException {
160
161
162 boolean doInclude = false;
163
164
165 Controller controller = null;
166
167
168 String uri = null;
169
170 ComponentContext tileContext = null;
171
172 try {
173
174
175 tileContext = ComponentContext.getContext(request);
176 doInclude = (tileContext != null);
177 ComponentDefinition definition = null;
178
179
180
181 if (definitionsFactory != null) {
182
183 try {
184 definition =
185 definitionsFactory.getDefinition(
186 definitionName,
187 request,
188 getServletContext());
189 } catch (NoSuchDefinitionException ex) {
190
191 log.debug("NoSuchDefinitionException " + ex.getMessage());
192 }
193 if (definition != null) {
194
195
196 uri = definition.getPath();
197 controller = definition.getOrCreateController();
198
199 if (tileContext == null) {
200 tileContext =
201 new ComponentContext(definition.getAttributes());
202 ComponentContext.setContext(tileContext, request);
203
204 } else {
205 tileContext.addMissing(definition.getAttributes());
206 }
207 }
208 }
209
210
211 definition = DefinitionsUtil.getActionDefinition(request);
212 if (definition != null) {
213
214
215 if (definition.getPath() != null) {
216 uri = definition.getPath();
217 }
218
219 if (definition.getOrCreateController() != null) {
220 controller = definition.getOrCreateController();
221 }
222
223 if (tileContext == null) {
224 tileContext =
225 new ComponentContext(definition.getAttributes());
226 ComponentContext.setContext(tileContext, request);
227 } else {
228 tileContext.addMissing(definition.getAttributes());
229 }
230 }
231
232 } catch (java.lang.InstantiationException ex) {
233
234 log.error("Can't create associated controller", ex);
235
236 throw new ServletException(
237 "Can't create associated controller",
238 ex);
239 } catch (DefinitionsFactoryException ex) {
240 throw new ServletException(ex);
241 }
242
243
244 if (uri == null) {
245 return false;
246 }
247
248
249 if (controller != null) {
250 try {
251 controller.execute(
252 tileContext,
253 request,
254 response,
255 getServletContext());
256
257 } catch (Exception e) {
258 throw new ServletException(e);
259 }
260 }
261
262
263
264 if (log.isDebugEnabled()) {
265 log.debug("uri=" + uri + " doInclude=" + doInclude);
266 }
267
268 if (doInclude) {
269 doInclude(uri, request, response);
270 } else {
271 doForward(uri, request, response);
272 }
273
274 return true;
275 }
276
277
278
279
280
281
282
283
284
285 protected void doForward(
286 String uri,
287 HttpServletRequest request,
288 HttpServletResponse response)
289 throws IOException, ServletException {
290
291 if (response.isCommitted()) {
292 this.doInclude(uri, request, response);
293
294 } else {
295 super.doForward(uri, request, response);
296 }
297 }
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313 protected void processForwardConfig(
314 HttpServletRequest request,
315 HttpServletResponse response,
316 ForwardConfig forward)
317 throws IOException, ServletException {
318
319
320 if (forward == null) {
321 return;
322 }
323
324 if (log.isDebugEnabled()) {
325 log.debug(
326 "processForwardConfig("
327 + forward.getPath()
328 + ")");
329 }
330
331
332 if (processTilesDefinition(forward.getPath(),
333 request,
334 response)) {
335 if (log.isDebugEnabled()) {
336 log.debug(
337 " '" + forward.getPath() + "' - processed as definition");
338 }
339 return;
340 }
341
342 if (log.isDebugEnabled()) {
343 log.debug(" '" + forward.getPath() + "' - processed as uri");
344 }
345
346
347 super.processForwardConfig(request, response, forward);
348 }
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 protected void internalModuleRelativeForward(
365 String uri,
366 HttpServletRequest request,
367 HttpServletResponse response)
368 throws IOException, ServletException {
369
370 if (processTilesDefinition(uri, request, response)) {
371 return;
372 }
373
374 super.internalModuleRelativeForward(uri, request, response);
375 }
376
377
378
379
380
381
382
383
384
385
386
387
388 protected void internalModuleRelativeInclude(
389 String uri,
390 HttpServletRequest request,
391 HttpServletResponse response)
392 throws IOException, ServletException {
393
394 if (processTilesDefinition(uri, request, response)) {
395 return;
396 }
397
398 super.internalModuleRelativeInclude(uri, request, response);
399 }
400
401
402
403
404 public DefinitionsFactory getDefinitionsFactory() {
405 return definitionsFactory;
406 }
407
408 }