1 16 package org.apache.myfaces.context.portlet; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.security.Principal ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 import java.util.Set ; 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 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 INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName(); 65 private static final Map EMPTY_UNMODIFIABLE_MAP = Collections.unmodifiableMap(new HashMap (0)); 66 67 PortletContext _portletContext; 68 PortletRequest _portletRequest; 69 PortletResponse _portletResponse; 70 71 private Map _applicationMap; 72 private Map _sessionMap; 73 private Map _requestMap; 74 private Map _requestParameterMap; 75 private Map _requestParameterValuesMap; 76 private Map _requestHeaderMap; 77 private Map _requestHeaderValuesMap; 78 private Map _requestCookieMap; 79 private Map _initParameterMap; 80 private boolean _isActionRequest; 81 82 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 99 { 100 String contentType = portletRequest.getProperty("Content-Type"); 101 102 String characterEncoding = lookupCharacterEncoding(contentType); 103 104 if (characterEncoding == null) { 105 PortletSession session = portletRequest.getPortletSession(false); 106 107 if (session != null) { 108 characterEncoding = (String ) 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 e) 117 { 118 if (log.isWarnEnabled()) 119 log.warn("Failed to set character encoding " + e); 120 } 121 } 122 } 123 124 private String lookupCharacterEncoding(String contentType) 125 { 126 String 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 characterEncoding = contentType.substring(8); 137 } 138 else 139 { 140 char charBefore = contentType.charAt(charsetFind - 1); 141 if (charBefore == ';' || Character.isWhitespace(charBefore)) 142 { 143 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 path) throws IOException 158 { 159 if (_isActionRequest) 160 { String msg = "Can not call dispatch() during a portlet ActionRequest"; 162 throw new IllegalStateException (msg); 163 } 164 165 PortletRequestDispatcher requestDispatcher 166 = _portletContext.getRequestDispatcher(path); 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 encodeActionURL(String url) { 186 checkNull(url, "url"); 187 return _portletResponse.encodeURL(url); 188 } 189 190 public String encodeNamespace(String name) { 191 if (_isActionRequest) 192 { String msg = "Can not call encodeNamespace() during a portlet ActionRequest"; 194 throw new IllegalStateException (msg); 195 } 196 197 return ((RenderResponse)_portletResponse).getNamespace() + name; 198 } 199 200 public String encodeResourceURL(String url) { 201 checkNull(url, "url"); 202 return _portletResponse.encodeURL(url); 203 } 204 205 public Map getApplicationMap() { 206 if (_applicationMap == null) 207 { 208 _applicationMap = new ApplicationMap(_portletContext); 209 } 210 return _applicationMap; 211 } 212 213 public String getAuthType() { 214 return _portletRequest.getAuthType(); 215 } 216 217 public Object getContext() { 218 return _portletContext; 219 } 220 221 public String getInitParameter(String name) { 222 return _portletContext.getInitParameter(name); 223 } 224 225 public Map getInitParameterMap() { 226 if (_initParameterMap == null) 227 { 228 if ((_initParameterMap = (Map ) _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 getRemoteUser() { 239 return _portletRequest.getRemoteUser(); 240 } 241 242 public Object getRequest() { 243 return _portletRequest; 244 } 245 246 public String getRequestContextPath() { 247 return _portletRequest.getContextPath(); 248 } 249 250 public Map getRequestCookieMap() { 251 return EMPTY_UNMODIFIABLE_MAP; 252 } 253 254 public Map getRequestHeaderMap() { 255 if (_requestHeaderMap == null) 256 { 257 _requestHeaderMap = new RequestHeaderMap(_portletRequest); 258 } 259 return _requestHeaderMap; 260 } 261 262 public Map getRequestHeaderValuesMap() { 263 if (_requestHeaderValuesMap == null) 264 { 265 _requestHeaderValuesMap = new RequestHeaderValuesMap(_portletRequest); 266 } 267 return _requestHeaderValuesMap; 268 } 269 270 public Locale getRequestLocale() { 271 return _portletRequest.getLocale(); 272 } 273 274 public Iterator getRequestLocales() { 275 return new EnumerationIterator(_portletRequest.getLocales()); 276 } 277 278 public Map getRequestMap() { 279 if (_requestMap == null) 280 { 281 _requestMap = new RequestMap(_portletRequest); 282 } 283 return _requestMap; 284 } 285 286 public Map getRequestParameterMap() { 287 if (_requestParameterMap == null) 288 { 289 _requestParameterMap = new RequestParameterMap(_portletRequest); 290 } 291 return _requestParameterMap; 292 } 293 294 public Iterator getRequestParameterNames() { 295 return new EnumerationIterator(_portletRequest.getParameterNames()); 297 } 298 299 public Map getRequestParameterValuesMap() { 300 if (_requestParameterValuesMap == null) 301 { 302 _requestParameterValuesMap = new RequestParameterValuesMap(_portletRequest); 303 } 304 return _requestParameterValuesMap; 305 } 306 307 public String getRequestPathInfo() { 308 return null; } 310 311 public String getRequestServletPath() { 312 return null; } 314 315 public URL getResource(String path) throws MalformedURLException { 316 checkNull(path, "path"); 317 318 return _portletContext.getResource(path); 319 } 320 321 public InputStream getResourceAsStream(String path) { 322 checkNull(path, "path"); 323 324 return _portletContext.getResourceAsStream(path); 325 } 326 327 public Set getResourcePaths(String path) { 328 checkNull(path, "path"); 329 return _portletContext.getResourcePaths(path); 330 } 331 332 public Object getResponse() { 333 return _portletResponse; 334 } 335 336 public Object getSession(boolean create) { 337 return _portletRequest.getPortletSession(create); 338 } 339 340 public Map getSessionMap() { 341 if (_sessionMap == null) 342 { 343 _sessionMap = new SessionMap(_portletRequest); 344 } 345 return _sessionMap; 346 } 347 348 public Principal getUserPrincipal() { 349 return _portletRequest.getUserPrincipal(); 350 } 351 352 public boolean isUserInRole(String role) { 353 checkNull(role, "role"); 354 355 return _portletRequest.isUserInRole(role); 356 } 357 358 public void log(String message) { 359 checkNull(message, "message"); 360 361 _portletContext.log(message); 362 } 363 364 public void log(String message, Throwable exception) { 365 checkNull(message, "message"); 366 checkNull(exception, "exception"); 367 368 _portletContext.log(message, exception); 369 } 370 371 public void redirect(String url) throws IOException { 372 if (_portletResponse instanceof ActionResponse) 373 { 374 ((ActionResponse)_portletResponse).sendRedirect(url); 375 } 376 else 377 { 378 throw new IllegalArgumentException ("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 o, String param) 398 { 399 if (o == null) 400 { 401 throw new NullPointerException (param + " can not be null."); 402 } 403 } 404 405 } | Popular Tags |