Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: indentation

...

  • LifecycleStage is an enum that describes the stages through which a request progresses. This is discussed in great detail in the Lifecycles etcEtc. page.
  • Interceptor defines the interface contract that interceptors use.
  • Intercepts is an annotation used to mark interceptors with the stages that they will intercept
  • ExecutionContext wraps up all the context associated with an ActionBean invocation, and is supplied to interceptors

...

Code Block
titleNoisyInterceptor.java
 
@Intercepts({LifecycleStage.ActionBeanResolution, 
    LifecycleStage.HandlerResolution, 
    LifecycleStage.BindingAndValidation, 
    LifecycleStage.CustomValidation, 
    LifecycleStage.EventHandling, 
    LifecycleStage.ResolutionExecution}) 
public class NoisyInterceptor implements Interceptor { 
    public Resolution intercept(ExecutionContext ctx) throws Exception { 
        System.out.println("Before " + ctx.getLifecycleStage()); 
        Resolution resolution = ctx.proceed(); 
        System.out.println("After " + ctx.getLifecycleStage()); 
        return resolution 
    } 
} 

While this interceptor does nothing of practical value, it does illustrate some basic concepts. Firstly the @Intercepts is used to specify what lifecycle stages are intercepted. In this case we specify all stages, so the interceptor will be invoked up to six times during every request to an ActionBean! Secondly, interceptors intercept around the lifecycle stage. As a result they can execute code before it and after it. When the interceptor is ready to execute the lifecycle code (and/or any downstream interceptors) it simply calls ExecutionContext.proceed().

...

Code Block
xml
xml
title"Configuring Interceptors in the web.xml (Stripes 1.5 and up)"
 
<init-param> 
    <param-name>Interceptor.Classes</param-name> 
    <param-value> 
        com.myco.NoisyInterceptor 
    </param-value> 
</init-param> 
</filter> 

...

Code Block
xml
xml
title"Configuring Interceptors in the web.xml (Stripes 1.4.x and earlier)"
 
<init-param> 
    <param-name>Interceptor.Classes</param-name> 
    <param-value> 
        com.myco.NoisyInterceptor, 
        net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor
    </param-value> 
</init-param> 
</filter> 

...

Code Block
titleSecurityInterceptor.java
 
@Intercepts(LifecycleStage.HandlerResolution) 
public class SecurityInterceptor implements Interceptor { 
    /** Intercepts execution and checks that the user has appropriate permissions. */ 
    public Resolution intercept(ExecutionContext ctx) throws Exception { 
        Resolution resolution = ctx.proceed(); 

        if (isPermitted(ctx.getActionBean(), ctx.getActionBeanContext()) { 
            return resolution; 
        } 
        else if (loggedIn(ctx.getActionBeanContext()) { 
            return new RedirectResolution("/security/Unauthorized.jsp"); 
        } 
        else { 
            return new RedirectResolution("/security/Login.jsp"); 
        } 
    } 

    /** Returns true if the user is logged in. */ 
    protected boolean isLoggedIn(ActionBeanContext ctx) { 
        return ((MyActionBeanContext) ctx).getUser() != null; 
    } 

    /** Returns true if the user is permitted to invoke the event requested. */ 
    protected boolean isPermitted() { ... } 
} 

...