Coverage Report - org.kuali.rice.ksb.messaging.web.KSBAction
 
Classes in this File Line Coverage Branch Coverage Complexity
KSBAction
0%
0/45
0%
0/32
3.444
 
 1  
 /*
 2  
  * Copyright 2008-2009 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.ksb.messaging.web;
 17  
 
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 
 26  
 import org.apache.struts.action.ActionErrors;
 27  
 import org.apache.struts.action.ActionForm;
 28  
 import org.apache.struts.action.ActionForward;
 29  
 import org.apache.struts.action.ActionMapping;
 30  
 import org.apache.struts.action.ActionMessages;
 31  
 import org.apache.struts.actions.DispatchAction;
 32  
 import org.kuali.rice.core.util.AttributeSet;
 33  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 34  
 import org.kuali.rice.kim.util.KimConstants;
 35  
 import org.kuali.rice.kns.exception.AuthorizationException;
 36  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 37  
 import org.kuali.rice.kns.service.KualiModuleService;
 38  
 import org.kuali.rice.kns.util.GlobalVariables;
 39  
 import org.kuali.rice.kns.util.KNSConstants;
 40  
 import org.kuali.rice.kns.util.KNSUtils;
 41  
 
 42  
 /**
 43  
  * An abstract super class for all Struts Actions in KEW.  Adds some custom
 44  
  * dispatch behavior by extending the Struts DispatchAction.
 45  
  *
 46  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 47  
  */
 48  0
 public abstract class KSBAction extends DispatchAction {
 49  
 
 50  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KSBAction.class);
 51  
 
 52  
         @Override
 53  
         public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 54  
 
 55  0
                 checkAuthorization(form, "");
 56  
                 
 57  
                 try {
 58  
 
 59  
                         
 60  0
                         ActionMessages messages = null;
 61  0
                         messages = establishRequiredState(request, form);
 62  0
                         if (messages != null && !messages.isEmpty()) {
 63  
                                 // XXX: HACK: FIXME:
 64  
                                 // obviously this implies that we can't return both ActionErrors
 65  
                                 // and ActionMessages... :(
 66  
                                 // probably establishRequiredState should be refactored to have
 67  
                                 // a generic 'should-we-continue'
 68  
                                 // boolean return, so that control flow can be more explicitly
 69  
                                 // specified by the subclass
 70  0
                                 if (messages instanceof ActionErrors) {
 71  0
                                         saveErrors(request, messages);
 72  
                                 } else {
 73  0
                                         saveMessages(request, messages);
 74  
                                 }
 75  0
                                 return mapping.findForward("requiredStateError");
 76  
                         }
 77  0
                         LOG.info(request.getQueryString());
 78  0
                         ActionForward returnForward = null;
 79  
 
 80  0
                         if (request.getParameterMap() != null) {
 81  0
                                 for (Iterator iter = request.getParameterMap().entrySet().iterator(); iter.hasNext();) {
 82  0
                                         String parameterName = (String) ((Map.Entry) iter.next()).getKey();
 83  0
                                         if (parameterName.startsWith("methodToCall.") && parameterName.endsWith(".x")) {
 84  0
                                                 String methodToCall = parameterName.substring(parameterName.indexOf("methodToCall.") + 13, parameterName.lastIndexOf(".x"));
 85  0
                                                 if (methodToCall != null && methodToCall.length() > 0) {
 86  0
                                                         returnForward = this.dispatchMethod(mapping, form, request, response, methodToCall);
 87  
                                                 }
 88  
                                         }
 89  0
                                 }
 90  
                         }
 91  0
                         if (returnForward == null) {
 92  0
                                 if (request.getParameter("methodToCall") != null && !"".equals(request.getParameter("methodToCall")) && !"execute".equals(request.getParameter("methodToCall"))) {
 93  0
                                         LOG.info("dispatch to methodToCall " + request.getParameter("methodToCall") + " called");
 94  0
                                         returnForward = super.execute(mapping, form, request, response);
 95  
                                 } else {
 96  0
                                         LOG.info("dispatch to default start methodToCall");
 97  0
                                         returnForward = start(mapping, form, request, response);
 98  
                                 }
 99  
                         }
 100  
 
 101  
                         
 102  
                         
 103  0
                         messages = establishFinalState(request, form);
 104  0
                         if (messages != null && !messages.isEmpty()) {
 105  0
                                 saveMessages(request, messages);
 106  0
                                 return mapping.findForward("finalStateError");
 107  
                         }
 108  0
                         return returnForward;
 109  0
                 } catch (Exception e) {
 110  0
                         LOG.error("Error processing action " + mapping.getPath(), e);
 111  0
                         throw new RuntimeException(e);
 112  
                 }
 113  
         }
 114  
         
 115  
         protected void checkAuthorization( ActionForm form, String methodToCall) throws AuthorizationException 
 116  
     {
 117  0
             String principalId = GlobalVariables.getUserSession().getPrincipalId();
 118  0
             AttributeSet roleQualifier = new AttributeSet(getRoleQualification(form, methodToCall));
 119  0
             AttributeSet permissionDetails = KNSUtils.getNamespaceAndActionClass(this.getClass());
 120  
             
 121  0
         if (!KimApiServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(principalId, KNSConstants.KNS_NAMESPACE,
 122  
                         KimConstants.PermissionTemplateNames.USE_SCREEN, permissionDetails, roleQualifier )) 
 123  
         {
 124  0
                 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), 
 125  
                             methodToCall,
 126  
                             this.getClass().getSimpleName());
 127  
         }
 128  0
     }
 129  
     
 130  
     /** 
 131  
      * override this method to add data from the form for role qualification in the authorization check
 132  
      */
 133  
     protected Map<String,String> getRoleQualification(ActionForm form, String methodToCall) {
 134  0
             return new HashMap<String,String>();
 135  
     }
 136  
 
 137  
         public abstract ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception;
 138  
 
 139  
         public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 140  0
                 return start(mapping, form, request, response);
 141  
         }
 142  
 
 143  
         public abstract ActionMessages establishRequiredState(HttpServletRequest request, ActionForm form) throws Exception;
 144  
 
 145  
         public ActionMessages establishFinalState(HttpServletRequest request, ActionForm form) throws Exception {
 146  0
                 return null;
 147  
         }
 148  
 
 149  
         public ActionForward noOp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 150  0
                 return mapping.findForward("basic");
 151  
         }
 152  
         
 153  
         protected static KualiModuleService getKualiModuleService() {
 154  0
         return KNSServiceLocatorWeb.getKualiModuleService();
 155  
     }
 156  
 
 157  
 }