This project has retired. For details please refer to its
Attic page.
ServletTilesRequestContext xref
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 * Servlet-based implementation of the TilesApplicationContext interface.
42 *
43 * @version $Rev: 736275 $ $Date: 2009-01-21 10:58:20 +0100 (mer, 21 gen 2009) $
44 */
45 public class ServletTilesRequestContext extends TilesApplicationContextWrapper
46 implements TilesRequestContext {
47
48 /***
49 * The request object to use.
50 */
51 private HttpServletRequest request;
52
53 /***
54 * The response object to use.
55 */
56 private HttpServletResponse response;
57
58 /***
59 * The request objects, lazily initialized.
60 */
61 private Object[] requestObjects;
62
63 /***
64 * The response output stream, lazily initialized.
65 */
66 private OutputStream outputStream;
67
68 /***
69 * The response writer, lazily initialized.
70 */
71 private PrintWriter writer;
72
73 /***
74 * <p>The lazily instantiated <code>Map</code> of header name-value
75 * combinations (immutable).</p>
76 */
77 private Map<String, String> header = null;
78
79
80 /***
81 * <p>The lazily instantitated <code>Map</code> of header name-values
82 * combinations (immutable).</p>
83 */
84 private Map<String, String[]> headerValues = null;
85
86
87 /***
88 * <p>The lazily instantiated <code>Map</code> of request
89 * parameter name-value.</p>
90 */
91 private Map<String, String> param = null;
92
93
94 /***
95 * <p>The lazily instantiated <code>Map</code> of request
96 * parameter name-values.</p>
97 */
98 private Map<String, String[]> paramValues = null;
99
100 /***
101 * <p>The lazily instantiated <code>Map</code> of request scope
102 * attributes.</p>
103 */
104 private Map<String, Object> requestScope = null;
105
106 /***
107 * <p>The lazily instantiated <code>Map</code> of session scope
108 * attributes.</p>
109 */
110 private Map<String, Object> sessionScope = null;
111
112
113 /***
114 * Creates a new instance of ServletTilesRequestContext.
115 *
116 * @param applicationContext The application context.
117 * @param request The request object.
118 * @param response The response object.
119 * @since 2.1.1
120 */
121 public ServletTilesRequestContext(
122 TilesApplicationContext applicationContext,
123 HttpServletRequest request, HttpServletResponse response) {
124 super(applicationContext);
125 initialize(request, response);
126 }
127
128 /***
129 * Creates a new instance of ServletTilesRequestContext.
130 *
131 * @param servletContext The servlet context.
132 * @param request The request object.
133 * @param response The response object.
134 * @deprecated Use
135 * {@link #ServletTilesRequestContext(TilesApplicationContext, HttpServletRequest, HttpServletResponse)}
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 /*** {@inheritDoc} */
148 public Map<String, String> getHeader() {
149
150 if ((header == null) && (request != null)) {
151 header = new ServletHeaderMap(request);
152 }
153 return (header);
154
155 }
156
157
158 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
213 public TilesApplicationContext getApplicationContext() {
214 return getWrappedApplicationContext();
215 }
216
217 /*** {@inheritDoc} */
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 * Forwards to a path.
228 *
229 * @param path The path to forward to.
230 * @throws IOException If something goes wrong during the operation.
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
268 public OutputStream getOutputStream() throws IOException {
269 if (outputStream == null) {
270 outputStream = response.getOutputStream();
271 }
272 return outputStream;
273 }
274
275 /*** {@inheritDoc} */
276 public Writer getWriter() throws IOException {
277 return getPrintWriter();
278 }
279
280 /*** {@inheritDoc} */
281 public PrintWriter getPrintWriter() throws IOException {
282 if (writer == null) {
283 writer = response.getWriter();
284 }
285 return writer;
286 }
287
288 /*** {@inheritDoc} */
289 public Locale getRequestLocale() {
290 return request.getLocale();
291 }
292
293 /*** {@inheritDoc} */
294 public Object[] getRequestObjects() {
295 if (requestObjects == null) {
296 requestObjects = new Object[2];
297 requestObjects[0] = request;
298 requestObjects[1] = response;
299 }
300 return requestObjects;
301 }
302
303 /*** {@inheritDoc} */
304 public HttpServletRequest getRequest() {
305 return request;
306 }
307
308 /*** {@inheritDoc} */
309 public HttpServletResponse getResponse() {
310 return response;
311 }
312
313 /***
314 * <p>Initialize (or reinitialize) this {@link ServletTilesRequestContext} instance
315 * for the specified Servlet API objects.</p>
316 *
317 * @param request The <code>HttpServletRequest</code> for this request
318 * @param response The <code>HttpServletResponse</code> for this request
319 */
320 public void initialize(HttpServletRequest request,
321 HttpServletResponse response) {
322
323
324 this.request = request;
325 this.response = response;
326
327 }
328
329
330 /***
331 * <p>Release references to allocated resources acquired in
332 * <code>initialize()</code> of via subsequent processing. After this
333 * method is called, subsequent calls to any other method than
334 * <code>initialize()</code> will return undefined results.</p>
335 */
336 public void release() {
337
338 header = null;
339 headerValues = null;
340 param = null;
341 paramValues = null;
342 requestScope = null;
343 sessionScope = null;
344
345
346 request = null;
347 response = null;
348 }
349
350
351 /*** {@inheritDoc} */
352 public boolean isUserInRole(String role) {
353 return request.isUserInRole(role);
354 }
355
356 /***
357 * Wraps a ServletException to create an IOException with the root cause if
358 * present.
359 *
360 * @param ex The exception to wrap.
361 * @param message The message of the exception.
362 * @return The wrapped exception.
363 * @since 2.0.6
364 * @deprecated Use
365 * {@link ServletUtil#wrapServletException(ServletException,String)}
366 * instead.
367 */
368 @Deprecated
369 protected IOException wrapServletException(ServletException ex,
370 String message) {
371 return ServletUtil.wrapServletException(ex, message);
372 }
373 }