4 Replies Latest reply on Sep 23, 2011 1:05 AM by John Okrasa

    org.jboss.weld.context.NonexistentConversationException: WELD-000321 No conversation found to restore for id 1

    John Okrasa Newbie

      Hello I'm trying to find a way to catch and redirect back to the login (restore a converstion or start a new one)


      in web.xml I have...




          <session-config>
              <session-timeout>1</session-timeout>
          </session-config>





      and




        <context-param>
          <param-name>org.apache.myfaces.ERROR_HANDLER</param-name>
          <param-value>org.mcgill.phire.jsf.exception.MyExceptionHandler</param-value>
        </context-param>





      so this will throw the


      org.jboss.weld.context.NonexistentConversationException: WELD-000321 No conversation found to restore for id 1  error


      I try to handle it...


      in faces-config I have...




          <factory>
              <exception-handler-factory>org.mcgill.phire.jsf.exception.MyExceptionHandlerFactory</exception-handler-factory>
          </factory>







      public class MyExceptionHandlerFactory extends ExceptionHandlerFactory {
      
        private ExceptionHandlerFactory parent;
      
        // this injection handles jsf
        public MyExceptionHandlerFactory(ExceptionHandlerFactory parent) {
          this.parent = parent;
        }
      
        //create your own ExceptionHandler
        @Override
        public ExceptionHandler getExceptionHandler() {
          ExceptionHandler result =
              new MyExceptionHandler(parent.getExceptionHandler());
          return result;
        }
      
      }
      
      --------------------------------------------------------
      
      @HandlesExceptions
      public class MyExceptionHandler extends ExceptionHandlerWrapper {
      
        //private static Log log = LogFactory.getLog(MyExceptionHandler.class);
        private static final Logger LOG = LoggerFactory.getLogger(MyExceptionHandler.class);
        private ExceptionHandler wrapped;
      
        public MyExceptionHandler(ExceptionHandler wrapped) {
          this.wrapped = wrapped;
        }
      
        @Override
        public ExceptionHandler getWrapped() {
          return wrapped;
        }
      
        @Override
        public void handle() throws FacesException {
          //Iterate over all unhandeled exceptions
          Iterator i = getUnhandledExceptionQueuedEvents().iterator();
          while (i.hasNext()) {
            ExceptionQueuedEvent event = (ExceptionQueuedEvent)i.next();
            ExceptionQueuedEventContext context =
              (ExceptionQueuedEventContext)event.getSource();
      
            //obtain throwable object
            Throwable t = context.getException();
      
            //here you do what ever you want with exception
            try{
            //log error
              LOG.error("Serious error happened!", t);
              ExceptionHandlerUtil.handleNavigationWithError("m300123");
                
              //redirect to error view etc....
            }finally{
              //after exception is handeled, remove it from queue
              i.remove();
            }
          }
          //let the parent handle the rest
          getWrapped().handle();
        }
      }
      
      ----------------------------------------------------
      
      TRYING EVERYTHING HERE BUT ALWAYS AN UNHANDLED CONVERSATION
      
      
      @HandlesExceptions
      public class ExceptionHandlerUtil {
      
          private static final Logger LOG = LoggerFactory.getLogger(MyExceptionHandler.class);
      
          private static BeanManager mgr = null;
      
          private ExceptionHandlerUtil() {
              //no-op
          }
      
          protected static void handleNavigationWithError(String errorNumber) {
              handleNavigationWithError(errorNumber, null, null);
          }
      
          private static BeanManager getBeanManager() {
      
              if (mgr == null) {
                  ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
                  mgr = (BeanManager) ctx.getAttribute(BeanManager.class.getName());
              }
              return mgr;
          }
      
          protected static void handleNavigationWithError(String errorNumber, String parameterName, Object parameterValue) {
              FacesContext fc = FacesContext.getCurrentInstance();
              //check for conversation context - this doesn't work...
      /*        try {
                  BeanManager bm = getBeanManager();
                  //(BeanManager)new InitialContext().lookup("java:comp/BeanManager");
                  if (bm.isScope(ConversationScoped.class)) {
                      Set<Bean<?>> conversations =  bm.getBeans(Conversation.class);
                      //for (javax.enterprise.inject.spi.Bean b : conversations) {
      //                for(org.jboss.weld.bean.builtin.ConversationBean b : conversations){
      //                     Conversation c = (Conversation) b;
      //                     if (!c.isTransient()) {
      //                        c.end();
      //                    }
      //                }
                  }
              } catch (Exception ex) {
                  //no-op
              }*/
      //        ConversationContext conversationContext = fc.getExternalContext().instance()
      //                .deploymentManager().instance().select(Context.class)
      //                .select(HttpConversationContext.class).get();
      //
      //        conversationContext.activate(null);
      
              Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
              NavigationHandler nav = fc.getApplication().getNavigationHandler();
      
              if (parameterName != null) {
                  requestMap.put(parameterName, parameterValue);
              }
      
              fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorNumber, null));
      
              try{
                  nav.handleNavigation(fc, null, "go_to_login");
                  fc.renderResponse();
              }
               catch (Exception e) {
      //           FacesContext ctx = FacesContext.getCurrentInstance();
      //           ExceptionQueuedEventContext eventContext = new ExceptionQueuedEventContext(ctx, e);
      //           eventContext.getAttributes().put("key", "org.jboss.weld.context.NonexistentConversationException");
      //           ctx.getApplication().publishEvent(ctx, ExceptionQueuedEvent.class, eventContext);
      /*              HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
                      response.addHeader("Pragma", "no-cache");
                      response.addHeader("Cache-Control", "no-cache");
                      // Stronger according to blog comment below that references HTTP spec
                      response.addHeader("Cache-Control", "no-store");
                      response.addHeader("Cache-Control", "must-revalidate");
                      // some date in the past
                      response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT");
                  try{
                   response.sendRedirect("/login/login.xhtml");       //jsf?faces-redirect=true");
                  }
                  catch(Exception ex){
                      LOG.error(e.getMessage());
                  }*/
               }
      
          }
      }
      




      Surely there must be something simple to catch/handle a session timeout or lost conversation?


      I am using GlassFish 3.1.1 all updated...


      the session timeout is intentional but I want to redirect back to the login/landing...


      Any help/suggestions greatly appreciated..


      Best Regards,


      JOHN.