KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.action.ActionMapping;
28
29 import com.sslexplorer.core.CoreUtil;
30 import com.sslexplorer.policyframework.NoPermissionException;
31 import com.sslexplorer.policyframework.Permission;
32 import com.sslexplorer.policyframework.PolicyConstants;
33 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
34 import com.sslexplorer.policyframework.PolicyUtil;
35 import com.sslexplorer.policyframework.Resource;
36 import com.sslexplorer.policyframework.ResourceStack;
37 import com.sslexplorer.policyframework.ResourceType;
38 import com.sslexplorer.policyframework.ResourceUtil;
39 import com.sslexplorer.policyframework.forms.AbstractResourcesForm;
40 import com.sslexplorer.properties.Property;
41 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey;
42 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey;
43 import com.sslexplorer.security.Constants;
44 import com.sslexplorer.security.SessionInfo;
45 import com.sslexplorer.table.actions.AbstractPagerAction;
46
47 /**
48  * Abstract class which adds generic behaviour for resources, extending the
49  * {@link com.sslexplorer.table.actions.AbstractPagerAction}
50  *
51  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
52  *
53  */

54 public abstract class AbstractResourcesDispatchAction extends AbstractPagerAction {
55     protected Permission editPermission;
56     protected Permission createPermission;
57     protected Permission removePermission;
58     protected Permission assignPermission;
59
60     /**
61      * Constructor
62      */

63     public AbstractResourcesDispatchAction() {
64         super();
65     }
66
67     /**
68      * Constructor
69      *
70      * @param resourceType
71      * @param requiredPermissions
72      * @param editPermission
73      * @param createPermission
74      * @param removePermission
75      * @param assignPermission
76      */

77     public AbstractResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions,
78                                            Permission editPermission, Permission createPermission,
79                                            Permission removePermission, Permission assignPermission) {
80         this(resourceType, requiredPermissions, editPermission, createPermission, removePermission, assignPermission, null);
81     }
82
83     /**
84      * Constructor for normal resource types that have the standard,
85      * Create / Edit / Assign, Edit / Assign, Delete and Assign permissions
86      *
87      * @param resourceType resource type
88      * @param requiresResources requires actual resources of type
89      */

90     public AbstractResourcesDispatchAction(ResourceType resourceType, ResourceType requiresResources) {
91         this(resourceType, new Permission[] {
92                         PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN,
93                         PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN },
94                         PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE,
95                         PolicyConstants.PERM_ASSIGN, requiresResources);
96     }
97
98     /**
99      * Constructor for normal resource types that need their permissions
100      * specifically stating.
101      *
102      * @param resourceType
103      * @param requiredPermissions
104      * @param editPermission
105      * @param createPermission
106      * @param removePermission
107      * @param assignPermission
108      * @param requiresResources
109      */

110     public AbstractResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions,
111                                            Permission editPermission, Permission createPermission,
112                                            Permission removePermission, Permission assignPermission,
113                                            ResourceType requiresResources) {
114         super(resourceType, requiredPermissions, requiresResources);
115         this.editPermission = editPermission;
116         this.createPermission = createPermission;
117         this.removePermission = removePermission;
118         this.assignPermission = assignPermission;
119     }
120
121     /* (non-Javadoc)
122      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, 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 list(mapping, form, request, response);
127     }
128
129     /**
130      * Confirm removal of a resource
131      * @param mapping
132      * @param form
133      * @param request
134      * @param response
135      * @return ActionForward
136      * @throws Exception
137      */

138     public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
139                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
140         if (getRemovePermission() != null) {
141             if (getResourceType() == null) {
142                 throw new Exception JavaDoc(
143                     "Concrete implementation of AbstractResourcesDispatchAction does not provide the ResourceType that it is maintaining.");
144             }
145             PolicyUtil.checkPermission(getResourceType(), getRemovePermission(), request);
146         }
147         return mapping.findForward("confirmRemove");
148     }
149
150     /**
151      * Create a resource
152      * @param mapping
153      * @param form
154      * @param request
155      * @param response
156      * @return ActionForward
157      * @throws Exception
158      */

159     public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
160                     throws Exception JavaDoc {
161         if (getCreatePermission() != null) {
162             if (getResourceType() == null) {
163                 throw new Exception JavaDoc(
164                     "Concrete implementation of AbstractResourcesDispatchAction does not provide the ResourceType that it is maintaining.");
165             }
166             PolicyUtil.checkPermission(getResourceType(), getCreatePermission(), request);
167         }
168         return mapping.findForward("create");
169     }
170
171     /**
172      * Remove a resource.
173      * @param mapping
174      * @param form
175      * @param request
176      * @param response
177      * @return ActionForward
178      * @throws Exception
179      */

180     public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
181                     throws Exception JavaDoc {
182         Resource r= getResourceById(((AbstractResourcesForm)form).getSelectedResource());
183         checkValid(r, new Permission[] { getRemovePermission() }, mapping, (AbstractResourcesForm) form, request);
184         doRemove(mapping, form, request, response);
185         PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(r, getSessionInfo(request));
186         return getRedirectWithMessages(mapping, request);
187     }
188     
189     /**
190      * Perform the removal of a resource
191      * @param mapping
192      * @param form
193      * @param request
194      * @param response
195      * @throws Exception
196      */

197     protected void doRemove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
198     throws Exception JavaDoc {
199         getResourceType().removeResource(((AbstractResourcesForm)form).getSelectedResource(), getSessionInfo(request));
200     }
201
202     /**
203      * View the resource.
204      *
205      * @param mapping
206      * @param form
207      * @param request
208      * @param response
209      * @return ActionForward
210      * @throws Exception
211      */

212     public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
213                     throws Exception JavaDoc {
214         edit(mapping, form, request, response);
215         return mapping.findForward("view");
216     }
217
218     /**
219      * Show information about the resource
220      *
221      * @param mapping mapping
222      * @param form form
223      * @param request request
224      * @param response response
225      * @return forward forward
226      * @throws Exception on any error
227      */

228     public ActionForward information(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
229                     throws Exception JavaDoc {
230         int id = ((AbstractResourcesForm) form).getSelectedResource();
231         Resource r = getResourceType().getResourceById(id);
232         try {
233             ResourceUtil.checkResourceManagementRights(r, getSessionInfo(request), new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE });
234         }
235         catch(NoPermissionException npe) {
236             ResourceUtil.checkResourceAccessRights(r, getSessionInfo(request));
237         }
238         request.setAttribute(Constants.REQ_ATTR_INFO_RESOURCE, r);
239         return mapping.findForward("resourceInformation");
240     }
241
242     /**
243      * Edit the resource.
244      *
245      * @param mapping
246      * @param form
247      * @param request
248      * @param response
249      * @return ActionForward
250      * @throws Exception
251      */

252     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
253                     throws Exception JavaDoc {
254
255         // Get the selected resource ID and call the abstract getResourceById
256
// method to actual the actual resource object
257
int id = ((AbstractResourcesForm) form).getSelectedResource();
258         Resource r = getResourceById(id);
259         if (r == null) {
260             throw new Exception JavaDoc("No resource with ID " + id);
261         }
262
263         // Make sure this resource is one that is valid
264
checkValid(r, new Permission[] { getEditPermission(), getCreatePermission(), getAssignPermission() }, mapping, (AbstractResourcesForm) form, request);
265         ResourceStack.pushToEditingStack(request.getSession(), r);
266         return mapping.findForward("edit");
267     }
268
269     /**
270      * Clone the resource.
271      *
272      * @param mapping
273      * @param form
274      * @param request
275      * @param response
276      * @return ActionForward
277      * @throws Exception
278      */

279     public ActionForward clone(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
280                     throws Exception JavaDoc {
281         // Get the selected resource ID and call the abstract getResourceById
282
// method to actual the actual resource object
283
int id = ((AbstractResourcesForm) form).getSelectedResource();
284         Resource r = getResourceById(id);
285         if (r == null) {
286             throw new Exception JavaDoc("No resource with ID " + id);
287         }
288
289         // Make sure this resource is one that is valid
290
checkValid(r, new Permission[] { getCreatePermission() }, mapping, (AbstractResourcesForm) form, request);
291         ResourceStack.pushToEditingStack(request.getSession(), r);
292         return mapping.findForward("clone");
293     }
294
295     /**
296      * Display the resource.
297      *
298      * @param mapping
299      * @param form
300      * @param request
301      * @param response
302      * @return ActionForward
303      * @throws Exception
304      */

305     public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
306                     throws Exception JavaDoc {
307         return mapping.findForward("display");
308     }
309
310     /**
311      * Change the selected view to
312      * {@link AbstractResourcesForm#ICONS_VIEW}.
313      *
314      * @param mapping mapping
315      * @param form form
316      * @param request request
317      * @param response response
318      * @return forward
319      * @throws Exception on any error
320      */

321     public ActionForward viewIcons(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
322                     throws Exception JavaDoc {
323         ((AbstractResourcesForm) form).setSelectedView(AbstractResourcesForm.ICONS_VIEW);
324         CoreUtil.storeUIState("ui_view_" + ((AbstractResourcesForm)form).getModel().getId() + "_" + getSessionInfo(request).getNavigationContext(), AbstractResourcesForm.ICONS_VIEW, request, response);
325         return mapping.findForward("display");
326     }
327
328     /**
329      * Change the selected view to
330      * {@link AbstractResourcesForm#LIST_VIEW}.
331      *
332      * @param mapping mapping
333      * @param form form
334      * @param request request
335      * @param response response
336      * @return forward
337      * @throws Exception on any error
338      */

339     public ActionForward viewList(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
340                     throws Exception JavaDoc {
341         ((AbstractResourcesForm) form).setSelectedView(AbstractResourcesForm.LIST_VIEW);
342         CoreUtil.storeUIState("ui_view_" + ((AbstractResourcesForm)form).getModel().getId() + "_" + getSessionInfo(request).getNavigationContext(), AbstractResourcesForm.LIST_VIEW, request, response);
343         return mapping.findForward("display");
344     }
345
346     /**
347      * @param id
348      * @return Resource
349      * @throws Exception
350      */

351     public Resource getResourceById(int id) throws Exception JavaDoc {
352         return getResourceType().getResourceById(id);
353     }
354
355     /**
356      * @param r
357      * @param permission
358      * @param mapping
359      * @param form
360      * @param request
361      * @throws NoPermissionException
362      */

363     protected void checkValid(Resource r, Permission[] permission, ActionMapping mapping, AbstractResourcesForm form, HttpServletRequest JavaDoc request)
364                     throws NoPermissionException {
365         ResourceUtil.checkResourceManagementRights(r, this.getSessionInfo(request), permission);
366     }
367
368     /**
369      * Get the edit permission.
370      *
371      * @return Permission
372      */

373     public Permission getEditPermission() {
374         return editPermission;
375     }
376
377     /**
378      * Get the assign permission
379      *
380      * @return Permission
381      */

382     public Permission getAssignPermission() {
383         return assignPermission;
384     }
385
386     /**
387      * Get the create permission
388      *
389      * @return Permission
390      */

391     public Permission getCreatePermission() {
392         return createPermission;
393     }
394
395     /**
396      * Get the removal/delete permission
397      *
398      * @return Permission
399      */

400     public Permission getRemovePermission() {
401         return removePermission;
402     }
403     
404     protected void saveError(HttpServletRequest JavaDoc request, String JavaDoc message, Resource resource) {
405         saveError(request, message, resource.getResourceDisplayName());
406     }
407     
408     protected void saveMessage(HttpServletRequest JavaDoc request, String JavaDoc message, Resource resource) {
409         saveMessage(request, message, resource.getResourceDisplayName());
410     }
411 }
Popular Tags