View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.web.struts.action;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.struts.Globals;
21  import org.apache.struts.action.Action;
22  import org.apache.struts.action.ActionForm;
23  import org.apache.struts.action.ActionForward;
24  import org.apache.struts.action.ActionMapping;
25  import org.kuali.rice.core.api.util.RiceConstants;
26  import org.kuali.rice.krad.util.KRADConstants;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  /**
34   * This is the struts action class for handling the exception for Kuali
35   * applications.
36   * 
37   */
38  public class AuthorizationExceptionAction extends Action {
39      
40      private static final String MESSAGE_FIELD = "message";
41      
42      private static final Log LOG = LogFactory.getLog(AuthorizationExceptionAction.class);
43  
44      /**
45       * Dispatches action to be taken during an AuthorizationException.
46       * 
47       * @see org.apache.struts.action.Action#execute(
48       *      org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, 
49       *      javax.servlet.http.HttpServletResponse)
50       */
51      @Override
52      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
53          if (LOG.isDebugEnabled()) {
54              LOG.debug(String.format("ENTRY %s%n%s", form.getClass().getSimpleName(), request.getRequestURI()));
55          }
56          
57          ActionForward forward = null;
58  
59          Throwable t = (Throwable) request.getAttribute(Globals.EXCEPTION_KEY);
60  
61          if (t == null) {
62              forward = mapping.findForward(KRADConstants.MAPPING_CLOSE);
63          } else {
64              forward = processException(mapping, form, request, t);
65          }
66          
67          if (LOG.isDebugEnabled()) {
68              LOG.debug(String.format("EXIT %s", (forward == null) ? "null" : forward.getPath()));
69          }
70  
71          return forward;
72      }
73      
74      private ActionForward processException(ActionMapping mapping, ActionForm form, HttpServletRequest request, Throwable t) throws Exception {
75          Map<String, String> properties = new HashMap<String, String>();
76          properties.put(MESSAGE_FIELD, t.getMessage());
77  
78          request.setAttribute(AuthorizationExceptionAction.class.getName(), properties);
79          
80          return mapping.findForward(RiceConstants.MAPPING_BASIC);
81      }
82  
83  }