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