1 18 package org.apache.beehive.netui.pageflow.interceptor.action; 19 20 import org.apache.beehive.netui.pageflow.PageFlowController; 21 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 22 import org.apache.beehive.netui.pageflow.interceptor.request.RequestInterceptorContext; 23 import org.apache.beehive.netui.util.logging.Logger; 24 import org.apache.beehive.netui.util.config.ConfigUtil; 25 import org.apache.beehive.netui.util.config.bean.PageflowActionInterceptors; 26 import org.apache.struts.action.ActionForward; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletRequest ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.Enumeration ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.ArrayList ; 38 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap; 39 import java.io.Serializable ; 40 41 42 45 public class ActionInterceptorContext 46 extends RequestInterceptorContext 47 { 48 private static final String ACTIVE_INTERCEPTOR_CONTEXT_ATTR = InternalConstants.ATTR_PREFIX + "interceptorContext"; 49 private static final String CACHE_ATTR = InternalConstants.ATTR_PREFIX + "actionInterceptorConfig"; 50 51 private static final Logger _log = Logger.getInstance( ActionInterceptorContext.class ); 52 53 private PageFlowController _pageFlow; 54 private InterceptorForward _originalForward; 55 private String _actionName; 56 57 58 public ActionInterceptorContext( HttpServletRequest request, HttpServletResponse response, 59 ServletContext servletContext, PageFlowController controller, 60 InterceptorForward originalForward, String actionName ) 61 { 62 super( request, response, servletContext ); 63 _pageFlow = controller; 64 _originalForward = originalForward; 65 _actionName = actionName; 66 } 67 68 71 public PageFlowController getPageFlow() 72 { 73 return _pageFlow; 74 } 75 76 81 public InterceptorForward getOriginalForward() 82 { 83 return _originalForward; 84 } 85 86 89 public String getActionName() 90 { 91 return _actionName; 92 } 93 94 99 public void setOverrideForward( InterceptorForward fwd, ActionInterceptor interceptor ) 100 { 101 setResultOverride( fwd, interceptor ); 102 103 if ( _originalForward == null ) _originalForward = new OriginalForward( getRequest() ); 108 109 getRequest().setAttribute( ACTIVE_INTERCEPTOR_CONTEXT_ATTR, this ); 113 } 114 115 public ActionInterceptor getOverridingActionInterceptor() 116 { 117 return ( ActionInterceptor ) super.getOverridingInterceptor(); 118 } 119 120 public InterceptorForward getInterceptorForward() 121 { 122 return ( InterceptorForward ) getResultOverride(); 123 } 124 125 public boolean hasInterceptorForward() 126 { 127 return hasResultOverride(); 128 } 129 130 public static ActionInterceptorContext getActiveContext( ServletRequest request, boolean consume ) 131 { 132 ActionInterceptorContext context = 133 ( ActionInterceptorContext ) request.getAttribute( ACTIVE_INTERCEPTOR_CONTEXT_ATTR ); 134 if ( consume ) request.removeAttribute( ACTIVE_INTERCEPTOR_CONTEXT_ATTR ); 135 return context; 136 } 137 138 139 private static class OriginalForward extends InterceptorForward 140 { 141 private Map _savedAttrs; 142 143 144 public OriginalForward( HttpServletRequest request ) 145 { 146 super( request ); 147 saveRequestAttrs( request ); 148 } 149 150 private void saveRequestAttrs( ServletRequest request ) 151 { 152 _savedAttrs = new HashMap (); 153 154 for ( Enumeration e = request.getAttributeNames(); e.hasMoreElements(); ) 155 { 156 String attrName = ( String ) e.nextElement(); 157 Object attrVal = request.getAttribute( attrName ); 158 159 if ( attrVal instanceof Serializable ) 160 { 161 _savedAttrs.put( attrName, attrVal ); 162 } 163 else 164 { 165 if ( _log.isWarnEnabled() ) 166 { 167 _log.warn( "Dropping non-serializable request attribute " + attrName + " (" + attrVal + ")." ); 168 } 169 } 170 } 171 } 172 173 public void rehydrateRequest( ServletRequest request ) 174 { 175 if ( _savedAttrs != null ) 176 { 177 for ( Iterator i = _savedAttrs.entrySet().iterator(); i.hasNext(); ) 178 { 179 Map.Entry entry = ( Map.Entry ) i.next(); 180 181 String attrName = ( String ) entry.getKey(); 182 if ( request.getAttribute( attrName ) == null ) 183 { 184 request.setAttribute( attrName, entry.getValue() ); 185 } 186 } 187 } 188 } 189 } 190 191 public List getActionInterceptors() 192 { 193 ServletContext servletContext = getServletContext(); 194 InternalConcurrentHashMap cache = 195 ( InternalConcurrentHashMap ) servletContext.getAttribute( CACHE_ATTR ); 196 197 if ( cache == null ) 198 { 199 cache = new InternalConcurrentHashMap(); 205 servletContext.setAttribute( CACHE_ATTR, cache ); 206 } 207 208 String modulePath = getPageFlow().getModulePath(); 209 String actionName = getActionName(); 210 HashMap cacheByPageFlow = ( HashMap ) cache.get( modulePath ); 211 if ( cacheByPageFlow != null ) 212 { 213 List interceptors = ( List ) cacheByPageFlow.get( actionName ); 214 if ( interceptors != null ) return interceptors; 215 } 216 217 if ( cacheByPageFlow == null ) cacheByPageFlow = new HashMap (); 221 PageflowActionInterceptors config = ConfigUtil.getConfig().getPageflowActionInterceptors(); 222 ArrayList interceptorsList = new ArrayList (); 223 224 if ( config == null ) 225 { 226 cacheByPageFlow.put( actionName, interceptorsList ); 227 cache.put( modulePath, cacheByPageFlow ); 228 return interceptorsList; 229 } 230 231 PageflowActionInterceptors.Global globalInterceptors = config.getGlobal(); 235 236 if ( globalInterceptors != null ) 237 { 238 addInterceptors( globalInterceptors.getActionInterceptorArray(), interceptorsList, ActionInterceptor.class ); 239 addSimpleInterceptors( globalInterceptors.getSimpleActionInterceptorArray(), interceptorsList ); 240 } 241 242 String pageFlowURI = getPageFlow().getURI(); 246 PageflowActionInterceptors.PerPageflow[] perPageFlowInterceptorsConfig = config.getPerPageflowArray(); 247 248 if ( perPageFlowInterceptorsConfig != null ) 249 { 250 for ( int i = 0; i < perPageFlowInterceptorsConfig.length; i++ ) 251 { 252 PageflowActionInterceptors.PerPageflow ppfi = perPageFlowInterceptorsConfig[i]; 253 254 if ( ppfi != null && pageFlowURI.equals( ppfi.getPageflowUri() ) ) 255 { 256 addInterceptors( perPageFlowInterceptorsConfig[i].getActionInterceptorArray(), interceptorsList, 260 ActionInterceptor.class ); 261 addSimpleInterceptors( perPageFlowInterceptorsConfig[i].getSimpleActionInterceptorArray(), 262 interceptorsList ); 263 264 PageflowActionInterceptors.PerPageflow.PerAction[] perActionConfigs = 265 perPageFlowInterceptorsConfig[i].getPerActionArray(); 266 267 if ( perActionConfigs != null ) 268 { 269 for ( int j = 0; j < perActionConfigs.length; j++ ) 270 { 271 PageflowActionInterceptors.PerPageflow.PerAction perActionConfig = perActionConfigs[j]; 272 273 if ( perActionConfig != null && actionName.equals( perActionConfig.getActionName() ) ) 274 { 275 addInterceptors( perActionConfig.getActionInterceptorArray(), interceptorsList, 279 ActionInterceptor.class ); 280 addSimpleInterceptors( perActionConfig.getSimpleActionInterceptorArray(), 281 interceptorsList ); 282 } 283 } 284 } 285 } 286 } 287 } 288 289 cacheByPageFlow.put( actionName, interceptorsList ); 290 cache.put( modulePath, cacheByPageFlow ); 291 return interceptorsList; 292 } 293 294 private static void addSimpleInterceptors( org.apache.beehive.netui.util.config.bean.SimpleActionInterceptor[] configBeans, 295 List interceptorsList ) 296 { 297 for ( int i = 0; i < configBeans.length; i++ ) 298 { 299 org.apache.beehive.netui.util.config.bean.SimpleActionInterceptor configBean = configBeans[i]; 300 String path = configBean.getInterceptPath(); 301 boolean afterAction = configBean.getAfterAction(); 302 SimpleActionInterceptorConfig config = new SimpleActionInterceptorConfig( path, afterAction ); 303 interceptorsList.add( new SimpleActionInterceptor( config ) ); 304 } 305 } 306 307 public void setOriginalForward( ActionForward origFwd ) 308 { 309 _originalForward = origFwd != null ? new InterceptorForward( origFwd, getServletContext(), _pageFlow ) : null; 310 } 311 312 public static void init( ServletContext servletContext ) 313 { 314 } 316 } 317 | Popular Tags |