|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  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.xml.dto.AttributeSet;  | 
  |  33 |     | 
   import org.kuali.rice.kim.service.KIMServiceLocator;  | 
  |  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 |     | 
     | 
  |  44 |     | 
     | 
  |  45 |     | 
     | 
  |  46 |     | 
     | 
  |  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 |     | 
                                     | 
  |  64 |     | 
                                     | 
  |  65 |     | 
                                     | 
  |  66 |     | 
                                     | 
  |  67 |     | 
                                     | 
  |  68 |     | 
                                     | 
  |  69 |     | 
                                     | 
  |  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 (!KIMServiceLocator.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 |     | 
     | 
  |  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 |     | 
   }  |