KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > context > servlet > ServletExternalContextImpl


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.myfaces.context.servlet;
17
18 import org.apache.myfaces.util.EnumerationIterator;
19
20 import javax.faces.FacesException;
21 import javax.faces.application.ViewHandler;
22 import javax.faces.context.ExternalContext;
23 import javax.servlet.*;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.security.Principal JavaDoc;
32 import java.util.*;
33 import java.lang.reflect.Method JavaDoc;
34
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.commons.logging.Log;
37 import org.apache.myfaces.context.ReleaseableExternalContext;
38
39 /**
40  * JSF 1.0 PRD2, 6.1.1
41  * @author Manfred Geiler (latest modification by $Author: matzew $)
42  * @author Anton Koinov
43  * @version $Revision: 1.19 $ $Date: 2005/01/26 17:03:11 $
44  *
45  * Revision 1.11 Sylvain Vieujot
46  * Forward the message when an exception is thrown in dispatch
47  */

48 public class ServletExternalContextImpl
49     extends ExternalContext implements ReleaseableExternalContext
50 {
51
52     private static final Log log = LogFactory.getLog(ServletExternalContextImpl.class);
53
54     private static final String JavaDoc INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName();
55
56     private ServletContext _servletContext;
57     private ServletRequest JavaDoc _servletRequest;
58     private ServletResponse JavaDoc _servletResponse;
59     private Map _applicationMap;
60     private Map _sessionMap;
61     private Map _requestMap;
62     private Map _requestParameterMap;
63     private Map _requestParameterValuesMap;
64     private Map _requestHeaderMap;
65     private Map _requestHeaderValuesMap;
66     private Map _requestCookieMap;
67     private Map _initParameterMap;
68     private boolean _isHttpServletRequest;
69     private String JavaDoc _requestServletPath;
70     private String JavaDoc _requestPathInfo;
71     private static Method JavaDoc setCharacterEncodingMethod = null;
72     
73     static {
74         try {
75             setCharacterEncodingMethod = ServletRequest JavaDoc.class.getMethod("setCharacterEncoding", new Class JavaDoc[]{String JavaDoc.class});
76         } catch (Exception JavaDoc e) {
77                     log.warn("Detecting request character encoding is disable.");
78                     log.warn("Failed to obtain ServletRequest#setCharacterEncoding() method: " + e);
79         }
80     }
81
82     public ServletExternalContextImpl(ServletContext servletContext,
83                                       ServletRequest JavaDoc servletRequest,
84                                       ServletResponse JavaDoc servletResponse)
85     {
86         _servletContext = servletContext;
87         _servletRequest = servletRequest;
88         _servletResponse = servletResponse;
89         _applicationMap = null;
90         _sessionMap = null;
91         _requestMap = null;
92         _requestParameterMap = null;
93         _requestParameterValuesMap = null;
94         _requestHeaderMap = null;
95         _requestHeaderValuesMap = null;
96         _requestCookieMap = null;
97         _initParameterMap = null;
98         _isHttpServletRequest = (servletRequest != null &&
99                                  servletRequest instanceof HttpServletRequest JavaDoc);
100         if (_isHttpServletRequest)
101         {
102             //HACK: MultipartWrapper scrambles the servletPath for some reason in Tomcat 4.1.29 embedded in JBoss 3.2.3!?
103
// (this was reported by frederic.auge [frederic.auge@laposte.net])
104
HttpServletRequest JavaDoc httpServletRequest = (HttpServletRequest JavaDoc)servletRequest;
105
106             _requestServletPath = httpServletRequest.getServletPath();
107             _requestPathInfo = httpServletRequest.getPathInfo();
108
109             // try to set character encoding as described in section 2.5.2.2 of JSF 1.1 spec
110
// we have to use reflection as method setCharacterEncoding is not supported Servlet API <= 2.3
111
try
112             {
113                 if (setCharacterEncodingMethod != null) {
114                     String JavaDoc contentType = httpServletRequest.getHeader("Content-Type");
115
116                     String JavaDoc characterEncoding = lookupCharacterEncoding(contentType);
117
118                     if (characterEncoding == null) {
119                         HttpSession JavaDoc session = httpServletRequest.getSession(false);
120
121                         if (session != null) {
122                             characterEncoding = (String JavaDoc) session.getAttribute(ViewHandler.CHARACTER_ENCODING_KEY);
123                         }
124
125                         if (characterEncoding != null) {
126                             setCharacterEncodingMethod.invoke(servletRequest, new Object JavaDoc[]{characterEncoding});
127                         }
128                     }
129                 }
130             } catch (Exception JavaDoc e)
131             {
132                 if (log.isWarnEnabled())
133                     log.warn("Failed to set character encoding " + e);
134             }
135         }
136     }
137
138
139     private String JavaDoc lookupCharacterEncoding(String JavaDoc contentType)
140     {
141         String JavaDoc characterEncoding = null;
142
143         if (contentType != null)
144         {
145             int charsetFind = contentType.indexOf("charset=");
146             if (charsetFind != -1)
147             {
148                 if (charsetFind == 0)
149                 {
150                     //charset at beginning of Content-Type, curious
151
characterEncoding = contentType.substring(8);
152                 }
153                 else
154                 {
155                     char charBefore = contentType.charAt(charsetFind - 1);
156                     if (charBefore == ';' || Character.isWhitespace(charBefore))
157                     {
158                         //Correct charset after mime type
159
characterEncoding = contentType.substring(charsetFind + 8);
160                     }
161                 }
162                 if (log.isDebugEnabled()) log.debug("Incoming request has Content-Type header with character encoding " + characterEncoding);
163             }
164             else
165             {
166                 if (log.isDebugEnabled()) log.debug("Incoming request has Content-Type header without character encoding: " + contentType);
167             }
168         }
169         return characterEncoding;
170     }
171
172
173     public void release()
174     {
175         _servletContext = null;
176         _servletRequest = null;
177         _servletResponse = null;
178         _applicationMap = null;
179         _sessionMap = null;
180         _requestMap = null;
181         _requestParameterMap = null;
182         _requestParameterValuesMap = null;
183         _requestHeaderMap = null;
184         _requestHeaderValuesMap = null;
185         _requestCookieMap = null;
186         _initParameterMap = null;
187     }
188
189
190     public Object JavaDoc getSession(boolean create)
191     {
192         if (!_isHttpServletRequest)
193         {
194             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
195         }
196         return ((HttpServletRequest JavaDoc)_servletRequest).getSession(create);
197     }
198
199     public Object JavaDoc getContext()
200     {
201         return _servletContext;
202     }
203
204     public Object JavaDoc getRequest()
205     {
206         return _servletRequest;
207     }
208
209     public Object JavaDoc getResponse()
210     {
211         return _servletResponse;
212     }
213
214     public Map getApplicationMap()
215     {
216         if (_applicationMap == null)
217         {
218             _applicationMap = new ApplicationMap(_servletContext);
219         }
220         return _applicationMap;
221     }
222
223     public Map getSessionMap()
224     {
225         if (_sessionMap == null)
226         {
227             if (!_isHttpServletRequest)
228             {
229                 throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
230             }
231             _sessionMap = new SessionMap((HttpServletRequest JavaDoc) _servletRequest);
232         }
233         return _sessionMap;
234     }
235
236     public Map getRequestMap()
237     {
238         if (_requestMap == null)
239         {
240             _requestMap = new RequestMap(_servletRequest);
241         }
242         return _requestMap;
243     }
244
245     public Map getRequestParameterMap()
246     {
247         if (_requestParameterMap == null)
248         {
249             _requestParameterMap = new RequestParameterMap(_servletRequest);
250         }
251         return _requestParameterMap;
252     }
253
254     public Map getRequestParameterValuesMap()
255     {
256         if (_requestParameterValuesMap == null)
257         {
258             _requestParameterValuesMap = new RequestParameterValuesMap(_servletRequest);
259         }
260         return _requestParameterValuesMap;
261     }
262
263     public Iterator getRequestParameterNames()
264     {
265         final Enumeration enumer = _servletRequest.getParameterNames();
266         Iterator it = new Iterator()
267         {
268             public boolean hasNext() {
269                 return enumer.hasMoreElements();
270             }
271
272             public Object JavaDoc next() {
273                 return enumer.nextElement();
274             }
275
276             public void remove() {
277                 throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
278             }
279         };
280         return it;
281     }
282
283     public Map getRequestHeaderMap()
284     {
285         if (_requestHeaderMap == null)
286         {
287             if (!_isHttpServletRequest)
288             {
289                 throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
290             }
291             _requestHeaderMap = new RequestHeaderMap((HttpServletRequest JavaDoc)_servletRequest);
292         }
293         return _requestHeaderMap;
294     }
295
296     public Map getRequestHeaderValuesMap()
297     {
298         if (_requestHeaderValuesMap == null)
299         {
300             if (!_isHttpServletRequest)
301             {
302                 throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
303             }
304             _requestHeaderValuesMap = new RequestHeaderValuesMap((HttpServletRequest JavaDoc)_servletRequest);
305         }
306         return _requestHeaderValuesMap;
307     }
308
309     public Map getRequestCookieMap()
310     {
311         if (_requestCookieMap == null)
312         {
313             if (!_isHttpServletRequest)
314             {
315                 throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
316             }
317             _requestCookieMap = new CookieMap((HttpServletRequest JavaDoc)_servletRequest);
318         }
319         return _requestCookieMap;
320     }
321
322     public Locale getRequestLocale()
323     {
324         return _servletRequest.getLocale();
325     }
326
327     public String JavaDoc getRequestPathInfo()
328     {
329         if (!_isHttpServletRequest)
330         {
331             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
332         }
333         //return ((HttpServletRequest)_servletRequest).getPathInfo();
334
//HACK: see constructor
335
return _requestPathInfo;
336     }
337
338     public String JavaDoc getRequestContextPath()
339     {
340         if (!_isHttpServletRequest)
341         {
342             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
343         }
344         return ((HttpServletRequest JavaDoc)_servletRequest).getContextPath();
345     }
346
347     public String JavaDoc getInitParameter(String JavaDoc s)
348     {
349         return _servletContext.getInitParameter(s);
350     }
351
352     public Map getInitParameterMap()
353     {
354         if (_initParameterMap == null)
355         {
356             // We cache it as an attribute in ServletContext itself (is this circular reference a problem?)
357
if ((_initParameterMap = (Map) _servletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null)
358             {
359                 _initParameterMap = new InitParameterMap(_servletContext);
360                 _servletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, _initParameterMap);
361             }
362         }
363         return _initParameterMap;
364     }
365
366     public Set getResourcePaths(String JavaDoc s)
367     {
368         return _servletContext.getResourcePaths(s);
369     }
370
371     public InputStream JavaDoc getResourceAsStream(String JavaDoc s)
372     {
373         return _servletContext.getResourceAsStream(s);
374     }
375
376     public String JavaDoc encodeActionURL(String JavaDoc s)
377     {
378         if (!_isHttpServletRequest)
379         {
380             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
381         }
382         return ((HttpServletResponse JavaDoc)_servletResponse).encodeURL(s);
383     }
384
385     public String JavaDoc encodeResourceURL(String JavaDoc s)
386     {
387         if (!_isHttpServletRequest)
388         {
389             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
390         }
391         return ((HttpServletResponse JavaDoc)_servletResponse).encodeURL(s);
392     }
393
394     public String JavaDoc encodeNamespace(String JavaDoc s)
395     {
396         return s;
397     }
398
399     public void dispatch(String JavaDoc requestURI) throws IOException JavaDoc, FacesException
400     {
401         RequestDispatcher requestDispatcher
402             = _servletRequest.getRequestDispatcher(requestURI);
403         try
404         {
405             requestDispatcher.forward(_servletRequest, _servletResponse);
406         }
407         catch (ServletException e)
408         {
409             if (e.getMessage() != null)
410             {
411                 throw new FacesException(e.getMessage(), e);
412             }
413             else
414             {
415                 throw new FacesException(e);
416             }
417         }
418     }
419
420     public String JavaDoc getRequestServletPath()
421     {
422         if (!_isHttpServletRequest)
423         {
424             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
425         }
426         //return ((HttpServletRequest)_servletRequest).getServletPath();
427
//HACK: see constructor
428
return _requestServletPath;
429     }
430
431     public String JavaDoc getAuthType()
432     {
433         if (!_isHttpServletRequest)
434         {
435             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
436         }
437         return ((HttpServletRequest JavaDoc)_servletRequest).getAuthType();
438     }
439
440     public String JavaDoc getRemoteUser()
441     {
442         if (!_isHttpServletRequest)
443         {
444             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
445         }
446         return ((HttpServletRequest JavaDoc)_servletRequest).getRemoteUser();
447     }
448
449     public boolean isUserInRole(String JavaDoc role)
450     {
451         if (!_isHttpServletRequest)
452         {
453             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
454         }
455         return ((HttpServletRequest JavaDoc)_servletRequest).isUserInRole(role);
456     }
457
458     public Principal JavaDoc getUserPrincipal()
459     {
460         if (!_isHttpServletRequest)
461         {
462             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
463         }
464         return ((HttpServletRequest JavaDoc)_servletRequest).getUserPrincipal();
465     }
466
467     public void log(String JavaDoc message) {
468         _servletContext.log(message);
469     }
470
471     public void log(String JavaDoc message, Throwable JavaDoc t) {
472         _servletContext.log(message, t);
473     }
474
475     public void redirect(String JavaDoc url) throws IOException JavaDoc
476     {
477         if (_servletResponse instanceof HttpServletResponse JavaDoc)
478         {
479             ((HttpServletResponse JavaDoc)_servletResponse).sendRedirect(url);
480         }
481         else
482         {
483             throw new IllegalArgumentException JavaDoc("Only HttpServletResponse supported");
484         }
485     }
486
487     public Iterator getRequestLocales()
488     {
489         if (!_isHttpServletRequest)
490         {
491             throw new IllegalArgumentException JavaDoc("Only HttpServletRequest supported");
492         }
493         return new EnumerationIterator(((HttpServletRequest JavaDoc)_servletRequest).getLocales());
494     }
495
496     public URL JavaDoc getResource(String JavaDoc s) throws MalformedURLException JavaDoc
497     {
498         return _servletContext.getResource(s);
499     }
500 }
501
Popular Tags