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