View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.web.controller;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.krad.UserSession;
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.uif.UifConstants;
23  import org.kuali.rice.krad.uif.UifParameters;
24  import org.kuali.rice.krad.uif.service.ViewService;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.util.KRADConstants;
27  import org.kuali.rice.krad.util.KRADUtils;
28  import org.kuali.rice.krad.web.form.DocumentFormBase;
29  import org.kuali.rice.krad.web.form.IncidentReportForm;
30  import org.kuali.rice.krad.web.form.UifFormBase;
31  import org.kuali.rice.krad.web.service.ModelAndViewService;
32  import org.springframework.web.servlet.ModelAndView;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  /**
38   * Spring Exception intercepter
39   *
40   * <p>
41   * Gets the data needed for the incident report from the request and builds the
42   * model and view for the incident report. This resolver intercepts any unhandled
43   * exception.
44   * </p>
45   *
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   */
48  public class UifHandlerExceptionResolver implements org.springframework.web.servlet.HandlerExceptionResolver {
49      private static final Logger LOG = Logger.getLogger(UifHandlerExceptionResolver.class);
50  
51      /**
52       * Builds the incident report model and view from the request that threw the exception
53       *
54       * @param request -
55       *            the request
56       * @param response -
57       *            the response
58       * @param handler -
59       *            the current handler when the exception occurred
60       * @param ex -
61       *            the exception
62       * @return the incident report model and view
63       * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
64       *      javax.servlet.http.HttpServletResponse, java.lang.Object,
65       *      java.lang.Exception)
66       */
67      @Override
68      public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
69              Exception ex) {
70          LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);
71  
72          // log exception
73          LOG.error(ex.getMessage(), ex);
74  
75          String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
76          String incidentViewId = "";
77  
78          UifFormBase form = (UifFormBase)request.getAttribute(UifConstants.REQUEST_FORM);
79          if (form instanceof DocumentFormBase) {
80              if (((DocumentFormBase) form).getDocument() != null) {
81                  incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
82              }
83              incidentViewId = ((DocumentFormBase) form).getViewId();
84          }
85  
86          if (GlobalVariables.getUifFormManager() != null) {
87              GlobalVariables.getUifFormManager().removeSessionForm(form);
88          }
89  
90          UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
91          IncidentReportForm incidentReportForm = new IncidentReportForm();
92          incidentReportForm.setSessionId(request.getSession().getId());
93  
94          // Set the post url map to the incident report controller and not 
95          // the one the exception occurred on
96          String postUrl = request.getRequestURL().toString();
97          postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
98          incidentReportForm.setFormPostUrl(postUrl);
99  
100         incidentReportForm.setException(ex);
101         incidentReportForm.setIncidentDocId(incidentDocId);
102         incidentReportForm.setIncidentViewId(incidentViewId);
103         incidentReportForm.setController(handler.getClass().toString());
104 
105         if (userSession != null) {
106             incidentReportForm.setUserId(userSession.getPrincipalId());
107             incidentReportForm.setUserName(userSession.getPrincipalName());
108             incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
109         }
110 
111         incidentReportForm.setDevMode(!KRADUtils.isProductionEnvironment());
112         incidentReportForm.setViewId("Uif-IncidentReportView");
113 
114         if (form != null) {
115             incidentReportForm.setAjaxRequest(form.isAjaxRequest());
116         } else {
117             String ajaxRequestParm = request.getParameter(UifParameters.AJAX_REQUEST);
118             if (StringUtils.isNotBlank(ajaxRequestParm)) {
119                 incidentReportForm.setAjaxRequest(Boolean.parseBoolean(ajaxRequestParm));
120             }
121         }
122 
123         // Set the view object
124         incidentReportForm.setView(getViewService().getViewById("Uif-IncidentReportView"));
125 
126         // Set the ajax return type
127         incidentReportForm.setAjaxReturnType(UifConstants.AjaxReturnTypes.UPDATEVIEW.getKey());
128 
129         incidentReportForm.setRequest(request);
130         incidentReportForm.postBind(request);
131 
132         ModelAndView modelAndView = getModelAndViewService().getModelAndView(incidentReportForm, "");
133         try {
134             getModelAndViewService().prepareView(request, modelAndView);
135         } catch (Exception e) {
136             LOG.error("An error stopped the incident form from loading", e);
137         }
138 
139         return modelAndView;
140     }
141 
142     protected ViewService getViewService() {
143         return KRADServiceLocatorWeb.getViewService();
144     }
145 
146     protected ModelAndViewService getModelAndViewService() {
147         return KRADServiceLocatorWeb.getModelAndViewService();
148     }
149 
150 }