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.util;
017
018import org.apache.struts.Globals;
019import org.kuali.rice.kim.api.identity.Person;
020import org.kuali.rice.krad.UserSession;
021import org.kuali.rice.krad.exception.KualiExceptionIncident;
022import org.kuali.rice.krad.util.KRADConstants;
023
024import javax.servlet.http.HttpServletRequest;
025import java.util.HashMap;
026import java.util.Map;
027
028/**
029 * Utility methods for use with the incident report functionality.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 * 
033 */
034public final class IncidentReportUtils {
035
036        /**
037     * Key to define the attribute stores exception properties such as
038     * user email, user name, component name, etc.
039     * <p>Value is exceptionProperties
040     */
041    public static final String EXCEPTION_PROPERTIES="exceptionProperties";
042    
043        private IncidentReportUtils() {
044                throw new UnsupportedOperationException("do not call");
045        }
046
047        public static Map<String, String> populateRequestForIncidentReport(Exception exception,
048                        String documentId, String componentName, HttpServletRequest request) {
049
050                // Create properties of form and user for additional information
051                // to be displayed or passing through JSP
052                Map<String, String> properties = new HashMap<String, String>();
053                properties.put(KualiExceptionIncident.DOCUMENT_ID, documentId);
054                String userEmail = "";
055                String userName = "";
056                String uuid = "";
057                // No specific forward for the caught exception, use default logic
058                // Get user information
059                UserSession userSession = (UserSession) request.getSession()
060                                .getAttribute(KRADConstants.USER_SESSION_KEY);
061                Person sessionUser = null;
062                if (userSession != null) {
063                        sessionUser = userSession.getPerson();
064                }
065                if (sessionUser != null) {
066                        userEmail = sessionUser.getEmailAddressUnmasked();
067                        userName = sessionUser.getName();
068                        uuid = sessionUser.getPrincipalName();
069                }
070                properties.put(KualiExceptionIncident.USER_EMAIL, userEmail);
071                properties.put(KualiExceptionIncident.USER_NAME, userName);
072                properties.put(KualiExceptionIncident.UUID, uuid);
073                properties.put(KualiExceptionIncident.COMPONENT_NAME, componentName);
074
075                // Reset the exception so the forward action can read it
076                request.setAttribute(Globals.EXCEPTION_KEY, exception);
077                // Set exception current information
078                request.setAttribute(EXCEPTION_PROPERTIES, properties);
079
080                return properties;
081
082        }
083}