Tapestry: Using ComponentRequestFilter

This example is similar to Securing Tapestry pages with Annotations, Part 1. My use case is to redirect a user to the starting page of a multiple step wizard in case a request is directly made to an intermediate step and the session has not been properly setup. One way to solve this problem (the ugly way) is to add a condition to every entry point in the page

public class MyPage
    @SessionState(create = false)
    private ShoppingCart shoppingCart;

    Object activate()
        if(shoppingCart == null)
           return Index.class;
        return null;

    Object doSomething()
        if(shoppingCart == null
           return Index.class;


The problem with this code is that you have to check this condition for all event-handlers(Form, EventLink etc). A more elegant solution would be to use annotation secured by a ComponentRequestFilter.

@RequireSession(value = ShoppingCart.class, redirectPage = "Index")
public class MyPage {

So let us start with the annotation

@interface RequireSession {

    Class<?> value()

    String redirectPage()


The RequireSession#value() should be set to the class which should be available(as instance) in the session when the page is requested. If the class is not available, the user should be redirected to the RequireSession#redirectPage() page.

The ComponentRequestFilter to do the magic is :-

class RequireSessionFilter implements ComponentRequestFilter

    private ComponentSource componentSource

    private ApplicationStateManager applicationStateManager

    private Response response

    private PageRenderLinkSource pageRenderLinkSource

    RequireSessionFilter(ComponentSource componentSource,
            Response response,
            ApplicationStateManager applicationStateManager,
            PageRenderLinkSource pageRenderLinkSource)
        this.componentSource = componentSource
        this.response = response
        this.applicationStateManager = applicationStateManager
        this.pageRenderLinkSource = pageRenderLinkSource

    void handleComponentEvent(ComponentEventRequestParameters parameters,
            ComponentRequestHandler handler)


    void handlePageRender(PageRenderRequestParameters parameters, ComponentRequestHandler handler)


    private boolean redirectIfObjectNotInSession(String pageName)
        Component component = componentSource.getPage(pageName)
            RequireSession annotation = component.class.getAnnotation(RequireSession)

                return true

        return false

    private void redirect(String pageName)


It filters both the page render requests and the component event requests. In case there is a @RequiresSession annotation, the session is checked for the given class and if it is not present, the user is redirected to the RequireSession#redirectPage() page.

Finally the ComponentRequestFilter contribution in AppModule

    public static void contributeRequestFilters(
            final OrderedConfiguration<ComponentRequestFilter> filters) {

                RequiresSessionFilter.class, "after:ErrorFilter");

Tagged: ,

4 thoughts on “Tapestry: Using ComponentRequestFilter

  1. Massimo Lusetti July 3, 2012 at 7:39 PM Reply

    You are using reflection every time you check for a page rendering or component event and that could be optimized, but this is definitively a nice use case.


  2. sody (@sod_y) July 4, 2012 at 12:50 AM Reply

    Are you using groovy for your application? And have you any problems with it?

    P.S. Agree with previous comment about optimization, I solve this using MetaDataLocator/MetaDataExtractor

  3. josete July 31, 2012 at 7:08 PM Reply

    Nice article .. i like tawus, always some way to surprise me in tapestry🙂
    Do @sod_y paste how he did use metadatalocator to improve this ?
    Thanks !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 92 other followers

%d bloggers like this: