1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.tiles.commands;
22
23 import java.io.IOException;
24
25 import jakarta.servlet.RequestDispatcher;
26 import jakarta.servlet.ServletException;
27 import jakarta.servlet.http.HttpServletResponse;
28
29 import org.apache.commons.chain.Command;
30 import org.apache.struts.chain.contexts.ServletActionContext;
31 import org.apache.struts.config.ForwardConfig;
32 import org.apache.struts.tiles.ComponentContext;
33 import org.apache.struts.tiles.ComponentDefinition;
34 import org.apache.struts.tiles.Controller;
35 import org.apache.struts.tiles.FactoryNotFoundException;
36 import org.apache.struts.tiles.NoSuchDefinitionException;
37 import org.apache.struts.tiles.TilesUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class TilesPreProcessor implements Command<ServletActionContext>
60 {
61
62
63
64
65
66
67
68
69 private final Logger log =
70 LoggerFactory.getLogger(TilesPreProcessor.class);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 @SuppressWarnings("deprecation")
94 public boolean execute(ServletActionContext sacontext) throws Exception {
95
96
97 ForwardConfig forwardConfig = sacontext.getForwardConfig();
98 if (forwardConfig == null || forwardConfig.getPath() == null)
99 {
100 log.debug("No forwardConfig or no path, so pass to next command.");
101 return (false);
102 }
103
104
105 ComponentDefinition definition = null;
106 try
107 {
108 definition = TilesUtil.getDefinition(forwardConfig.getPath(),
109 sacontext.getRequest(),
110 sacontext.getContext());
111 }
112 catch (FactoryNotFoundException ex)
113 {
114
115 log.debug("Tiles DefinitionFactory not found, so pass to next command.");
116 return false;
117 }
118 catch (NoSuchDefinitionException ex)
119 {
120
121 log.debug("NoSuchDefinitionException {}", ex.getMessage());
122 }
123
124
125 boolean doInclude = false;
126 ComponentContext tileContext = null;
127
128
129
130 tileContext = ComponentContext.getContext(sacontext.getRequest());
131 doInclude = (tileContext != null || sacontext.getResponse().isCommitted());
132
133
134 Controller controller = null;
135
136
137 String uri = null;
138
139 if (definition != null)
140 {
141
142
143
144 uri = definition.getPath();
145 controller = definition.getOrCreateController();
146
147 if (tileContext == null) {
148 tileContext =
149 new ComponentContext(definition.getAttributes());
150 ComponentContext.setContext(tileContext, sacontext.getRequest());
151
152 } else {
153 tileContext.addMissing(definition.getAttributes());
154 }
155 }
156
157
158
159
160
161
162 definition = org.apache.struts.tiles.DefinitionsUtil.getActionDefinition(sacontext.getRequest());
163 if (definition != null) {
164
165
166 if (definition.getPath() != null) {
167 log.debug("Override forward uri {} with action uri {}",
168 uri, definition.getPath());
169 uri = definition.getPath();
170 }
171
172 if (definition.getOrCreateController() != null) {
173 log.debug("Override forward controller with action controller");
174 controller = definition.getOrCreateController();
175 }
176
177 if (tileContext == null) {
178 tileContext =
179 new ComponentContext(definition.getAttributes());
180 ComponentContext.setContext(tileContext, sacontext.getRequest());
181 } else {
182 tileContext.addMissing(definition.getAttributes());
183 }
184 }
185
186
187 if (uri == null) {
188 log.debug("no uri computed, so pass to next command");
189 return false;
190 }
191
192
193 if (controller != null) {
194 log.trace("Execute controller: {}", controller);
195 controller.execute(
196 tileContext,
197 sacontext.getRequest(),
198 sacontext.getResponse(),
199 sacontext.getContext());
200 }
201
202
203
204
205 if (doInclude) {
206 log.info("Tiles process complete; doInclude with {}", uri);
207 doInclude(sacontext, uri);
208 } else {
209 log.info("Tiles process complete; forward to {}", uri);
210 doForward(sacontext, uri);
211 }
212
213 log.debug("Tiles processed, so clearing forward config from context.");
214 sacontext.setForwardConfig( null );
215 return (false);
216 }
217
218
219
220
221
222
223
224
225
226
227 protected void doInclude(
228 ServletActionContext context,
229 String uri)
230 throws IOException, ServletException {
231
232 RequestDispatcher rd = getRequiredDispatcher(context, uri);
233
234 if (rd != null) {
235 rd.include(context.getRequest(), context.getResponse());
236 }
237 }
238
239
240
241
242
243
244
245 protected void doForward(
246 ServletActionContext context,
247 String uri)
248 throws IOException, ServletException {
249
250 RequestDispatcher rd = getRequiredDispatcher(context, uri);
251
252 if (rd != null) {
253 rd.forward(context.getRequest(), context.getResponse());
254 }
255 }
256
257
258
259
260
261
262
263
264
265
266
267
268 private RequestDispatcher getRequiredDispatcher(ServletActionContext context, String uri) throws IOException {
269 RequestDispatcher rd = context.getContext().getRequestDispatcher(uri);
270 if (rd == null) {
271 log.debug("No request dispatcher found for {}", uri);
272 HttpServletResponse response = context.getResponse();
273 response.sendError(
274 HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
275 "Error getting RequestDispatcher for " + uri);
276 }
277 return rd;
278 }
279 }