1 2 Previous Next 17 Replies Latest reply on Oct 24, 2011 6:22 PM by lightguard

    ContextNotActiveException

    hantsy

      I have a link in a page(the backbean is conversation scoped stateful session bean)...


      <a
                                         href="#{request.contextPath}/forwarding/caseExportPdfServlet?id=#{forwardingCaseEdit.currentForwardingCase.id}">
                                         Export PDF </a>
      



      I want to use a normal link to servelt to generate pdf report.


      @WebServlet("/forwarding/caseExportPdfServlet")
      public class ExportPdfServlet extends HttpServlet {
           private static final long serialVersionUID = 1L;
           private static final Logger log = LoggerFactory
                     .getLogger(ExportPdfServlet.class);
      
           @Inject
           @RequestParam
           Long id;
      
           @Inject
           EntityManager em;
      
           @Inject
           private ResourceProvider resourceProvider;
      
           @Inject
           @Jasper
           ReportCompiler compiler;
      
           @Inject
           @PDF
           @Jasper
           ReportRenderer<Report> pdfRenderer;
      
           /**
            * @see HttpServlet#HttpServlet()
            */
           public ExportPdfServlet() {
                super();
                // TODO Auto-generated constructor stub
           }
      
           /**
            * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
            *      response)
            */
           protected void doGet(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {
                processRequest(request, response);
           }
      
           /**
            * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
            *      response)
            */
           protected void doPost(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {
                processRequest(request, response);
           }
      
           protected void processRequest(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {
                if (log.isDebugEnabled()) {
                     log.debug("call processRequest");
                     log.debug("request param id@" + id);
                }
                ForwardingCase _case = em.find(ForwardingCase.class, id);
                OutputStream _out = response.getOutputStream();
      
                InputStream sourceReport = resourceProvider
                          .loadResourceStream("/reports/forwarding-case.jrxml");
                JRDataSource jasperDataSource = new JRBeanCollectionDataSource(
                          _case.getForwardingItems());
      
                // source
                ReportDefinition report = compiler.compile(sourceReport);
      
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("forwardingCase", _case);
      
                Report reportInstance = report.fill(jasperDataSource, params);
      
                // ByteArrayOutputStream os = new ByteArrayOutputStream(_out); //
                // OutputStream
                // Render output as the desired content
                pdfRenderer.render(reportInstance, _out);
                _out.flush();
           }
      
      }
      
      




      But when I clicked the link, and got the following exception:


      org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped
           org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:667)
           org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:77)
           org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:124)
           org.jboss.weld.proxies.EntityManager$ManagedPersistenceContext$-886017761$Proxy$_$$_WeldClientProxy.find(EntityManager$ManagedPersistenceContext$-886017761$Proxy$_$$_WeldClientProxy.java)
           com.telopsys.siorc.forwarding.servlets.ExportPdfServlet.processRequest(ExportPdfServlet.java:86)
           com.telopsys.siorc.forwarding.servlets.ExportPdfServlet.doGet(ExportPdfServlet.java:72)
           javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
           javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
           org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67)
           com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
           org.jboss.solder.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65)
           org.jboss.solder.servlet.event.ServletEventBridgeFilter.doFilter(ServletEventBridgeFilter.java:72)
      

        • 1. Re: ContextNotActiveException
          hantsy

          How to fix this problem?
          Thanks

          • 2. Re: ContextNotActiveException
            lightguard

            I responded in your other thread.

            • 3. Re: ContextNotActiveException
              hantsy

              I can not understand why I must append the cid in the url...
              I can not use a transient converstation int the servlet? Why weld can not start a transient conversation in the servlet ?


              <a href="#{request.contextPath}/forwarding/caseExportPdfServlet?id=#{forwardingCaseEdit.currentForwardingCase.id}&cid=#{conversation.id}">Export PDF </a>
              
              



              And got the following error.


              javax.faces.view.facelets.FaceletException: Error Parsing /forwarding/viewForwardingCase.xhtml: Error Traced[line: 43] The reference to entity "cid" must end with the ';' delimiter.
                   at com.sun.faces.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:387)
                   at com.sun.faces.facelets.compiler.SAXCompiler.doMetadataCompile(SAXCompiler.java:370)
                   at com.sun.faces.facelets.compiler.Compiler.metadataCompile(Compiler.java:123)
                   at com.sun.faces.facelets.impl.DefaultFaceletFactory.createMetadataFacelet(DefaultFaceletFactory.java:353)
                   at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:231)
                   at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:164)
                   at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:102)
                   at org.jboss.seam.faces.view.SeamViewMetadata.createMetadataView(SeamViewMetadata.java:63)
                   at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:239)
                   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
                   at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
                   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
                   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                   at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                   at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                   at org.jboss.solder.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                   at org.jboss.solder.servlet.event.ServletEventBridgeFilter.doFilter(ServletEventBridgeFilter.java:72)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
                   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
                   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139)
                   at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57)
                   at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49)
                   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154)
                   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
                   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
                   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667)
                   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952)
                   at java.lang.Thread.run(Thread.java:662)
              

              • 4. Re: ContextNotActiveException
                hantsy

                I changed the url to the following:


                <a href="#{request.contextPath}/forwarding/caseExportPdfServlet?id=#{forwardingCaseEdit.currentForwardingCase.id}&amp;cid=#{conversation.id}">Export PDF </a>
                




                Got the right url in browser.


                http://localhost:8080/siorc/forwarding/caseExportPdfServlet?id=1&cid=1
                




                But there was no lucky , I got the same exception.


                org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped
                     org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:667)
                     org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:77)
                     org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:124)
                     org.jboss.weld.proxies.EntityManager$ManagedPersistenceContext$-886017761$Proxy$_$$_WeldClientProxy.find(EntityManager$ManagedPersistenceContext$-886017761$Proxy$_$$_WeldClientProxy.java)
                     com.telopsys.siorc.forwarding.servlets.ExportPdfServlet.processRequest(ExportPdfServlet.java:93)
                     com.telopsys.siorc.forwarding.servlets.ExportPdfServlet.doGet(ExportPdfServlet.java:75)
                     javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
                     javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
                     org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67)
                     com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
                     org.jboss.solder.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65)
                     org.jboss.solder.servlet.event.ServletEventBridgeFilter.doFilter(ServletEventBridgeFilter.java:72)
                

                • 5. Re: ContextNotActiveException
                  lightguard

                  You may have to use Seam Conversation for this as you can't join a conversation. The conversation in CDI 1.1 was tied very tightly to JSF, which is why without using an extension, or creating your own conversation scope you can't use it outside of JSF.

                  • 6. Re: ContextNotActiveException
                    hantsy

                    Why I can not discard the current conversation, and start a transient conversation automatically in the servlet(without any conversation code)...

                    • 7. Re: ContextNotActiveException
                      lightguard

                      I told you why. Conversations in CDI 1.0 are tightly coupled with JSF. A servet isn't JSF.

                      • 8. Re: ContextNotActiveException
                        hantsy


                        @Inject
                             EntityManager em;
                        



                        The conversation scoped EntityManager caused the problem...is this a bug?

                        • 9. Re: ContextNotActiveException
                          lightguard

                          No.

                          • 10. Re: ContextNotActiveException
                            hantsy

                            I used none conversation scoped entityManager...


                                 @Produces
                                 @PersistenceContext
                                 @Utility
                                 EntityManager em;
                            



                            Inject it in the serlvet.


                            
                                 @Inject
                                 @Utility
                                 EntityManager em;
                            



                            The problem was disappeared.


                            Why I can not consider this is a bug? There is no document explain this...


                            • 11. Re: ContextNotActiveException
                              lightguard

                              If you understand conversations and how they don't work in a servlet until CDI 1.1 it would be apparent.


                              You're solution is working because this is going to create a @DependentScoped EntityManager, meaning it will be created at least once per request, possibly more often then that. This solution will probably have performance issues within the rest of the application.


                              You could do this from JSF perfectly fine by sending whatever you want to the output stream on the request (pull that out of the external context from the FacesContext) and calling responseComplete. You can do this from any action method. I've done it multiple times in previous jobs.

                              • 12. Re: ContextNotActiveException
                                hantsy

                                I know the jsf way, but I will encounter the same problem when I injected mail session in the conversation scoped bean.


                                Caused by: org.jboss.weld.exceptions.UnserializableDependencyException: WELD-001413 The bean @New Session bean [class com.telopsys.siorc.forwarding.ForwardingCaseEditAction with qualifiers [@New]; local interfaces are [ForwardingCaseEditAction] declares passivating scope but has non-serializable dependency Managed Bean [class org.jboss.seam.reports.jasper.JasperSeamReportCompiler] with qualifiers [@Any @Jasper]
                                     at org.jboss.weld.bootstrap.Validator.validateInjectionPointPassivationCapable(Validator.java:328)
                                     at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:290)
                                     at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106)
                                     at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:129)
                                     at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:351)
                                     at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:336)
                                     at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:404)
                                     at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:82)
                                     at org.jboss.as.weld.services.WeldService.start(WeldService.java:89)
                                     ... 5 more
                                
                                

                                • 13. Re: ContextNotActiveException
                                  hantsy

                                  The related code.


                                  // export pdf
                                       @Inject
                                       FacesContext context;
                                  
                                       @Inject
                                       @Jasper
                                       ReportCompiler compiler;
                                  
                                       @Inject
                                       @PDF
                                       @Jasper
                                       ReportRenderer<Report> pdfRenderer;
                                  
                                       public void export() throws IOException {
                                            if (log.isDebugEnabled()) {
                                                 log.debug("call export...");
                                            }
                                            ExternalContext externalContext = context.getExternalContext();
                                            externalContext.setResponseContentType("application/pdf");
                                  
                                            OutputStream _out = externalContext.getResponseOutputStream();
                                  
                                            InputStream sourceReport = resourceProvider
                                                      .loadResourceStream("/reports/forwarding-case.jrxml");
                                            JRDataSource jasperDataSource = new JRBeanCollectionDataSource(
                                                      this.currentForwardingCase.getForwardingItems());
                                  
                                            // source
                                            ReportDefinition report = compiler.compile(sourceReport);
                                  
                                            Map<String, Object> params = new HashMap<String, Object>();
                                            params.put("forwardingCase", this.currentForwardingCase);
                                  
                                            Report reportInstance = report.fill(jasperDataSource, params);
                                  
                                            // ByteArrayOutputStream os = new ByteArrayOutputStream(_out); //
                                            // OutputStream
                                            // Render output as the desired content
                                            pdfRenderer.render(reportInstance, _out);
                                            //_out.flush();
                                            externalContext.responseFlushBuffer();
                                  
                                            context.responseComplete();
                                  
                                       }
                                  
                                  



                                  It is so problematic in the CDI 1.0 spec?

                                  • 14. Re: ContextNotActiveException
                                    lightguard

                                    So mark the non-serializable injection points as transient.

                                    1 2 Previous Next