This project has retired. For details please refer to its
Attic page.
ServletRequest 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.request.servlet;
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.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Map;
32
33 import javax.servlet.RequestDispatcher;
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38 import org.apache.tiles.request.AbstractClientRequest;
39 import org.apache.tiles.request.ApplicationContext;
40 import org.apache.tiles.request.attribute.Addable;
41 import org.apache.tiles.request.collection.HeaderValuesMap;
42 import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
43 import org.apache.tiles.request.collection.ScopeMap;
44 import org.apache.tiles.request.servlet.extractor.ParameterExtractor;
45 import org.apache.tiles.request.servlet.extractor.RequestScopeExtractor;
46 import org.apache.tiles.request.servlet.extractor.HeaderExtractor;
47 import org.apache.tiles.request.servlet.extractor.SessionScopeExtractor;
48
49
50
51
52
53
54 public class ServletRequest extends AbstractClientRequest {
55
56
57
58
59 private static final List<String> SCOPES
60 = Collections.unmodifiableList(Arrays.asList(REQUEST_SCOPE, "session", APPLICATION_SCOPE));
61
62
63
64
65 private HttpServletRequest request;
66
67
68
69
70 private HttpServletResponse response;
71
72
73
74
75 private OutputStream outputStream;
76
77
78
79
80 private PrintWriter writer;
81
82
83
84
85
86 private Map<String, String> header = null;
87
88
89
90
91
92 private Addable<String> responseHeaders = null;
93
94
95
96
97
98
99 private Map<String, String[]> headerValues = null;
100
101
102
103
104
105
106 private Map<String, String> param = null;
107
108
109
110
111
112
113 private Map<String, Object> requestScope = null;
114
115
116
117
118
119 private Map<String, Object> sessionScope = null;
120
121
122
123
124
125
126
127
128
129 public ServletRequest(
130 ApplicationContext applicationContext,
131 HttpServletRequest request, HttpServletResponse response) {
132 super(applicationContext);
133 this.request = request;
134 this.response = response;
135 }
136
137
138 public Map<String, String> getHeader() {
139
140 if ((header == null) && (request != null)) {
141 header = new ReadOnlyEnumerationMap<String>(new HeaderExtractor(request, null));
142 }
143 return (header);
144
145 }
146
147
148 public Addable<String> getResponseHeaders() {
149
150 if ((responseHeaders == null) && (response != null)) {
151 responseHeaders = new HeaderExtractor(null, response);
152 }
153 return (responseHeaders);
154
155 }
156
157
158 public Map<String, String[]> getHeaderValues() {
159
160 if ((headerValues == null) && (request != null)) {
161 headerValues = new HeaderValuesMap(new HeaderExtractor(request, response));
162 }
163 return (headerValues);
164
165 }
166
167
168
169 public Map<String, String> getParam() {
170
171 if ((param == null) && (request != null)) {
172 param = new ReadOnlyEnumerationMap<String>(new ParameterExtractor(request));
173 }
174 return (param);
175
176 }
177
178
179
180 @SuppressWarnings("unchecked")
181 public Map<String, String[]> getParamValues() {
182 return request.getParameterMap();
183 }
184
185 @Override
186 public Map<String, Object> getContext(String scope) {
187 if(REQUEST_SCOPE.equals(scope)){
188 return getRequestScope();
189 }else if("session".equals(scope)){
190 return getSessionScope();
191 }else if(APPLICATION_SCOPE.equals(scope)){
192 return getApplicationScope();
193 }
194 throw new IllegalArgumentException(scope + " does not exist. Call getAvailableScopes() first to check.");
195 }
196
197
198 public Map<String, Object> getRequestScope() {
199
200 if ((requestScope == null) && (request != null)) {
201 requestScope = new ScopeMap(new RequestScopeExtractor(request));
202 }
203 return (requestScope);
204
205 }
206
207
208
209 public Map<String, Object> getSessionScope() {
210
211 if ((sessionScope == null) && (request != null)) {
212 sessionScope = new ScopeMap(new SessionScopeExtractor(request));
213 }
214 return (sessionScope);
215
216 }
217
218 @Override
219 public List<String> getAvailableScopes() {
220 return SCOPES;
221 }
222
223
224 public void doForward(String path) throws IOException {
225 if (response.isCommitted()) {
226 doInclude(path);
227 } else {
228 forward(path);
229 }
230 }
231
232
233
234 public void doInclude(String path) throws IOException {
235 RequestDispatcher rd = request.getRequestDispatcher(path);
236
237 if (rd == null) {
238 throw new IOException("No request dispatcher returned for path '"
239 + path + "'");
240 }
241
242 try {
243 rd.include(request, response);
244 } catch (ServletException ex) {
245 throw ServletUtil.wrapServletException(ex, "ServletException including path '"
246 + path + "'.");
247 }
248 }
249
250
251
252
253
254
255
256 private void forward(String path) throws IOException {
257 RequestDispatcher rd = request.getRequestDispatcher(path);
258
259 if (rd == null) {
260 throw new IOException("No request dispatcher returned for path '"
261 + path + "'");
262 }
263
264 try {
265 rd.forward(request, response);
266 } catch (ServletException ex) {
267 throw ServletUtil.wrapServletException(ex, "ServletException including path '"
268 + path + "'.");
269 }
270 }
271
272
273 public OutputStream getOutputStream() throws IOException {
274 if (outputStream == null) {
275 outputStream = response.getOutputStream();
276 }
277 return outputStream;
278 }
279
280
281 public Writer getWriter() throws IOException {
282 return getPrintWriter();
283 }
284
285
286 public PrintWriter getPrintWriter() throws IOException {
287 if (writer == null) {
288 writer = response.getWriter();
289 }
290 return writer;
291 }
292
293
294 public boolean isResponseCommitted() {
295 return response.isCommitted();
296 }
297
298
299 public void setContentType(String contentType) {
300 response.setContentType(contentType);
301 }
302
303
304 public Locale getRequestLocale() {
305 return request.getLocale();
306 }
307
308 public HttpServletRequest getRequest() {
309 return request;
310 }
311
312 public HttpServletResponse getResponse() {
313 return response;
314 }
315
316
317 public boolean isUserInRole(String role) {
318 return request.isUserInRole(role);
319 }
320 }