View Javadoc

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  public abstract class KSBAction extends DispatchAction {
46  
47  	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  		checkAuthorization(form, "");
53  		
54  		try {
55  
56  			
57  			ActionMessages messages = null;
58  			messages = establishRequiredState(request, form);
59  			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  				if (messages instanceof ActionErrors) {
68  					saveErrors(request, messages);
69  				} else {
70  					saveMessages(request, messages);
71  				}
72  				return mapping.findForward("requiredStateError");
73  			}
74  			LOG.info(request.getQueryString());
75  			ActionForward returnForward = null;
76  
77  			if (request.getParameterMap() != null) {
78  				for (Iterator iter = request.getParameterMap().entrySet().iterator(); iter.hasNext();) {
79  					String parameterName = (String) ((Map.Entry) iter.next()).getKey();
80  					if (parameterName.startsWith("methodToCall.") && parameterName.endsWith(".x")) {
81  						String methodToCall = parameterName.substring(parameterName.indexOf("methodToCall.") + 13, parameterName.lastIndexOf(".x"));
82  						if (methodToCall != null && methodToCall.length() > 0) {
83  							returnForward = this.dispatchMethod(mapping, form, request, response, methodToCall);
84  						}
85  					}
86  				}
87  			}
88  			if (returnForward == null) {
89  				if (request.getParameter("methodToCall") != null && !"".equals(request.getParameter("methodToCall")) && !"execute".equals(request.getParameter("methodToCall"))) {
90  					LOG.info("dispatch to methodToCall " + request.getParameter("methodToCall") + " called");
91  					returnForward = super.execute(mapping, form, request, response);
92  				} else {
93  					LOG.info("dispatch to default start methodToCall");
94  					returnForward = start(mapping, form, request, response);
95  				}
96  			}
97  
98  			
99  			
100 			messages = establishFinalState(request, form);
101 			if (messages != null && !messages.isEmpty()) {
102 				saveMessages(request, messages);
103 				return mapping.findForward("finalStateError");
104 			}
105 			return returnForward;
106 		} catch (Exception e) {
107 			LOG.error("Error processing action " + mapping.getPath(), e);
108 			throw new RuntimeException(e);
109 		}
110 	}
111 	
112 	protected void checkAuthorization( ActionForm form, String methodToCall) throws AuthorizationException 
113     {
114     	String principalId = GlobalVariables.getUserSession().getPrincipalId();
115     	Map<String, String> roleQualifier = new HashMap<String, String>(getRoleQualification(form, methodToCall));
116     	Map<String, String> permissionDetails = KRADUtils.getNamespaceAndActionClass(this.getClass());
117     	
118         if (!KimApiServiceLocator.getPermissionService().isAuthorizedByTemplateName(principalId, KRADConstants.KRAD_NAMESPACE,
119         		KimConstants.PermissionTemplateNames.USE_SCREEN, permissionDetails, roleQualifier ))
120         {
121         	throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), 
122             		methodToCall,
123             		this.getClass().getSimpleName());
124         }
125     }
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     	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 		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 		return null;
144 	}
145 
146 	public ActionForward noOp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
147 		return mapping.findForward("basic");
148 	}
149 	
150 	protected static KualiModuleService getKualiModuleService() {
151         return KRADServiceLocatorWeb.getKualiModuleService();
152     }
153 
154 }