001 /*
002 * Copyright 2011 The Kuali Foundation Licensed under the Educational Community
003 * License, Version 1.0 (the "License"); you may not use this file except in
004 * compliance with the License. You may obtain a copy of the License at
005 * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
006 * or agreed to in writing, software distributed under the License is
007 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
008 * KIND, either express or implied. See the License for the specific language
009 * governing permissions and limitations under the License.
010 */
011 package org.kuali.rice.kns.web.spring;
012
013 import javax.servlet.http.HttpServletRequest;
014 import javax.servlet.http.HttpServletResponse;
015
016 import org.apache.log4j.Logger;
017 import org.kuali.rice.kns.UserSession;
018 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
019 import org.kuali.rice.kns.service.SessionDocumentService;
020 import org.kuali.rice.kns.uif.history.History;
021 import org.kuali.rice.kns.uif.history.HistoryEntry;
022 import org.kuali.rice.kns.uif.service.ViewService;
023 import org.kuali.rice.kns.uif.util.UifWebUtils;
024 import org.kuali.rice.kns.util.KNSConstants;
025 import org.kuali.rice.kns.util.WebUtils;
026 import org.kuali.rice.kns.web.spring.form.DocumentFormBase;
027 import org.kuali.rice.kns.web.spring.form.IncidentReportForm;
028 import org.kuali.rice.kns.web.spring.form.UifFormBase;
029 import org.springframework.web.servlet.ModelAndView;
030
031 /**
032 * Spring Exception intercepter
033 *
034 * <p>
035 * Gets the data needed for the incident report from the request and builds the
036 * model and view for the incident report. This resolver intercepts any unhandled
037 * exception.
038 * </p>
039 *
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 */
042 public class UifHandlerExceptionResolver implements org.springframework.web.servlet.HandlerExceptionResolver {
043
044 private static final Logger LOG = Logger.getLogger(UifHandlerExceptionResolver.class);
045
046 /**
047 * Builds the incident report model and view from the
048 * request that threw the exception.
049 *
050 * @param request
051 * the request
052 * @param response
053 * the response
054 * @param handler
055 * the current handler when the exception occurred
056 * @param ex
057 * the exception
058 * @return the incident report model and view
059 * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
060 * javax.servlet.http.HttpServletResponse, java.lang.Object,
061 * java.lang.Exception)
062 */
063 @Override
064 public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
065 Exception ex) {
066 LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);
067 String incidentDocId = request.getParameter(KNSConstants.DOCUMENT_DOCUMENT_NUMBER);
068 String incidentViewId = "";
069
070 // log exception
071 LOG.error(ex.getMessage(), ex);
072
073 UifFormBase form = UifWebUtils.getFormFromRequest(request);
074 if (form instanceof DocumentFormBase) {
075 incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
076 incidentViewId = ((DocumentFormBase) form).getViewId();
077 }
078
079 UserSession userSession = (UserSession) request.getSession().getAttribute(KNSConstants.USER_SESSION_KEY);
080 IncidentReportForm incidentReportForm = new IncidentReportForm();
081 // Set the post url map to the incident report controller and not
082 // the one the exception occurred on
083 String postUrl = request.getRequestURL().toString();
084 postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
085 incidentReportForm.setFormPostUrl(postUrl);
086 incidentReportForm.setException(ex);
087 incidentReportForm.setIncidentDocId(incidentDocId);
088 incidentReportForm.setIncidentViewId(incidentViewId);
089 incidentReportForm.setController(handler.getClass().toString());
090 incidentReportForm.setUserId(userSession.getPrincipalId());
091 incidentReportForm.setUserName(userSession.getPrincipalName());
092 incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
093 incidentReportForm.setDevMode(!WebUtils.isProductionEnvironment());
094
095 // Set the view object
096 incidentReportForm.setView(getViewService().getView("Incident-Report",
097 incidentReportForm.getViewRequestParameters()));
098
099 // Add a new History entry to avoid errors in the postHandle
100 History history = new History();
101 HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
102 history.setCurrent(entry);
103 incidentReportForm.setFormHistory(history);
104
105 // Set render full view to force full render
106 incidentReportForm.setRenderFullView(true);
107
108 ModelAndView modelAndView = UifWebUtils.getUIFModelAndView(incidentReportForm, "Incident-Report", "");
109 try {
110 UifWebUtils.postControllerHandle(request, response, handler, modelAndView);
111 } catch (Exception e) {
112 LOG.error("An error stopped the incident form from loading", e);
113 }
114
115 return modelAndView;
116 }
117
118 protected ViewService getViewService() {
119 return KNSServiceLocatorWeb.getViewService();
120 }
121
122 protected SessionDocumentService getSessionDocumentService() {
123 return KNSServiceLocatorWeb.getSessionDocumentService();
124 }
125
126 }