Created
February 5, 2014 17:39
-
-
Save mariofts/8829180 to your computer and use it in GitHub Desktop.
Tratamento de ViewExpired JSF. Baseado em: http://stackoverflow.com/questions/8144195/check-if-session-exists-jsf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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