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 jakarta.servlet.ServletException;
27 import jakarta.servlet.http.HttpServletRequest;
28 import jakarta.servlet.http.HttpServletResponse;
29
30 import org.apache.struts.action.ActionServlet;
31 import org.apache.struts.action.RequestProcessor;
32 import org.apache.struts.config.ForwardConfig;
33 import org.apache.struts.config.ModuleConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
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 private static final long serialVersionUID = -6522610348048179731L;
57
58
59
60
61 protected DefinitionsFactory definitionsFactory = null;
62
63
64
65
66 private transient final Logger log =
67 LoggerFactory.getLogger(TilesRequestProcessor.class);
68
69
70
71
72
73
74
75
76 public void init(ActionServlet servlet, ModuleConfig moduleConfig)
77 throws ServletException {
78
79 super.init(servlet, moduleConfig);
80 this.initDefinitionsMapping();
81 }
82
83
84
85
86
87 protected void initDefinitionsMapping() throws ServletException {
88
89 definitionsFactory =
90 (
91 (TilesUtilStrutsImpl) TilesUtil
92 .getTilesUtil())
93 .getDefinitionsFactory(
94 getServletContext(),
95 moduleConfig);
96
97 if (definitionsFactory == null) {
98
99 log.info("Definition Factory not found for module '{}'. "
100 + "Have you declared the appropriate plugin in struts-config.xml ?",
101 moduleConfig.getPrefix());
102
103 return;
104 }
105
106 log.info("Tiles definition factory found for request processor '{}'.",
107 moduleConfig.getPrefix());
108
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 @Deprecated
130 protected boolean processTilesDefinition(
131 String definitionName,
132 boolean contextRelative,
133 HttpServletRequest request,
134 HttpServletResponse response)
135 throws IOException, ServletException {
136
137 return processTilesDefinition(definitionName, request, response);
138
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @SuppressWarnings("deprecation")
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 log.debug("uri={} doInclude={}" , uri, doInclude);
265
266 if (doInclude) {
267 doInclude(uri, request, response);
268 } else {
269 doForward(uri, request, response);
270 }
271
272 return true;
273 }
274
275
276
277
278
279
280
281
282
283 protected void doForward(
284 String uri,
285 HttpServletRequest request,
286 HttpServletResponse response)
287 throws IOException, ServletException {
288
289 if (response.isCommitted()) {
290 this.doInclude(uri, request, response);
291
292 } else {
293 super.doForward(uri, request, response);
294 }
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 protected void processForwardConfig(
312 HttpServletRequest request,
313 HttpServletResponse response,
314 ForwardConfig forward)
315 throws IOException, ServletException {
316
317
318 if (forward == null) {
319 return;
320 }
321
322 log.debug("processForwardConfig({})", forward.getPath());
323
324
325 if (processTilesDefinition(forward.getPath(),
326 request, response)) {
327 log.debug(" '{}' - processed as definition", forward.getPath());
328 return;
329 }
330
331 log.debug(" '{}' - processed as uri", forward.getPath());
332
333
334 super.processForwardConfig(request, response, forward);
335 }
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351 protected void internalModuleRelativeForward(
352 String uri,
353 HttpServletRequest request,
354 HttpServletResponse response)
355 throws IOException, ServletException {
356
357 if (processTilesDefinition(uri, request, response)) {
358 return;
359 }
360
361 super.internalModuleRelativeForward(uri, request, response);
362 }
363
364
365
366
367
368
369
370
371
372
373
374
375 protected void internalModuleRelativeInclude(
376 String uri,
377 HttpServletRequest request,
378 HttpServletResponse response)
379 throws IOException, ServletException {
380
381 if (processTilesDefinition(uri, request, response)) {
382 return;
383 }
384
385 super.internalModuleRelativeInclude(uri, request, response);
386 }
387
388
389
390
391 public DefinitionsFactory getDefinitionsFactory() {
392 return definitionsFactory;
393 }
394 }