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