1 package com.icesoft.faces.webapp.http.servlet; 2 3 import com.icesoft.faces.context.BridgeExternalContext; 4 import com.icesoft.faces.util.EnumerationIterator; 5 import com.icesoft.faces.webapp.command.CommandQueue; 6 import com.icesoft.faces.webapp.command.Redirect; 7 import com.icesoft.faces.webapp.command.SetCookie; 8 import com.icesoft.faces.webapp.http.common.Configuration; 9 import com.icesoft.faces.webapp.xmlhttp.PersistentFacesCommonlet; 10 import com.icesoft.jasper.Constants; 11 import com.icesoft.util.SeamUtilities; 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 import javax.faces.FacesException; 16 import javax.faces.context.FacesContext; 17 import javax.faces.render.ResponseStateManager; 18 import javax.servlet.ServletContext ; 19 import javax.servlet.ServletException ; 20 import javax.servlet.http.Cookie ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import javax.servlet.http.HttpSession ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStreamWriter ; 27 import java.io.Writer ; 28 import java.lang.reflect.Field ; 29 import java.net.MalformedURLException ; 30 import java.net.URI ; 31 import java.net.URL ; 32 import java.util.Collections ; 33 import java.util.Enumeration ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Locale ; 37 import java.util.Map ; 38 import java.util.Set ; 39 40 public class ServletExternalContext extends BridgeExternalContext { 42 43 private static Log log = LogFactory.getLog(ServletExternalContext.class); 44 45 private static String postBackKey; 46 47 static { 48 try { 52 Field field = ResponseStateManager.class.getField("VIEW_STATE_PARAM"); 53 if (null != field) { 54 postBackKey = (String ) field.get(ResponseStateManager.class); 55 } 56 } catch (Exception e) { 57 } 58 } 59 60 private String viewIdentifier; 61 private ServletContext context; 62 private HttpServletRequest request; 63 private HttpServletResponse response; 64 private HttpSession session; 65 private Map applicationMap; 66 private Map sessionMap; 67 private Map requestParameterMap; 68 private Map requestParameterValuesMap; 69 private Map initParameterMap; 70 private Map requestMap; 71 private Map requestCookieMap; 72 private Map responseCookieMap; 73 private CommandQueue commandQueue; 74 private Redirector redirector; 75 private CookieTransporter cookieTransporter; 76 private boolean standardScope; 77 78 public ServletExternalContext(String viewIdentifier, HttpServletRequest request, HttpServletResponse response, CommandQueue commandQueue, Configuration configuration) { 79 this.viewIdentifier = viewIdentifier; 80 this.request = request; 81 this.response = response; 82 this.commandQueue = commandQueue; 83 this.session = this.request.getSession(); 84 this.context = this.session.getServletContext(); 85 this.standardScope = configuration.getAttributeAsBoolean("standardRequestScope", false); 86 this.applicationMap = new ServletApplicationMap(this.context); 87 this.sessionMap = new ServletSessionMap(this.session); 88 this.requestMap = new CopyingRequestAttributesMap(this.request); 89 this.requestCookieMap = new HashMap (); 90 this.initParameterMap = new HashMap (); 91 Enumeration names = this.context.getInitParameterNames(); 92 while (names.hasMoreElements()) { 93 String key = (String ) names.nextElement(); 94 initParameterMap.put(key, this.context.getInitParameter(key)); 95 } 96 97 this.update(request, response); 98 this.insertNewViewrootToken(); 99 } 100 101 public Object getSession(boolean create) { 102 return session; 103 } 104 105 public Object getContext() { 106 return context; 107 } 108 109 public Object getRequest() { 110 return request; 111 } 112 113 public Object getResponse() { 114 return response; 115 } 116 117 public Map getApplicationMap() { 118 return applicationMap; 119 } 120 121 public Map getSessionMap() { 122 return sessionMap; 123 } 124 125 public Map getApplicationSessionMap() { 126 return sessionMap; 127 } 128 129 public Map getRequestMap() { 130 return requestMap; 131 } 132 133 public void update(HttpServletRequest request, HttpServletResponse response) { 134 boolean persistSeamKey = false; 136 if (requestParameterMap != null) { 137 persistSeamKey = (requestParameterMap.containsKey(PersistentFacesCommonlet.SEAM_LIFECYCLE_SHORTCUT)); 138 } 139 140 requestParameterMap = new HashMap (); 141 requestParameterValuesMap = new HashMap (); 142 insertPostbackKey(); 143 Enumeration parameterNames = request.getParameterNames(); 144 while (parameterNames.hasMoreElements()) { 145 String name = (String ) parameterNames.nextElement(); 146 Object value = request.getParameter(name); 147 requestParameterMap.put(name, value); 148 requestParameterValuesMap.put(name, request.getParameterValues(name)); 149 } 150 151 if (persistSeamKey) { 152 requestParameterMap.put( 153 PersistentFacesCommonlet.SEAM_LIFECYCLE_SHORTCUT, 154 Boolean.TRUE); 155 } 156 157 requestCookieMap = new HashMap (); 158 Cookie[] cookies = request.getCookies(); 159 if (cookies != null) { 160 for (int i = 0; i < cookies.length; i++) { 161 Cookie cookie = cookies[i]; 162 requestCookieMap.put(cookie.getName(), cookie); 163 } 164 } 165 responseCookieMap = new HashMap (); 166 167 this.response = response; 168 } 169 170 public void updateOnReload(HttpServletRequest request, HttpServletResponse response) { 171 Map previousRequestMap = this.requestMap; 172 this.requestMap = new CopyingRequestAttributesMap(request); 173 this.requestMap.putAll(previousRequestMap); 175 this.request = request; 176 this.update(request, response); 177 } 178 179 public Map getRequestParameterMap() { 180 return requestParameterMap; 181 } 182 183 public Map getRequestParameterValuesMap() { 184 return requestParameterValuesMap; 185 } 186 187 public Iterator getRequestParameterNames() { 188 return requestParameterMap.keySet().iterator(); 189 } 190 191 public Map getRequestHeaderMap() { 193 return Collections.EMPTY_MAP; 194 } 195 196 public Map getRequestHeaderValuesMap() { 198 return Collections.EMPTY_MAP; 199 } 200 201 public Map getRequestCookieMap() { 202 return requestCookieMap; 203 } 204 205 public Locale getRequestLocale() { 206 return request.getLocale(); 207 } 208 209 public Iterator getRequestLocales() { 210 return new EnumerationIterator(request.getLocales()); 211 } 212 213 private String requestPathInfo; 214 215 public void setRequestPathInfo(String viewId) { 216 requestPathInfo = viewId; 217 } 218 219 public String getRequestPathInfo() { 220 221 if (requestPathInfo != null && requestPathInfo.trim().length() > 0) { 222 return requestPathInfo; 223 } 224 225 if (requestPathInfo == null) { 228 requestPathInfo = request.getPathInfo(); 229 } 230 231 requestPathInfo = convertEmptyStringToNull(requestPathInfo); 236 return requestPathInfo; 237 } 238 239 247 private static String convertEmptyStringToNull(String val) { 248 if (val == null) { 249 return val; 250 } 251 252 if (val.trim().length() == 0) { 253 return null; 254 } 255 256 return val; 257 } 258 259 public String getRequestURI() { 260 261 String uri = (String ) request.getAttribute(Constants.INC_REQUEST_URI); 265 if (uri == null || uri.trim().length() > 0) { 266 uri = request.getRequestURI(); 267 } 268 269 return uri; 270 } 271 272 public String getRequestContextPath() { 273 return request.getContextPath(); 274 } 275 276 private String requestServletPath; 277 278 public void setRequestServletPath(String path) { 279 requestServletPath = path; 280 } 281 282 public String getRequestServletPath() { 283 return null == requestServletPath ? request.getServletPath() : requestServletPath; 284 } 285 286 public String getInitParameter(String name) { 287 return context.getInitParameter(name); 288 } 289 290 public Map getInitParameterMap() { 291 return initParameterMap; 292 } 293 294 public Set getResourcePaths(String path) { 295 return context.getResourcePaths(path); 296 } 297 298 public URL getResource(String path) throws MalformedURLException { 299 return context.getResource(path); 300 } 301 302 public InputStream getResourceAsStream(String path) { 303 return context.getResourceAsStream(path); 304 } 305 306 public String encodeActionURL(String url) { 307 return url; 308 } 309 310 public String encodeResourceURL(String url) { 311 try { 312 return response.encodeURL(url); 313 } catch (Exception e) { 314 return url; 315 } 316 } 317 318 public String encodeNamespace(String name) { 319 320 String ns = (String ) request.getAttribute(Constants.NAMESPACE_KEY); 325 326 if (ns != null) { 327 return ns + name; 328 } 329 330 return name; 331 } 332 333 public void dispatch(String path) throws IOException , FacesException { 334 try { 335 request.getRequestDispatcher(path).forward(request, response); 336 } catch (ServletException se) { 337 throw new FacesException(se); 338 } 339 } 340 341 public void redirect(String requestURI) throws IOException { 342 URI uri = URI.create(SeamUtilities.encodeSeamConversationId(requestURI, viewIdentifier)); 343 String query = uri.getQuery(); 344 if (query == null) { 345 redirector.redirect(uri + "?rvn=" + viewIdentifier); 346 } else if (query.matches(".*rvn=.*")) { 347 redirector.redirect(uri.toString()); 348 } else { 349 redirector.redirect(uri + "&rvn=" + viewIdentifier); 350 } 351 FacesContext.getCurrentInstance().responseComplete(); 352 } 353 354 public void log(String message) { 355 context.log(message); 356 } 357 358 public void log(String message, Throwable throwable) { 359 context.log(message, throwable); 360 } 361 362 public String getAuthType() { 363 return request.getAuthType(); 364 } 365 366 public String getRemoteUser() { 367 return request.getRemoteUser(); 368 } 369 370 public java.security.Principal getUserPrincipal() { 371 return request.getUserPrincipal(); 372 } 373 374 public boolean isUserInRole(String role) { 375 return request.isUserInRole(role); 376 } 377 378 public void addCookie(Cookie cookie) { 379 responseCookieMap.put(cookie.getName(), cookie); 380 cookieTransporter.send(cookie); 381 } 382 383 public Map getResponseCookieMap() { 384 return responseCookieMap; 385 } 386 387 public Map collectBundles() { 391 Map result = new HashMap (); 392 Iterator entries = requestMap.entrySet().iterator(); 393 while (entries.hasNext()) { 394 Map.Entry entry = (Map.Entry ) entries.next(); 395 Object value = entry.getValue(); 396 if (value != null) { 397 String className = value.getClass().getName(); 398 if ((className.indexOf("LoadBundleTag") > 0) || (className.indexOf("BundleMap") > 0)) { result.put(entry.getKey(), value); 401 } 402 } 403 } 404 405 return result; 406 } 407 408 public Writer getWriter(String encoding) throws IOException { 409 try { 410 return new OutputStreamWriter (response.getOutputStream(), encoding); 411 } catch (IllegalStateException e) { 412 return response.getWriter(); 414 } 415 } 416 417 430 private void insertNewViewrootToken() { 431 if (SeamUtilities.isSeamEnvironment()) { 432 requestParameterMap.put( 433 PersistentFacesCommonlet.SEAM_LIFECYCLE_SHORTCUT, 434 Boolean.TRUE); 435 } 436 } 437 438 public void switchToNormalMode() { 439 redirector = new Redirector() { 440 public void redirect(String uri) { 441 try { 442 response.sendRedirect(uri); 443 } catch (IOException e) { 444 throw new RuntimeException (e); 445 } 446 } 447 }; 448 449 cookieTransporter = new CookieTransporter() { 450 public void send(Cookie cookie) { 451 response.addCookie(cookie); 452 } 453 }; 454 } 455 456 public void switchToPushMode() { 457 redirector = new Redirector() { 458 public void redirect(String uri) { 459 commandQueue.put(new Redirect(uri)); 460 } 461 }; 462 463 cookieTransporter = new CookieTransporter() { 464 public void send(Cookie cookie) { 465 commandQueue.put(new SetCookie(cookie)); 466 } 467 }; 468 469 resetRequestMap(); 470 } 471 472 476 public void resetRequestMap() { 477 if (standardScope) { 478 requestMap.clear(); 479 } 480 } 481 482 public void injectBundles(Map bundles) { 483 requestMap.putAll(bundles); 484 } 485 486 public void dispose() { 487 requestMap.clear(); 488 commandQueue.take(); 489 } 490 491 private interface Redirector { 492 void redirect(String uri); 493 } 494 495 private interface CookieTransporter { 496 void send(Cookie cookie); 497 } 498 499 private void insertPostbackKey() { 500 if (null != postBackKey) { 501 requestParameterMap.put(postBackKey, "not reload"); 502 requestParameterValuesMap.put(postBackKey, new String []{"not reload"}); 503 } 504 } 505 } 506 | Popular Tags |