1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.portals.bridges.jsf;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.security.Principal;
24 import java.util.Enumeration;
25 import java.util.Iterator;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.Set;
29
30 import javax.faces.FacesException;
31 import javax.faces.context.ExternalContext;
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.Log;
41 import org.apache.commons.logging.LogFactory;
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 if (null != s)
371 {
372 if (s.startsWith("http") || (s.startsWith("/")))
373 {
374 return this.portletResponse.encodeURL(s);
375 }
376 }
377 return s;
378 }
379
380 /***
381 * @see javax.faces.context.ExternalContext#encodeResourceURL(java.lang.String)
382 */
383 public String encodeResourceURL(String s)
384 {
385 if (null != s)
386 {
387 if (s.startsWith("http") || (s.startsWith("/")))
388 {
389 return this.portletResponse.encodeURL(s);
390 }
391 }
392 return s;
393 }
394
395 /***
396 * @see javax.faces.context.ExternalContext#encodeNamespace(java.lang.String)
397 */
398 public String encodeNamespace(String pNamespace)
399 {
400
401 String vRetEncodedNamespace = null;
402
403 if (!(this.portletResponse instanceof RenderResponse))
404 {
405 throw new IllegalArgumentException("Only RenderResponse can be used to encode namespace");
406 }
407 else
408 {
409 vRetEncodedNamespace = ((RenderResponse)this.portletResponse).getNamespace()+pNamespace;
410 }
411
412 return vRetEncodedNamespace;
413 };
414
415 /***
416 * @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
417 */
418 public void dispatch(String requestURI) throws IOException, FacesException
419 {
420 if (!(this.portletResponse instanceof RenderResponse))
421 {
422 throw new IllegalArgumentException("Only RenderResponse can be dispatched");
423 }
424 if (!(this.portletRequest instanceof RenderRequest))
425 {
426 throw new IllegalArgumentException("Only RenderRequest can be dispatched");
427 }
428 PortletRequestDispatcher portletRequestDispatcher = this.portletContext.getRequestDispatcher(requestURI);
429 try
430 {
431 portletRequestDispatcher
432 .include((RenderRequest) this.portletRequest, (RenderResponse) this.portletResponse);
433 }
434 catch (PortletException e)
435 {
436 if (e.getMessage() != null)
437 {
438 throw new FacesException(e.getMessage(), e);
439 }
440 else
441 {
442 throw new FacesException(e);
443 }
444 }
445 }
446
447 /***
448 * @see javax.faces.context.ExternalContext#getRequestServletPath()
449 */
450 public String getRequestServletPath()
451 {
452 return (String) this.portletRequest.getAttribute(FacesPortlet.REQUEST_SERVLET_PATH);
453 }
454
455 /***
456 * @see javax.faces.context.ExternalContext#getAuthType()
457 */
458 public String getAuthType()
459 {
460 return this.portletRequest.getAuthType();
461 }
462
463 /***
464 * @see javax.faces.context.ExternalContext#getRemoteUser()
465 */
466 public String getRemoteUser()
467 {
468 return this.portletRequest.getRemoteUser();
469 }
470
471 /***
472 * @see javax.faces.context.ExternalContext#isUserInRole(java.lang.String)
473 */
474 public boolean isUserInRole(String role)
475 {
476 return this.portletRequest.isUserInRole(role);
477 }
478
479 /***
480 * @see javax.faces.context.ExternalContext#getUserPrincipal()
481 */
482 public Principal getUserPrincipal()
483 {
484 return this.portletRequest.getUserPrincipal();
485 }
486
487 /***
488 * @see javax.faces.context.ExternalContext#log(java.lang.String)
489 */
490 public void log(String message)
491 {
492 this.portletContext.log(message);
493 }
494
495 /***
496 * @see javax.faces.context.ExternalContext#log(java.lang.String, java.lang.Throwable)
497 */
498 public void log(String message, Throwable t)
499 {
500 this.portletContext.log(message, t);
501 }
502
503 /***
504 * @see javax.faces.context.ExternalContext#redirect(java.lang.String)
505 */
506 public void redirect(String url) throws IOException
507 {
508 }
509
510 /***
511 * @see javax.faces.context.ExternalContext#getRequestLocales()
512 */
513 public Iterator getRequestLocales()
514 {
515 return new EnumerationIterator(this.portletRequest.getLocales());
516 }
517
518 /***
519 * @see javax.faces.context.ExternalContext#getResource(java.lang.String)
520 */
521 public URL getResource(String s) throws MalformedURLException
522 {
523 return this.portletContext.getResource(s);
524 }
525 }