1 18 package org.apache.beehive.netui.pageflow.scoping.internal; 19 20 import org.apache.beehive.netui.pageflow.scoping.ScopedRequest; 21 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpSession ; 25 import javax.servlet.http.HttpServletRequestWrapper ; 26 import javax.servlet.RequestDispatcher ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.ServletContext ; 29 import java.util.Enumeration ; 30 import java.util.Map ; 31 import java.util.Collections ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.HashMap ; 35 import java.util.List ; 36 import java.util.Set ; 37 import java.util.HashSet ; 38 import java.io.UnsupportedEncodingException ; 39 40 import org.apache.log4j.Logger; 41 import org.apache.commons.codec.net.URLCodec; 42 import org.apache.commons.codec.DecoderException; 43 44 45 52 public class ScopedRequestImpl 53 extends HttpServletRequestWrapper 54 implements ScopedRequest 55 { 56 private String _requestURI; 57 private String _servletPath; 58 private String _forwardedURI; 59 private ScopedAttributeContainer _scopedContainer; 60 private List _listenScopes; 61 private String _overridePathInfo = null; 62 private boolean _isActiveRequest = false; 63 private boolean _seeOuterRequestAttributes = false; 64 private Set _visibleOuterRequestAttrs; 65 private Map _additionalParameters; 66 67 68 static final String ATTR_PREFIX = "_netui:"; 69 private static final String OUR_SESSION_ATTR = ATTR_PREFIX + "scopedSession"; 70 private static final String STORED_ATTRS_ATTR = ATTR_PREFIX + "storedAttrs"; 71 72 private static final Logger _log = Logger.getLogger( ScopedRequestImpl.class ); 73 private static final URLCodec URL_CODEC = new URLCodec(); 74 75 76 public ScopedRequestImpl( HttpServletRequest req, String overrideRequestURI, Object scopeKey, 77 ServletContext servletContext, boolean seeOuterRequestAttributes ) 78 { 79 super( req ); 80 81 _scopedContainer = new ScopedAttributeContainer( scopeKey ); 82 setRequestURI( overrideRequestURI ); 83 _seeOuterRequestAttributes = seeOuterRequestAttributes; 84 85 if ( ! seeOuterRequestAttributes ) _visibleOuterRequestAttrs = new HashSet (); 86 } 87 88 91 public ScopedRequestImpl( HttpServletRequest req, String overrideRequestURI, Object scopeKey, 92 ServletContext context ) 93 { 94 this( req, overrideRequestURI, scopeKey, context, false ); 95 } 96 97 public String getRequestedSessionId() 98 { 99 return super.getRequestedSessionId(); 100 } 101 102 public String getRequestURI() 103 { 104 return _requestURI; 105 } 106 107 public void setRequestURI( String uri ) 108 { 109 _requestURI = uri; 110 111 if ( uri == null ) 112 { 113 _servletPath = null; 114 return; 115 } 116 117 assert uri.startsWith( getOuterRequest().getContextPath() ) : uri; 121 setServletPath( uri.substring( getOuterRequest().getContextPath().length() ) ); 122 } 123 124 public void setRequestURI( String contextPath, String servletPath ) 125 { 126 _requestURI = contextPath + servletPath; 127 setServletPath( servletPath ); 128 } 129 130 private void setServletPath( String servletPath ) 131 { 132 String encoding = getCharacterEncoding(); 133 134 try 135 { 136 if ( encoding == null ) encoding = "utf-8"; 137 servletPath = URL_CODEC.decode( servletPath, encoding ); 138 } 139 catch ( DecoderException e ) 140 { 141 _log.error( "error decoding path " + servletPath, e ); 142 } 143 catch ( UnsupportedEncodingException e ) 144 { 145 _log.error( "unsupported encoding " + encoding + " while decoding path " + servletPath, e ); 146 } 147 148 _servletPath = ScopedServletUtils.normalizeURI( servletPath ); 149 } 150 151 public StringBuffer getRequestURL() 152 { 153 HttpServletRequest outerRequest = getOuterRequest(); 154 StringBuffer url = new StringBuffer ( outerRequest.getScheme() ); 155 url.append( "://" ).append( outerRequest.getServerName() ); 156 url.append( ':' ).append( outerRequest.getServerPort() ); 157 url.append( getRequestURI() ); 158 return url; 159 } 160 161 public String getServletPath() 162 { 163 return _servletPath; 164 } 165 166 public String getParameter( String paramName ) 167 { 168 String retVal = getLocalParameter( paramName ); 169 if ( retVal == null ) 170 { 171 retVal = getListenScopeParameter( paramName ); 172 } 173 174 return retVal; 175 } 176 177 183 public void addParameter( String name, String value ) 184 { 185 if ( _additionalParameters == null ) 186 { 187 _additionalParameters = new HashMap (); 188 } 189 190 _additionalParameters.put( name, value ); 191 } 192 193 198 public String getLocalParameter( String paramName ) 199 { 200 if ( _additionalParameters != null ) 201 { 202 String overrideParam = ( String ) _additionalParameters.get( paramName ); 203 204 if ( overrideParam != null ) 205 { 206 return overrideParam; 207 } 208 } 209 210 ServletRequest request = getRequest(); 211 String retVal = request.getParameter( _scopedContainer.getScopedName( paramName ) ); 212 213 if ( retVal == null && _isActiveRequest && paramName.startsWith( AUTOSCOPE_PREFIX ) ) 214 { 215 retVal = request.getParameter( paramName ); 216 } 217 218 return retVal; 219 } 220 221 226 public String getListenScopeParameter( String paramName ) 227 { 228 String retVal = null; 229 230 if ( _listenScopes != null ) 231 { 232 for ( int i = 0, len = _listenScopes.size(); retVal == null && i < len; ++i ) 233 { 234 String key = ScopedServletUtils.getScopedName( paramName, _listenScopes.get( i ) ); 235 retVal = getRequest().getParameter( key ); 236 } 237 } 238 239 return retVal; 240 } 241 242 243 public Enumeration getParameterNames() 244 { 245 ArrayList paramNames = new ArrayList (); 246 247 for ( Enumeration e = getRequest().getParameterNames(); e.hasMoreElements(); ) 248 { 249 String scopedParamName = ( String ) e.nextElement(); 250 251 if ( _scopedContainer.isInScope( scopedParamName ) ) 252 { 253 paramNames.add( _scopedContainer.removeScope( scopedParamName ) ); 254 } 255 else if ( _isActiveRequest && scopedParamName.startsWith( AUTOSCOPE_PREFIX ) ) 256 { 257 paramNames.add( scopedParamName ); 258 } 259 else if ( _listenScopes != null ) 260 { 261 for ( int i = 0, len = _listenScopes.size(); i < len; ++i ) 262 { 263 Object scope = _listenScopes.get( i ); 264 265 if ( ScopedAttributeContainer.isInScope( scopedParamName, scope ) ) 266 { 267 paramNames.add( ScopedAttributeContainer.removeScope( scopedParamName, scope ) ); 268 } 269 } 270 } 271 } 272 273 return Collections.enumeration( paramNames ); 274 } 275 276 public String [] getParameterValues( String paramName ) 277 { 278 ServletRequest request = getRequest(); 279 String [] retVals = request.getParameterValues( _scopedContainer.getScopedName( paramName ) ); 280 281 if ( retVals == null && _isActiveRequest && paramName.startsWith( AUTOSCOPE_PREFIX ) ) 282 { 283 retVals = request.getParameterValues( paramName ); 284 } 285 286 if ( retVals == null && _listenScopes != null ) 287 { 288 for ( int i = 0, len = _listenScopes.size(); retVals == null && i < len; ++i ) 289 { 290 String key = ScopedServletUtils.getScopedName( paramName, _listenScopes.get( i ) ); 291 retVals = request.getParameterValues( key ); 292 } 293 } 294 295 return retVals; 296 } 297 298 public Map getParameterMap() 299 { 300 return filterParameterMap( getRequest().getParameterMap() ); 301 } 302 303 306 public Map filterParameterMap( Map parameterMap ) 307 { 308 HashMap map = new HashMap (); 309 310 for ( Iterator i = parameterMap.entrySet().iterator(); i.hasNext(); ) 311 { 312 Map.Entry entry = ( Map.Entry ) i.next(); 313 String scopedParamName = ( String ) entry.getKey(); 314 315 if ( _scopedContainer.isInScope( scopedParamName ) ) 316 { 317 map.put( _scopedContainer.removeScope( scopedParamName ), entry.getValue() ); 318 } 319 else if ( _isActiveRequest && scopedParamName.startsWith( AUTOSCOPE_PREFIX ) ) 320 { 321 map.put( scopedParamName, entry.getValue() ); 322 } 323 else if ( _listenScopes != null ) 324 { 325 for ( int j = 0, len = _listenScopes.size(); j < len; ++j ) 326 { 327 if ( ScopedAttributeContainer.isInScope( scopedParamName, _listenScopes.get( j ) ) ) 328 { 329 map.put( ScopedAttributeContainer.removeScope( scopedParamName, _listenScopes.get( j ) ), 330 entry.getValue() ); 331 } 332 } 333 } 334 } 335 336 return map; 337 } 338 339 343 public void addListenScope( Object scopeKey ) 344 { 345 assert scopeKey != null; 346 347 if ( _listenScopes == null ) 348 { 349 _listenScopes = new ArrayList (); 350 } 351 352 _listenScopes.add( scopeKey ); 353 } 354 355 public RequestDispatcher getRequestDispatcher( String uri ) 356 { 357 return new ScopedRequestDispatcher( uri ); 358 } 359 360 public void doForward() 361 { 362 String forwardedURI = _forwardedURI; 363 364 if ( forwardedURI != null ) 365 { 366 if ( ! forwardedURI.startsWith( "/" ) ) 367 { 368 int lastSlash = _requestURI.lastIndexOf( '/' ); 369 assert lastSlash != -1 : _requestURI; 370 setRequestURI( _requestURI.substring( 0, lastSlash + 1 ) + forwardedURI ); 371 } 372 else 373 { 374 setRequestURI( getOuterRequest().getContextPath(), forwardedURI ); 375 } 376 377 parseQueryParameters(); 379 } 380 else 381 { 382 setRequestURI( null ); 383 } 384 } 385 386 private void parseQueryParameters() 387 { 388 int queryIndex = _requestURI.indexOf("?"); 389 if (queryIndex < 0) 390 { 391 return; 392 } 393 394 String queryString = _requestURI.substring(queryIndex + 1); 395 396 _requestURI = _requestURI.substring(0, queryIndex); 398 399 if (queryString.length() == 0) 400 { 401 return; 402 } 403 404 HashMap queryParameters = new HashMap (); 405 ParseUtils.parseQueryString(queryString, queryParameters, getCharacterEncoding()); 406 407 Iterator itor = queryParameters.keySet().iterator(); 408 while (itor.hasNext()) 409 { 410 Object key = itor.next(); 411 addParameter((String ) key, (String ) queryParameters.get(key)); 412 } 413 } 414 415 420 public void setForwardedURI( String uri ) 421 { 422 _forwardedURI = uri; 423 } 424 425 public String getForwardedURI() 426 { 427 return _forwardedURI; 428 } 429 430 433 public boolean didRedirect() 434 { 435 return false; 436 } 437 438 441 public void persistAttributes() 442 { 443 String attrName = getScopedName( STORED_ATTRS_ATTR ); 444 getSession().setAttribute( attrName, _scopedContainer.getSerializableAttrs() ); 445 } 446 447 450 public void restoreAttributes() 451 { 452 String attrName = getScopedName( STORED_ATTRS_ATTR ); 453 Map savedAttrs = ( Map ) getSession().getAttribute( attrName ); 454 Map currentAttrs = _scopedContainer.getAttrMap(); 455 456 if ( savedAttrs != null ) 457 { 458 if ( currentAttrs != null ) 459 { 460 savedAttrs.putAll( currentAttrs ); 461 } 462 463 _scopedContainer.setAttrMap( savedAttrs ); 464 } 465 } 466 467 public final HttpServletRequest getOuterRequest() 468 { 469 return ( HttpServletRequest ) getRequest(); 470 } 471 472 public final Object getAttribute( String attrName ) 473 { 474 return getAttribute( attrName, true ); 475 } 476 477 public final Object getAttribute( String attrName, boolean allowOuterRequestAttributes ) 478 { 479 if ( ! allowOuterRequestAttributes ) return _scopedContainer.getAttribute( attrName ); 480 481 ServletRequest outerRequest = getRequest(); 482 483 if ( ! _seeOuterRequestAttributes && _visibleOuterRequestAttrs.contains( attrName ) ) 484 { 485 return outerRequest.getAttribute( attrName ); 486 } 487 488 Object value = _scopedContainer.getAttribute( attrName ); 489 490 if ( value == null && _seeOuterRequestAttributes ) 491 { 492 value = outerRequest.getAttribute( attrName ); 493 } 494 495 return value; 496 } 497 498 public final void setAttribute( String attrName, Object o ) 499 { 500 if ( ! _seeOuterRequestAttributes && _visibleOuterRequestAttrs.contains( attrName ) ) 501 { 502 getRequest().setAttribute( attrName, o ); 503 } 504 else 505 { 506 _scopedContainer.setAttribute( attrName, o ); 507 } 508 } 509 510 public final Enumeration getAttributeNames() 511 { 512 Set set = new HashSet (); 513 514 if ( ! _seeOuterRequestAttributes ) 515 { 516 for ( Enumeration e = getRequest().getAttributeNames(); e.hasMoreElements(); ) 517 { 518 Object attrName = e.nextElement(); 519 if ( _visibleOuterRequestAttrs.contains( attrName ) ) set.add( attrName ); 520 } 521 } 522 523 for ( Enumeration e = _scopedContainer.getAttributeNames(); e.hasMoreElements(); ) 524 { 525 set.add( e.nextElement() ); 526 } 527 528 if ( _seeOuterRequestAttributes ) 529 { 530 for ( Enumeration e = getRequest().getAttributeNames(); e.hasMoreElements(); ) 531 { 532 set.add( e.nextElement() ); 533 } 534 } 535 536 return Collections.enumeration( set ); 537 } 538 539 public final void removeAttribute( String attrName ) 540 { 541 if ( ! _seeOuterRequestAttributes && _visibleOuterRequestAttrs.contains( attrName ) ) 542 { 543 getRequest().removeAttribute( attrName ); 544 } 545 else 546 { 547 _scopedContainer.removeAttribute( attrName ); 548 } 549 } 550 551 public void registerOuterAttribute( String attrName ) 552 { 553 assert ! _seeOuterRequestAttributes : 554 "(attribute " + attrName + ") " + 555 "this method is not valid unless the ScopedRequest is configured not to see outer request attributes"; 556 _visibleOuterRequestAttrs.add( attrName ); 557 } 558 559 public final Object getScopeKey() 560 { 561 return _scopedContainer.getScopeKey(); 562 } 563 564 public void renameScope( Object newScopeKey ) 565 { 566 _scopedContainer.renameScope( newScopeKey ); 567 } 568 569 public static void renameSessionScope( Object oldScopeKey, Object newScopeKey, HttpServletRequest outerRequest ) 570 { 571 HttpSession realSession = outerRequest.getSession( false ); 572 573 if ( realSession != null ) 574 { 575 String realSessionAttr = ScopedServletUtils.getScopedName( OUR_SESSION_ATTR, oldScopeKey ); 576 Object ourSession = realSession.getAttribute( realSessionAttr ); 577 realSessionAttr = ScopedServletUtils.getScopedName( OUR_SESSION_ATTR, newScopeKey ); 578 realSession.setAttribute( realSessionAttr, ourSession ); 579 } 580 } 581 582 public String getPathInfo() 583 { 584 return _overridePathInfo; 585 } 586 587 public void setPathInfo( String pathInfo ) 588 { 589 _overridePathInfo = pathInfo; 590 } 591 592 595 public void setActiveRequest() 596 { 597 _isActiveRequest = true; 598 } 599 600 public final String getScopedName( String baseName ) 601 { 602 return _scopedContainer.getScopedName( baseName ); 603 } 604 605 609 public boolean hasListenScopes() 610 { 611 return _listenScopes != null && _listenScopes.size() > 0; 612 } 613 } 614 615 | Popular Tags |