1 18 package org.apache.beehive.netui.pageflow.scoping; 19 20 import org.apache.beehive.netui.pageflow.scoping.internal.ScopedRequestImpl; 21 import org.apache.beehive.netui.pageflow.scoping.internal.ScopedResponseImpl; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpSession ; 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.ServletRequestWrapper ; 29 import javax.servlet.ServletResponse ; 30 import javax.servlet.ServletResponseWrapper ; 31 32 import org.apache.log4j.Logger; 33 34 import java.net.URI ; 35 import java.net.URISyntaxException ; 36 37 38 43 public class ScopedServletUtils 44 { 45 public static final String SCOPE_ID_PARAM = "jpfScopeID"; 46 47 static final String ATTR_PREFIX = "_netui:"; 48 private static final String OVERRIDE_REQUEST_ATTR = ATTR_PREFIX + "overrideRequest"; 49 private static final String OVERRIDE_RESPONSE_ATTR = ATTR_PREFIX + "overrideResponse"; 50 51 private static final Logger logger = Logger.getLogger( ScopedServletUtils.class ); 52 53 54 65 public static ScopedRequest getScopedRequest( HttpServletRequest realRequest, String overrideURI, 66 ServletContext servletContext, Object scopeKey ) 67 { 68 return getScopedRequest( realRequest, overrideURI, servletContext, scopeKey, false ); 69 } 70 71 84 public static ScopedRequest getScopedRequest( HttpServletRequest realRequest, String overrideURI, 85 ServletContext servletContext, Object scopeKey, 86 boolean seeOuterRequestAttributes ) 87 { 88 assert ! ( realRequest instanceof ScopedRequest ); 89 90 String requestAttr = getScopedName( OVERRIDE_REQUEST_ATTR, scopeKey ); 91 ScopedRequest scopedRequest = ( ScopedRequest ) realRequest.getAttribute( requestAttr ); 92 93 if ( scopedRequest == null ) 97 { 98 if ( overrideURI != null && ! overrideURI.startsWith( "/" ) ) 102 { 103 overrideURI = "/" + overrideURI; 104 } 105 106 scopedRequest = 107 new ScopedRequestImpl( realRequest, overrideURI, scopeKey, servletContext, seeOuterRequestAttributes ); 108 realRequest.setAttribute( requestAttr, scopedRequest ); 109 } 110 111 return scopedRequest; 112 } 113 114 121 public static ScopedResponse getScopedResponse( HttpServletResponse realResponse, 122 ScopedRequest scopedRequest ) 123 { 124 assert ! ( realResponse instanceof ScopedResponse ); 125 126 String responseAttr = getScopedName( OVERRIDE_RESPONSE_ATTR, 127 scopedRequest.getScopeKey() ); 128 HttpServletRequest outerRequest = scopedRequest.getOuterRequest(); 129 ScopedResponse scopedResponse = ( ScopedResponse ) outerRequest.getAttribute( responseAttr ); 130 131 if ( scopedResponse == null ) 135 { 136 scopedResponse = new ScopedResponseImpl( realResponse ); 137 outerRequest.setAttribute( responseAttr, scopedResponse ); 138 } 139 140 return scopedResponse; 141 } 142 143 151 public static void renameScope( Object oldScopeKey, Object newScopeKey, HttpServletRequest request ) 152 { 153 assert ! ( request instanceof ScopedRequest ); 154 155 String requestAttr = getScopedName( OVERRIDE_REQUEST_ATTR, oldScopeKey ); 156 String responseAttr = getScopedName( OVERRIDE_RESPONSE_ATTR, oldScopeKey ); 157 ScopedRequest scopedRequest = ( ScopedRequest ) request.getAttribute( requestAttr ); 158 ScopedResponse scopedResponse = ( ScopedResponse ) request.getAttribute( responseAttr ); 159 160 if ( scopedRequest != null ) 161 { 162 scopedRequest.renameScope( newScopeKey ); 163 request.removeAttribute( requestAttr ); 164 requestAttr = getScopedName( OVERRIDE_REQUEST_ATTR, newScopeKey ); 165 request.setAttribute( requestAttr, scopedRequest ); 166 } 167 else 168 { 169 ScopedRequestImpl.renameSessionScope( oldScopeKey, newScopeKey, request ); 170 } 171 172 if ( scopedResponse != null ) 173 { 174 request.removeAttribute( responseAttr ); 175 responseAttr = getScopedName( OVERRIDE_RESPONSE_ATTR, newScopeKey ); 176 request.setAttribute( responseAttr, scopedResponse ); 177 } 178 } 179 180 187 public static String getScopedName( String baseName, Object scopeKey ) 188 { 189 return scopeKey + baseName; 190 } 191 192 198 204 205 212 public static HttpServletRequest getOuterRequest( HttpServletRequest request ) 213 { 214 ScopedRequest scopedRequest = unwrapRequest( request ); 215 return scopedRequest != null ? scopedRequest.getOuterRequest() : request; 216 } 217 218 225 public static ServletRequest getOuterServletRequest( ServletRequest request ) 226 { 227 ScopedRequest scopedRequest = unwrapRequest( request ); 228 return scopedRequest != null ? scopedRequest.getOuterRequest() : request; 229 } 230 231 240 public static ScopedRequest unwrapRequest( ServletRequest request ) 241 { 242 while ( request instanceof ServletRequestWrapper ) 243 { 244 if ( request instanceof ScopedRequest ) 245 { 246 return ( ScopedRequest ) request; 247 } 248 else 249 { 250 request = ( ( ServletRequestWrapper ) request ).getRequest(); 251 } 252 } 253 254 return null; 255 } 256 257 266 public static ScopedResponse unwrapResponse( ServletResponse response ) 267 { 268 while ( response instanceof ServletResponseWrapper ) 269 { 270 if ( response instanceof ScopedResponse ) 271 { 272 return ( ScopedResponse ) response; 273 } 274 else 275 { 276 response = ( ( ServletResponseWrapper ) response ).getResponse(); 277 } 278 } 279 280 return null; 281 } 282 283 289 public static String getScopedSessionAttrName( String attrName, HttpServletRequest request ) 290 { 291 String requestScopeParam = request.getParameter( SCOPE_ID_PARAM ); 292 293 if ( requestScopeParam != null ) 294 { 295 return getScopedName( attrName, requestScopeParam ); 296 } 297 298 ScopedRequest scopedRequest = unwrapRequest( request ); 299 return scopedRequest != null ? scopedRequest.getScopedName( attrName ) : attrName; 300 } 301 302 308 public static Object getScopedSessionAttr( String attrName, HttpServletRequest request ) 309 { 310 HttpSession session = request.getSession( false ); 311 312 if ( session != null ) 313 { 314 return session.getAttribute( getScopedSessionAttrName( attrName, request ) ); 315 } 316 else 317 { 318 return null; 319 } 320 } 321 322 328 public static void setScopedSessionAttr( String attrName, Object val, HttpServletRequest request ) 329 { 330 request.getSession().setAttribute( getScopedSessionAttrName( attrName, request ), val ); 331 } 332 333 339 public static void removeScopedSessionAttr( String attrName, HttpServletRequest request ) 340 { 341 HttpSession session = request.getSession( false ); 342 343 if ( session != null ) 344 { 345 session.removeAttribute( getScopedSessionAttrName( attrName, request ) ); 346 } 347 } 348 349 356 public static Object getScopedRequestAttribute( String attrName, ServletRequest request ) 357 { 358 if ( request instanceof ScopedRequest ) 359 { 360 return ( ( ScopedRequest ) request ).getAttribute( attrName, false ); 361 } 362 363 return request.getAttribute( attrName ); 364 } 365 366 371 public static final String getRelativeURI( HttpServletRequest request ) 372 { 373 return request.getServletPath(); 374 } 375 376 382 public static final String getRelativeURI( HttpServletRequest request, String uri ) 383 { 384 return getRelativeURI( request.getContextPath(), uri ); 385 } 386 387 393 public static final String getRelativeURI( String contextPath, String uri ) 394 { 395 String requestUrl = uri; 396 int overlap = requestUrl.indexOf( contextPath + '/' ); 397 assert overlap != -1 : "contextPath: " + contextPath + ", uri: " + uri; 398 return requestUrl.substring( overlap + contextPath.length() ); 399 } 400 401 405 public static String normalizeURI( String uri ) 406 { 407 if ( uri.indexOf( "./" ) != -1 ) 412 { 413 try 414 { 415 uri = new URI ( uri ).normalize().toString(); 416 } 417 catch ( URISyntaxException e ) 418 { 419 logger.error( "Could not parse relative URI " + uri ); 420 } 421 } 422 423 return uri; 424 } 425 426 427 430 public static String decodeURI( HttpServletRequest request ) 431 { 432 return request.getContextPath() + request.getServletPath(); } 434 } 435 | Popular Tags |