DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Tapestry: Using ComponentRequestFilter

Tapestry: Using ComponentRequestFilter

Taha Siddiqi user avatar by
Taha Siddiqi
·
Jul. 12, 12 · Interview
Like (0)
Save
Tweet
Share
2.81K Views

Join the DZone community and get the full member experience.

Join For Free

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;

    @OnEvent(EventConstants.ACTIVATE)
    Object activate()
    {
        if(shoppingCart == null)
        {
           return Index.class;
        }
        return null;
    }

    @OnEvent(EventConstants.SUCCESS)
    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

@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@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
    }

    @Override
    void handleComponentEvent(ComponentEventRequestParameters parameters,
            ComponentRequestHandler handler)
    {
        if(redirectIfObjectNotInSession(parameters.activePageName))
        {
            return
        }

        handler.handleComponentEvent(parameters);
    }

    @Override
    void handlePageRender(PageRenderRequestParameters parameters, ComponentRequestHandler handler)
    {
        if(redirectIfObjectNotInSession(parameters.logicalPageName))
        {
            return
        }

        handler.handlePageRender(parameters)
    }

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

            if(!applicationStateManager.exists(annotation.value()))
            {
                redirect(annotation.redirectPage())
                return true
            }
        }

        return false
    }

    private void redirect(String pageName)
    {
        response.sendRedirect(pageRenderLinkSource.createPageRenderLink(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

@Contribute(ComponentRequestHandler.class)
public static void contributeRequestFilters(
        final OrderedConfiguration<ComponentRequestFilter> filters) {

    filters.addInstance("RequiresSessionFilter",
            RequiresSessionFilter.class, "after:ErrorFilter");
}

 

 

 

Annotation Use case Session (web analytics) Requests Filter (software) Event

Published at DZone with permission of Taha Siddiqi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • Fargate vs. Lambda: The Battle of the Future
  • Top 10 Best Practices for Web Application Testing
  • When to Choose Redpanda Instead of Apache Kafka

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: