glassfish
  1. glassfish
  2. GLASSFISH-20982

@RequestScoped and @SessionScoped beans do not work in requests initiated by a forward

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Critical Critical
    • Resolution: Cannot Reproduce
    • Affects Version/s: 4.0
    • Fix Version/s: None
    • Component/s: cdi
    • Labels:
      None
    • Environment:

      Linux, jdk 1.7.0_45

      Description

      When a method on a @RequestScoped or @SessionScoped bean is invoked where the request came from a forward, a org.jboss.weld.context.ContextNotActiveException is thrown.

      In my setup, I have a second application that handles some resource requests. In my main app, I request the resource with a forward:

      request.getServletContext().getContext("/resource").getRequestDispatcher(path).forward(request, response)
      

      I also have some common auditing code which uses @RequestScoped and @SessionScoped beans. These beans work correctly in the main app with a user invoked request, but not in the servlet handling the forward (and possibly include).

      @RequestScoped
      public class RequestDataTracker {
      
          private String action;
          private final UUID requestId = UUID.randomUUID();
          private String sessionId;
          private String username;
      
          public String getSessionId() {
              return sessionId;
          }
      
          public void setSessionId(String sessionId) {
              if (this.sessionId == null) {
                  this.sessionId = sessionId;
              }
          }
      
          public String getUsername() {
              return username;
          }
      
          public void setUsername(String username) {
              if (this.username == null) {
                  this.username = username;
              }
          }
      
          public UUID getRequestId() {
              return requestId;
          }
      
          public String getAction() {
              return action;
          }
      
          public void setAction(String action) {
              this.action = action;
          }
      
          @Override
          public String toString() {
              return "RequestDataTracker{" + "action=" + action + ", requestId=" + requestId + ", sessionId=" + sessionId + ", username=" + username + '}';
          }
      
      }
      

      I have a BaseLoggingServlet that extends HttpServlet that has the logic for logging. The LoggingServlet extends that and implements methods to get and set data in an injected RequestDataTracker

      public abstract class LoggingServlet extends BaseLoggingServlet {
      
          @Inject
          protected RequestDataTracker requestDataTracker;
      
          @Override
          protected String getUsername(HttpServletRequest request) {
              String username = SessionUtil.getSessionObject(request, CmdCtrConsts.USERNAME, String.class, false);
              if(username == null){
                  username = (String) request.getAttribute(CmdCtrConsts.ACTUAL_USERNAME);
              }
              requestDataTracker.setUsername(username);
              return requestDataTracker.getUsername();
          }
      
          @Override
          protected UUID getRequestId(HttpServletRequest request) {
               return requestDataTracker.getRequestId();
          }
      
          @Override
          protected String getSessionId(HttpServletRequest request) {
              String sessionId = super.getSessionId(request);
              requestDataTracker.setSessionId(sessionId);
              return requestDataTracker.getSessionId();
          }
      }
      

      The following exception is thrown:

      org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped
      	at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:667)
      	at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:74)
      	at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
      	at com.me.myproduct.logging.performance.RequestDataTracker$Proxy$_$$_WeldClientProxy.setSessionId(Unknown Source)
      	at com.me.myproduct.core.common.logging.LoggingServlet.logAndProcess(LoggingServlet.java:27)
      	at com.me.myproduct.core.common.logging.LoggingServlet.doGet(LoggingServlet.java:48)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      	at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
      	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
      	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
      	at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
      	at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
      	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
      	at com.me.myproduct.core.servlets.RequestDispatcherUtil.forward(RequestDispatcherUtil.java:51)
      	at com.me.myproduct.core.servlets.portlet.ServeResourceDispatcherServlet.processRequest(ServeResourceDispatcherServlet.java:64)
      	at com.me.myproduct.core.common.logging.LoggingServlet.logAndProcess(LoggingServlet.java:35)
      	at com.me.myproduct.core.common.logging.LoggingServlet.doGet(LoggingServlet.java:48)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      	at com.me.myproduct.core.filters.SecurityFilter.doFilter(SecurityFilter.java:99)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
      	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      	at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
      	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
      	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
      	at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
      	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
      	at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
      	at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
      	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
      	at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
      	at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
      	at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      	at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
      	at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
      	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
      	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
      	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
      	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
      	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      	at java.lang.Thread.run(Thread.java:744)
      

        Activity

        Hide
        jjsnyder83 added a comment -

        Please provide a reproducible test case.

        Show
        jjsnyder83 added a comment - Please provide a reproducible test case.

          People

          • Assignee:
            jjsnyder83
            Reporter:
            electricsam
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: