gdg
2020-12-28 605a19169121bbcaaf559030dd1d9aae614f8026
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.nanometer.smartlab.exception;
 
import com.nanometer.smartlab.util.FacesUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.ExpiredSessionException;
 
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;
import java.util.Iterator;
 
/**
 * when view expired, invole js function "whenExpired()", popup a dialog to notice user
 *
 * Created by johnny on 14-3-15.
 */
public class MyExceptionHandler extends ExceptionHandlerWrapper {
 
    private static Logger logger = Logger.getLogger(MyExceptionHandler.class);
 
    private ExceptionHandler wrapped;
 
    public MyExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }
 
    @Override
    public ExceptionHandler getWrapped() {
        return this.wrapped;
    }
 
    @Override
    public void handle() throws FacesException {
        Iterable<ExceptionQueuedEvent> events = this.wrapped.getUnhandledExceptionQueuedEvents();
        for (Iterator<ExceptionQueuedEvent> it = events.iterator(); it.hasNext(); ) {
            ExceptionQueuedEvent event = it.next();
            ExceptionQueuedEventContext eqec = event.getContext();
            Throwable throwable = eqec.getException();
            try {
                if (throwable != null)
                if (throwable instanceof ViewExpiredException
                        || throwable instanceof ExpiredSessionException) {
                    FacesContext context = eqec.getContext();
                    NavigationHandler navHandler = context.getApplication().getNavigationHandler();
                    SecurityUtils.getSubject().logout();
                    FacesUtils.js("whenExpired()");
                } else {
                    FacesUtils.error("系统错误");
                    logger.error("Error: ", throwable);
                }
            } finally {
                it.remove();
            }
        }
        this.wrapped.handle();
    }
}