View Javadoc

1   /*
2    * Copyright 2005-2010 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 java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.struts.Globals;
27  import org.apache.struts.action.Action;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.kuali.rice.core.util.RiceConstants;
32  import org.kuali.rice.kns.util.KNSConstants;
33  
34  /**
35   * This is the struts action class for handling the exception for Kuali
36   * applications.
37   * 
38   */
39  public class AuthorizationExceptionAction extends Action {
40      
41      private static final String MESSAGE_FIELD = "message";
42      
43      private static final Log LOG = LogFactory.getLog(AuthorizationExceptionAction.class);
44  
45      /**
46       * Dispatches action to be taken during an AuthorizationException.
47       * 
48       * @see org.apache.struts.action.Action#execute(
49       *      org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, 
50       *      javax.servlet.http.HttpServletResponse)
51       */
52      @Override
53      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
54          if (LOG.isDebugEnabled()) {
55              LOG.debug(String.format("ENTRY %s%n%s", form.getClass().getSimpleName(), request.getRequestURI()));
56          }
57          
58          ActionForward forward = null;
59  
60          Throwable t = (Throwable) request.getAttribute(Globals.EXCEPTION_KEY);
61  
62          if (t == null) {
63              forward = mapping.findForward(KNSConstants.MAPPING_CLOSE);
64          } else {
65              forward = processException(mapping, form, request, t);
66          }
67          
68          if (LOG.isDebugEnabled()) {
69              LOG.debug(String.format("EXIT %s", (forward == null) ? "null" : forward.getPath()));
70          }
71  
72          return forward;
73      }
74      
75      private ActionForward processException(ActionMapping mapping, ActionForm form, HttpServletRequest request, Throwable t) throws Exception {
76          Map<String, String> properties = new HashMap<String, String>();
77          properties.put(MESSAGE_FIELD, t.getMessage());
78  
79          request.setAttribute(AuthorizationExceptionAction.class.getName(), properties);
80          
81          return mapping.findForward(RiceConstants.MAPPING_BASIC);
82      }
83  
84  }