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