View Javadoc

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.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.service.SessionDocumentService;
22  import org.kuali.rice.krad.uif.view.History;
23  import org.kuali.rice.krad.uif.view.HistoryEntry;
24  import org.kuali.rice.krad.uif.service.ViewService;
25  import org.kuali.rice.krad.uif.util.UifWebUtils;
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  import java.util.Map;
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          String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
72          String incidentViewId = "";
73  
74          // log exception
75          LOG.error(ex.getMessage(), ex);
76  
77          UifFormBase form = UifWebUtils.getFormFromRequest(request);
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  
85          UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
86          IncidentReportForm incidentReportForm = new IncidentReportForm();
87  
88          // Set the post url map to the incident report controller and not 
89          // the one the exception occurred on
90          String postUrl = request.getRequestURL().toString();        
91          postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
92          incidentReportForm.setFormPostUrl(postUrl);
93          incidentReportForm.setException(ex);
94          incidentReportForm.setIncidentDocId(incidentDocId);
95          incidentReportForm.setIncidentViewId(incidentViewId);
96          incidentReportForm.setController(handler.getClass().toString());
97          incidentReportForm.setUserId(userSession.getPrincipalId());
98          incidentReportForm.setUserName(userSession.getPrincipalName());
99          incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
100         incidentReportForm.setDevMode(!KRADUtils.isProductionEnvironment());
101 
102         // Set the view object
103         incidentReportForm.setView(getViewService().getViewById("Incident-Report"));
104 
105         // Add a new History entry to avoid errors in the postHandle
106         History history = new History();
107         HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
108         history.setCurrent(entry);
109         incidentReportForm.setFormHistory(history);
110 
111         // Set render full view to force full render
112         incidentReportForm.setRenderFullView(true);
113 
114         ModelAndView modelAndView = UifWebUtils.getUIFModelAndView(incidentReportForm, "");
115         try {
116             UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
117         } catch (Exception e) {
118             LOG.error("An error stopped the incident form from loading", e);
119         }
120 
121         return modelAndView;
122     }
123 
124     protected ViewService getViewService() {
125         return KRADServiceLocatorWeb.getViewService();
126     }
127 
128     protected SessionDocumentService getSessionDocumentService() {
129         return KRADServiceLocatorWeb.getSessionDocumentService();
130     }
131 
132 }