4 Replies Latest reply on Apr 1, 2008 3:05 AM by Drew Kutchar

    Can someone explain how Seam Filters work?

    Drew Kutchar Newbie

      I wasn't able to find a lot of documentation on Seam Filters (extending org.jboss.seam.web.AbstractFilter). I have created a simple filter and I was able to install it and I can see it being called when I hit the web pages. Now I would like to set some information in the Session Context based on the URL, but it seems  that Filters don't have knowledge of Seam contexts. I can't even raise an event using Events.instance().raiseEvent().


      I assume this is because I have @BypassInterceptors on my filter class, but I am not sure why I need that there. It seems like all the built-in Seam filters have that annotation.


      Thanks,
      DK


        • 1. Re: Can someone explain how Seam Filters work?
          Clint Popetz Apprentice

          Based on my understanding of seam (which is by no way authoratative) you are correct: in the general case you don't have access to seam contexts when a filter is called.  There are two ways Seam sets up the contexts:




          • If you're using JSF, the contexts are set up based upon the JSF lifecycle.  For instance, the Event and Session contexts are created and restored just before the restore-view phase.  So for JSF, a servlet filter cannot access seam contexts.  But you can @Observe the org.jboss.seam.beforePhase (or afterPhase) events for the facelets lifecycle from a standard component (not a Filter), and have access to contexts from there.  So, for example, if you @Observe beforePhase and take action when the phaseId is RESTORE_VIEW, and @Observe afterPhase and take action when the phaseId is RENDER_RESPONSE, that's roughly equivalent to a filter for the jsf case.




          • If you're not using JSF, and have the seam <web:context-filter/> mapped for a set of URLs, then that filter sets up the contexts.  In that case, I think you can annotate your filter with @Filter(within=""org.jboss.seam.web.contextFilter") and have access to contexts and events. 



          In both cases, Bijection should work as long as you haven't annotated your component with BypassInterceptors.



          Hope that helps.  The seam source is your best guide to understanding guts like this, and it's very well written and pretty well commented.

          • 2. Re: Can someone explain how Seam Filters work?
            Clint Popetz Apprentice

            Re-reading that...I said something confusing:




            But you can @Observe the org.jboss.seam.beforePhase (or afterPhase) events for the facelets lifecycle from a standard component (not a Filter), and have access to contexts from there.

            But of course @Filters are components, so you can @Observe a seam jsf phase event from them.  But that seems obtuse to me, because when I see a @Filter I sort of expect it to work on the level of the HttpServletRequest.  (The @Filter annotation is really just a way to avoid registering your filter in web.xml, and it provides nice ordering too.)

            • 3. Re: Can someone explain how Seam Filters work?
              Mark Michalek Newbie

              Have you tried accessing the Seam context this way?



               new ContextualHttpServletRequest() {
                 public void process() {
                    // Do you work
                 }
               }.run();


              • 4. Re: Can someone explain how Seam Filters work?
                Drew Kutchar Newbie

                Thanks Mark, that fixed it. I gave you 5 stars!