View Javadoc

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