You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by ss...@yahoo.com.INVALID on 2014/09/08 02:25:56 UTC

Request Filter

I am trying to figure out how to implement a RequestFilter such that when a session time out occurs, Tapestry can redirect user to my error page saying “Your session has timed out”.


Here is my code, take from examples posted by Taha in the past


public class RequireSessionFilter implements ComponentRequestFilter
{
 
    private ComponentSource componentSource;
 
    private ApplicationStateManager applicationStateManager;
 
    private Response response;
 
    public RequireSessionFilter() {
  super();
 }





 private PageRenderLinkSource pageRenderLinkSource;
 
    RequireSessionFilter(ComponentSource componentSource,
            Response response,
            ApplicationStateManager applicationStateManager,
            PageRenderLinkSource pageRenderLinkSource)
    {
        this.componentSource = componentSource;
        this.response = response;
        this.applicationStateManager = applicationStateManager;
        this.pageRenderLinkSource = pageRenderLinkSource;
    }
 
    
    
 
    private boolean redirectIfObjectNotInSession(String pageName)
    {
     System.out.println("**************** Page Name :"+ pageName);
        
     if(componentSource != null)
     {
     Component component = (Component) componentSource.getPage(pageName);
        if(component != null && component.getClass().isAnnotationPresent(RequireSession.class))
        {
            RequireSession annotation = component.getClass().getAnnotation(RequireSession.class);
 
            if(!applicationStateManager.exists(annotation.value()))
            {
             System.out.println("**************** redirect page :"+ annotation.redirectPage());
                redirect(annotation.redirectPage());
                return true;
            }
      }
     }
        return false;
    }
 
    private void redirect(String pageName)
    {
        try {
   response.sendRedirect(pageRenderLinkSource.createPageRenderLink(pageName));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }



 @Override
 public void handlePageRender(PageRenderRequestParameters parameters,
   ComponentRequestHandler handler) throws IOException {
   if(redirectIfObjectNotInSession(parameters.getLogicalPageName()))
         {
             return;
         }
  
         handler.handlePageRender(parameters);
  
 }


 @Override
 public void handleComponentEvent(
   ComponentEventRequestParameters parameters,
   ComponentRequestHandler handler) throws IOException {
        if(redirectIfObjectNotInSession(parameters.getActivePageName()))
         {
             return;
         }
  
         ((ComponentRequestHandler) handler).handleComponentEvent(parameters);



  
 }

 
}



Here is how I defined my annotation


@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequireSession {


 
    Class<?> value();
 
    String redirectPage();
 
}



Now the page that gets the annotation is also what implements a ExceptionReporter


@RequireSession(value = UBForm.class, redirectPage = "Index")
public class UBPage implements ExceptionReporter 

{

….

}


Here is my AppModule.java


@Contribute(ComponentRequestHandler.class)
    public static void contributeRequestFilters(
            final OrderedConfiguration<ComponentRequestFilter> filters) {
     
        filters.addInstance("RequireSessionFilter",
                RequireSessionFilter.class, "after:ErrorFilter");
    }


    
    public RequestExceptionHandler buildAppRequestExceptionHandler(
            final Logger logger,
            final ResponseRenderer renderer,
            final ComponentSource componentSource)
    {
        return new RequestExceptionHandler()
        {
            public void handleRequestException(Throwable exception) throws IOException
            {
                String cc = componentSource.getActivePage().getClass().getName();
                logger.error("Page reported back:"+ cc);


                //default
                String source = "ExceptionReport";
                if (cc.indexOf("UBPage") != -1)
                {
                   logger.error("Erro page was UBPage");
                   source = "UBError";
                }
                  
                if (exception instanceof OperationException)
                {
                  logger.error("Error was caused due to timeout");
                }
                logger.error("Unexpected runtime exception: " + exception.getMessage(), exception);
     
                ExceptionReporter index = (ExceptionReporter) componentSource.getPage(source);
     
                index.reportException(exception);
     
                renderer.renderPageMarkupResponse(source);
            }
        };
    }
     
    public void contributeServiceOverride(
            MappedConfiguration<Class, Object> configuration,
     
            @Local
            RequestExceptionHandler handler)
    {
        configuration.add(RequestExceptionHandler.class, handler);
    }    
    
}



When I try to go to UBPage, Tapestry is reporting the following exception in the logs


java.lang.NullPointerException
com.navicure.ui.customerapp.services.AppModule$2.handleRequestException(AppModule.java:161)
$RequestExceptionHandler_7bc7ed0ff6bc3.handleRequestException(Unknown Source)
org.apache.tapestry5.internal.services.RequestErrorFilter.service(RequestErrorFilter.java:42)
$RequestHandler_7bc7ed0ff6bf2.service(Unknown Source)
org.apache.tapestry5.services.TapestryModule$3.service(TapestryModule.java:902)
$RequestHandler_7bc7ed0ff6bf2.service(Unknown Source)
org.apache.tapestry5.services.TapestryModule$2.service(TapestryModule.java:892)
$RequestHandler_7bc7ed0ff6bf2.service(Unknown Source)
org.apache.tapestry5.internal.services.StaticFilesFilter.service(StaticFilesFilter.java:90)
$RequestHandler_7bc7ed0ff6bf2.service(Unknown Source)
com.navicure.ui.customerapp.services.AppModule$1.service(AppModule.java:117)
$RequestFilter


The error line from above in AppModule is


                String cc = componentSource.getActivePage().getClass().getName();



Looks like either componetSource is NULL or getActivePage is NULL



Can someone please help? Am I setting the RequestFilter correctly

when I say  


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