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