| 1 18 package org.apache.beehive.netui.pageflow; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.core.urls.FreezableMutableURI; 23 import org.apache.beehive.netui.core.urls.MutableURI; 24 import org.apache.beehive.netui.core.urls.URIContext; 25 import org.apache.beehive.netui.core.urls.URLRewriterService; 26 import org.apache.beehive.netui.core.urls.URLType; 27 import org.apache.beehive.netui.core.urltemplates.URLTemplateDescriptor; 28 import org.apache.beehive.netui.pageflow.internal.ActionResultImpl; 29 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 30 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 31 import org.apache.beehive.netui.pageflow.internal.AdapterManager; 32 import org.apache.beehive.netui.pageflow.internal.PageFlowRequestWrapper; 33 import org.apache.beehive.netui.pageflow.internal.URIContextFactory; 34 import org.apache.beehive.netui.pageflow.scoping.ScopedRequest; 35 import org.apache.beehive.netui.pageflow.scoping.ScopedResponse; 36 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 37 import org.apache.beehive.netui.util.internal.FileUtils; 38 import org.apache.beehive.netui.util.internal.ServletUtils; 39 import org.apache.beehive.netui.util.config.ConfigUtil; 40 import org.apache.beehive.netui.util.config.bean.UrlConfig; 41 import org.apache.beehive.netui.util.logging.Logger; 42 import org.apache.beehive.netui.script.common.ImplicitObjectUtil; 43 import org.apache.struts.action.ActionForm; 44 import org.apache.struts.action.ActionMapping; 45 import org.apache.struts.action.ActionServlet; 46 import org.apache.struts.action.ActionMessage; 47 import org.apache.struts.config.FormBeanConfig; 48 import org.apache.struts.config.ModuleConfig; 49 import org.apache.struts.upload.MultipartRequestWrapper; 50 import org.apache.struts.util.RequestUtils; 51 52 import javax.servlet.ServletContext ; 53 import javax.servlet.ServletRequest ; 54 import javax.servlet.http.HttpServletRequest ; 55 import javax.servlet.http.HttpServletResponse ; 56 import javax.servlet.http.HttpSession ; 57 import java.io.PrintStream ; 58 import java.net.URISyntaxException ; 59 import java.util.Collections ; 60 import java.util.HashMap ; 61 import java.util.Map ; 62 import java.util.Stack ; 63 import java.util.List ; 64 import java.util.ArrayList ; 65 import java.util.Iterator ; 66 67 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap; 68 69 70 71 74 public class PageFlowUtils 75 implements PageFlowConstants, InternalConstants 76 { 77 private static final Logger _log = Logger.getInstance( PageFlowUtils.class ); 78 79 private static final String ACTION_URI_ATTR = ATTR_PREFIX + "_actionURI"; 80 private static final int PAGEFLOW_EXTENSION_LEN = PAGEFLOW_EXTENSION.length(); 81 private static final String DEFAULT_AUTORESOLVE_EXTENSIONS[] = new String []{ ACTION_EXTENSION, PAGEFLOW_EXTENSION }; 82 83 84 85 private static Map _formNameMaps = 86 new InternalConcurrentHashMap(); 87 88 89 96 public static String getModulePath( HttpServletRequest request, String requestURI ) 97 { 98 return getModulePathForRelativeURI( getRelativeURI( request, requestURI, null ) ); 99 } 100 101 107 public static String getModulePath( HttpServletRequest request ) 108 { 109 return getModulePathForRelativeURI( InternalUtils.getDecodedServletPath( request ) ); 110 } 111 112 117 public static String getModulePathForRelativeURI( String uri ) 118 { 119 assert uri.length() > 0; 120 assert uri.charAt( 0 ) == '/' : uri; 121 122 int slash = uri.lastIndexOf( '/' ); 124 uri = uri.substring( 0, slash ); 125 126 return uri; 127 } 128 129 136 public static final String getRelativeURI( HttpServletRequest request, PageFlowController relativeTo ) 137 { 138 if ( relativeTo == null ) return InternalUtils.getDecodedServletPath( request ); 139 return getRelativeURI( request, InternalUtils.getDecodedURI( request ), relativeTo ); 140 } 141 142 150 public static final String getRelativeURI( HttpServletRequest request, String uri, PageFlowController relativeTo ) 151 { 152 String contextPath = request.getContextPath(); 153 if ( relativeTo != null ) contextPath += relativeTo.getModulePath(); 154 int overlap = uri.indexOf( contextPath + '/' ); 155 if ( overlap == -1 ) return null; 156 return uri.substring( overlap + contextPath.length() ); 157 } 158 159 166 public static String getBeginActionURI( String requestURI ) 167 { 168 InternalStringBuilder retVal = new InternalStringBuilder(); 170 int lastSlash = requestURI.lastIndexOf( '/' ); 171 172 if ( lastSlash != -1 ) 173 { 174 retVal.append( requestURI.substring( 0, lastSlash ) ); 175 } 176 177 retVal.append( '/' ).append( BEGIN_ACTION_NAME ).append( ACTION_EXTENSION ); 178 return retVal.toString(); 179 } 180 181 190 public static final Stack getPageFlowStack( HttpServletRequest request ) 191 { 192 return PageFlowStack.get( request, true ).getLegacyStack(); 193 } 194 195 202 public static void destroyPageFlowStack( HttpServletRequest request ) 203 { 204 PageFlowStack.destroy( request ); 205 } 206 207 214 public static PageFlowController getNestingPageFlow( HttpServletRequest request ) 215 { 216 PageFlowStack jpfStack = PageFlowStack.get( request, false ); 217 218 if ( jpfStack != null && ! jpfStack.isEmpty() ) 219 { 220 PageFlowController top = jpfStack.peek().getPageFlow(); 221 return top; 222 } 223 224 return null; 225 } 226 227 234 public static final PageFlowController getCurrentPageFlow( HttpServletRequest request ) 235 { 236 ActionResolver cur = getCurrentActionResolver( request ); 237 return cur != null && cur.isPageFlow() ? ( PageFlowController ) cur : null; 238 } 239 240 245 public static ActionResolver getCurrentActionResolver( HttpServletRequest request ) 246 { 247 HttpServletRequest unwrappedRequest = unwrapMultipart( request ); 251 String currentLongLivedModulePath = 252 ( String ) ScopedServletUtils.getScopedSessionAttr( CURRENT_LONGLIVED_ATTR, unwrappedRequest ); 253 ActionResolver retVal; 254 255 if ( currentLongLivedModulePath != null ) 256 { 257 retVal = getLongLivedPageFlow( currentLongLivedModulePath, unwrappedRequest ); 258 } 259 else 260 { 261 retVal = ( ActionResolver ) ScopedServletUtils.getScopedSessionAttr( CURRENT_JPF_ATTR, unwrappedRequest ); 262 } 263 264 return retVal; 265 } 266 267 275 public static GlobalApp getGlobalApp( HttpServletRequest request ) 276 { 277 SharedFlowController sf = getSharedFlow( InternalConstants.GLOBALAPP_CLASSNAME, request ); 278 return sf instanceof GlobalApp ? ( GlobalApp ) sf : null; 279 } 280 281 290 public static Map getSharedFlows( HttpServletRequest request ) 291 { 292 Map sharedFlows = ImplicitObjectUtil.getSharedFlow( request ); 293 return sharedFlows != null ? sharedFlows : Collections.EMPTY_MAP; 294 } 295 296 303 public static SharedFlowController getSharedFlow( String sharedFlowClassName, HttpServletRequest request ) 304 { 305 HttpSession session = request.getSession( false ); 306 307 if ( session != null ) 308 { 309 return ( SharedFlowController ) session.getAttribute( SHARED_FLOW_ATTR_PREFIX + sharedFlowClassName ); 310 } 311 312 return null; 313 } 314 315 320 public static void removeSharedFlow( String sharedFlowClassName, HttpServletRequest request ) 321 { 322 HttpSession session = request.getSession( false ); 323 if ( session != null ) request.getSession().removeAttribute( SHARED_FLOW_ATTR_PREFIX + sharedFlowClassName ); 324 } 325 326 327 333 public static void removeLongLivedPageFlow( String modulePath, HttpServletRequest request ) 334 { 335 HttpServletRequest unwrappedRequest = unwrapMultipart( request ); 336 String attrName = InternalUtils.getLongLivedFlowAttr( modulePath ); 337 ScopedServletUtils.removeScopedSessionAttr( attrName, unwrappedRequest ); 338 339 String currentLongLivedModulePath = 343 ( String ) ScopedServletUtils.getScopedSessionAttr( CURRENT_LONGLIVED_ATTR, unwrappedRequest ); 344 345 if ( modulePath.equals( currentLongLivedModulePath ) ) 346 { 347 ScopedServletUtils.removeScopedSessionAttr( CURRENT_LONGLIVED_ATTR, unwrappedRequest ); 348 } 349 } 350 351 358 public static PageFlowController getLongLivedPageFlow( String modulePath, HttpServletRequest request ) 359 { 360 String attr = InternalUtils.getLongLivedFlowAttr( modulePath ); 361 PageFlowController retVal = ( PageFlowController ) 362 ScopedServletUtils.getScopedSessionAttr( attr, unwrapMultipart( request ) ); 363 return retVal; 364 } 365 366 377 public static void setOutputForms( ActionMapping mapping, Forward fwd, HttpServletRequest request, 378 boolean overwrite ) 379 { 380 if ( fwd == null ) return; 381 382 if ( mapping != null ) setOutputForms( mapping, fwd.getOutputForms(), request, overwrite ); 386 387 InternalUtils.setForwardedFormBean( request, fwd.getFirstOutputForm( request ) ); 388 } 389 390 399 public static void setOutputForms( ActionMapping mapping, Forward fwd, HttpServletRequest request ) 400 { 401 if ( fwd == null ) 402 { 403 return; 404 } 405 406 if ( mapping != null ) 407 { 408 setOutputForms( mapping, fwd.getOutputForms(), request ); 409 } 410 411 InternalUtils.setForwardedFormBean( request, fwd.getFirstOutputForm( request ) ); 412 } 413 414 422 public static void setOutputForms( ActionMapping mapping, ActionForm[] outputForms, 423 HttpServletRequest request ) 424 { 425 setOutputForms( mapping, outputForms, request, true ); 426 } 427 428 438 public static void setOutputForms( ActionMapping mapping, ActionForm[] outputForms, 439 HttpServletRequest request, boolean overwrite ) 440 { 441 try 442 { 443 assert mapping.getScope() == null 447 || mapping.getScope().equals( "request" ) 448 || mapping.getScope().equals( "session" ) 449 : mapping.getScope(); 450 451 452 for ( int i = 0; i < outputForms.length; ++i ) 453 { 454 setOutputForm( mapping, outputForms[i], request, overwrite ); 455 } 456 } 457 catch ( Exception e ) 458 { 459 _log.error( "Error while setting Struts form-beans", e ); 460 } 461 } 462 463 private static List getFormNamesFromModuleConfig( String formBeanClassName, ModuleConfig moduleConfig ) 464 { 465 String modulePrefix = moduleConfig.getPrefix(); 466 Map formNameMap = ( Map ) _formNameMaps.get( modulePrefix ); 468 if ( formNameMap == null ) 469 { 470 formNameMap = new HashMap (); 471 FormBeanConfig[] formBeans = moduleConfig.findFormBeanConfigs(); 472 473 for ( int j = 0; j < formBeans.length; ++j ) 474 { 475 assert formBeans[j] != null; 476 String formBeanType = InternalUtils.getFormBeanType( formBeans[j] ); 477 List formBeanNames = ( List ) formNameMap.get( formBeanType ); 478 479 if ( formBeanNames == null ) 480 { 481 formBeanNames = new ArrayList (); 482 formNameMap.put( formBeanType, formBeanNames ); 483 } 484 485 formBeanNames.add( formBeans[j].getName() ); 486 } 487 488 _formNameMaps.put( modulePrefix, formNameMap ); 489 } 490 491 return ( List ) formNameMap.get( formBeanClassName ); 492 } 493 494 504 public static void setOutputForm( ActionMapping mapping, ActionForm form, 505 HttpServletRequest request, boolean overwrite ) 506 { 507 if ( form != null ) 508 { 509 ModuleConfig moduleConfig = mapping.getModuleConfig(); 510 Class formClass = InternalUtils.unwrapFormBean( form ).getClass(); 511 512 List formNames = getFormNamesFromModuleConfig( formClass.getName(), moduleConfig ); 516 517 if ( formNames == null ) 518 { 519 String formName = generateFormBeanName( formClass, request ); 520 InternalUtils.setFormInScope( formName, form, mapping, request, overwrite ); 521 } 522 else 523 { 524 assert formNames.size() > 0; 526 for ( Iterator ii = formNames.iterator(); ii.hasNext(); ) 527 { 528 String formName = ( String ) ii.next(); 529 InternalUtils.setFormInScope( formName, form, mapping, request, overwrite ); 530 } 531 } 532 } 533 } 534 535 548 public static String getFormBeanName( ActionForm formInstance, HttpServletRequest request ) 549 { 550 return getFormBeanName( formInstance.getClass(), request ); 551 } 552 553 566 public static String getFormBeanName( Class formBeanClass, HttpServletRequest request ) 567 { 568 ModuleConfig moduleConfig = RequestUtils.getRequestModuleConfig( request ); 569 List names = getFormNamesFromModuleConfig( formBeanClass.getName(), moduleConfig ); 570 571 if ( names != null ) 572 { 573 assert names.size() > 0; return ( String ) names.get( 0 ); 575 } 576 577 return generateFormBeanName( formBeanClass, request ); 578 } 579 580 592 private static String generateFormBeanName( Class formBeanClass, HttpServletRequest request ) 593 { 594 ModuleConfig moduleConfig = RequestUtils.getRequestModuleConfig( request ); 595 String formBeanClassName = formBeanClass.getName(); 596 597 String formType = formBeanClassName; 603 int lastQualifier = formType.lastIndexOf( '$' ); 604 605 if ( lastQualifier == -1 ) 606 { 607 lastQualifier = formType.lastIndexOf( '.' ); 608 } 609 610 String formName = formType.substring( lastQualifier + 1 ); 611 formName = Character.toLowerCase( formName.charAt( 0 ) ) + formName.substring( 1 ); 612 613 if ( moduleConfig.findFormBeanConfig( formName ) != null ) 614 { 615 formName = formType.replace( '.', '_' ).replace( '$', '_' ); 616 assert moduleConfig.findFormBeanConfig( formName ) == null : formName; 617 } 618 619 return formName; 620 } 621 622 628 public static String getPageFlowClassName( String uri ) 629 { 630 assert uri != null; 631 assert uri.length() > 0; 632 633 if ( uri.charAt( 0 ) == '/' ) uri = uri.substring( 1 ); 634 635 assert FileUtils.osSensitiveEndsWith( uri, PAGEFLOW_EXTENSION ) : uri; 636 if ( FileUtils.osSensitiveEndsWith( uri, PAGEFLOW_EXTENSION ) ) 637 { 638 uri = uri.substring( 0, uri.length() - PAGEFLOW_EXTENSION_LEN ); 639 } 640 641 return uri.replace( '/', '.' ); 642 } 643 644 652 public static String getJpfClassName( String uri ) 653 { 654 return getPageFlowClassName( uri ); 655 } 656 657 664 public static String getPageFlowURI( String className ) 665 { 666 return '/' + className.replace( '.', '/' ) + PAGEFLOW_EXTENSION; 667 } 668 669 680 public static String getModuleConfPath( String modulePath ) 681 { 682 return new PageFlowActionServlet.DefaultModuleConfigLocator().getModuleConfigPath( modulePath ); 683 } 684 685 686 693 public static String getActionURI( ServletRequest request ) 694 { 695 return ( String ) request.getAttribute( ACTION_URI_ATTR ); 696 } 697 698 701 static void setActionURI( HttpServletRequest request ) 702 { 703 request.setAttribute( ACTION_URI_ATTR, InternalUtils.getDecodedURI( request ) ); 704 } 705 706 730 public static SecurityProtocol getSecurityProtocol( String uri, ServletContext servletContext, 731 HttpServletRequest request ) 732 { 733 return AdapterManager.getServletContainerAdapter( servletContext ).getSecurityProtocol( uri, request ); 734 } 735 736 739 public static Boolean isSecureResource( String uri, ServletContext context ) 740 { 741 return null; 743 } 744 745 754 public static void addPageInput( String name, Object value, ServletRequest request ) 755 { 756 addActionOutput( name, value, request ); 757 } 758 759 767 public static void addActionOutput( String name, Object value, ServletRequest request ) 768 { 769 Map map = InternalUtils.getActionOutputMap( request, true );
|