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.pageflow.internal.InternalConstants; 23 import org.apache.beehive.netui.pageflow.internal.PageFlowRequestWrapper; 24 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 25 import org.apache.beehive.netui.pageflow.handler.Handlers; 26 import org.apache.beehive.netui.pageflow.handler.FlowControllerHandlerContext; 27 import org.apache.beehive.netui.pageflow.handler.ForwardRedirectHandler; 28 import org.apache.beehive.netui.pageflow.config.PageFlowControllerConfig; 29 import org.apache.beehive.netui.util.internal.ServletUtils; 30 import org.apache.beehive.netui.util.logging.Logger; 31 import org.apache.struts.config.ModuleConfig; 32 import org.apache.struts.action.RequestProcessor; 33 34 import javax.servlet.ServletException ; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import java.io.Serializable ; 39 import java.io.IOException ; 40 import java.util.Map ; 41 import java.util.List ; 42 import java.util.Collection ; 43 44 45 46 54 public class PageFlowActionServlet extends AutoRegisterActionServlet 55 { 56 private static final Logger _log = Logger.getInstance( PageFlowActionServlet.class ); 57 58 private Handlers _handlers; 59 60 private static final ModuleConfigLocator[] DEFAULT_MODULE_CONFIG_LOCATORS = 61 { 62 new DefaultModuleConfigLocator(), 63 new LegacyModuleConfigLocator() 64 }; 65 66 67 75 protected ModuleConfigLocator[] getDefaultModuleConfigLocators() 76 { 77 return DEFAULT_MODULE_CONFIG_LOCATORS; 78 } 79 80 87 public static class DefaultModuleConfigLocator implements ModuleConfigLocator, Serializable 88 { 89 public String getModuleConfigPath( String moduleName ) 90 { 91 InternalStringBuilder moduleConfPath = new InternalStringBuilder( getGenDir() ); 92 moduleConfPath.append( '/' ).append( PageFlowConstants.PAGEFLOW_MODULE_CONFIG_PREFIX ); 93 94 if ( moduleName.length() > 1 ) 95 { 96 moduleConfPath.append( moduleName.replace( '/', '-' ) ); 97 } 98 99 moduleConfPath.append( PageFlowConstants.PAGEFLOW_MODULE_CONFIG_EXTENSION ); 100 return moduleConfPath.toString(); 101 } 102 103 protected String getGenDir() 104 { 105 return PageFlowConstants.PAGEFLOW_MODULE_CONFIG_GEN_DIR; 106 } 107 } 108 109 115 protected static class LegacyModuleConfigLocator extends DefaultModuleConfigLocator 116 { 117 protected String getGenDir() 118 { 119 return InternalConstants.WEBINF_DIR; 120 } 121 } 122 123 public void init() 124 throws ServletException 125 { 126 ServletContext servletContext = getServletContext(); 130 131 if ( ! PageFlowContextListener.isInit( servletContext ) ) 132 { 133 PageFlowContextListener.performInitializations( servletContext ); 134 } 135 136 _handlers = Handlers.get( servletContext ); 137 138 super.init(); 139 } 140 141 protected void process( HttpServletRequest request, HttpServletResponse response ) 142 throws IOException , ServletException 143 { 144 String servletPath = InternalUtils.getDecodedServletPath( request ); 147 if ( servletPath.endsWith( InternalConstants.SHARED_FLOW_EXTENSION ) || 148 servletPath.endsWith( InternalConstants.FACES_BACKING_EXTENSION ) ) 149 { 150 if ( _log.isDebugEnabled() ) 151 { 152 _log.debug( "Attempt to hit restricted URI " + servletPath + "; 404 error returned." ); 153 } 154 155 response.sendError( HttpServletResponse.SC_NOT_FOUND ); 156 return; 157 } 158 159 FlowControllerHandlerContext handlerContext = new FlowControllerHandlerContext( request, response, null ); 161 _handlers.getReloadableClassHandler().reloadClasses( handlerContext ); 162 163 super.process( request, response ); 164 } 165 166 175 public String getModuleConfPath( String modulePath ) 176 { 177 return super.getModuleConfPath( modulePath ); 178 } 179 180 186 public void addServletMapping( String servletName, String urlPattern ) 187 { 188 if ( ! urlPattern.endsWith( PageFlowConstants.JPF_EXTENSION ) ) 189 { 190 super.addServletMapping( servletName, urlPattern ); 191 } 192 } 193 194 199 protected boolean moduleCanHandlePath( ModuleConfig moduleConfig, RequestProcessor rp, String servletPath ) 200 { 201 if ( moduleConfig.getPrefix().equals( "" ) && servletPath.lastIndexOf( '/' ) > 0 202 && rp instanceof PageFlowRequestProcessor ) 203 { 204 return false; 205 } 206 207 return true; 208 } 209 210 214 protected boolean processUnhandledAction( HttpServletRequest request, HttpServletResponse response, String uri ) 215 throws IOException , ServletException 216 { 217 PageFlowRequestWrapper rw = PageFlowRequestWrapper.get( request ); 221 if ( rw.getOriginalServletPath() != null ) return false; 222 223 SharedFlowController sharedFlowToTry = null; 224 String uriBaseName = ServletUtils.getBaseName( uri ); 225 int firstDot = uriBaseName.indexOf( '.' ); 226 int lastDot = uriBaseName.lastIndexOf( '.' ); 227 228 if ( firstDot != -1 && firstDot != lastDot ) 229 { 230 String sharedFlowName = uriBaseName.substring( 0, firstDot ); 231 232 try 233 { 234 RequestContext rc = new RequestContext( request, response ); 235 Map defaultSharedFlows = FlowControllerFactory.get( getServletContext() ).getDefaultSharedFlows( rc ); 236 237 if ( defaultSharedFlows != null ) 238 { 239 sharedFlowToTry = ( SharedFlowController ) defaultSharedFlows.get( sharedFlowName ); 240 uriBaseName = uriBaseName.substring( firstDot + 1 ); 241 } 242 } 243 catch ( ClassNotFoundException e ) 244 { 245 throw new ServletException ( e ); 246 } 247 catch ( InstantiationException e ) 248 { 249 throw new ServletException ( e ); 250 } 251 catch ( IllegalAccessException e ) 252 { 253 throw new ServletException ( e ); 254 } 255 } 256 else 257 { 258 sharedFlowToTry = FlowControllerFactory.getGlobalApp( request, response, getServletContext() ); 259 } 260 261 265 if ( sharedFlowToTry != null ) 266 { 267 InternalStringBuilder sfActionURI = new InternalStringBuilder( sharedFlowToTry.getModulePath() ); 268 sfActionURI.append( '/' ); 269 sfActionURI.append( uriBaseName ); 270 rw.setOriginalServletPath( uri ); 271 ForwardRedirectHandler frh = _handlers.getForwardRedirectHandler(); 272 FlowControllerHandlerContext context = new FlowControllerHandlerContext( request, response, null ); 273 frh.forward( context, sfActionURI.toString() ); 274 return true; 275 } 276 277 return false; 278 } 279 } 280 | Popular Tags |