View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.webapp.filter.portlet;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.lang.reflect.Method;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.security.Principal;
27  import java.util.Enumeration;
28  import java.util.Iterator;
29  import java.util.Locale;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import javax.faces.context.ExternalContext;
34  import javax.portlet.PortletRequest;
35  import javax.portlet.PortletResponse;
36  
37  /**
38   * This class encapsulate the ExternalContext given, adding support for
39   * inputFileUpload (necessary when this is encapsulated with 
40   * MultipartRequestWrapper). For make available the parameters map on a
41   * multipart content request, it is necessary to encapsulate the original
42   * request with MultipartRequestWrapper, but the original ExternalContext
43   * is tied with the original request so we need to do this wrapper using
44   * the delegate pattern starting with TomahawkFacesContextFactory. 
45   * <p>
46   * NOTE: This class should is used(instantiated) only by 
47   * TomahawkFacesContextWrapper. By that reason, it could change
48   * in the future.
49   * </p>
50   * 
51   * @since 1.1.7
52   * @author Martin Marinschek
53   */
54  public class PortletExternalContextWrapper extends ExternalContext {
55      private ExternalContext _delegate;
56      private PortletRequest _portletRequest;
57      private PortletResponse _portletResponse;
58      private boolean _multipartContent;
59  
60      //This maps are only used if multipartContent = true
61      private Map _sessionMap;
62      private Map _requestMap;
63      private Map _requestParameterMap;
64      private Map _requestParameterValuesMap;
65      private Map _requestHeaderMap;
66      private Map _requestHeaderValuesMap;
67      //end multipartContent = true;
68  
69      public PortletExternalContextWrapper(ExternalContext delegate, Object request, Object response, boolean multipartContent) {
70          this._delegate = delegate;
71          this._portletRequest = (PortletRequest)request;
72          this._portletResponse = (PortletResponse)response;
73          this._multipartContent = multipartContent;
74      }
75  
76      public void dispatch(String path) throws IOException {
77          _delegate.dispatch(path);
78      }
79  
80      public String encodeActionURL(String url) {
81          return _delegate.encodeActionURL(url);
82      }
83  
84      public String encodeNamespace(String name) {
85          return _delegate.encodeNamespace(name);
86      }
87  
88      public String encodeResourceURL(String url) {
89          return _delegate.encodeResourceURL(url);
90      }
91  
92      public Map getApplicationMap() {
93          return _delegate.getApplicationMap();
94      }
95  
96      public String getAuthType() {
97          return _delegate.getAuthType();
98      }
99  
100     public Object getContext() {
101         return _delegate.getContext();
102     }
103 
104     public String getInitParameter(String name) {
105         return _delegate.getInitParameter(name);
106     }
107 
108     public Map getInitParameterMap() {
109         return _delegate.getInitParameterMap();
110     }
111 
112     public String getRemoteUser() {
113         return _delegate.getRemoteUser();
114     }
115 
116     public Object getRequest() {
117         return _portletRequest==null?_delegate.getRequest():_portletRequest;
118     }
119 
120     public String getRequestContextPath() {
121         return _delegate.getRequestContextPath();
122     }
123 
124     public Map getRequestCookieMap() {
125         return _delegate.getRequestCookieMap();
126     }
127 
128     public Map getRequestHeaderMap() {
129         if (_multipartContent)
130         {
131             if (_requestHeaderMap == null)
132             {
133                 _requestHeaderMap = new RequestHeaderMap((PortletRequest)_portletRequest);
134             }
135             return _requestHeaderMap;                        
136         }
137         else
138         {
139             return _delegate.getRequestHeaderMap();
140         }
141     }
142 
143     public Map getRequestHeaderValuesMap() {
144         if (_multipartContent)
145         {
146             if (_requestHeaderValuesMap == null)
147             {
148                 _requestHeaderValuesMap = new RequestHeaderValuesMap((PortletRequest)_portletRequest);
149             }
150             return _requestHeaderValuesMap;
151         }
152         else
153         {        
154             return _delegate.getRequestHeaderValuesMap();
155         }
156     }
157 
158     public Locale getRequestLocale() {
159         return _delegate.getRequestLocale();
160     }
161 
162     public Iterator getRequestLocales() {
163         return _delegate.getRequestLocales();
164     }
165 
166     public Map getRequestMap() {
167         if (_multipartContent)
168         {            
169             if (_requestMap == null)
170             {
171                 _requestMap = new RequestMap(_portletRequest);
172             }
173             return _requestMap;
174         }
175         else
176         {
177             return _delegate.getRequestMap();
178         }
179     }
180 
181     public Map getRequestParameterMap() {
182         if (_multipartContent)
183         {            
184             if (_requestParameterMap == null)
185             {
186                 _requestParameterMap = new RequestParameterMap(_portletRequest);
187             }
188             return _requestParameterMap;
189         }
190         else
191         {        
192             return _delegate.getRequestParameterMap();
193         }
194     }
195 
196     public Iterator getRequestParameterNames() {
197         if (_multipartContent)
198         {            
199             final Enumeration enumer = _portletRequest.getParameterNames();
200             Iterator it = new Iterator()
201             {
202                 public boolean hasNext() {
203                     return enumer.hasMoreElements();
204                 }
205 
206                 public Object next() {
207                     return enumer.nextElement();
208                 }
209 
210                 public void remove() {
211                     throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException");
212                 }
213             };
214             return it;
215         }
216         else
217         {        
218             return _delegate.getRequestParameterNames();
219         }
220     }
221 
222     public Map getRequestParameterValuesMap() {
223         if (_multipartContent)
224         {            
225             if (_requestParameterValuesMap == null)
226             {
227                 _requestParameterValuesMap = new RequestParameterValuesMap(_portletRequest);
228             }
229             return _requestParameterValuesMap;
230         }
231         else
232         {        
233             return _delegate.getRequestParameterValuesMap();
234         }
235     }
236 
237     public String getRequestPathInfo() {
238         return _delegate.getRequestPathInfo();
239     }
240 
241     public String getRequestServletPath() {
242         return _delegate.getRequestServletPath();
243     }
244 
245     public URL getResource(String path) throws MalformedURLException {
246         return _delegate.getResource(path);
247     }
248 
249     public InputStream getResourceAsStream(String path) {
250         return _delegate.getResourceAsStream(path);
251     }
252 
253     public Set getResourcePaths(String path) {
254         return _delegate.getResourcePaths(path);
255     }
256 
257     public Object getResponse() {
258         return _portletResponse==null?_delegate.getResponse():_portletResponse;
259     }
260 
261     public Object getSession(boolean create) {
262         return _delegate.getSession(create);
263     }
264 
265     public Map getSessionMap() {
266         if (_multipartContent)
267         {            
268             if (_sessionMap == null)
269             {
270                 _sessionMap = new SessionMap((PortletRequest) _portletRequest);
271             }
272             return _sessionMap;
273         }
274         else
275         {
276             return _delegate.getSessionMap();            
277         }
278     }
279 
280     public Principal getUserPrincipal() {
281         return _delegate.getUserPrincipal();
282     }
283 
284     public boolean isUserInRole(String role) {
285         return _delegate.isUserInRole(role);
286     }
287 
288     public void log(String message) {
289         _delegate.log(message);
290     }
291 
292     public void log(String message, Throwable exception) {
293         _delegate.log(message, exception);
294     }
295 
296     public void redirect(String url) throws IOException {
297         _delegate.redirect(url);
298     }
299     
300     //Methods since 1.2
301     
302     public String getResponseContentType()
303     {
304         try
305         {
306             Method method = _delegate.getClass().getMethod(
307                     "getResponseContentType", 
308                     null);
309             return (String) method.invoke(_delegate, null);
310         }
311         catch (NoSuchMethodException e)
312         {
313             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
314         }
315         catch (Exception e)
316         {
317             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
318         }
319     }
320 
321     public void setRequest(java.lang.Object request)
322     {
323         try
324         {
325             Method method = _delegate.getClass().getMethod(
326                     "setRequest", 
327                     new Class[]{java.lang.Object.class});
328             method.invoke(_delegate, new Object[]{request});
329         }
330         catch (NoSuchMethodException e)
331         {
332             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
333         }
334         catch (Exception e)
335         {
336             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
337         }
338     }
339 
340     public void setRequestCharacterEncoding(java.lang.String encoding)
341         throws java.io.UnsupportedEncodingException{
342 
343         try
344         {
345             Method method = _delegate.getClass().getMethod(
346                     "setRequestCharacterEncoding", 
347                     new Class[]{java.lang.String.class});
348             method.invoke(_delegate, new Object[]{encoding});
349         }
350         catch (NoSuchMethodException e)
351         {
352             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
353         }
354         catch (Exception e)
355         {
356             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
357         }
358     }
359     
360     public void setResponse(java.lang.Object response)
361     {
362         try
363         {
364             Method method = _delegate.getClass().getMethod(
365                     "setResponse", 
366                     new Class[]{java.lang.Object.class});
367             method.invoke(_delegate, new Object[]{response});
368         }
369         catch (NoSuchMethodException e)
370         {
371             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
372         }
373         catch (Exception e)
374         {
375             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
376         }
377     }
378     
379     public void setResponseCharacterEncoding(java.lang.String encoding)
380     {
381         try
382         {
383             Method method = _delegate.getClass().getMethod(
384                     "setResponseCharacterEncoding", 
385                     new Class[]{java.lang.String.class});
386             method.invoke(_delegate, new Object[]{encoding});
387         }
388         catch (NoSuchMethodException e)
389         {
390             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
391         }
392         catch (Exception e)
393         {
394             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
395         }
396     }
397 
398     public String getResponseCharacterEncoding()
399     {
400         try
401         {
402             Method method = _delegate.getClass().getMethod(
403                     "getResponseCharacterEncoding", 
404                     null);
405             return (String) method.invoke(_delegate, null);
406         }
407         catch (NoSuchMethodException e)
408         {
409             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
410         }
411         catch (Exception e)
412         {
413             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
414         }
415     }
416         
417     public String getRequestCharacterEncoding()
418     {
419         try
420         {
421             Method method = _delegate.getClass().getMethod(
422                     "getRequestCharacterEncoding", 
423                     null);
424             return (String) method.invoke(_delegate, null);
425         }
426         catch (NoSuchMethodException e)
427         {
428             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
429         }
430         catch (Exception e)
431         {
432             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
433         }
434     }
435         
436 }