1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.portals.bridges.jsf;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.security.Principal;
23 import java.util.Enumeration;
24 import java.util.Iterator;
25 import java.util.Locale;
26 import java.util.Map;
27 import java.util.Set;
28
29 import javax.faces.FacesException;
30 import javax.faces.context.ExternalContext;
31
32 import javax.portlet.PortletContext;
33 import javax.portlet.PortletException;
34 import javax.portlet.PortletRequest;
35 import javax.portlet.PortletRequestDispatcher;
36 import javax.portlet.PortletResponse;
37 import javax.portlet.RenderRequest;
38 import javax.portlet.RenderResponse;
39
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.commons.logging.Log;
42
43 /***
44 * <p>
45 * JSF 1.0 PRD2, 6.1.1
46 * </p>
47 * <p>
48 * See MyFaces project for servlet implementation.
49 * </p>
50 *
51 * @author <a href="dlestrat@apache.org">David Le Strat </a>
52 */
53 public class PortletExternalContextImpl extends ExternalContext
54 {
55 private static final Log log = LogFactory.getLog(PortletExternalContextImpl.class);
56
57 /*** The init parameter map attribute. */
58 private static final String INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName();
59
60 /*** The portlet context. */
61 private PortletContext portletContext;
62
63 /*** The portlet request. */
64 private PortletRequest portletRequest;
65
66 /*** The portlet response. */
67 private PortletResponse portletResponse;
68
69 /*** The application map. */
70 private Map applicationMap;
71
72 /*** The session map. */
73 private Map sessionMap;
74
75 /*** The request map. */
76 private Map requestMap;
77
78 /*** The request parameter map. */
79 private Map requestParameterMap;
80
81 /*** The request parameter values map. */
82 private Map requestParameterValuesMap;
83
84 /*** The request header map. */
85 private Map requestHeaderMap;
86
87 /*** The request header values map. */
88 private Map requestHeaderValuesMap;
89
90 /*** The request cookie map. */
91 private Map requestCookieMap;
92
93 /*** The init parameter map. */
94 private Map initParameterMap;
95
96 /*** The request path info. */
97 private String requestPathInfo;
98
99 /*** The request servlet path. */
100 private String requestServletPath;
101
102 /***
103 * @param portletContext The {@link PortletContext}.
104 * @param portletRequest The {@link PortletRequest}.
105 * @param portletResponse The {@link PortletResponse}.
106 */
107 public PortletExternalContextImpl(PortletContext portletContext, PortletRequest portletRequest,
108 PortletResponse portletResponse)
109 {
110 this.portletContext = portletContext;
111 this.portletRequest = portletRequest;
112 this.portletResponse = portletResponse;
113 this.applicationMap = null;
114 this.sessionMap = null;
115 this.requestMap = null;
116 this.requestParameterMap = null;
117 this.requestParameterValuesMap = null;
118 this.requestHeaderMap = null;
119 this.requestHeaderValuesMap = null;
120 this.requestCookieMap = null;
121 this.initParameterMap = null;
122 this.requestPathInfo = null;
123 this.requestServletPath = null;
124 }
125
126 /***
127 * <p>
128 * Reset the member variables.
129 * </p>
130 */
131 public void release()
132 {
133 this.portletContext = null;
134 this.portletRequest = null;
135 this.portletResponse = null;
136 this.applicationMap = null;
137 this.sessionMap = null;
138 this.requestMap = null;
139 this.requestParameterMap = null;
140 this.requestParameterValuesMap = null;
141 this.requestHeaderMap = null;
142 this.requestHeaderValuesMap = null;
143 this.requestCookieMap = null;
144 this.initParameterMap = null;
145 this.requestPathInfo = null;
146 this.requestServletPath = null;
147 }
148
149 /***
150 * @see javax.faces.context.ExternalContext#getSession(boolean)
151 */
152 public Object getSession(boolean create)
153 {
154 return this.portletRequest.getPortletSession(create);
155 }
156
157 /***
158 * @see javax.faces.context.ExternalContext#getContext()
159 */
160 public Object getContext()
161 {
162 return this.portletContext;
163 }
164
165 /***
166 * @see javax.faces.context.ExternalContext#getRequest()
167 */
168 public Object getRequest()
169 {
170 return this.portletRequest;
171 }
172
173 /***
174 * @see javax.faces.context.ExternalContext#getResponse()
175 */
176 public Object getResponse()
177 {
178 return this.portletResponse;
179 }
180
181 /***
182 * @see javax.faces.context.ExternalContext#getApplicationMap()
183 */
184 public Map getApplicationMap()
185 {
186 if (this.applicationMap == null)
187 {
188 this.applicationMap = new ApplicationMap(this.portletContext);
189 }
190 return this.applicationMap;
191 }
192
193 /***
194 * @see javax.faces.context.ExternalContext#getSessionMap()
195 */
196 public Map getSessionMap()
197 {
198 if (this.sessionMap == null)
199 {
200 this.sessionMap = new SessionMap(this.portletRequest);
201 }
202 return this.sessionMap;
203 }
204
205 /***
206 * @see javax.faces.context.ExternalContext#getRequestMap()
207 */
208 public Map getRequestMap()
209 {
210 if (this.requestMap == null)
211 {
212 this.requestMap = new RequestMap(this.portletRequest);
213 }
214 return this.requestMap;
215 }
216
217 /***
218 * @see javax.faces.context.ExternalContext#getRequestParameterMap()
219 */
220 public Map getRequestParameterMap()
221 {
222 if (this.requestParameterMap == null)
223 {
224 this.requestParameterMap = new RequestParameterMap(this.portletRequest);
225 }
226 return this.requestParameterMap;
227 }
228
229 /***
230 * @see javax.faces.context.ExternalContext#getRequestParameterValuesMap()
231 */
232 public Map getRequestParameterValuesMap()
233 {
234 if (this.requestParameterValuesMap == null)
235 {
236 this.requestParameterValuesMap = new RequestParameterValuesMap(this.portletRequest);
237 }
238 return this.requestParameterValuesMap;
239 }
240
241 /***
242 * @see javax.faces.context.ExternalContext#getRequestParameterNames()
243 */
244 public Iterator getRequestParameterNames()
245 {
246 final Enumeration names = this.portletRequest.getParameterNames();
247 Iterator it = new Iterator()
248 {
249 public boolean hasNext()
250 {
251 return names.hasMoreElements();
252 }
253
254 public Object next()
255 {
256 return names.nextElement();
257 }
258
259 public void remove()
260 {
261 throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException");
262 }
263 };
264 return it;
265 }
266
267 /***
268 * @see javax.faces.context.ExternalContext#getRequestHeaderMap()
269 */
270 public Map getRequestHeaderMap()
271 {
272 if (this.requestHeaderMap == null)
273 {
274 requestHeaderMap = new RequestHeaderMap(this.portletRequest);
275
276
277 }
278 return requestHeaderMap;
279 }
280
281 /***
282 * @see javax.faces.context.ExternalContext#getRequestHeaderValuesMap()
283 */
284 public Map getRequestHeaderValuesMap()
285 {
286 if (this.requestHeaderValuesMap == null)
287 {
288 requestHeaderValuesMap = new RequestHeaderValuesMap(this.portletRequest);
289 }
290 return requestHeaderValuesMap;
291 }
292
293 /***
294 * @see javax.faces.context.ExternalContext#getRequestCookieMap()
295 */
296 public Map getRequestCookieMap()
297 {
298 return null;
299 }
300
301 /***
302 * @see javax.faces.context.ExternalContext#getRequestLocale()
303 */
304 public Locale getRequestLocale()
305 {
306 return this.portletRequest.getLocale();
307 }
308
309 /***
310 * @see javax.faces.context.ExternalContext#getRequestPathInfo()
311 */
312 public String getRequestPathInfo()
313 {
314 return null;
315 }
316
317 /***
318 * @see javax.faces.context.ExternalContext#getRequestContextPath()
319 */
320 public String getRequestContextPath()
321 {
322 return this.portletRequest.getContextPath();
323 }
324
325 /***
326 * @see javax.faces.context.ExternalContext#getInitParameter(java.lang.String)
327 */
328 public String getInitParameter(String s)
329 {
330 return this.portletContext.getInitParameter(s);
331 }
332
333 /***
334 * @see javax.faces.context.ExternalContext#getInitParameterMap()
335 */
336 public Map getInitParameterMap()
337 {
338 if (this.initParameterMap == null)
339 {
340 if ((this.initParameterMap = (Map) this.portletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null)
341 {
342 this.initParameterMap = new InitParameterMap(this.portletContext);
343 this.portletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, this.initParameterMap);
344 }
345 }
346 return this.initParameterMap;
347 }
348
349 /***
350 * @see javax.faces.context.ExternalContext#getResourcePaths(java.lang.String)
351 */
352 public Set getResourcePaths(String s)
353 {
354 return this.portletContext.getResourcePaths(s);
355 }
356
357 /***
358 * @see javax.faces.context.ExternalContext#getResourceAsStream(java.lang.String)
359 */
360 public InputStream getResourceAsStream(String s)
361 {
362 return this.portletContext.getResourceAsStream(s);
363 }
364
365 /***
366 * @see javax.faces.context.ExternalContext#encodeActionURL(java.lang.String)
367 */
368 public String encodeActionURL(String s)
369 {
370 return this.portletResponse.encodeURL(s);
371 }
372
373 /***
374 * @see javax.faces.context.ExternalContext#encodeResourceURL(java.lang.String)
375 */
376 public String encodeResourceURL(String s)
377 {
378 if ((null != s) && (!s.startsWith("http") && (!s.startsWith("/"))))
379 {
380 s = "/" + s;
381 }
382 return this.portletResponse.encodeURL(s);
383 }
384
385 /***
386 * @see javax.faces.context.ExternalContext#encodeNamespace(java.lang.String)
387 */
388 public String encodeNamespace(String s)
389 {
390 return s;
391 }
392
393 /***
394 * @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
395 */
396 public void dispatch(String requestURI) throws IOException, FacesException
397 {
398 if (!(this.portletResponse instanceof RenderResponse))
399 {
400 throw new IllegalArgumentException("Only RenderResponse can be dispatched");
401 }
402 if (!(this.portletRequest instanceof RenderRequest))
403 {
404 throw new IllegalArgumentException("Only RenderRequest can be dispatched");
405 }
406 PortletRequestDispatcher portletRequestDispatcher = this.portletContext.getRequestDispatcher(requestURI);
407 try
408 {
409 portletRequestDispatcher
410 .include((RenderRequest) this.portletRequest, (RenderResponse) this.portletResponse);
411 }
412 catch (PortletException e)
413 {
414 if (e.getMessage() != null)
415 {
416 throw new FacesException(e.getMessage(), e);
417 }
418 else
419 {
420 throw new FacesException(e);
421 }
422 }
423 }
424
425 /***
426 * @see javax.faces.context.ExternalContext#getRequestServletPath()
427 */
428 public String getRequestServletPath()
429 {
430 return (String) this.portletRequest.getAttribute(FacesPortlet.REQUEST_SERVLET_PATH);
431 }
432
433 /***
434 * @see javax.faces.context.ExternalContext#getAuthType()
435 */
436 public String getAuthType()
437 {
438 return this.portletRequest.getAuthType();
439 }
440
441 /***
442 * @see javax.faces.context.ExternalContext#getRemoteUser()
443 */
444 public String getRemoteUser()
445 {
446 return this.portletRequest.getRemoteUser();
447 }
448
449 /***
450 * @see javax.faces.context.ExternalContext#isUserInRole(java.lang.String)
451 */
452 public boolean isUserInRole(String role)
453 {
454 return this.portletRequest.isUserInRole(role);
455 }
456
457 /***
458 * @see javax.faces.context.ExternalContext#getUserPrincipal()
459 */
460 public Principal getUserPrincipal()
461 {
462 return this.portletRequest.getUserPrincipal();
463 }
464
465 /***
466 * @see javax.faces.context.ExternalContext#log(java.lang.String)
467 */
468 public void log(String message)
469 {
470 this.portletContext.log(message);
471 }
472
473 /***
474 * @see javax.faces.context.ExternalContext#log(java.lang.String, java.lang.Throwable)
475 */
476 public void log(String message, Throwable t)
477 {
478 this.portletContext.log(message, t);
479 }
480
481 /***
482 * @see javax.faces.context.ExternalContext#redirect(java.lang.String)
483 */
484 public void redirect(String url) throws IOException
485 {
486 }
487
488 /***
489 * @see javax.faces.context.ExternalContext#getRequestLocales()
490 */
491 public Iterator getRequestLocales()
492 {
493 return new EnumerationIterator(this.portletRequest.getLocales());
494 }
495
496 /***
497 * @see javax.faces.context.ExternalContext#getResource(java.lang.String)
498 */
499 public URL getResource(String s) throws MalformedURLException
500 {
501 return this.portletContext.getResource(s);
502 }
503 }