View Javadoc
1   /**
2    * Copyright 2005-2014 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   * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
43   */
44  @Deprecated
45  public class KualiExceptionHandlerAction extends Action {
46  	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  		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>ProcessDefinition application Exception - Exception is stored in Http Request</li>
70  	 * <li>ProcessDefinition 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  		if (LOG.isDebugEnabled()) {
83  			String lm = String.format("ENTRY %s%n%s", form.getClass()
84  					.getSimpleName(), request.getRequestURI());
85  			LOG.debug(lm);
86  		}
87  
88  		// Get exception thrown
89  		Exception e = (Exception) request.getAttribute(Globals.EXCEPTION_KEY);
90  
91  		// Initialize defined action mapping from struts-config
92  		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  		if (e == null) {
98  			if (form instanceof KualiExceptionIncidentForm) {
99  				KualiExceptionIncidentForm formObject = (KualiExceptionIncidentForm) form;
100 				// Manage conditions: submit or cancel
101 				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 					KualiExceptionIncidentService reporterService = KRADServiceLocatorWeb
109 							.getKualiExceptionIncidentService();
110 					// An instance of the ExceptionIncident is created by
111 					// the
112 					// ExceptionIncidentService
113 					Map reducedMap = new HashMap();
114 					Enumeration<String> names = request.getParameterNames();
115 					while (names.hasMoreElements()) {
116 						String name = names.nextElement();
117 						reducedMap.put(name, request.getParameter(name));
118 					}
119 					KualiExceptionIncident exceptionIncident = reporterService
120 							.getExceptionIncident(reducedMap);
121 					// Report the incident
122 					reporterService.report(exceptionIncident);
123 				} else {
124 					// Set return after canceling
125 					ActionForward cancelForward = mapping
126 							.findForward(KRADConstants.MAPPING_CANCEL);
127 					if (cancelForward == null) {
128 						cancelForward = returnForward;
129 					} else {
130 						returnForward = cancelForward;
131 					}
132 				}
133 			}
134 		} else {
135 			// ProcessDefinition the received exception from HTTP request
136 			returnForward = processException(mapping, form, request, e);
137 		}
138 
139 		// Not specified, return
140 		if (returnForward == null) {
141 			returnForward = mapping.findForward(KRADConstants.MAPPING_CLOSE);
142 		}
143 
144 		if (LOG.isDebugEnabled()) {
145 			String lm = String.format("EXIT %s",
146 					(returnForward == null) ? "null" : returnForward.getPath());
147 			LOG.debug(lm);
148 		}
149 
150 		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 		KualiExceptionIncidentService reporterService = KRADServiceLocatorWeb
175 				.getKualiExceptionIncidentService();
176 		// Get exception properties from the Http Request
177 		Map<String, String> properties = (Map<String, String>) request
178 				.getAttribute(IncidentReportUtils.EXCEPTION_PROPERTIES);
179 		// Construct the exception incident object
180 		KualiExceptionIncident ei = reporterService.getExceptionIncident(
181 				exception, properties);
182 		// Set full exception properties in Http Request and forward to JSP
183 		request.setAttribute(KualiExceptionHandlerAction.class
184 				.getSimpleName(), ei.toProperties());
185 		return mapping.findForward(RiceConstants.MAPPING_BASIC);
186 	}
187 }