1 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 ; 18 import javax.servlet.http.*; 19 import javax.portlet.*; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.*; 25 37 public class ExternalContextImpl extends ExternalContext implements PortletExternalContext { 38 private PortletConfig portletConfig_ ; 39 private PortletContext portletContext_; 40 private ServletContext servletContext_; 41 private PortletRequestImp portletRequest_; 42 private PortletResponseImp portletResponse_; 43 private ResourceBundle applicationResource_ ; 44 private String 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 config, Object request, Object 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 { 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 getSession( boolean create ) { 84 return portletRequest_.getPortletSession( create ); 85 } 86 87 public Object getContext() { return servletContext_; } 88 89 public Object getRequest() { return portletRequest_; } 90 91 public Object getResponse() { return portletResponse_; } 92 93 public PortletConfig getConfig() { return portletConfig_ ; } 94 95 public ResourceBundle getApplicationResourceBundle() { return applicationResource_ ; } 96 97 public String getInitParameter( String 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 getRequestContextPath() { 148 String path = portletRequest_.getContextPath(); 149 return path; 150 } 151 152 public String getRequestPathInfo() { 153 if (requestPathInfo_ == null) { 154 requestPathInfo_ = 155 (String ) 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 s ) { return portletContext_.getResourcePaths( s ); } 164 public InputStream getResourceAsStream( String s ) { 165 return portletContext_.getResourceAsStream( s ); 166 } 167 168 public URL getResource( String s ) throws MalformedURLException { 169 return portletContext_.getResource( s ); 170 } 171 172 public String encodeActionURL( String s ) { 173 if(!renderPhase_) 174 throw new IllegalStateException ("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 encodeResourceURL( String s ) { return s; } 191 192 public String encodeURL( String s ) { return s ; } 193 194 public String encodeNamespace( String s ) { return s; } 195 196 public void dispatch( String requestURI ) throws IOException { 197 if (!(portletResponse_ instanceof RenderResponse)) throw new UnsupportedOperationException (); 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 sI ) throws IOException { 207 } 208 209 public void log(String message) { portletContext_.log(message) ; } 210 211 public void log(String message, Throwable throwable) { 212 portletContext_.log(message, throwable) ; 213 } 214 215 public String getRequestServletPath() { 216 throw new UnsupportedOperationException (); 217 } 218 219 public java.lang.String getAuthType() { return portletRequest_.getAuthType(); } 220 221 public java.lang.String getRemoteUser() { return portletRequest_.getRemoteUser() ; } 222 223 final public java.security.Principal getUserPrincipal() { 224 return portletRequest_.getUserPrincipal() ; 225 } 226 227 final public boolean isUserInRole(java.lang.String 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 |