001/**
002 * Copyright 2005-2016 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.kns.web.struts.form.pojo;
017
018import org.apache.log4j.Logger;
019import org.apache.struts.action.ActionForm;
020import org.apache.struts.action.ActionForward;
021import org.apache.struts.action.ActionMapping;
022import org.apache.struts.action.ExceptionHandler;
023import org.apache.struts.config.ExceptionConfig;
024import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
025import org.kuali.rice.kns.util.IncidentReportUtils;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029import java.util.Map;
030
031/**
032 * This class is the exception handler for the base exception class java.lang.Throwable
033 * and is defined as global exception in the struts-config.xml. 
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class StrutsExceptionIncidentHandler extends ExceptionHandler {
039    private static final Logger LOG=
040        Logger.getLogger(StrutsExceptionIncidentHandler.class);
041    
042    /**
043     * This is defined in struts-config.xml for forwarding this exception to a specified
044     * exception handler.
045     * <p>Value is exceptionIncidentHandler
046     */
047    public static final String EXCEPTION_INCIDENT_HANDLER="exceptionIncidentHandler";
048    
049    /**
050     * This overridden method extract exception information such as component name,
051     * user name and email, etc.
052     * 
053     * @see org.apache.struts.action.ExceptionHandler#execute(
054     * java.lang.Exception,
055     *  org.apache.struts.config.ExceptionConfig,
056     *   org.apache.struts.action.ActionMapping,
057     *    org.apache.struts.action.ActionForm,
058     *     javax.servlet.http.HttpServletRequest,
059     *      javax.servlet.http.HttpServletResponse)
060     */
061    public ActionForward execute(Exception exception,
062            ExceptionConfig exceptionConfig,
063            ActionMapping mapping,
064            ActionForm form,
065            HttpServletRequest request,
066            HttpServletResponse response) {
067        
068        if (LOG.isTraceEnabled()) {
069            String message=String.format("ENTRY %s", exception.getMessage());
070            LOG.trace(message);
071        }
072        
073        LOG.error("Exception being handled by Exception Handler", exception);
074
075        String documentId="";
076        if (form instanceof KualiDocumentFormBase) {
077            KualiDocumentFormBase docForm=(KualiDocumentFormBase)form;
078            if (docForm.getDocument() != null) {
079                documentId=docForm.getDocument().getDocumentNumber();
080            }
081        }
082        
083        Map<String, String> properties = IncidentReportUtils.populateRequestForIncidentReport(exception, documentId, form.getClass().getSimpleName(), request);
084        
085        ActionForward forward=mapping.findForward(EXCEPTION_INCIDENT_HANDLER);
086        
087        if (LOG.isTraceEnabled()) {
088            String message=String.format("ENTRY %s%n%s%n%s",
089                    exception.getMessage(),
090                    properties.toString(),
091                    (forward==null)?"null":forward.getPath());
092            LOG.trace(message);
093        }
094
095        return forward;
096    }
097
098}
099