Advertisement
Guest User

handler

a guest
Mar 26th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package br.com.service;
  2.  
  3.  
  4.  
  5. import java.io.IOException;
  6. import java.util.Iterator;
  7.  
  8. import javax.faces.FacesException;
  9. import javax.faces.application.FacesMessage;
  10. import javax.faces.context.ExceptionHandler;
  11. import javax.faces.context.ExceptionHandlerWrapper;
  12. import javax.faces.context.ExternalContext;
  13. import javax.faces.context.FacesContext;
  14. import javax.faces.event.ExceptionQueuedEvent;
  15. import javax.faces.event.ExceptionQueuedEventContext;
  16.  
  17. import br.com.util.FacesUtil;
  18.  
  19. public class ControleExceptionHandler extends ExceptionHandlerWrapper{
  20.  
  21.    
  22.     private ExceptionHandler wrapped;
  23.    
  24.    
  25.     public ControleExceptionHandler(ExceptionHandler wrapped) {
  26.         this.wrapped = wrapped;
  27.     }
  28.    
  29.     @Override
  30.     public ExceptionHandler getWrapped() {
  31.         return this.wrapped;
  32.     }
  33.    
  34.      
  35.     @Override
  36.     public void handle() throws FacesException{
  37.        
  38.         Iterator<ExceptionQueuedEvent> events = getUnhandledExceptionQueuedEvents().iterator();
  39.        
  40.         while(events.hasNext()){
  41.            
  42.             ExceptionQueuedEvent event = events.next();
  43.             ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
  44.            
  45.             Throwable exception = context.getException();
  46.            
  47.             UsuarioException usuarioException = getUsuarioException(exception);
  48.            
  49.             boolean handled = false;
  50.            
  51.             try {
  52.                 if (usuarioException != null){
  53.                     handled = true;
  54.                     FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_ERROR,      usuarioException.getMessage());
  55.                 } else {
  56.                     handled = true;
  57.                     redirect("/index.xhtml");
  58.                 }
  59.                
  60.                
  61.             } finally {
  62.                 if(handled){
  63.                     events.remove();
  64.                 }
  65.             }
  66.            
  67.         }
  68.        
  69.         getWrapped().handle();
  70.     }
  71.    
  72.    
  73.     private void redirect(String page){
  74.        
  75.         try{
  76.             FacesContext fc = FacesContext.getCurrentInstance();
  77.             ExternalContext ec = fc.getExternalContext();
  78.             String contextPage = ec.getRequestContextPath();
  79.            
  80.             ec.redirect(contextPage + page);
  81.             fc.responseComplete();
  82.         } catch (IOException ioe){
  83.             throw new FacesException(ioe);
  84.         }
  85.        
  86.     }
  87.    
  88.     //metodo recursivo para interar em cada exceção da lista se existe uma exceção do tipo procurado
  89.     private UsuarioException getUsuarioException(Throwable exception){
  90.        
  91.         if (exception instanceof UsuarioException){
  92.             return (UsuarioException) exception;
  93.         } else if (exception.getCause() !=null){
  94.             return getUsuarioException(exception.getCause());
  95.         }
  96.          
  97.         return null;
  98.     }
  99.    
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement