1 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 ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.security.Principal ; 32 import java.util.*; 33 import java.lang.reflect.Method ; 34 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.commons.logging.Log; 37 import org.apache.myfaces.context.ReleaseableExternalContext; 38 39 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 INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName(); 55 56 private ServletContext _servletContext; 57 private ServletRequest _servletRequest; 58 private ServletResponse _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 _requestServletPath; 70 private String _requestPathInfo; 71 private static Method setCharacterEncodingMethod = null; 72 73 static { 74 try { 75 setCharacterEncodingMethod = ServletRequest .class.getMethod("setCharacterEncoding", new Class []{String .class}); 76 } catch (Exception 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 servletRequest, 84 ServletResponse 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 ); 100 if (_isHttpServletRequest) 101 { 102 HttpServletRequest httpServletRequest = (HttpServletRequest )servletRequest; 105 106 _requestServletPath = httpServletRequest.getServletPath(); 107 _requestPathInfo = httpServletRequest.getPathInfo(); 108 109 try 112 { 113 if (setCharacterEncodingMethod != null) { 114 String contentType = httpServletRequest.getHeader("Content-Type"); 115 116 String characterEncoding = lookupCharacterEncoding(contentType); 117 118 if (characterEncoding == null) { 119 HttpSession session = httpServletRequest.getSession(false); 120 121 if (session != null) { 122 characterEncoding = (String ) session.getAttribute(ViewHandler.CHARACTER_ENCODING_KEY); 123 } 124 125 if (characterEncoding != null) { 126 setCharacterEncodingMethod.invoke(servletRequest, new Object []{characterEncoding}); 127 } 128 } 129 } 130 } catch (Exception e) 131 { 132 if (log.isWarnEnabled()) 133 log.warn("Failed to set character encoding " + e); 134 } 135 } 136 } 137 138 139 private String lookupCharacterEncoding(String contentType) 140 { 141 String 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 characterEncoding = contentType.substring(8); 152 } 153 else 154 { 155 char charBefore = contentType.charAt(charsetFind - 1); 156 if (charBefore == ';' || Character.isWhitespace(charBefore)) 157 { 158 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 getSession(boolean create) 191 { 192 if (!_isHttpServletRequest) 193 { 194 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 195 } 196 return ((HttpServletRequest )_servletRequest).getSession(create); 197 } 198 199 public Object getContext() 200 { 201 return _servletContext; 202 } 203 204 public Object getRequest() 205 { 206 return _servletRequest; 207 } 208 209 public Object 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 ("Only HttpServletRequest supported"); 230 } 231 _sessionMap = new SessionMap((HttpServletRequest ) _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 next() { 273 return enumer.nextElement(); 274 } 275 276 public void remove() { 277 throw new UnsupportedOperationException (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 ("Only HttpServletRequest supported"); 290 } 291 _requestHeaderMap = new RequestHeaderMap((HttpServletRequest )_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 ("Only HttpServletRequest supported"); 303 } 304 _requestHeaderValuesMap = new RequestHeaderValuesMap((HttpServletRequest )_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 ("Only HttpServletRequest supported"); 316 } 317 _requestCookieMap = new CookieMap((HttpServletRequest )_servletRequest); 318 } 319 return _requestCookieMap; 320 } 321 322 public Locale getRequestLocale() 323 { 324 return _servletRequest.getLocale(); 325 } 326 327 public String getRequestPathInfo() 328 { 329 if (!_isHttpServletRequest) 330 { 331 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 332 } 333 return _requestPathInfo; 336 } 337 338 public String getRequestContextPath() 339 { 340 if (!_isHttpServletRequest) 341 { 342 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 343 } 344 return ((HttpServletRequest )_servletRequest).getContextPath(); 345 } 346 347 public String getInitParameter(String s) 348 { 349 return _servletContext.getInitParameter(s); 350 } 351 352 public Map getInitParameterMap() 353 { 354 if (_initParameterMap == null) 355 { 356 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 s) 367 { 368 return _servletContext.getResourcePaths(s); 369 } 370 371 public InputStream getResourceAsStream(String s) 372 { 373 return _servletContext.getResourceAsStream(s); 374 } 375 376 public String encodeActionURL(String s) 377 { 378 if (!_isHttpServletRequest) 379 { 380 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 381 } 382 return ((HttpServletResponse )_servletResponse).encodeURL(s); 383 } 384 385 public String encodeResourceURL(String s) 386 { 387 if (!_isHttpServletRequest) 388 { 389 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 390 } 391 return ((HttpServletResponse )_servletResponse).encodeURL(s); 392 } 393 394 public String encodeNamespace(String s) 395 { 396 return s; 397 } 398 399 public void dispatch(String requestURI) throws IOException , 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 getRequestServletPath() 421 { 422 if (!_isHttpServletRequest) 423 { 424 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 425 } 426 return _requestServletPath; 429 } 430 431 public String getAuthType() 432 { 433 if (!_isHttpServletRequest) 434 { 435 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 436 } 437 return ((HttpServletRequest )_servletRequest).getAuthType(); 438 } 439 440 public String getRemoteUser() 441 { 442 if (!_isHttpServletRequest) 443 { 444 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 445 } 446 return ((HttpServletRequest )_servletRequest).getRemoteUser(); 447 } 448 449 public boolean isUserInRole(String role) 450 { 451 if (!_isHttpServletRequest) 452 { 453 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 454 } 455 return ((HttpServletRequest )_servletRequest).isUserInRole(role); 456 } 457 458 public Principal getUserPrincipal() 459 { 460 if (!_isHttpServletRequest) 461 { 462 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 463 } 464 return ((HttpServletRequest )_servletRequest).getUserPrincipal(); 465 } 466 467 public void log(String message) { 468 _servletContext.log(message); 469 } 470 471 public void log(String message, Throwable t) { 472 _servletContext.log(message, t); 473 } 474 475 public void redirect(String url) throws IOException 476 { 477 if (_servletResponse instanceof HttpServletResponse ) 478 { 479 ((HttpServletResponse )_servletResponse).sendRedirect(url); 480 } 481 else 482 { 483 throw new IllegalArgumentException ("Only HttpServletResponse supported"); 484 } 485 } 486 487 public Iterator getRequestLocales() 488 { 489 if (!_isHttpServletRequest) 490 { 491 throw new IllegalArgumentException ("Only HttpServletRequest supported"); 492 } 493 return new EnumerationIterator(((HttpServletRequest )_servletRequest).getLocales()); 494 } 495 496 public URL getResource(String s) throws MalformedURLException 497 { 498 return _servletContext.getResource(s); 499 } 500 } 501 | Popular Tags |