Skip to content

Instantly share code, notes, and snippets.

@mariofts
Created February 5, 2014 17:39
Show Gist options
  • Save mariofts/8829180 to your computer and use it in GitHub Desktop.
Save mariofts/8829180 to your computer and use it in GitHub Desktop.
package br.com.caelum.notasfiscais.exception;
import java.util.Iterator;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewExpiredException;
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;
public class CustomExceptionHandler extends ExceptionHandlerWrapper {
private ExceptionHandler wrapped;
public CustomExceptionHandler(ExceptionHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ExceptionHandler getWrapped() {
return this.wrapped;
}
@Override
public void handle() throws FacesException {
//pega o contexto do JSF
FacesContext facesContext = FacesContext.getCurrentInstance();
//percore as listas de exceptions esperando tratamento
for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
//obtém a próxima exception da lista
Throwable exception = iter.next().getContext().getException();
//se for ViewExpired...
if (exception instanceof ViewExpiredException) {
//redireciona para login
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, "login?faces-redirec=true");
facesContext.renderResponse();
//remove esta exception da lista
iter.remove();
}
}
//delega para o tratamento default
getWrapped().handle();
}
}
package br.com.caelum.notasfiscais.exception;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory{
private ExceptionHandlerFactory parent;
public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
this.parent = parent;
}
@Override
public ExceptionHandler getExceptionHandler() {
ExceptionHandler result = parent.getExceptionHandler();
result = new CustomExceptionHandler(result);
return result;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<lifecycle>
<phase-listener>br.com.caelum.notasfiscais.listener.CicloDeVidaListener</phase-listener>
<phase-listener>br.com.caelum.notasfiscais.listener.Autorizador</phase-listener>
</lifecycle>
<!--Registrando o tratador de exceptiions -->
<factory>
<exception-handler-factory>br.com.caelum.notasfiscais.exception.CustomExceptionHandlerFactory</exception-handler-factory>
</factory>
</faces-config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment