1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.action;
17
18 import org.apache.log4j.Logger;
19 import org.apache.struts.Globals;
20 import org.apache.struts.action.Action;
21 import org.apache.struts.action.ActionForm;
22 import org.apache.struts.action.ActionForward;
23 import org.apache.struts.action.ActionMapping;
24 import org.kuali.rice.core.api.util.RiceConstants;
25 import org.kuali.rice.kns.util.IncidentReportUtils;
26 import org.kuali.rice.kns.web.struts.form.KualiExceptionIncidentForm;
27 import org.kuali.rice.krad.exception.KualiExceptionIncident;
28 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29 import org.kuali.rice.krad.service.KualiExceptionIncidentService;
30 import org.kuali.rice.krad.util.KRADConstants;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import java.util.Enumeration;
35 import java.util.HashMap;
36 import java.util.Map;
37
38
39
40
41
42
43 public class KualiExceptionHandlerAction extends Action {
44 private static final Logger LOG = Logger
45 .getLogger(KualiExceptionHandlerAction.class);
46
47
48
49
50
51
52
53
54
55
56
57 public ActionForward execute(ActionMapping mapping, ActionForm form,
58 HttpServletRequest request, HttpServletResponse response)
59 throws Exception {
60 return executeException(mapping, form, request, response);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public ActionForward executeException(ActionMapping mapping,
77 ActionForm form, HttpServletRequest request,
78 HttpServletResponse response) throws Exception {
79
80 if (LOG.isDebugEnabled()) {
81 String lm = String.format("ENTRY %s%n%s", form.getClass()
82 .getSimpleName(), request.getRequestURI());
83 LOG.debug(lm);
84 }
85
86
87 Exception e = (Exception) request.getAttribute(Globals.EXCEPTION_KEY);
88
89
90 ActionForward returnForward = null;
91
92
93
94
95 if (e == null) {
96 if (form instanceof KualiExceptionIncidentForm) {
97 KualiExceptionIncidentForm formObject = (KualiExceptionIncidentForm) form;
98
99 if (!formObject.isCancel()) {
100
101
102
103
104
105
106 KualiExceptionIncidentService reporterService = KRADServiceLocatorWeb
107 .getKualiExceptionIncidentService();
108
109
110
111 Map reducedMap = new HashMap();
112 Enumeration<String> names = request.getParameterNames();
113 while (names.hasMoreElements()) {
114 String name = names.nextElement();
115 reducedMap.put(name, request.getParameter(name));
116 }
117 KualiExceptionIncident exceptionIncident = reporterService
118 .getExceptionIncident(reducedMap);
119
120 reporterService.report(exceptionIncident);
121 } else {
122
123 ActionForward cancelForward = mapping
124 .findForward(KRADConstants.MAPPING_CANCEL);
125 if (cancelForward == null) {
126 cancelForward = returnForward;
127 } else {
128 returnForward = cancelForward;
129 }
130 }
131 }
132 } else {
133
134 returnForward = processException(mapping, form, request, e);
135 }
136
137
138 if (returnForward == null) {
139 returnForward = mapping.findForward(KRADConstants.MAPPING_CLOSE);
140 }
141
142 if (LOG.isDebugEnabled()) {
143 String lm = String.format("EXIT %s",
144 (returnForward == null) ? "null" : returnForward.getPath());
145 LOG.debug(lm);
146 }
147
148 return returnForward;
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 @SuppressWarnings("unchecked")
167 protected ActionForward processException(ActionMapping mapping,
168 ActionForm form, HttpServletRequest request, Exception exception)
169 throws Exception {
170
171
172 KualiExceptionIncidentService reporterService = KRADServiceLocatorWeb
173 .getKualiExceptionIncidentService();
174
175 Map<String, String> properties = (Map<String, String>) request
176 .getAttribute(IncidentReportUtils.EXCEPTION_PROPERTIES);
177
178 KualiExceptionIncident ei = reporterService.getExceptionIncident(
179 exception, properties);
180
181 request.setAttribute(KualiExceptionHandlerAction.class
182 .getName(), ei.toProperties());
183 return mapping.findForward(RiceConstants.MAPPING_BASIC);
184 }
185 }