View Javadoc
1   /**
2    * Copyright 2005-2016 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.bind;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.krad.UserSession;
20  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21  import org.kuali.rice.krad.uif.view.History;
22  import org.kuali.rice.krad.uif.view.HistoryEntry;
23  import org.kuali.rice.krad.uif.service.ViewService;
24  import org.kuali.rice.krad.uif.util.UifWebUtils;
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.springframework.web.servlet.ModelAndView;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /**
37   * Spring Exception intercepter
38   *
39   * <p>
40   * Gets the data needed for the incident report from the request and builds the
41   * model and view for the incident report. This resolver intercepts any unhandled 
42   * exception.
43   * </p>
44   * 
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class UifHandlerExceptionResolver implements org.springframework.web.servlet.HandlerExceptionResolver {
48      private static final Logger LOG = Logger.getLogger(UifHandlerExceptionResolver.class);
49  
50      /**
51       * Builds the incident report model and view from the request that threw the exception
52       * 
53       * @param request -
54       *            the request
55       * @param response -
56       *            the response
57       * @param handler -
58       *            the current handler when the exception occurred
59       * @param ex -
60       *            the exception
61       * @return the incident report model and view
62       * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
63       *      javax.servlet.http.HttpServletResponse, java.lang.Object,
64       *      java.lang.Exception)
65       */
66      @Override
67      public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
68              Exception ex) {    
69          LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);
70  
71          // log exception
72          LOG.error(ex.getMessage(), ex);
73  
74          String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
75          String incidentViewId = "";
76  
77          UifFormBase form = GlobalVariables.getUifFormManager().getCurrentForm();
78          if (form instanceof DocumentFormBase) {
79              if (((DocumentFormBase) form).getDocument() != null) {
80                  incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
81              }
82              incidentViewId = ((DocumentFormBase) form).getViewId();
83          }
84          GlobalVariables.getUifFormManager().removeForm(form);
85  
86          UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
87          IncidentReportForm incidentReportForm = new IncidentReportForm();
88  
89          // Set the post url map to the incident report controller and not 
90          // the one the exception occurred on
91          String postUrl = request.getRequestURL().toString();        
92          postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
93          incidentReportForm.setFormPostUrl(postUrl);
94  
95          incidentReportForm.setException(ex);
96          incidentReportForm.setIncidentDocId(incidentDocId);
97          incidentReportForm.setIncidentViewId(incidentViewId);
98          incidentReportForm.setController(handler.getClass().toString());
99          incidentReportForm.setUserId(userSession.getPrincipalId());
100         incidentReportForm.setUserName(userSession.getPrincipalName());
101         incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
102         incidentReportForm.setDevMode(!KRADUtils.isProductionEnvironment());
103         incidentReportForm.setViewId("Uif-IncidentReportView");
104 
105         // Set the view object
106         incidentReportForm.setView(getViewService().getViewById("Uif-IncidentReportView"));
107 
108         // Add a new History entry to avoid errors in the postHandle
109         History history = new History();
110         HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
111         history.setCurrent(entry);
112         incidentReportForm.setFormHistory(history);
113 
114         // Set render full view to force full render
115         incidentReportForm.setRenderFullView(true);
116 
117         ModelAndView modelAndView = UifWebUtils.getUIFModelAndView(incidentReportForm, "");
118         try {
119             UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
120         } catch (Exception e) {
121             LOG.error("An error stopped the incident form from loading", e);
122         }
123 
124         GlobalVariables.getUifFormManager().setCurrentForm(incidentReportForm);
125 
126         return modelAndView;
127     }
128 
129     protected ViewService getViewService() {
130         return KRADServiceLocatorWeb.getViewService();
131     }
132 
133 }