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 ); 770 771 if ( map.containsKey( name ) ) 772 { 773 if ( _log.isWarnEnabled() ) 774 { 775 _log.warn( "Overwriting action output\"" + name + "\"." ); 776 } 777 } 778 779 map.put( name, value ); 780 } 781 782 790 public static Object getPageInput( String name, ServletRequest request ) 791 { 792 return getActionOutput( name, request ); 793 } 794 795 802 public static Object getActionOutput( String name, ServletRequest request ) 803 { 804 Map map = InternalUtils.getActionOutputMap( request, false ); 805 return map != null ? map.get( name ) : null; 806 } 807 808 817 public static void addValidationError( String propertyName, String messageKey, Object [] messageArgs, 818 ServletRequest request ) 819 { 820 InternalUtils.addActionError( propertyName, new ActionMessage( messageKey, messageArgs ), request ); 821 } 822 823 824 833 public static void addValidationError( String propertyName, String messageKey, Object messageArg, 834 ServletRequest request ) 835 { 836 addActionError( request, propertyName, messageKey, new Object []{ messageArg } ); 837 } 838 839 847 public static void addValidationError( String propertyName, String messageKey, ServletRequest request ) 848 { 849 addActionError( request, propertyName, messageKey ); 850 } 851 852 860 public static void addActionError( ServletRequest request, String propertyName, String messageKey, 861 Object [] messageArgs ) 862 { 863 InternalUtils.addActionError( propertyName, new ActionMessage( messageKey, messageArgs ), request ); 864 } 865 866 873 public static void addActionError( ServletRequest request, String propertyName, String messageKey ) 874 { 875 InternalUtils.addActionError( propertyName, new ActionMessage( messageKey, null ), request ); 876 } 877 878 886 public static void addActionError( ServletRequest request, String propertyName, String messageKey, 887 Object messageArg ) 888 { 889 Object [] messageArgs = new Object []{ messageArg }; 890 InternalUtils.addActionError( propertyName, new ActionMessage( messageKey, messageArgs ), request ); 891 } 892 893 902 public static void addActionError( ServletRequest request, String propertyName, String messageKey, 903 Object messageArg1, Object messageArg2 ) 904 { 905 Object [] messageArgs = new Object []{ messageArg1, messageArg2 }; 906 InternalUtils.addActionError( propertyName, new ActionMessage( messageKey, messageArgs ), request ); 907 } 908 909 919 public static void addActionError( ServletRequest request, String propertyName, String messageKey, 920 Object messageArg1, Object messageArg2, Object messageArg3 ) 921 { 922 Object [] messageArgs = new Object []{ messageArg1, messageArg2, messageArg3 }; 923 InternalUtils.addActionError( propertyName, new ActionMessage( messageKey, messageArgs ), request ); 924 } 925 926 934 public static void addActionErrorExpression( HttpServletRequest request, String propertyName, String expression, 935 Object [] messageArgs ) 936 { 937 ExpressionMessage msg = new ExpressionMessage( expression, messageArgs ); 938 InternalUtils.addActionError( propertyName, msg, request ); 939 } 940 941 956 public static ActionResult strutsLookup( ServletContext context, ServletRequest request, 957 HttpServletResponse response, String actionOverride, 958 String [] autoResolveExtensions ) 959 throws Exception 960 { 961 ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest( request ); 962 ScopedResponse scopedResponse = ScopedServletUtils.unwrapResponse( response ); 963 assert scopedRequest != null : request.getClass().getName(); 964 assert scopedResponse != null : response.getClass().getName(); 965 assert request instanceof HttpServletRequest : request.getClass().getName(); 966 967 if ( scopedRequest == null ) 968 { 969 throw new IllegalArgumentException ( "request must be of type " + ScopedRequest.class.getName() ); 970 } 971 if ( scopedResponse == null ) 972 { 973 throw new IllegalArgumentException ( "response must be of type " + ScopedResponse.class.getName() ); 974 } 975 976 ActionServlet as = InternalUtils.getActionServlet( context ); 977 978 if ( as == null ) 979 { 980 _log.error( "There is no initialized ActionServlet. The ActionServlet must be set to load-on-startup." ); 981 return null; 982 } 983 984 if ( actionOverride != null ) 985 { 986 assert actionOverride.charAt( 0 ) == '/' : actionOverride; 988 InternalStringBuilder uri = new InternalStringBuilder( scopedRequest.getContextPath() ); 989 uri.append( actionOverride ); 990 uri.append( PageFlowConstants.ACTION_EXTENSION ); 991 scopedRequest.setRequestURI( uri.toString() ); 992 } 993 994 scopedRequest.setForwardedURI( null ); 999 1000 PageFlowRequestWrapper wrappedRequest = PageFlowRequestWrapper.wrapRequest( ( HttpServletRequest ) request ); 1004 as.doGet( wrappedRequest, scopedResponse ); 1006 String returnURI; 1007 1008 if ( ! scopedResponse.didRedirect() ) 1009 { 1010 returnURI = scopedRequest.getForwardedURI(); 1011 1012 if ( autoResolveExtensions == null ) 1013 { 1014 autoResolveExtensions = DEFAULT_AUTORESOLVE_EXTENSIONS; 1015 } 1016 1017 if ( returnURI != null ) 1018 { 1019 for ( int i = 0; i < autoResolveExtensions.length; ++i ) 1020 { 1021 if ( FileUtils.uriEndsWith( returnURI, autoResolveExtensions[i] ) ) 1022 { 1023 scopedRequest.doForward(); 1024 return strutsLookup( context, wrappedRequest, scopedResponse, null, autoResolveExtensions ); 1025 } 1026 } 1027 } 1028 } 1029 else 1030 { 1031 returnURI = scopedResponse.getRedirectURI(); 1032 } 1033 1034 if ( returnURI != null ) 1035 { 1036 return new ActionResultImpl( returnURI, scopedResponse.didRedirect(), scopedResponse.getStatusCode(), 1037 scopedResponse.getStatusMessage(), scopedResponse.isError() ); 1038 } 1039 else 1040 { 1041 return null; 1042 } 1043 } 1044 1045 1050 public static HttpServletRequest unwrapMultipart( HttpServletRequest request ) 1051 { 1052 if ( request instanceof MultipartRequestWrapper ) 1053 { 1054 request = ( ( MultipartRequestWrapper ) request ).getRequest(); 1055 } 1056 1057 return request; 1058 } 1059 1060 1070 public static GlobalApp ensureGlobalApp( HttpServletRequest request, HttpServletResponse response ) 1071 { 1072 ServletContext servletContext = InternalUtils.getServletContext( request ); 1073 return ensureGlobalApp( request, response, servletContext ); 1074 } 1075 1076 1086 public static GlobalApp ensureGlobalApp( HttpServletRequest request, HttpServletResponse response, 1087 ServletContext servletContext ) 1088 { 1089 GlobalApp ga = getGlobalApp( request ); 1090 1091 if ( ga != null ) 1092 { 1093 ga.reinitialize( request, response, servletContext ); 1094 } 1095 else 1096 { 1097 ga = FlowControllerFactory.getGlobalApp( request, response, servletContext ); 1098 } 1099 1100 return ga; 1101 } 1102 1103 1107 public static Map getBindingUpdateErrors( ServletRequest request ) 1108 { 1109 return InternalUtils.getBindingUpdateErrors( request ); 1110 } 1111 1112 1116 public static ModuleConfig ensureModuleConfig( String modulePath, ServletRequest request, ServletContext context ) 1117 { 1118 return InternalUtils.ensureModuleConfig( modulePath, request, context ); 1119 } 1120 1121 1124 public static ModuleConfig getGlobalAppConfig( ServletContext servletContext ) 1125 { 1126 return InternalUtils.getModuleConfig( GLOBALAPP_MODULE_CONTEXT_PATH, servletContext ); 1127 } 1128 1129 1133 public static ModuleConfig getModuleConfig( String modulePath, ServletContext context ) 1134 { 1135 return InternalUtils.getModuleConfig( modulePath, context ); 1136 } 1137 1138 1145 public static String getFileExtension( String filename ) 1146 { 1147 return FileUtils.getFileExtension( filename ); 1148 } 1149 1150 1154 public static String getPageFlowClassName( String modulePath, ServletRequest request, ServletContext context ) 1155 { 1156 return InternalUtils.getFlowControllerClassName( modulePath, request, context ); 1157 } 1158 1159 1162 public static boolean ensureAppDeployment( HttpServletRequest request, HttpServletResponse response, 1163 ServletContext servletContext ) 1164 { 1165 return false; 1166 } 1167 1168 1175 public static boolean isAbsoluteURI( String uri ) 1176 { 1177 return FileUtils.isAbsoluteURI( uri ); 1178 } 1179 1180 1183 public static final PageFlowController ensureCurrentPageFlow( HttpServletRequest request, 1184 HttpServletResponse response, 1185 ServletContext servletContext ) 1186 { 1187 try 1188 { 1189 FlowControllerFactory factory = FlowControllerFactory.get( servletContext ); 1190 return factory.getPageFlowForRequest( new RequestContext( request, response ) ); 1191 } 1192 catch ( InstantiationException e ) 1193 { 1194 _log.error( "Could not instantiate PageFlowController for request " + request.getRequestURI(), e ); 1195 } 1196 catch ( IllegalAccessException e ) 1197 { 1198 _log.error( "Could not instantiate PageFlowController for request " + request.getRequestURI(), e ); 1199 } 1200 1201 return null; 1202 } 1203 1204 1207 public static final PageFlowController ensureCurrentPageFlow( HttpServletRequest request, 1208 HttpServletResponse response ) 1209 { 1210 ServletContext servletContext = InternalUtils.getServletContext( request ); 1211 1212 if ( servletContext == null && _log.isWarnEnabled() ) 1213 { 1214 _log.warn( "could not get ServletContext from request " + request ); 1215 } 1216 1217 return ensureCurrentPageFlow( request, response, servletContext ); 1218 } 1219 1220 1224 public static void addBindingUpdateError( ServletRequest request, String expression, String message, Throwable e ) 1225 { 1226 InternalUtils.addBindingUpdateError( request, expression, message, e ); 1227 } 1228 1229 1233 public static void dumpRequest( HttpServletRequest request, PrintStream output ) 1234 { 1235 ServletUtils.dumpRequest( request, output ); 1236 } 1237 1238 1242 public static void dumpServletContext( ServletContext context, PrintStream output ) 1243 { 1244 ServletUtils.dumpServletContext( context, output ); 1245 } 1246 1247 1250 public static void preventCache( HttpServletResponse response ) 1251 { 1252 ServletUtils.preventCache( response ); 1253 } 1254 1255 1259 public static void setCurrentActionResolver( ActionResolver resolver, HttpServletRequest request ) 1260 { 1261 InternalUtils.setCurrentActionResolver( resolver, request ); 1262 } 1263 1264 1277 public static MutableURI getActionURI( ServletContext servletContext, HttpServletRequest request, 1278 HttpServletResponse response, String actionName ) 1279 throws URISyntaxException 1280 { 1281 if ( actionName.length() < 1 ) throw new IllegalArgumentException ( "actionName must be non-empty" ); 1282 1283 InternalStringBuilder actionURI = new InternalStringBuilder( request.getContextPath() ); 1284 1285 if ( actionName.charAt( 0 ) != '/' ) 1286 { 1287 actionURI.append( getModulePath( request ) ); 1288 actionURI.append( '/' ); 1289 } 1290 1291 actionURI.append( actionName ); 1292 if ( ! actionName.endsWith( ACTION_EXTENSION ) ) actionURI.append( ACTION_EXTENSION ); 1293 1294 FreezableMutableURI uri = new FreezableMutableURI(); 1295 uri.setEncoding( response.getCharacterEncoding() ); 1296 uri.setURI( actionURI.toString(), true ); 1297 return uri; 1298 } 1299 1300 1316 public static String getRewrittenActionURI( ServletContext servletContext, HttpServletRequest request, 1317 HttpServletResponse response, String actionName, Map params, 1318 String fragment, boolean forXML ) 1319 throws URISyntaxException 1320 { 1321 MutableURI uri = getActionURI( servletContext, request, response, actionName ); 1322 if ( params != null ) uri.addParameters( params, false ); 1323 if ( fragment != null ) uri.setFragment( uri.encode( fragment ) ); 1324 1325 boolean needsToBeSecure = needsToBeSecure( servletContext, request, uri.getPath(), true ); 1326 URLRewriterService.rewriteURL( servletContext, request, response, uri, URLType.ACTION, needsToBeSecure ); 1327 String key = getURLTemplateKey( URLType.ACTION, needsToBeSecure ); 1328 URIContext uriContext = URIContextFactory.getInstance( forXML ); 1329 1330 return URLRewriterService.getTemplatedURL( request, uri, key, uriContext ); 1331 } 1332 1333 1350 public static String getRewrittenResourceURI( ServletContext servletContext, HttpServletRequest request, 1351 HttpServletResponse response, String path, Map params, 1352 String fragment, boolean forXML ) 1353 throws URISyntaxException 1354 { 1355 return rewriteResourceOrHrefURL( servletContext, request, response, path, params, fragment, forXML, URLType.RESOURCE ); 1356 } 1357 1358 1375 public static String getRewrittenHrefURI( ServletContext servletContext, HttpServletRequest request, 1376 HttpServletResponse response, String path, Map params, 1377 String fragment, boolean forXML ) 1378 throws URISyntaxException 1379 { 1380 return rewriteResourceOrHrefURL( servletContext, request, response, path, params, fragment, forXML, URLType.ACTION ); 1381 } 1382 1383 private static String rewriteResourceOrHrefURL( ServletContext servletContext, HttpServletRequest request, 1384 HttpServletResponse response, String path, Map params, 1385 String fragment, boolean forXML, URLType urlType ) 1386 throws URISyntaxException 1387 { 1388 boolean encoded = false; 1389 UrlConfig urlConfig = ConfigUtil.getConfig().getUrlConfig(); 1390 1391 if (urlConfig != null && urlConfig.isSetUrlEncodeUrls()) { 1392 encoded = !urlConfig.getUrlEncodeUrls(); 1393 } 1394 1395 FreezableMutableURI uri = new FreezableMutableURI(); 1396 uri.setEncoding( response.getCharacterEncoding() ); 1397 uri.setURI( path, encoded ); 1398 1399 if ( params != null ) 1400 { 1401 uri.addParameters( params, false ); 1402 } 1403 1404 if ( fragment != null ) 1405 { 1406 uri.setFragment( uri.encode( fragment ) ); 1407 } 1408 1409 URIContext uriContext = URIContextFactory.getInstance( forXML ); 1410 if ( uri.isAbsolute() ) 1411 { 1412 return uri.getURIString( uriContext ); 1413 } 1414 1415 if ( path.length() != 0 && path.charAt( 0 ) != '/' ) 1416 { 1417 String reqUri = request.getRequestURI(); 1418 String reqPath = reqUri.substring( 0, reqUri.lastIndexOf( '/' ) + 1 ); 1419 uri.setPath( reqPath + uri.getPath() ); 1420 } 1421 1422 boolean needsToBeSecure = needsToBeSecure( servletContext, request, uri.getPath(), true ); 1423 URLRewriterService.rewriteURL( servletContext, request, response, uri, urlType, needsToBeSecure ); 1424 String key = getURLTemplateKey( urlType, needsToBeSecure ); 1425 1426 return URLRewriterService.getTemplatedURL( request, uri, key, uriContext ); 1427 } 1428 1429 1453 public static boolean needsToBeSecure(ServletContext context, ServletRequest request, 1454 String uri, boolean stripContextPath) 1455 { 1456 String secureCheck = uri; 1458 if (stripContextPath) { 1459 String contextPath = ((HttpServletRequest ) request).getContextPath(); 1460 if (secureCheck.startsWith(contextPath)) { 1461 secureCheck = secureCheck.substring(contextPath.length()); 1462 } 1463 } 1464 1465 boolean secure = false; 1466 if (secureCheck.indexOf('?') > -1) { 1467 secureCheck = secureCheck.substring(0, secureCheck.indexOf('?')); 1468 } 1469 1470 SecurityProtocol sp = getSecurityProtocol(secureCheck, context, (HttpServletRequest ) request); 1471 if (sp.equals(SecurityProtocol.UNSPECIFIED)) { 1472 secure = request.isSecure(); 1473 } 1474 else { 1475 secure = sp.equals(SecurityProtocol.SECURE); 1476 } 1477 1478 return secure; 1479 } 1480 1481 1489 public static String getURLTemplateKey( URLType urlType, boolean needsToBeSecure ) 1490 { 1491 String key = URLTemplateDescriptor.ACTION_TEMPLATE; 1492 if ( urlType.equals( URLType.ACTION ) ) 1493 { 1494 if ( needsToBeSecure ) 1495 { 1496 key = URLTemplateDescriptor.SECURE_ACTION_TEMPLATE; 1497 } 1498 else 1499 { 1500 key = URLTemplateDescriptor.ACTION_TEMPLATE; 1501 } 1502 } 1503 else if ( urlType.equals( URLType.RESOURCE ) ) 1504 { 1505 if ( needsToBeSecure ) 1506 { 1507 key = URLTemplateDescriptor.SECURE_RESOURCE_TEMPLATE; 1508 } 1509 else 1510 { 1511 key = URLTemplateDescriptor.RESOURCE_TEMPLATE; 1512 } 1513 } 1514 1515 return key; 1516 } 1517} 1518 | Popular Tags |