001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.controller;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.krad.UserSession;
021import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
022import org.kuali.rice.krad.uif.UifConstants;
023import org.kuali.rice.krad.uif.UifParameters;
024import org.kuali.rice.krad.uif.service.ViewService;
025import org.kuali.rice.krad.util.GlobalVariables;
026import org.kuali.rice.krad.util.KRADConstants;
027import org.kuali.rice.krad.util.KRADUtils;
028import org.kuali.rice.krad.web.form.DocumentFormBase;
029import org.kuali.rice.krad.web.form.IncidentReportForm;
030import org.kuali.rice.krad.web.form.UifFormBase;
031import org.kuali.rice.krad.web.service.ModelAndViewService;
032import org.springframework.web.servlet.ModelAndView;
033
034import javax.servlet.http.HttpServletRequest;
035import javax.servlet.http.HttpServletResponse;
036
037/**
038 * Spring Exception intercepter
039 *
040 * <p>
041 * Gets the data needed for the incident report from the request and builds the
042 * model and view for the incident report. This resolver intercepts any unhandled
043 * exception.
044 * </p>
045 *
046 * @author Kuali Rice Team (rice.collab@kuali.org)
047 */
048public class UifHandlerExceptionResolver implements org.springframework.web.servlet.HandlerExceptionResolver {
049    private static final Logger LOG = Logger.getLogger(UifHandlerExceptionResolver.class);
050
051    /**
052     * Builds the incident report model and view from the request that threw the exception
053     *
054     * @param request -
055     *            the request
056     * @param response -
057     *            the response
058     * @param handler -
059     *            the current handler when the exception occurred
060     * @param ex -
061     *            the exception
062     * @return the incident report model and view
063     * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
064     *      javax.servlet.http.HttpServletResponse, java.lang.Object,
065     *      java.lang.Exception)
066     */
067    @Override
068    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
069            Exception ex) {
070        LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);
071
072        // log exception
073        LOG.error(ex.getMessage(), ex);
074
075        String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
076        String incidentViewId = "";
077
078        UifFormBase form = (UifFormBase)request.getAttribute(UifConstants.REQUEST_FORM);
079        if (form instanceof DocumentFormBase) {
080            if (((DocumentFormBase) form).getDocument() != null) {
081                incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
082            }
083            incidentViewId = ((DocumentFormBase) form).getViewId();
084        }
085
086        if (GlobalVariables.getUifFormManager() != null) {
087            GlobalVariables.getUifFormManager().removeSessionForm(form);
088        }
089
090        UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
091        IncidentReportForm incidentReportForm = new IncidentReportForm();
092        incidentReportForm.setSessionId(request.getSession().getId());
093
094        // Set the post url map to the incident report controller and not 
095        // the one the exception occurred on
096        String postUrl = request.getRequestURL().toString();
097        postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
098        incidentReportForm.setFormPostUrl(postUrl);
099
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}