KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > actions > AbstractResourceDispatchAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.policyframework.actions;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33
34 import com.sslexplorer.boot.PropertyList;
35 import com.sslexplorer.core.CoreUtil;
36 import com.sslexplorer.core.UserDatabaseManager;
37 import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
38 import com.sslexplorer.core.forms.CoreForm;
39 import com.sslexplorer.input.MultiSelectDataSource;
40 import com.sslexplorer.input.MultiSelectSelectionModel;
41 import com.sslexplorer.navigation.FavoriteResourceType;
42 import com.sslexplorer.policyframework.AccessRightsGrantedPoliciesDatasource;
43 import com.sslexplorer.policyframework.NoPermissionException;
44 import com.sslexplorer.policyframework.OwnedResource;
45 import com.sslexplorer.policyframework.Permission;
46 import com.sslexplorer.policyframework.Policy;
47 import com.sslexplorer.policyframework.PolicyConstants;
48 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
49 import com.sslexplorer.policyframework.PolicyUtil;
50 import com.sslexplorer.policyframework.Resource;
51 import com.sslexplorer.policyframework.ResourceStack;
52 import com.sslexplorer.policyframework.ResourceType;
53 import com.sslexplorer.policyframework.ResourceUtil;
54 import com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm;
55 import com.sslexplorer.policyframework.forms.AbstractResourceForm;
56 import com.sslexplorer.security.Constants;
57 import com.sslexplorer.security.LogonControllerFactory;
58 import com.sslexplorer.security.SessionInfo;
59 import com.sslexplorer.security.User;
60
61 /**
62  * Abstract implementation of an
63  * {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction} that allows
64  * viewing, editing and creating of
65  * {@link com.sslexplorer.policyframework.Resource} implementations.
66  *
67  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
68  */

69 public abstract class AbstractResourceDispatchAction extends AuthenticatedDispatchAction {
70     final static Log log = LogFactory.getLog(AbstractResourceDispatchAction.class);
71
72     protected Permission editPermission;
73     protected Permission createPermission;
74     protected Permission removePermission;
75     protected Permission assignPermission;
76
77     /**
78      * Constructor that places now restriction on permissions required or
79      * resources required.
80      */

81     public AbstractResourceDispatchAction() {
82         super();
83     }
84
85     /**
86      * Constructor for normal resource types that have the standard,
87      * Create / Edit / Assign, Edit / Assign, Delete and Assign permissions
88      *
89      * @param resourceType resource type
90      */

91     public AbstractResourceDispatchAction(ResourceType resourceType) {
92         this(resourceType, new Permission[] { PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN },
93                         PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE,
94                         PolicyConstants.PERM_ASSIGN);
95     }
96
97     /**
98      * Constructor for specific cases
99      *
100      * @param resourceType resource type of permissions required
101      * @param requiredPermissions required permissions
102      * @param editPermission required edit permission
103      * @param createPermission required create permission
104      * @param removePermission required remove permission
105      * @param assignPermission required assign permission
106      */

107     public AbstractResourceDispatchAction(ResourceType resourceType, Permission[] requiredPermissions, Permission editPermission,
108                                           Permission createPermission, Permission removePermission, Permission assignPermission) {
109         super(resourceType, requiredPermissions);
110         this.editPermission = editPermission;
111         this.createPermission = createPermission;
112         this.removePermission = removePermission;
113         this.assignPermission = assignPermission;
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
120      * org.apache.struts.action.ActionForm,
121      * javax.servlet.http.HttpServletRequest,
122      * javax.servlet.http.HttpServletResponse)
123      */

124     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
125                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
126         return edit(mapping, form, request, response);
127     }
128
129     /**
130      * Clone a new instance of the resource.
131      *
132      * @param mapping mapping
133      * @param form form
134      * @param request request
135      * @param response response
136      * @return forward
137      * @throws Exception on any error
138      */

139     public ActionForward clone(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
140                     throws Exception JavaDoc {
141         if (getCreateResourcePermission() != null) {
142             if (getResourceType() == null) {
143                 throw new Exception JavaDoc(
144                     "Concrete implementation of AbstractResourceDispatchAction does not provide the ResourceType that it is maintaining.");
145             }
146             PolicyUtil.checkPermission(getResourceType(), getCreateResourcePermission(), request);
147         }
148         Resource sourceResource = ResourceStack.popFromEditingStack(request.getSession());
149         ResourceStack.pushToEditingStack(request.getSession(), sourceResource.getResourceType().cloneResource(sourceResource, getSessionInfo(request)));
150         ActionForward fwd = edit(mapping, form, request, response);
151         ((AbstractResourceForm) form).setCreating();
152         return fwd;
153     }
154
155     /**
156      * Create a new instance of the resource. Not all resource types will
157      * require this as creation is usually done through a wizard.
158      *
159      * @param mapping mapping
160      * @param form form
161      * @param request request
162      * @param response response
163      * @return forward
164      * @throws Exception on any error
165      */

166     public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
167                     throws Exception JavaDoc {
168         if (getCreateResourcePermission() != null) {
169             if (getResourceType() == null) {
170                 throw new Exception JavaDoc(
171                     "Concrete implementation of AbstractResourceDispatchAction does not provide the ResourceType that it is maintaining.");
172             }
173             PolicyUtil.checkPermission(getResourceType(), getCreateResourcePermission(), request);
174         }
175         ResourceStack.pushToEditingStack(request.getSession(), createResource(mapping, form, request, response));
176         ActionForward fwd = edit(mapping, form, request, response);
177         ((AbstractResourceForm) form).setCreating();
178         return fwd;
179     }
180
181     /**
182      * If the resource supports creation using the edit screen (most dont, they
183      * now use wizards) this method must return an instance of the support
184      * resource. <code>null</code> should be returned if creation isn't
185      * supported here
186      *
187      * @param mapping mapping
188      * @param form form
189      * @param request request
190      * @param response response
191      * @return resource
192      * @throws Exception on any error
193      */

194     public abstract Resource createResource(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
195                     HttpServletResponse JavaDoc response) throws Exception JavaDoc;
196
197     /**
198      * Commit the resource being edited.
199      *
200      * @param mapping mapping
201      * @param form form
202      * @param request request
203      * @param response response
204      * @return forward
205      * @throws Exception
206      */

207     public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
208                     throws Exception JavaDoc {
209         AbstractResourceForm resourceForm = (AbstractResourceForm) form;
210         resourceForm.apply();
211         Resource resource = resourceForm.getResource();
212         if(resourceForm.getEditing()) {
213             resource.getResourceType().updateResource(resource, getSessionInfo(request));
214         }
215         else {
216             resource = commitCreatedResource(mapping, resourceForm, request, response);
217         }
218         doUpdate(mapping, form, request, response);
219         
220         // Profiles are a special case that cannot have their policies changes
221
if(resource.getResourceType() != PolicyConstants.PROFILE_RESOURCE_TYPE || !resource.getResourceName().equals("Default")) {
222             PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, resourceForm.getSelectedPoliciesList(), getSessionInfo(request));
223         }
224         if (resource.getResourceType() instanceof FavoriteResourceType) {
225             ResourceUtil.setResourceGlobalFavorite(resource, ((AbstractFavoriteResourceForm) resourceForm).isFavorite());
226         }
227         return cleanUpAndReturnToReferer(mapping, form, request, response);
228     }
229
230     /**
231      * <p> Method to be overridden when extra work needs to be done on update, in its abstract form it does nothing.
232      *
233      * @param mapping mapping
234      * @param form form
235      * @param request request
236      * @param response response
237      * @throws Exception
238      */

239     protected void doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc{
240     }
241
242     /**
243      * View the resource. This sets the read only flag on the form and the
244      * process as it if were an edit.
245      *
246      * @param mapping mapping
247      * @param form form
248      * @param request request
249      * @param response response
250      * @return forward
251      * @throws Exception
252      */

253     public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
254                     throws Exception JavaDoc {
255         ((AbstractResourceForm) form).setReadOnly();
256         return edit(mapping, form, request, response);
257     }
258
259     /**
260      * Edit the resource. An instance of the resource to edit should have been
261      * passed in as a session attribute {@link Constants#EDITING_RESOURCE_STACK}.
262      * <p>
263      * Permissions will be checked and the form initialised.
264      *
265      * @param mapping mapping
266      * @param form form
267      * @param request request
268      * @param response response
269      * @return forward
270      * @throws Exception on any error
271      */

272     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
273                     throws Exception JavaDoc {
274         ((AbstractResourceForm) form).setEditing();
275         Resource resource = ResourceStack.peekEditingStack(request.getSession());
276         SessionInfo session = this.getSessionInfo(request);
277         checkValid(resource, new Permission[] { getEditResourcePermission(), getCreateResourcePermission(), getAssignPermission() }, mapping, (AbstractResourceForm) form, request);
278         User ownerUser = null;
279         if (resource instanceof OwnedResource) {
280             String JavaDoc owner = ((OwnedResource) resource).getOwnerUsername();
281             if (owner != null && !owner.equals("")) {
282                 ownerUser = UserDatabaseManager.getInstance().getUserDatabase(session.getUser().getRealm()).getAccount(owner);
283             }
284         }
285         PropertyList selectedPolicies = new PropertyList();
286         MultiSelectDataSource policies = createAvailablePoliciesDataSource(resource, mapping, form, request, response);
287         List JavaDoc l = PolicyDatabaseFactory.getInstance().getPoliciesAttachedToResource(resource, session.getUser().getRealm());
288         for (Iterator JavaDoc i = l.iterator(); i.hasNext();) {
289             selectedPolicies.add(String.valueOf(((Policy) i.next()).getResourceId()));
290         }
291         String JavaDoc referer = CoreUtil.getReferer(request);
292         ((CoreForm) form).setReferer(referer);
293         MultiSelectSelectionModel policyModel = new MultiSelectSelectionModel(session, policies, selectedPolicies);
294         ((AbstractResourceForm) form).initialise(session.getUser(), resource, true, policyModel, selectedPolicies, ownerUser, isAssignOnly(resource, session));
295         return display(mapping, form, request, response);
296     }
297
298     /**
299      * Werather the operation has assign only permissions.
300      *
301      * @param resource
302      * @param session
303      * @return boolean
304      * @throws Exception
305      */

306     private boolean isAssignOnly(Resource resource, SessionInfo session) throws Exception JavaDoc {
307         if (!LogonControllerFactory.getInstance().isAdministrator(session.getUser())){
308             boolean canAssign = PolicyDatabaseFactory.getInstance().isPermitted(resource.getResourceType(), new Permission[] {PolicyConstants.PERM_ASSIGN}, session.getUser(), false);
309             boolean canEditAssign = PolicyDatabaseFactory.getInstance().isPermitted(resource.getResourceType(), new Permission[] {PolicyConstants.PERM_EDIT_AND_ASSIGN}, session.getUser(), false);
310             boolean canCreateEditAssign = PolicyDatabaseFactory.getInstance().isPermitted(resource.getResourceType(), new Permission[] {PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN}, session.getUser(), false);
311             if (canAssign & !canEditAssign & !canCreateEditAssign)
312                 return true;
313         }
314         return false;
315     }
316
317     /**
318      * Confirm removal of the resource. This would usually forward on to a
319      * struts path that points to the
320      * {@link com.sslexplorer.navigation.actions.ConfirmAction}.
321      *
322      * @param mapping mapping
323      * @param form form
324      * @param request request
325      * @param response response
326      * @return forward
327      * @throws Exception on any error
328      */

329     public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
330                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
331         return mapping.findForward("confirmRemove");
332     }
333
334     /**
335      * Refresh this page. Simple forwards on to <i>refresh</i> forward from the
336      * action mapping.
337      *
338      * @param mapping mapping
339      * @param form form
340      * @param request request
341      * @param response response
342      * @return forward
343      * @throws Exception on any error
344      */

345     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
346                     throws Exception JavaDoc {
347         return mapping.findForward("refresh");
348     }
349
350     /**
351      * Display the resource. The <i>Required Field</i> message will be also be
352      * added.
353      *
354      * @param mapping mapping
355      * @param form form
356      * @param request request
357      * @param response response
358      * @return forward
359      * @throws Exception on any error
360      */

361     public ActionForward display(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
362                     throws Exception JavaDoc {
363         CoreUtil.addRequiredFieldMessage(this, request);
364         return mapping.findForward("display");
365     }
366
367     /**
368      * Get the permission that is required for editing the resource or
369      * <code>null</code> if no permission is required.
370      *
371      * @return edit permission
372      */

373     public Permission getEditResourcePermission() {
374         return editPermission;
375     }
376
377     /**
378      * Get the permission that is required for assign policies to a resource
379      * <code>null</code> if no permission is required.
380      *
381      * @return assign permission
382      */

383     public Permission getAssignPermission() {
384         return assignPermission;
385     }
386
387     /**
388      * Get the permission that is required for creating the resource or
389      * <code>null</code> if no permission is required.
390      *
391      * @return create permission
392      */

393
394     public Permission getCreateResourcePermission() {
395         return createPermission;
396     }
397
398     /**
399      * Get the permission that is required for removing the resource or
400      * <code>null</code> if no permission is required.
401      *
402      * @return remove permission
403      */

404     public Permission getRemoveResourcePermission() {
405         return removePermission;
406     }
407
408     /*
409      * (non-Javadoc)
410      *
411      * @see com.sslexplorer.core.actions.AuthenticatedDispatchAction#cleanUpAndReturnToReferer(org.apache.struts.action.ActionMapping,
412      * org.apache.struts.action.ActionForm,
413      * javax.servlet.http.HttpServletRequest,
414      * javax.servlet.http.HttpServletResponse)
415      */

416     public ActionForward cleanUpAndReturnToReferer(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
417                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
418         ResourceStack.popFromEditingStack(request.getSession());
419         return super.cleanUpAndReturnToReferer(mapping, form, request, response);
420     }
421
422     /*
423      * (non-Javadoc)
424      *
425      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
426      * org.apache.struts.action.ActionForm,
427      * javax.servlet.http.HttpServletRequest,
428      * javax.servlet.http.HttpServletResponse)
429      */

430     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
431         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT;
432     }
433
434     /**
435      * Check the resource is valid for management.
436      *
437      * @param r resource
438      * @param permissions permissions
439      * @param mapping mappnig
440      * @param form form
441      * @param request request
442      * @throws NoPermissionException
443      */

444     protected void checkValid(Resource r, Permission[] permissions, ActionMapping mapping, AbstractResourceForm form,
445                     HttpServletRequest JavaDoc request) throws NoPermissionException {
446         ResourceUtil.checkResourceManagementRights(r, this.getSessionInfo(request), permissions);
447     }
448     
449     /**
450      * Create the resource.
451      *
452      * @param mapping mapping
453      * @param form form
454      * @param request request
455      * @param response response
456      * @return newly created resource ID
457      * @throws Exception on any error
458      */

459     protected Resource commitCreatedResource(ActionMapping mapping, AbstractResourceForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
460         throw new Exception JavaDoc("commitCreatedResource() must be overidden to actually create the resource.");
461     }
462
463     protected MultiSelectDataSource createAvailablePoliciesDataSource(Resource resource, ActionMapping mapping, ActionForm form,
464                     HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
465         return new AccessRightsGrantedPoliciesDatasource(null);
466     }
467 }
Popular Tags