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.orchestra.lib.jsf;
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.Iterator;
28  import java.util.Locale;
29  import java.util.Map;
30  import java.util.Set;
31  
32  import javax.faces.context.ExternalContext;
33  
34  import org.apache.myfaces.orchestra.requestParameterProvider.RequestParameterProviderManager;
35  
36  /**
37   * Class used by _PortletFacesContextWrapper to allow orchestra work in portlets
38   * 
39   * This class wraps encodeActionURL and encodeResourceURL to include
40   * conversationContext param like RequestParameterResponseWrapper does. In portlets
41   * we can't do the same than is servlets, because the params added here must be
42   * encoded by the portlet container and in portlets we don't have servlet redirect
43   * cases.
44   * 
45   * @author Leonardo Uribe(latest modification by $Author: lu4242 $)
46   * @version $Revision: 798382 $ $Date: 2009-07-27 22:23:02 -0500 (Mon, 27 Jul 2009) $
47   */
48  public class PortletExternalContextWrapper extends ExternalContext
49  {
50      private ExternalContext _delegate;
51  
52      public PortletExternalContextWrapper(ExternalContext context)
53      {
54          super();
55          this._delegate = context;
56      }
57  
58      public void dispatch(String arg0) throws IOException
59      {
60          _delegate.dispatch(arg0);
61      }
62  
63      public String encodeActionURL(String url)
64      {
65          if (url != null)
66          {
67              url = RequestParameterProviderManager.getInstance().encodeAndAttachParameters(url);
68          }
69          return _delegate.encodeActionURL(url);
70      }
71  
72      public String encodeNamespace(String arg0)
73      {
74          return _delegate.encodeNamespace(arg0);
75      }
76  
77      public String encodeResourceURL(String url)
78      {
79          if (url != null)
80          {
81              url = RequestParameterProviderManager.getInstance().encodeAndAttachParameters(url);
82          }
83          return _delegate.encodeResourceURL(url);
84      }
85  
86      public Map getApplicationMap()
87      {
88          return _delegate.getApplicationMap();
89      }
90  
91      public String getAuthType()
92      {
93          return _delegate.getAuthType();
94      }
95  
96      public Object getContext()
97      {
98          return _delegate.getContext();
99      }
100 
101     public String getInitParameter(String arg0)
102     {
103         return _delegate.getInitParameter(arg0);
104     }
105 
106     public Map getInitParameterMap()
107     {
108         return _delegate.getInitParameterMap();
109     }
110 
111     public String getRemoteUser()
112     {
113         return _delegate.getRemoteUser();
114     }
115 
116     public Object getRequest()
117     {
118         return _delegate.getRequest();
119     }
120 
121     public String getRequestContextPath()
122     {
123         return _delegate.getRequestContextPath();
124     }
125 
126     public Map getRequestCookieMap()
127     {
128         return _delegate.getRequestCookieMap();
129     }
130 
131     public Map getRequestHeaderMap()
132     {
133         return _delegate.getRequestHeaderMap();
134     }
135 
136     public Map getRequestHeaderValuesMap()
137     {
138         return _delegate.getRequestHeaderValuesMap();
139     }
140 
141     public Locale getRequestLocale()
142     {
143         return _delegate.getRequestLocale();
144     }
145 
146     public Iterator getRequestLocales()
147     {
148         return _delegate.getRequestLocales();
149     }
150 
151     public Map getRequestMap()
152     {
153         return _delegate.getRequestMap();
154     }
155 
156     public Map getRequestParameterMap()
157     {
158         return _delegate.getRequestParameterMap();
159     }
160 
161     public Iterator getRequestParameterNames()
162     {
163         return _delegate.getRequestParameterNames();
164     }
165 
166     public Map getRequestParameterValuesMap()
167     {
168         return _delegate.getRequestParameterValuesMap();
169     }
170 
171     public String getRequestPathInfo()
172     {
173         return _delegate.getRequestPathInfo();
174     }
175 
176     public String getRequestServletPath()
177     {
178         return _delegate.getRequestServletPath();
179     }
180 
181     public URL getResource(String arg0) throws MalformedURLException
182     {
183         return _delegate.getResource(arg0);
184     }
185 
186     public InputStream getResourceAsStream(String arg0)
187     {
188         return _delegate.getResourceAsStream(arg0);
189     }
190 
191     public Set getResourcePaths(String arg0)
192     {
193         return _delegate.getResourcePaths(arg0);
194     }
195 
196     public Object getResponse()
197     {
198         return _delegate.getResponse();
199     }
200 
201     public Object getSession(boolean arg0)
202     {
203         return _delegate.getSession(arg0);
204     }
205 
206     public Map getSessionMap()
207     {
208         return _delegate.getSessionMap();
209     }
210 
211     public Principal getUserPrincipal()
212     {
213         return _delegate.getUserPrincipal();
214     }
215 
216     public boolean isUserInRole(String arg0)
217     {
218         return _delegate.isUserInRole(arg0);
219     }
220 
221     public void redirect(String arg0) throws IOException
222     {
223         _delegate.redirect(arg0);
224     }
225 
226     public void log(String s, Throwable throwable)
227     {
228         _delegate.log(s, throwable);
229     }
230 
231     public void log(String s)
232     {
233         _delegate.log(s);
234     }
235     
236     //Methods since 1.2
237     
238     //Methods since 1.2
239     
240     public String getResponseContentType()
241     {
242         try
243         {
244             Method method = _delegate.getClass().getMethod(
245                     "getResponseContentType", 
246                     null);
247             return (String) method.invoke(_delegate, null);
248         }
249         catch (NoSuchMethodException e)
250         {
251             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
252         }
253         catch (Exception e)
254         {
255             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
256         }
257     }
258 
259     public void setRequest(java.lang.Object request)
260     {
261         try
262         {
263             Method method = _delegate.getClass().getMethod(
264                     "setRequest", 
265                     new Class[]{java.lang.Object.class});
266             method.invoke(_delegate, new Object[]{request});
267         }
268         catch (NoSuchMethodException e)
269         {
270             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
271         }
272         catch (Exception e)
273         {
274             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
275         }
276     }
277 
278     public void setRequestCharacterEncoding(java.lang.String encoding)
279         throws java.io.UnsupportedEncodingException
280     {
281         try
282         {
283             Method method = _delegate.getClass().getMethod(
284                     "setRequestCharacterEncoding", 
285                     new Class[]{java.lang.String.class});
286             method.invoke(_delegate, new Object[]{encoding});
287         }
288         catch (NoSuchMethodException e)
289         {
290             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
291         }
292         catch (Exception e)
293         {
294             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
295         }
296     }
297     
298     public void setResponse(java.lang.Object response)
299     {
300         try
301         {
302             Method method = _delegate.getClass().getMethod(
303                     "setResponse", 
304                     new Class[]{java.lang.Object.class});
305             method.invoke(_delegate, new Object[]{response});
306         }
307         catch (NoSuchMethodException e)
308         {
309             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
310         }
311         catch (Exception e)
312         {
313             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
314         }
315     }
316     
317     public void setResponseCharacterEncoding(java.lang.String encoding)
318     {
319         try
320         {
321             Method method = _delegate.getClass().getMethod(
322                     "setResponseCharacterEncoding", 
323                     new Class[]{java.lang.String.class});
324             method.invoke(_delegate, new Object[]{encoding});
325         }
326         catch (NoSuchMethodException e)
327         {
328             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
329         }
330         catch (Exception e)
331         {
332             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
333         }
334     }
335 
336     public String getResponseCharacterEncoding()
337     {
338         try
339         {
340             Method method = _delegate.getClass().getMethod(
341                     "getResponseCharacterEncoding", 
342                     null);
343             return (String) method.invoke(_delegate, null);
344         }
345         catch (NoSuchMethodException e)
346         {
347             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
348         }
349         catch (Exception e)
350         {
351             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
352         }
353     }
354         
355     public String getRequestCharacterEncoding()
356     {
357         try
358         {
359             Method method = _delegate.getClass().getMethod(
360                     "getRequestCharacterEncoding", 
361                     null);
362             return (String) method.invoke(_delegate, null);
363         }
364         catch (NoSuchMethodException e)
365         {
366             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
367         }
368         catch (Exception e)
369         {
370             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
371         }
372     }
373 }