KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > faces > context > ExternalContextImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlet.faces.context;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.lang.StringUtils;
9 import org.exoplatform.commons.map.*;
10 import org.exoplatform.commons.utils.EnumIterator;
11 import org.exoplatform.faces.context.PortletExternalContext;
12 import org.exoplatform.services.log.LogUtil;
13 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.*;
14 import org.exoplatform.services.portletcontainer.pci.ExoWindowID;
15
16 import javax.faces.context.ExternalContext;
17 import javax.servlet.ServletContext JavaDoc;
18 import javax.servlet.http.*;
19 import javax.portlet.*;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.*;
25 /**
26  * The JSR-127 specification chapter 6.1.1 outlines the requirements of the ExternalContext that Java Server Faces
27  * depends on. The ExternalContext implements a thin layer between two external resources and the portlet facility,
28  * so they can share resource data - namely a bridge.
29  *
30  * The FacesPortletContextFactoryImpl is the callee of this object.
31  *
32  * @author Ove Ranheim (oranheim@users.sourceforge.net)
33  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
34  * @since Nov 6, 2003 4:18:34 PM
35  *
36  */

37 public class ExternalContextImpl extends ExternalContext implements PortletExternalContext {
38   private PortletConfig portletConfig_ ;
39   private PortletContext portletContext_;
40   private ServletContext JavaDoc servletContext_;
41   private PortletRequestImp portletRequest_;
42   private PortletResponseImp portletResponse_;
43   private ResourceBundle applicationResource_ ;
44   private String JavaDoc requestPathInfo_ ;
45   private boolean renderPhase_ ;
46
47   private Map m_applicationMap;
48   private Map m_requestParameterMap;
49   private Map m_requestParameterValuesMap;
50   private Map m_requestHeaderMap;
51   private Map m_requestHeaderValuesMap;
52   private Map m_requestCookieMap;
53   private Map m_initParameterMap;
54
55
56   public ExternalContextImpl(Object JavaDoc config, Object JavaDoc request, Object JavaDoc response, boolean renderPhase) {
57     portletConfig_ = (PortletConfig) config ;
58     HttpServletResponse httpResponse = (HttpServletResponse) response ;
59     applicationResource_ = portletConfig_.getResourceBundle(httpResponse.getLocale()) ;
60     portletContext_ = portletConfig_.getPortletContext() ;
61     servletContext_ = ((PortletContextImpl)portletContext_).getWrappedServletContext() ;
62     portletRequest_ = (PortletRequestImp) request;
63     portletResponse_ = (PortletResponseImp) response;
64     renderPhase_ = renderPhase ;
65   }
66
67   protected void finalize() throws Throwable JavaDoc {
68     m_applicationMap = null;
69     m_requestParameterMap = null;
70     m_requestParameterValuesMap = null;
71     m_requestHeaderMap = null;
72     m_requestHeaderValuesMap = null;
73     m_requestCookieMap = null;
74     m_initParameterMap = null;
75     portletConfig_ = null;
76     portletContext_ = null;
77     portletRequest_ = null;
78     portletResponse_ = null;
79     requestPathInfo_ = null ;
80     super.finalize();
81   }
82
83   public Object JavaDoc getSession( boolean create ) {
84     return portletRequest_.getPortletSession( create );
85   }
86
87   public Object JavaDoc getContext() { return servletContext_; }
88
89   public Object JavaDoc getRequest() { return portletRequest_; }
90
91   public Object JavaDoc getResponse() { return portletResponse_; }
92     
93   public PortletConfig getConfig() { return portletConfig_ ; }
94
95   public ResourceBundle getApplicationResourceBundle() { return applicationResource_ ; }
96
97   public String JavaDoc getInitParameter( String JavaDoc s ) { return portletContext_.getInitParameter( s ); }
98
99   public Map getInitParameterMap() {
100     if (m_initParameterMap == null) {
101       m_initParameterMap = new InitParameterMap( portletContext_ );
102     }
103     return m_initParameterMap;
104   }
105
106
107   public Map getApplicationMap() {
108     if (m_applicationMap == null) {
109       m_applicationMap = new ApplicationMap( portletContext_ );
110     }
111     return m_applicationMap;
112   }
113
114   public Map getSessionMap() {
115     return (PortletSessionImp) portletRequest_.getPortletSession();
116   }
117
118   public Map getRequestMap() { return portletRequest_ ; }
119
120   public Map getRequestParameterMap() {
121     if (m_requestParameterMap == null) {
122       m_requestParameterMap = new RequestParameterMap( portletRequest_ );
123     }
124     return m_requestParameterMap;
125   }
126
127   public Map getRequestParameterValuesMap() {
128     if (m_requestParameterValuesMap == null) {
129       m_requestParameterValuesMap = new RequestParameterValuesMap( portletRequest_ );
130     }
131     return m_requestParameterValuesMap;
132   }
133
134   public Iterator getRequestParameterNames() {
135     return new EnumIterator( portletRequest_.getParameterNames() );
136   }
137
138   public Map getRequestHeaderMap() { return null ; }
139
140   public Map getRequestHeaderValuesMap() { return null ; }
141
142   public Map getRequestCookieMap() { return null ; }
143
144   public Locale getRequestLocale() { return portletRequest_.getLocale(); }
145   public Iterator getRequestLocales() { return null ; }
146
147   public String JavaDoc getRequestContextPath() {
148     String JavaDoc path = portletRequest_.getContextPath();
149     return path;
150   }
151
152   public String JavaDoc getRequestPathInfo() {
153     if (requestPathInfo_ == null) {
154       requestPathInfo_ =
155         (String JavaDoc) portletRequest_.getAttribute("javax.servlet.include.path_info") ;
156     }
157     return requestPathInfo_ ;
158   }
159
160
161   public Cookie[] getRequestCookies() { return null ; }
162
163   public Set getResourcePaths( String JavaDoc s ) { return portletContext_.getResourcePaths( s ); }
164   public InputStream JavaDoc getResourceAsStream( String JavaDoc s ) {
165     return portletContext_.getResourceAsStream( s );
166   }
167   
168   public URL JavaDoc getResource( String JavaDoc s ) throws MalformedURLException JavaDoc {
169     return portletContext_.getResource( s );
170   }
171
172   public String JavaDoc encodeActionURL( String JavaDoc s ) {
173     if(!renderPhase_)
174       throw new IllegalStateException JavaDoc("You cannot use this in processAction phase") ;
175     RenderResponse renderResponse = (RenderResponse) portletResponse_ ;
176     if(s == null || s.length() == 0) {
177       return renderResponse.createActionURL().toString() ;
178     } else if ("http://".startsWith(s)) {
179       return s ;
180     } else {
181       int question = s.indexOf('?') ;
182       if (question > 0) {
183         s = StringUtils.replace(s, "?", "&" , 1) ;
184       }
185       s = "&.view-id=" + s ;
186       return renderResponse.createActionURL().toString() + s ;
187     }
188   }
189
190   public String JavaDoc encodeResourceURL( String JavaDoc s ) { return s; }
191
192   public String JavaDoc encodeURL( String JavaDoc s ) { return s ; }
193
194   public String JavaDoc encodeNamespace( String JavaDoc s ) { return s; }
195
196   public void dispatch( String JavaDoc requestURI ) throws IOException JavaDoc {
197     if (!(portletResponse_ instanceof RenderResponse)) throw new UnsupportedOperationException JavaDoc();
198     PortletRequestDispatcher prequestDispatcher = portletContext_.getRequestDispatcher( requestURI );
199     try {
200       prequestDispatcher.include( (RenderRequest) portletRequest_, (RenderResponse) portletResponse_ );
201     } catch (PortletException e) {
202       getLog().error("error: " , e);
203     }
204   }
205
206   public void redirect( String JavaDoc sI ) throws IOException JavaDoc {
207   }
208   
209   public void log(String JavaDoc message) { portletContext_.log(message) ; }
210   
211   public void log(String JavaDoc message, Throwable JavaDoc throwable) {
212     portletContext_.log(message, throwable) ;
213   }
214   
215   public String JavaDoc getRequestServletPath() {
216     throw new UnsupportedOperationException JavaDoc();
217   }
218   
219   public java.lang.String JavaDoc getAuthType() { return portletRequest_.getAuthType(); }
220   
221   public java.lang.String JavaDoc getRemoteUser() { return portletRequest_.getRemoteUser() ; }
222   
223   final public java.security.Principal JavaDoc getUserPrincipal() {
224     return portletRequest_.getUserPrincipal() ;
225   }
226
227   final public boolean isUserInRole(java.lang.String JavaDoc role) {
228     return portletRequest_.isUserInRole(role) ;
229   }
230   
231   public ExoWindowID getWindowID() {
232     return (ExoWindowID) portletRequest_.getPortletWindowInternal().getWindowID() ;
233   }
234   
235   private Log getLog() {
236     return LogUtil.getLog("org.exoplatform.portlet.faces.faces");
237    }
238 }
239
Popular Tags