KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > context > portlet > PortletExternalContextImpl


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.portlet;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.security.Principal JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.faces.FacesException;
31 import javax.faces.application.ViewHandler;
32 import javax.faces.context.ExternalContext;
33 import javax.portlet.ActionRequest;
34 import javax.portlet.ActionResponse;
35 import javax.portlet.PortletContext;
36 import javax.portlet.PortletException;
37 import javax.portlet.PortletRequest;
38 import javax.portlet.PortletRequestDispatcher;
39 import javax.portlet.PortletResponse;
40 import javax.portlet.PortletSession;
41 import javax.portlet.RenderRequest;
42 import javax.portlet.RenderResponse;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.apache.myfaces.context.ReleaseableExternalContext;
47 import org.apache.myfaces.util.EnumerationIterator;
48
49 /**
50  * An ExternalContext implementation for JSF applications that run inside a
51  * a Portlet.
52  *
53  * @author Stan Silvert (latest modification by $Author: matzew $)
54  * @version $Revision: 1.1 $ $Date: 2005/01/26 17:03:09 $
55  * $Log: PortletExternalContextImpl.java,v $
56  * Revision 1.1 2005/01/26 17:03:09 matzew
57  * MYFACES-86. portlet support provided by Stan Silver (JBoss Group)
58  *
59  */

60 public class PortletExternalContextImpl extends ExternalContext implements ReleaseableExternalContext {
61     
62     private static final Log log = LogFactory.getLog(PortletExternalContextImpl.class);
63     
64     private static final String JavaDoc INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName();
65     private static final Map JavaDoc EMPTY_UNMODIFIABLE_MAP = Collections.unmodifiableMap(new HashMap JavaDoc(0));
66     
67     PortletContext _portletContext;
68     PortletRequest _portletRequest;
69     PortletResponse _portletResponse;
70     
71     private Map JavaDoc _applicationMap;
72     private Map JavaDoc _sessionMap;
73     private Map JavaDoc _requestMap;
74     private Map JavaDoc _requestParameterMap;
75     private Map JavaDoc _requestParameterValuesMap;
76     private Map JavaDoc _requestHeaderMap;
77     private Map JavaDoc _requestHeaderValuesMap;
78     private Map JavaDoc _requestCookieMap;
79     private Map JavaDoc _initParameterMap;
80     private boolean _isActionRequest;
81     
82     /** Creates a new instance of PortletFacesContextImpl */
83     public PortletExternalContextImpl(PortletContext portletContext,
84                                       PortletRequest portletRequest,
85                                       PortletResponse portletResponse)
86     {
87         _portletContext = portletContext;
88         _portletRequest = portletRequest;
89         _portletResponse = portletResponse;
90         _isActionRequest = (portletRequest != null &&
91                                  portletRequest instanceof ActionRequest);
92         
93         if (_isActionRequest)
94         {
95             ActionRequest actionRequest = (ActionRequest)portletRequest;
96
97             // try to set character encoding as described in section 2.5.2.2 of JSF 1.1 spec
98
try
99             {
100                 String JavaDoc contentType = portletRequest.getProperty("Content-Type");
101
102                 String JavaDoc characterEncoding = lookupCharacterEncoding(contentType);
103
104                 if (characterEncoding == null) {
105                     PortletSession session = portletRequest.getPortletSession(false);
106
107                     if (session != null) {
108                         characterEncoding = (String JavaDoc) session.getAttribute(ViewHandler.CHARACTER_ENCODING_KEY,
109                                                                           PortletSession.PORTLET_SCOPE);
110                     }
111
112                     if (characterEncoding != null) {
113                         actionRequest.setCharacterEncoding(characterEncoding);
114                     }
115                 }
116             } catch (Exception JavaDoc e)
117             {
118                 if (log.isWarnEnabled())
119                     log.warn("Failed to set character encoding " + e);
120             }
121         }
122     }
123     
124     private String JavaDoc lookupCharacterEncoding(String JavaDoc contentType)
125     {
126         String JavaDoc characterEncoding = null;
127
128         if (contentType != null)
129         {
130             int charsetFind = contentType.indexOf("charset=");
131             if (charsetFind != -1)
132             {
133                 if (charsetFind == 0)
134                 {
135                     //charset at beginning of Content-Type, curious
136
characterEncoding = contentType.substring(8);
137                 }
138                 else
139                 {
140                     char charBefore = contentType.charAt(charsetFind - 1);
141                     if (charBefore == ';' || Character.isWhitespace(charBefore))
142                     {
143                         //Correct charset after mime type
144
characterEncoding = contentType.substring(charsetFind + 8);
145                     }
146                 }
147                 if (log.isDebugEnabled()) log.debug("Incoming request has Content-Type header with character encoding " + characterEncoding);
148             }
149             else
150             {
151                 if (log.isDebugEnabled()) log.debug("Incoming request has Content-Type header without character encoding: " + contentType);
152             }
153         }
154         return characterEncoding;
155     }
156     
157     public void dispatch(String JavaDoc path) throws IOException JavaDoc
158     {
159         if (_isActionRequest)
160         { // dispatch only allowed for RenderRequest
161
String JavaDoc msg = "Can not call dispatch() during a portlet ActionRequest";
162             throw new IllegalStateException JavaDoc(msg);
163         }
164         
165         PortletRequestDispatcher requestDispatcher
166             = _portletContext.getRequestDispatcher(path); //TODO: figure out why I need named dispatcher
167
try
168         {
169             requestDispatcher.include((RenderRequest)_portletRequest,
170                                       (RenderResponse)_portletResponse);
171         }
172         catch (PortletException e)
173         {
174             if (e.getMessage() != null)
175             {
176                 throw new FacesException(e.getMessage(), e);
177             }
178             else
179             {
180                 throw new FacesException(e);
181             }
182         }
183     }
184     
185     public String JavaDoc encodeActionURL(String JavaDoc url) {
186         checkNull(url, "url");
187         return _portletResponse.encodeURL(url);
188     }
189     
190     public String JavaDoc encodeNamespace(String JavaDoc name) {
191         if (_isActionRequest)
192         { // encodeNamespace only allowed for RenderRequest
193
String JavaDoc msg = "Can not call encodeNamespace() during a portlet ActionRequest";
194             throw new IllegalStateException JavaDoc(msg);
195         }
196         
197         return ((RenderResponse)_portletResponse).getNamespace() + name;
198     }
199     
200     public String JavaDoc encodeResourceURL(String JavaDoc url) {
201         checkNull(url, "url");
202         return _portletResponse.encodeURL(url);
203     }
204     
205     public Map JavaDoc getApplicationMap() {
206         if (_applicationMap == null)
207         {
208             _applicationMap = new ApplicationMap(_portletContext);
209         }
210         return _applicationMap;
211     }
212     
213     public String JavaDoc getAuthType() {
214         return _portletRequest.getAuthType();
215     }
216     
217     public Object JavaDoc getContext() {
218         return _portletContext;
219     }
220     
221     public String JavaDoc getInitParameter(String JavaDoc name) {
222         return _portletContext.getInitParameter(name);
223     }
224     
225     public Map JavaDoc getInitParameterMap() {
226         if (_initParameterMap == null)
227         {
228             // We cache it as an attribute in PortletContext itself (is this circular reference a problem?)
229
if ((_initParameterMap = (Map JavaDoc) _portletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null)
230             {
231                 _initParameterMap = new InitParameterMap(_portletContext);
232                 _portletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, _initParameterMap);
233             }
234         }
235         return _initParameterMap;
236     }
237     
238     public String JavaDoc getRemoteUser() {
239         return _portletRequest.getRemoteUser();
240     }
241     
242     public Object JavaDoc getRequest() {
243         return _portletRequest;
244     }
245     
246     public String JavaDoc getRequestContextPath() {
247         return _portletRequest.getContextPath();
248     }
249     
250     public Map JavaDoc getRequestCookieMap() {
251         return EMPTY_UNMODIFIABLE_MAP;
252     }
253     
254     public Map JavaDoc getRequestHeaderMap() {
255         if (_requestHeaderMap == null)
256         {
257             _requestHeaderMap = new RequestHeaderMap(_portletRequest);
258         }
259         return _requestHeaderMap;
260     }
261     
262     public Map JavaDoc getRequestHeaderValuesMap() {
263         if (_requestHeaderValuesMap == null)
264         {
265             _requestHeaderValuesMap = new RequestHeaderValuesMap(_portletRequest);
266         }
267         return _requestHeaderValuesMap;
268     }
269     
270     public Locale JavaDoc getRequestLocale() {
271         return _portletRequest.getLocale();
272     }
273     
274     public Iterator JavaDoc getRequestLocales() {
275         return new EnumerationIterator(_portletRequest.getLocales());
276     }
277     
278     public Map JavaDoc getRequestMap() {
279         if (_requestMap == null)
280         {
281             _requestMap = new RequestMap(_portletRequest);
282         }
283         return _requestMap;
284     }
285     
286     public Map JavaDoc getRequestParameterMap() {
287         if (_requestParameterMap == null)
288         {
289             _requestParameterMap = new RequestParameterMap(_portletRequest);
290         }
291         return _requestParameterMap;
292     }
293     
294     public Iterator JavaDoc getRequestParameterNames() {
295         // TODO: find out why it is not done this way in ServletExternalContextImpl
296
return new EnumerationIterator(_portletRequest.getParameterNames());
297     }
298     
299     public Map JavaDoc getRequestParameterValuesMap() {
300         if (_requestParameterValuesMap == null)
301         {
302             _requestParameterValuesMap = new RequestParameterValuesMap(_portletRequest);
303         }
304         return _requestParameterValuesMap;
305     }
306     
307     public String JavaDoc getRequestPathInfo() {
308         return null; // must return null
309
}
310     
311     public String JavaDoc getRequestServletPath() {
312         return null; // must return null
313
}
314     
315     public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc {
316         checkNull(path, "path");
317         
318         return _portletContext.getResource(path);
319     }
320     
321     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
322         checkNull(path, "path");
323         
324         return _portletContext.getResourceAsStream(path);
325     }
326     
327     public Set JavaDoc getResourcePaths(String JavaDoc path) {
328         checkNull(path, "path");
329         return _portletContext.getResourcePaths(path);
330     }
331     
332     public Object JavaDoc getResponse() {
333         return _portletResponse;
334     }
335     
336     public Object JavaDoc getSession(boolean create) {
337         return _portletRequest.getPortletSession(create);
338     }
339     
340     public Map JavaDoc getSessionMap() {
341         if (_sessionMap == null)
342         {
343             _sessionMap = new SessionMap(_portletRequest);
344         }
345         return _sessionMap;
346     }
347     
348     public Principal JavaDoc getUserPrincipal() {
349         return _portletRequest.getUserPrincipal();
350     }
351     
352     public boolean isUserInRole(String JavaDoc role) {
353         checkNull(role, "role");
354         
355         return _portletRequest.isUserInRole(role);
356     }
357     
358     public void log(String JavaDoc message) {
359         checkNull(message, "message");
360         
361         _portletContext.log(message);
362     }
363     
364     public void log(String JavaDoc message, Throwable JavaDoc exception) {
365         checkNull(message, "message");
366         checkNull(exception, "exception");
367         
368         _portletContext.log(message, exception);
369     }
370     
371     public void redirect(String JavaDoc url) throws IOException JavaDoc {
372         if (_portletResponse instanceof ActionResponse)
373         {
374             ((ActionResponse)_portletResponse).sendRedirect(url);
375         }
376         else
377         {
378             throw new IllegalArgumentException JavaDoc("Only ActionResponse supported");
379         }
380     }
381     
382     public void release() {
383         _portletContext = null;
384         _portletRequest = null;
385         _portletResponse = null;
386         _applicationMap = null;
387         _sessionMap = null;
388         _requestMap = null;
389         _requestParameterMap = null;
390         _requestParameterValuesMap = null;
391         _requestHeaderMap = null;
392         _requestHeaderValuesMap = null;
393         _requestCookieMap = null;
394         _initParameterMap = null;
395     }
396     
397     private void checkNull(Object JavaDoc o, String JavaDoc param)
398     {
399         if (o == null)
400         {
401             throw new NullPointerException JavaDoc(param + " can not be null.");
402         }
403     }
404     
405 }
Popular Tags