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