All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jhonsapp.util.jsf.exception.handler.JsfExceptionHandler Maven / Gradle / Ivy

Go to download

This project provides useful classes that facilitate the construction of new components web.

The newest version!
package br.com.jhonsapp.util.jsf.exception.handler;

import java.util.Iterator;

import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;
import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.com.jhonsapp.util.jsf.project.root.pages.WebProjectRootPages;
import br.com.jhonsapp.util.jsf.redirect.RedirectFacesUtil;

/**
 * Class responsible for handling application exceptions.
 * 
 * @author Jhonathan Camacho
 *
 */
public class JsfExceptionHandler extends ExceptionHandlerWrapper {
	
	private static Log log = LogFactory.getLog(JsfExceptionHandler.class);
	private ExceptionHandler wrapped;
	
	@Inject
	private WebProjectRootPages rootPages;
	
	public JsfExceptionHandler(ExceptionHandler wrapped) {
		this.wrapped = wrapped;
	}
	
	@Override
	public ExceptionHandler getWrapped() {
		return this.wrapped;
	}
	
	@Override
	public void handle() throws FacesException {
		Iterator events = getUnhandledExceptionQueuedEvents().iterator();
		 
		while (events.hasNext()) {
			ExceptionQueuedEvent event = events.next();
			ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
			
			Throwable exception = context.getException();
			
			boolean handled = false;
			
			try {
				if (exception instanceof ViewExpiredException) {
					handled = true;
					RedirectFacesUtil.execute("/");
					
				} else {
					handled = true;
					
					log.error("Erro de Sistema: " + exception.getMessage(), exception);
					
					RedirectFacesUtil.execute(rootPages.getError());
				}
			} finally {
				if (handled) {
					events.remove();
				}
			}
		}
		
		getWrapped().handle();
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy