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.servlet;
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.servlet.ServletRequest;
35  import javax.servlet.ServletResponse;
36  import javax.servlet.http.HttpServletRequest;
37  
38  /**
39   * This class enhances a standard ExternalContext with support for handling a request
40   * that is a multi-part-mime request.
41   * <p>
42   * In particular, this is needed to provide support for the inputFileUpload component.
43   * When an html "file upload" element is embedded in a form, the browser will generate
44   * a multi-part-mime message to the server containing the posted form input fields
45   * in one mime part, and the contents of the selected files in additional mime parts.
46   * JSF only expects one mime part, so we need to provide access by default to just
47   * the posted form params, but allow the inputFileUpload component to access the
48   * additional mime parts.
49   * <p>
50   * NOTE: This class is an internal implementation detail of the Tomahawk library, intended
51   * for use only by the TomahawkFacesContextWrapper class. This class is NOT part of the
52   * Tomahawk stable API and may be modified in minor releases. User code should not use or
53   * subclass this class.
54   * </p>
55   * 
56   * @since  1.1.7
57   * @author Martin Marinschek
58   */
59  
60  public class ServletExternalContextWrapper extends ExternalContext {
61      private ExternalContext _delegate;
62      private ServletRequest _servletRequest;
63      private ServletResponse _servletResponse;
64      private boolean _multipartContent;
65  
66      //This maps are only used if multipartContent = true
67      private Map _sessionMap;
68      private Map _requestMap;
69      private Map _requestParameterMap;
70      private Map _requestParameterValuesMap;
71      private Map _requestHeaderMap;
72      private Map _requestHeaderValuesMap;
73      private Map _requestCookieMap;
74      //end multipartContent = true;
75  
76      public ServletExternalContextWrapper(ExternalContext delegate, ServletRequest request, ServletResponse response, boolean multipartContent) {
77          this._delegate = delegate;
78          this._servletRequest = request;
79          this._servletResponse = response;
80          this._multipartContent = multipartContent;
81      }
82  
83      public void dispatch(String path) throws IOException {
84          _delegate.dispatch(path);
85      }
86  
87      public String encodeActionURL(String url) {
88          return _delegate.encodeActionURL(url);
89      }
90  
91      public String encodeNamespace(String name) {
92          return _delegate.encodeNamespace(name);
93      }
94  
95      public String encodeResourceURL(String url) {
96          return _delegate.encodeResourceURL(url);
97      }
98  
99      public Map getApplicationMap() {
100         return _delegate.getApplicationMap();
101     }
102 
103     public String getAuthType() {
104         return _delegate.getAuthType();
105     }
106 
107     public Object getContext() {
108         return _delegate.getContext();
109     }
110 
111     public String getInitParameter(String name) {
112         return _delegate.getInitParameter(name);
113     }
114 
115     public Map getInitParameterMap() {
116         return _delegate.getInitParameterMap();
117     }
118 
119     public String getRemoteUser() {
120         return _delegate.getRemoteUser();
121     }
122 
123     public Object getRequest() {
124         // Why is this done?
125         return _servletRequest==null?_delegate.getRequest():_servletRequest;
126     }
127 
128     public String getRequestContextPath() {
129         return _delegate.getRequestContextPath();
130     }
131 
132     public Map getRequestCookieMap() {
133         if (_multipartContent)
134         {
135             if (_requestCookieMap == null)
136             {
137                 _requestCookieMap = new CookieMap((HttpServletRequest)_servletRequest);
138             }
139             return _requestCookieMap;
140         }
141         else
142         {
143             return _delegate.getRequestCookieMap();
144         }
145     }
146 
147     public Map getRequestHeaderMap() {
148         if (_multipartContent)
149         {
150             if (_requestHeaderMap == null)
151             {
152                 _requestHeaderMap = new RequestHeaderMap((HttpServletRequest)_servletRequest);
153             }
154             return _requestHeaderMap;                        
155         }
156         else
157         {
158             return _delegate.getRequestHeaderMap();
159         }
160     }
161 
162     public Map getRequestHeaderValuesMap() {
163         if (_multipartContent)
164         {
165             if (_requestHeaderValuesMap == null)
166             {
167                 _requestHeaderValuesMap = new RequestHeaderValuesMap((HttpServletRequest)_servletRequest);
168             }
169             return _requestHeaderValuesMap;
170         }
171         else
172         {        
173             return _delegate.getRequestHeaderValuesMap();
174         }
175     }
176 
177     public Locale getRequestLocale() {
178         return _delegate.getRequestLocale();
179     }
180 
181     public Iterator getRequestLocales() {
182         return _delegate.getRequestLocales();
183     }
184 
185     public Map getRequestMap() {
186         if (_multipartContent)
187         {            
188             if (_requestMap == null)
189             {
190                 _requestMap = new RequestMap(_servletRequest);
191             }
192             return _requestMap;
193         }
194         else
195         {
196             return _delegate.getRequestMap();
197         }
198     }
199 
200     public Map getRequestParameterMap() {
201         if (_multipartContent)
202         {            
203             if (_requestParameterMap == null)
204             {
205                 _requestParameterMap = new RequestParameterMap(_servletRequest);
206             }
207             return _requestParameterMap;
208         }
209         else
210         {        
211             return _delegate.getRequestParameterMap();
212         }
213     }
214 
215     public Iterator getRequestParameterNames() {
216         if (_multipartContent)
217         {            
218             final Enumeration enumer = _servletRequest.getParameterNames();
219             Iterator it = new Iterator()
220             {
221                 public boolean hasNext() {
222                     return enumer.hasMoreElements();
223                 }
224 
225                 public Object next() {
226                     return enumer.nextElement();
227                 }
228 
229                 public void remove() {
230                     throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException");
231                 }
232             };
233             return it;
234         }
235         else
236         {        
237             return _delegate.getRequestParameterNames();
238         }
239     }
240 
241     public Map getRequestParameterValuesMap() {
242         if (_multipartContent)
243         {            
244             if (_requestParameterValuesMap == null)
245             {
246                 _requestParameterValuesMap = new RequestParameterValuesMap(_servletRequest);
247             }
248             return _requestParameterValuesMap;
249         }
250         else
251         {        
252             return _delegate.getRequestParameterValuesMap();
253         }
254     }
255 
256     public String getRequestPathInfo() {
257         return _delegate.getRequestPathInfo();
258     }
259 
260     public String getRequestServletPath() {
261         return _delegate.getRequestServletPath();
262     }
263 
264     public URL getResource(String path) throws MalformedURLException {
265         return _delegate.getResource(path);
266     }
267 
268     public InputStream getResourceAsStream(String path) {
269         return _delegate.getResourceAsStream(path);
270     }
271 
272     public Set getResourcePaths(String path) {
273         return _delegate.getResourcePaths(path);
274     }
275 
276     public Object getResponse() {
277         return _servletResponse==null?_delegate.getResponse():_servletResponse;
278     }
279 
280     public Object getSession(boolean create) {
281         return _delegate.getSession(create);
282     }
283 
284     public Map getSessionMap() {
285         if (_multipartContent)
286         {            
287             if (_sessionMap == null)
288             {
289                 _sessionMap = new SessionMap((HttpServletRequest) _servletRequest);
290             }
291             return _sessionMap;
292         }
293         else
294         {
295             return _delegate.getSessionMap();            
296         }
297     }
298 
299     public Principal getUserPrincipal() {
300         return _delegate.getUserPrincipal();
301     }
302 
303     public boolean isUserInRole(String role) {
304         return _delegate.isUserInRole(role);
305     }
306 
307     public void log(String message) {
308         _delegate.log(message);
309     }
310 
311     public void log(String message, Throwable exception) {
312         _delegate.log(message, exception);
313     }
314 
315     public void redirect(String url) throws IOException {
316         _delegate.redirect(url);
317     }
318     
319     //Methods since 1.2
320     
321     public String getResponseContentType()
322     {
323         try
324         {
325             Method method = _delegate.getClass().getMethod(
326                     "getResponseContentType", 
327                     null);
328             return (String) method.invoke(_delegate, null);
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 setRequest(java.lang.Object request)
341     {
342         try
343         {
344             Method method = _delegate.getClass().getMethod(
345                     "setRequest", 
346                     new Class[]{java.lang.Object.class});
347             method.invoke(_delegate, new Object[]{request});
348         }
349         catch (NoSuchMethodException e)
350         {
351             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
352         }
353         catch (Exception e)
354         {
355             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
356         }
357     }
358 
359     public void setRequestCharacterEncoding(java.lang.String encoding)
360         throws java.io.UnsupportedEncodingException{
361 
362         try
363         {
364             Method method = _delegate.getClass().getMethod(
365                     "setRequestCharacterEncoding", 
366                     new Class[]{java.lang.String.class});
367             method.invoke(_delegate, new Object[]{encoding});
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 setResponse(java.lang.Object response)
380     {
381         try
382         {
383             Method method = _delegate.getClass().getMethod(
384                     "setResponse", 
385                     new Class[]{java.lang.Object.class});
386             method.invoke(_delegate, new Object[]{response});
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 void setResponseCharacterEncoding(java.lang.String encoding)
399     {
400         try
401         {
402             Method method = _delegate.getClass().getMethod(
403                     "setResponseCharacterEncoding", 
404                     new Class[]{java.lang.String.class});
405             method.invoke(_delegate, new Object[]{encoding});
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 getResponseCharacterEncoding()
418     {
419         try
420         {
421             Method method = _delegate.getClass().getMethod(
422                     "getResponseCharacterEncoding", 
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     public String getRequestCharacterEncoding()
437     {
438         try
439         {
440             Method method = _delegate.getClass().getMethod(
441                     "getRequestCharacterEncoding", 
442                     null);
443             return (String) method.invoke(_delegate, null);
444         }
445         catch (NoSuchMethodException e)
446         {
447             throw new RuntimeException("JSF 1.2 method not implemented: "+e.getMessage());
448         }
449         catch (Exception e)
450         {
451             throw new RuntimeException("Error calling JSF 1.2 method: "+e.getMessage());
452         }
453     }
454 }