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