Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get a SessionScoped CDI bean from inside a Filter?
    text
    copied!<p>This question is related to a previous one on <a href="https://stackoverflow.com/questions/7796612/what-is-the-method-for-gracefully-handling-session-timeout">writing a session timeout handler</a>.</p> <p>The answer in that thread involved accessing various session-scoped managed beans from the servlet. The recommendation (as seen <a href="https://stackoverflow.com/questions/6883430/prevent-accessing-restricted-page-without-login-in-jsf2">here</a>) is to do this in the filter:</p> <pre><code>HttpSession session = request.getSession(false); User user = (session != null) ? (User) session.getAttribute("user") : null; </code></pre> <p>Presumably this fetches a session bean of class <strong>User</strong>. The problem is this doesn't work.</p> <p>What goes wrong are that the beans are there in the session attributes, but they are wrapped by Weld facilities. I wrote the <strong>doFilter()</strong> method as follows:</p> <pre><code>public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; String sp = req.getServletPath(); System.out.println("------------------------"); System.out.println("doFilter(): " + sp); if (!sp.startsWith("/javax")) { // eliminates many requests HttpSession session = req.getSession(); Enumeration&lt;String&gt; en = session.getAttributeNames(); int count = 0; while (en.hasMoreElements()) { String e = en.nextElement(); System.out.println("Attribute " + ++count + ": " + e); } } chain.doFilter(request, response); } </code></pre> <p>When this dumps out the session attributes, I typically get something like this:</p> <pre><code>INFO: ------------------------ INFO: doFilter(): /Display.xhtml INFO: Attribute 1: org.jboss.weld.context.http.HttpSessionContext#org.jboss.weld.bean-WEB-INF/lib/myfaces-extcdi-bundle-jsf20-1.0.1-ManagedBean-class org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.EditableWindowContextManagerProxy INFO: Attribute 2: org.jboss.weld.context.http.HttpSessionContext#org.jboss.weld.bean-MyApp5-ManagedBean-class com.app.Login INFO: Attribute 3: org.jboss.weld.context.conversation.ConversationIdGenerator INFO: Attribute 4: com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap INFO: Attribute 5: org.jboss.weld.context.ConversationContext.conversations INFO: Attribute 6: facelets.ui.DebugOutput INFO: Attribute 7: javax.faces.request.charset INFO: Attribute 8: org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowContext:EXISTING_WINDOW_ID_LIST </code></pre> <p>Attribute #2 seems to represent the bean that I want. Needless to say a call to <strong>session.getAttribute("login")</strong> doesn't work.</p> <p>Can anyone say how to access the underlying managed bean? I would prefer to do it in a way that was not tied to Weld, but that may not be possible.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload