title: Failed to retrieve Session from FacesContext inside a Servlet Filter
body: <p>After autheticating my user, I want to put a reference in the session to current logged in user.</p>
  
  <p>Here how I do it in the <code>setCurrentUser</code> method : </p>
  
  <pre><code>FacesContext facesContext = FacesContext.getCurrentInstance();
  HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
  session.setAttribute("CURRENT_USER", currentUser);
  </code></pre>
  
  <p>Unfortunately, the <code>session</code> reference is always null !</p>
  
  <p>Alternatively, I tried with the sessionMap</p>
  
  <pre><code>FacesContext facesContext = FacesContext.getCurrentInstance();
  Map&lt;String, Object&gt; sessionMap = facesContext.getExternalContext().getSessionMap();
  sessionMap.put("CURRENT_USER", currentUser);
  </code></pre>
  
  <p>It miserably failed with this exception :</p>
  
  <pre><code>java.lang.UnsupportedOperationException
      at java.util.AbstractMap.put(AbstractMap.java:186)
      (...)
  </code></pre>
  
  <p>What am I doing wrong ?</p>
  
  <p>The full code of my controller </p>
  
  <p><strong>UserController.java</strong></p>
  
  <pre><code>public class UserController implements Filter {
      private FilterConfig fc;
      private static final String CURRENT_USER = "CURRENT_USER";
  
      public void init(FilterConfig filterConfig) throws ServletException {
          fc = filterConfig;
          log("&gt;&gt; Filter initialized");
      }
  
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
          // Authenticate user
          // ...
  
          // Save refernce in Session
          setCurrentUser(currentUser);
  
          //(...)
      }
  
      public static void setCurrentUser(User u) {
          FacesContext facesContext = FacesContext.getCurrentInstance();
          HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
          session.setAttribute(CURRENT_USER, u);// session is always NULL
      }
  
      public static User getCurrentUser() {
          FacesContext facesContext = FacesContext.getCurrentInstance();
          HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);       
          return (User)session.getAttribute(CURRENT_USER);
      }
  
      //...   
  }
  </code></pre>
  
  <p><strong>JSF 2.0</strong><br>
  <strong>JBoss 5.1.0.GA</strong></p>