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.action;
017
018import org.apache.commons.logging.Log;
019import org.apache.commons.logging.LogFactory;
020import org.apache.struts.Globals;
021import org.apache.struts.action.Action;
022import org.apache.struts.action.ActionForm;
023import org.apache.struts.action.ActionForward;
024import org.apache.struts.action.ActionMapping;
025import org.kuali.rice.core.api.util.RiceConstants;
026import org.kuali.rice.krad.util.KRADConstants;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030import java.util.HashMap;
031import java.util.Map;
032
033/**
034 * This is the struts action class for handling the exception for Kuali
035 * applications.
036 * 
037 */
038public class AuthorizationExceptionAction extends Action {
039    
040    private static final String MESSAGE_FIELD = "message";
041    
042    private static final Log LOG = LogFactory.getLog(AuthorizationExceptionAction.class);
043
044    /**
045     * Dispatches action to be taken during an AuthorizationException.
046     * 
047     * @see org.apache.struts.action.Action#execute(
048     *      org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, 
049     *      javax.servlet.http.HttpServletResponse)
050     */
051    @Override
052    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
053        if (LOG.isDebugEnabled()) {
054            LOG.debug(String.format("ENTRY %s%n%s", form.getClass().getSimpleName(), request.getRequestURI()));
055        }
056        
057        ActionForward forward = null;
058
059        Throwable t = (Throwable) request.getAttribute(Globals.EXCEPTION_KEY);
060
061        if (t == null) {
062            forward = mapping.findForward(KRADConstants.MAPPING_CLOSE);
063        } else {
064            forward = processException(mapping, form, request, t);
065        }
066        
067        if (LOG.isDebugEnabled()) {
068            LOG.debug(String.format("EXIT %s", (forward == null) ? "null" : forward.getPath()));
069        }
070
071        return forward;
072    }
073    
074    private ActionForward processException(ActionMapping mapping, ActionForm form, HttpServletRequest request, Throwable t) throws Exception {
075        Map<String, String> properties = new HashMap<String, String>();
076        properties.put(MESSAGE_FIELD, t.getMessage());
077
078        request.setAttribute(AuthorizationExceptionAction.class.getName(), properties);
079        
080        return mapping.findForward(RiceConstants.MAPPING_BASIC);
081    }
082
083}