13

How do I retrieve the FacesContext within a Filter?

I followed following article on how to retrieve the FacesContext in a Filter:

http://ocpsoft.org/java/jsf-java/jsf-20-extension-development-accessing-facescontext-in-a-filter/

But the problem is that it is not working with Flash scope. Following NPE is thrown:

java.lang.NullPointerException
at com.sun.faces.context.flash.ELFlash.loggingGetPhaseMapForWriting(ELFlash.java:751)
at com.sun.faces.context.flash.ELFlash.getPhaseMapForWriting(ELFlash.java:785)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:392)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:112)

I want to add redirection in my filter and use flash scope to save some data and also Messages, which is not working.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
djmj
  • 5,579
  • 5
  • 54
  • 92

1 Answers1

23

How do I retrieve the FacesContext within a Filter?

You cannot. The FacesContext is created by the FacesServlet and thus only available within any Java code which is processed by the FacesServlet, which covers all JSF artifacts, such as managed beans and phase listeners. The article only shows how to manually create the FacesContext, but this approach is ultimately useless. The FacesContext is just an abstraction of everything already available by standard Servlet API such as HttpServletRequest, HttpSession, ServletContext, etc. Just use them directly the same way as JSF is doing "under the hoods".

You have 2 options:

  1. Use a JSF PhaseListener instead. Depending on the concrete functional requirement which you didn't tell anything about, this may be a rather clumsy solution/workaround.

  2. Don't use JSF-provided Flash scope facility, but homebrew one yourself. The principle is rather simple: set a cookie on initial request, send a redirect, in the redirected request lookup the cookie and remove it (so that it's not there anymore on any subsequent request). That's exactly how the JSF Flash scope works under the hoods. See also Set notification message as request attribute which should show after sendRedirect for a concrete example.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • "thus only available within any Java code which is processed by the FacesServlet, which covers all JSF artifacts, such as managed beans" Now i am confused. When I lookup a managed bean using `HttpSession.getAttribute()` the current FacesContext is still null. – djmj Dec 27 '12 at 01:51
  • 1
    The filter runs before the `FacesServlet`. So getting the session scoped managed bean from there where JSF has stored "under the covers" before the `FacesServlet` has run doesn't give you access to the `FacesContext`. – BalusC Dec 27 '12 at 02:56
  • @BalusC: what if I want to intercept Request/Response in FacesServlet where FacesContext is available? I want to set some cookies in all responses back, for which Filter is the ideal location. However, the values depend on some functionality evaluated and set inside FacesContext which comes as null in Filter. Can you please guide how to deal with such situation? – Vicky Feb 02 '19 at 00:00
  • @BalusC: That is, is there a spot inside FacesServlet where I can simply intercept all ServletRequest/Responses (like we do in a normal Filter) and set some cookies? – Vicky Feb 02 '19 at 00:11
  • @Nik: a `PhaseLIstener` listening on `beforePhase` of `RENDER_RESPONSE` would work well. – BalusC Feb 02 '19 at 13:10