dev@glassfish.java.net

using NavigationHandler in ExceptionHandlerWrapper

From: Derek Knapp <derek_at_itracmedia.com>
Date: Thu, 04 Aug 2011 15:57:25 -0400

Why will the following code not redirect to my error page? I have been
Googling forever, with no luck :(




package com.itrac.exception;

import java.util.Iterator;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

/**
  *
  * @author derek
  */
public class ItracExceptionHandler extends ExceptionHandlerWrapper
{
     private ExceptionHandler wrapped;

     public ItracExceptionHandler(ExceptionHandler wrapped)
     {
         this.wrapped = wrapped;
     }

     @Override
     public ExceptionHandler getWrapped()
     {
         return wrapped;
     }

     @Override
     public void handle() throws FacesException
     {
         for (Iterator<ExceptionQueuedEvent> i =
getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();)
         {
             ExceptionQueuedEvent event = i.next();
             ExceptionQueuedEventContext context =
(ExceptionQueuedEventContext) event.getSource();
             Throwable t = context.getException();

             // redirect to error page
             FacesContext fc = FacesContext.getCurrentInstance();
             NavigationHandler nav =
fc.getApplication().getNavigationHandler();
             nav.handleNavigation(fc, null, "/error");
             fc.renderResponse();

             // remove it if you processed it
             i.remove();
         }

         // let the next ExceptionHandler(s) deal with the others
         getWrapped().handle();
     }
}