4

In my application I have a WebFilter. This Webfilter should check a coockie. But the use of FacesContext.getCurrentInstance() gives a Nullpointer exception. How can I solve this?

The WebFilter:

@Inject
private CookieManager cm;   

[...]

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if(cm.isDoCheck()){
        cm.doCheck();
    }
    chain.doFilter(request, response);
}
[...]

The CookieManager which does the FacesContext.getCurrentInstance():

[...]
private void doCheck(){
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, Object> cookies = context.getExternalContext().getRequestCookieMap();

    Cookie cookie = (Cookie) cookies.get("frontend");
    if(cookie != null){
        setSessionHash(cookie.getValue());
    }
}
[...]

context.getExternalContext().getRequestCookieMap(); gives the

StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
timmornYE
  • 708
  • 2
  • 8
  • 22

1 Answers1

5

The FacesContext is created by FacesServlet. Any servlet filter is invoked before any servlet. The FacesContext is therefore per definition not available in any servlet filter.

As to the concrete functional requirement of grabbing the request cookies, you also seem to have completely missed the fact that FacesContext is a facade of among others the ServletRequest and ServletResponse. The methods of ExternalContext all delegate under the covers to ServletRequest/ServletResponse methods (this is clearly mentioned in its javadoc, for example getRequestCookieMap()). The cookie methods you need is just readily available via ServletRequest argument of the doFilter() method.

HttpServletRequest hsr = (HttpServletRequest) request;
Cookie[] cookies = hsr.getCookies();
// Loop over cookies to find the one matching the name.

Noted should be that there is a hack/workaround available to create FacesContext in the filter yourself based on ServletRequest and ServletResponse variables, but this makes after all no utter sense if the information is readily available in those variables themselves.

I suggest to take a little JSF pause and learn basic Servlet API as well. That's basically what JSF is using under the covers (you see, its FacesServlet is "just" a servlet). Reading the method descriptions in the ExternalContext javadoc should also hint you where exactly in the basic Servlet API all those methods are getting their information from.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you @BalusC. I understand. I will study this basics. At all, the question which raises for me after your answer: What would be the right way to do what I want? I must call this `doCheck()` method also from other methods of my application, not only from the filter. So the approach to use the `ServletRequest` argument of the `doFilter()` method is not suitable for me. – timmornYE Aug 16 '13 at 11:45
  • 1
    A filter is the perfect place to check a cookie on a broad range of requests. A filter allows you to change the request destination by simply calling dispatcher.forward() or response.sendRedirect() instead of chain.doFilter(). See also http://stackoverflow.com/tags/servlet-filters/info – BalusC Aug 16 '13 at 11:46