1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.servlet.context;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.PrintWriter;
26 import java.io.Writer;
27 import java.util.Locale;
28 import java.util.Map;
29
30 import javax.servlet.RequestDispatcher;
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.apache.tiles.TilesApplicationContext;
37 import org.apache.tiles.context.TilesApplicationContextWrapper;
38 import org.apache.tiles.context.TilesRequestContext;
39
40
41
42
43
44
45 public class ServletTilesRequestContext extends TilesApplicationContextWrapper
46 implements TilesRequestContext {
47
48
49
50
51 private HttpServletRequest request;
52
53
54
55
56 private HttpServletResponse response;
57
58
59
60
61 private Object[] requestObjects;
62
63
64
65
66 private OutputStream outputStream;
67
68
69
70
71 private PrintWriter writer;
72
73
74
75
76
77 private Map<String, String> header = null;
78
79
80
81
82
83
84 private Map<String, String[]> headerValues = null;
85
86
87
88
89
90
91 private Map<String, String> param = null;
92
93
94
95
96
97
98 private Map<String, String[]> paramValues = null;
99
100
101
102
103
104 private Map<String, Object> requestScope = null;
105
106
107
108
109
110 private Map<String, Object> sessionScope = null;
111
112
113
114
115
116
117
118
119
120
121 public ServletTilesRequestContext(
122 TilesApplicationContext applicationContext,
123 HttpServletRequest request, HttpServletResponse response) {
124 super(applicationContext);
125 initialize(request, response);
126 }
127
128
129
130
131
132
133
134
135
136
137
138 @Deprecated
139 public ServletTilesRequestContext(ServletContext servletContext,
140 HttpServletRequest request,
141 HttpServletResponse response) {
142 super(new ServletTilesApplicationContext(servletContext));
143 initialize(request, response);
144 }
145
146
147
148 public Map<String, String> getHeader() {
149
150 if ((header == null) && (request != null)) {
151 header = new ServletHeaderMap(request, response);
152 }
153 return (header);
154
155 }
156
157
158
159 public Map<String, String[]> getHeaderValues() {
160
161 if ((headerValues == null) && (request != null)) {
162 headerValues = new ServletHeaderValuesMap(request);
163 }
164 return (headerValues);
165
166 }
167
168
169
170 public Map<String, String> getParam() {
171
172 if ((param == null) && (request != null)) {
173 param = new ServletParamMap(request);
174 }
175 return (param);
176
177 }
178
179
180
181 public Map<String, String[]> getParamValues() {
182
183 if ((paramValues == null) && (request != null)) {
184 paramValues = new ServletParamValuesMap(request);
185 }
186 return (paramValues);
187
188 }
189
190
191
192 public Map<String, Object> getRequestScope() {
193
194 if ((requestScope == null) && (request != null)) {
195 requestScope = new ServletRequestScopeMap(request);
196 }
197 return (requestScope);
198
199 }
200
201
202
203 public Map<String, Object> getSessionScope() {
204
205 if ((sessionScope == null) && (request != null)) {
206 sessionScope = new ServletSessionScopeMap(request);
207 }
208 return (sessionScope);
209
210 }
211
212
213 public TilesApplicationContext getApplicationContext() {
214 return getWrappedApplicationContext();
215 }
216
217
218 public void dispatch(String path) throws IOException {
219 if (response.isCommitted() || ServletUtil.isForceInclude(request)) {
220 include(path);
221 } else {
222 forward(path);
223 }
224 }
225
226
227
228
229
230
231
232 protected void forward(String path) throws IOException {
233 RequestDispatcher rd = request.getRequestDispatcher(path);
234
235 if (rd == null) {
236 throw new IOException("No request dispatcher returned for path '"
237 + path + "'");
238 }
239
240 try {
241 rd.forward(request, response);
242 } catch (ServletException ex) {
243 throw ServletUtil.wrapServletException(ex, "ServletException including path '"
244 + path + "'.");
245 }
246 }
247
248
249
250 public void include(String path) throws IOException {
251 ServletUtil.setForceInclude(request, true);
252 RequestDispatcher rd = request.getRequestDispatcher(path);
253
254 if (rd == null) {
255 throw new IOException("No request dispatcher returned for path '"
256 + path + "'");
257 }
258
259 try {
260 rd.include(request, response);
261 } catch (ServletException ex) {
262 throw ServletUtil.wrapServletException(ex, "ServletException including path '"
263 + path + "'.");
264 }
265 }
266
267
268 public OutputStream getOutputStream() throws IOException {
269 if (outputStream == null) {
270 outputStream = response.getOutputStream();
271 }
272 return outputStream;
273 }
274
275
276 public Writer getWriter() throws IOException {
277 return getPrintWriter();
278 }
279
280
281 public PrintWriter getPrintWriter() throws IOException {
282 if (writer == null) {
283 writer = response.getWriter();
284 }
285 return writer;
286 }
287
288
289 public boolean isResponseCommitted() {
290 return response.isCommitted();
291 }
292
293
294 public void setContentType(String contentType) {
295 response.setContentType(contentType);
296 }
297
298
299 public Locale getRequestLocale() {
300 return request.getLocale();
301 }
302
303
304 public Object[] getRequestObjects() {
305 if (requestObjects == null) {
306 requestObjects = new Object[2];
307 requestObjects[0] = request;
308 requestObjects[1] = response;
309 }
310 return requestObjects;
311 }
312
313
314 public HttpServletRequest getRequest() {
315 return request;
316 }
317
318
319 public HttpServletResponse getResponse() {
320 return response;
321 }
322
323
324
325
326
327
328
329
330 public void initialize(HttpServletRequest request,
331 HttpServletResponse response) {
332
333
334 this.request = request;
335 this.response = response;
336
337 }
338
339
340
341
342
343
344
345
346 public void release() {
347
348 header = null;
349 headerValues = null;
350 param = null;
351 paramValues = null;
352 requestScope = null;
353 sessionScope = null;
354
355
356 request = null;
357 response = null;
358 }
359
360
361
362 public boolean isUserInRole(String role) {
363 return request.isUserInRole(role);
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377
378 @Deprecated
379 protected IOException wrapServletException(ServletException ex,
380 String message) {
381 return ServletUtil.wrapServletException(ex, message);
382 }
383 }