Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
KualiExceptionHandlerAction |
|
| 4.0;4 |
1 | /* | |
2 | * Copyright 2005-2008 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.kuali.rice.kns.web.struts.action; | |
18 | ||
19 | import org.apache.log4j.Logger; | |
20 | import org.apache.struts.Globals; | |
21 | import org.apache.struts.action.Action; | |
22 | import org.apache.struts.action.ActionForm; | |
23 | import org.apache.struts.action.ActionForward; | |
24 | import org.apache.struts.action.ActionMapping; | |
25 | import org.kuali.rice.core.util.RiceConstants; | |
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.kns.util.IncidentReportUtils; | |
31 | import org.kuali.rice.krad.util.KRADConstants; | |
32 | ||
33 | import javax.servlet.http.HttpServletRequest; | |
34 | import javax.servlet.http.HttpServletResponse; | |
35 | import java.util.Enumeration; | |
36 | import java.util.HashMap; | |
37 | import java.util.Map; | |
38 | ||
39 | /** | |
40 | * This is the struts action class for handling the exception for Kuali | |
41 | * applications. | |
42 | * | |
43 | */ | |
44 | 0 | public class KualiExceptionHandlerAction extends Action { |
45 | 0 | private static final Logger LOG = Logger |
46 | .getLogger(KualiExceptionHandlerAction.class); | |
47 | ||
48 | /** | |
49 | * This overridden method dispatches action to be taken based on | |
50 | * "methodToCall" parameter. The exception is processed when there is no | |
51 | * "methodToCall" specified. | |
52 | * | |
53 | * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, | |
54 | * org.apache.struts.action.ActionForm, | |
55 | * javax.servlet.http.HttpServletRequest, | |
56 | * javax.servlet.http.HttpServletResponse) | |
57 | */ | |
58 | public ActionForward execute(ActionMapping mapping, ActionForm form, | |
59 | HttpServletRequest request, HttpServletResponse response) | |
60 | throws Exception { | |
61 | 0 | return executeException(mapping, form, request, response); |
62 | } | |
63 | ||
64 | /** | |
65 | * This overridden method processes the exception and post exception (when | |
66 | * user either submit/cancel the exception JSP page). | |
67 | * <ul> | |
68 | * <li>Process application Exception - Exception is stored in Http Request</li> | |
69 | * <li>Process exception incident reporting - No exception, only form data</li> | |
70 | * </ul> | |
71 | * | |
72 | * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, | |
73 | * org.apache.struts.action.ActionForm, | |
74 | * javax.servlet.http.HttpServletRequest, | |
75 | * javax.servlet.http.HttpServletResponse) | |
76 | */ | |
77 | public ActionForward executeException(ActionMapping mapping, | |
78 | ActionForm form, HttpServletRequest request, | |
79 | HttpServletResponse response) throws Exception { | |
80 | ||
81 | 0 | if (LOG.isDebugEnabled()) { |
82 | 0 | String lm = String.format("ENTRY %s%n%s", form.getClass() |
83 | .getSimpleName(), request.getRequestURI()); | |
84 | 0 | LOG.debug(lm); |
85 | } | |
86 | ||
87 | // Get exception thrown | |
88 | 0 | Exception e = (Exception) request.getAttribute(Globals.EXCEPTION_KEY); |
89 | ||
90 | // Initialize defined action mapping from struts-config | |
91 | 0 | ActionForward returnForward = null; |
92 | ||
93 | // In case there is no exception, either a post back after page was | |
94 | // filled in | |
95 | // or just an error from directly accessing this struts action | |
96 | 0 | if (e == null) { |
97 | 0 | if (form instanceof KualiExceptionIncidentForm) { |
98 | 0 | KualiExceptionIncidentForm formObject = (KualiExceptionIncidentForm) form; |
99 | // Manage conditions: submit or cancel | |
100 | 0 | if (!formObject.isCancel()) { |
101 | // Locate the post exception handler service. The service id | |
102 | // is | |
103 | // defined in the application properties | |
104 | // Only process the post exception handling when the | |
105 | // service | |
106 | // is specified | |
107 | 0 | KualiExceptionIncidentService reporterService = KRADServiceLocatorWeb |
108 | .getKualiExceptionIncidentService(); | |
109 | // An instance of the ExceptionIncident is created by | |
110 | // the | |
111 | // ExceptionIncidentService | |
112 | 0 | Map reducedMap = new HashMap(); |
113 | 0 | Enumeration<String> names = request.getParameterNames(); |
114 | 0 | while (names.hasMoreElements()) { |
115 | 0 | String name = names.nextElement(); |
116 | 0 | reducedMap.put(name, request.getParameter(name)); |
117 | 0 | } |
118 | 0 | KualiExceptionIncident exceptionIncident = reporterService |
119 | .getExceptionIncident(reducedMap); | |
120 | // Report the incident | |
121 | 0 | reporterService.report(exceptionIncident); |
122 | 0 | } else { |
123 | // Set return after canceling | |
124 | 0 | ActionForward cancelForward = mapping |
125 | .findForward(KRADConstants.MAPPING_CANCEL); | |
126 | 0 | if (cancelForward == null) { |
127 | 0 | cancelForward = returnForward; |
128 | } else { | |
129 | 0 | returnForward = cancelForward; |
130 | } | |
131 | } | |
132 | 0 | } |
133 | } else { | |
134 | // Process the received exception from HTTP request | |
135 | 0 | returnForward = processException(mapping, form, request, e); |
136 | } | |
137 | ||
138 | // Not specified, return | |
139 | 0 | if (returnForward == null) { |
140 | 0 | returnForward = mapping.findForward(KRADConstants.MAPPING_CLOSE); |
141 | } | |
142 | ||
143 | 0 | if (LOG.isDebugEnabled()) { |
144 | 0 | String lm = String.format("EXIT %s", |
145 | (returnForward == null) ? "null" : returnForward.getPath()); | |
146 | 0 | LOG.debug(lm); |
147 | } | |
148 | ||
149 | 0 | return returnForward; |
150 | } | |
151 | ||
152 | /** | |
153 | * This method process the caught exception by creating an exception | |
154 | * information properties list and forward these properties to the exception | |
155 | * incident handler JSP. | |
156 | * | |
157 | * @param exception | |
158 | * @param mapping | |
159 | * @param request | |
160 | * @param documentId | |
161 | * Id of the document that Struts threw exception during its | |
162 | * processing. null if not the document processing that caused | |
163 | * the exception | |
164 | * @return | |
165 | * @throws Exception | |
166 | */ | |
167 | @SuppressWarnings("unchecked") | |
168 | protected ActionForward processException(ActionMapping mapping, | |
169 | ActionForm form, HttpServletRequest request, Exception exception) | |
170 | throws Exception { | |
171 | // Only process the exception handling when the service | |
172 | // is specified | |
173 | 0 | KualiExceptionIncidentService reporterService = KRADServiceLocatorWeb |
174 | .getKualiExceptionIncidentService(); | |
175 | // Get exception properties from the Http Request | |
176 | 0 | Map<String, String> properties = (Map<String, String>) request |
177 | .getAttribute(IncidentReportUtils.EXCEPTION_PROPERTIES); | |
178 | // Construct the exception incident object | |
179 | 0 | KualiExceptionIncident ei = reporterService.getExceptionIncident( |
180 | exception, properties); | |
181 | // Set full exception properties in Http Request and forward to JSP | |
182 | 0 | request.setAttribute(KualiExceptionHandlerAction.class |
183 | .getName(), ei.toProperties()); | |
184 | 0 | return mapping.findForward(RiceConstants.MAPPING_BASIC); |
185 | } | |
186 | } |