1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.context;
20
21 import javax.faces.context.ExceptionHandler;
22 import javax.faces.context.ExceptionHandlerWrapper;
23 import javax.faces.context.FacesContext;
24 import javax.faces.context.PartialViewContext;
25 import javax.faces.event.AbortProcessingException;
26 import javax.faces.event.ExceptionQueuedEvent;
27 import javax.faces.event.ExceptionQueuedEventContext;
28 import javax.faces.event.SystemEvent;
29
30
31
32
33
34
35
36 public class SwitchAjaxExceptionHandlerWrapperImpl extends ExceptionHandlerWrapper
37 {
38 private ExceptionHandler _requestExceptionHandler;
39 private ExceptionHandler _ajaxExceptionHandler;
40 private Boolean _isAjaxRequest;
41
42 public SwitchAjaxExceptionHandlerWrapperImpl(
43 ExceptionHandler requestExceptionHandler,
44 ExceptionHandler ajaxExceptionHandler)
45 {
46 _requestExceptionHandler = requestExceptionHandler;
47 _ajaxExceptionHandler = ajaxExceptionHandler;
48 }
49
50 @Override
51 public void processEvent(SystemEvent exceptionQueuedEvent)
52 throws AbortProcessingException
53 {
54
55 isAjaxRequest(exceptionQueuedEvent);
56 super.processEvent(exceptionQueuedEvent);
57 }
58
59 protected boolean isAjaxRequest(SystemEvent exceptionQueuedEvent)
60 {
61 if (_isAjaxRequest == null)
62 {
63 if (exceptionQueuedEvent instanceof ExceptionQueuedEvent)
64 {
65 ExceptionQueuedEvent eqe = (ExceptionQueuedEvent)exceptionQueuedEvent;
66 ExceptionQueuedEventContext eqec = eqe.getContext();
67 if (eqec != null)
68 {
69 FacesContext facesContext = eqec.getContext();
70 if (facesContext != null)
71 {
72 return isAjaxRequest(facesContext);
73 }
74 }
75 }
76 return isAjaxRequest();
77 }
78 return _isAjaxRequest;
79 }
80
81 protected boolean isAjaxRequest(FacesContext facesContext)
82 {
83 if (_isAjaxRequest == null)
84 {
85 facesContext = (facesContext == null) ? FacesContext.getCurrentInstance() : facesContext;
86 PartialViewContext pvc = facesContext.getPartialViewContext();
87 if (pvc == null)
88 {
89 return false;
90 }
91 _isAjaxRequest = pvc.isAjaxRequest();
92 }
93 return _isAjaxRequest;
94 }
95
96 protected boolean isAjaxRequest()
97 {
98 if (_isAjaxRequest == null)
99 {
100 FacesContext facesContext = FacesContext.getCurrentInstance();
101 PartialViewContext pvc = facesContext.getPartialViewContext();
102 if (pvc == null)
103 {
104 return false;
105 }
106 _isAjaxRequest = pvc.isAjaxRequest();
107 }
108 return _isAjaxRequest;
109 }
110
111 @Override
112 public ExceptionHandler getWrapped()
113 {
114 if (isAjaxRequest())
115 {
116 return _ajaxExceptionHandler;
117 }
118 else
119 {
120 return _requestExceptionHandler;
121 }
122 }
123 }