KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > actions > ShowProfilesDispatchAction


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.properties.actions;
21
22 import java.util.List JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.action.ActionMessages;
33
34 import com.sslexplorer.core.CoreUtil;
35 import com.sslexplorer.policyframework.NoPermissionException;
36 import com.sslexplorer.policyframework.OwnedResource;
37 import com.sslexplorer.policyframework.Permission;
38 import com.sslexplorer.policyframework.PolicyConstants;
39 import com.sslexplorer.policyframework.PolicyUtil;
40 import com.sslexplorer.policyframework.Resource;
41 import com.sslexplorer.policyframework.ResourceUtil;
42 import com.sslexplorer.policyframework.actions.AbstractResourcesDispatchAction;
43 import com.sslexplorer.policyframework.forms.AbstractResourcesForm;
44 import com.sslexplorer.properties.ProfilesFactory;
45 import com.sslexplorer.properties.PropertyProfile;
46 import com.sslexplorer.properties.forms.ProfilesForm;
47 import com.sslexplorer.security.Constants;
48 import com.sslexplorer.security.SessionInfo;
49
50 /**
51  * This dispatch action is used for display and maintaing lists of property
52  * profiles. Property profiles are the one type of resource in SSL-Explorer that
53  * still has a 'scope', i.e. Personal Profiles may still be created.
54  *
55  * @author Brett Smith <brett@3sp.com>
56  */

57
58 public class ShowProfilesDispatchAction extends AbstractResourcesDispatchAction {
59
60     /**
61      * Constructor
62      */

63     public ShowProfilesDispatchAction() {
64         super(PolicyConstants.PROFILE_RESOURCE_TYPE, PolicyConstants.PROFILE_RESOURCE_TYPE);
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
71      * org.apache.struts.action.ActionForm,
72      * javax.servlet.http.HttpServletRequest,
73      * javax.servlet.http.HttpServletResponse)
74      */

75     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
76                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
77         ActionForward fwd = super.unspecified(mapping, form, request, response);
78         ProfilesForm profilesForm = (ProfilesForm) form;
79         SessionInfo session = this.getSessionInfo(request);
80         List JavaDoc globalProfiles = null;
81         List JavaDoc personalProfiles = null;
82         if (session.getNavigationContext() == SessionInfo.MANAGEMENT_CONSOLE_CONTEXT) {
83             profilesForm.setProfileScope(Constants.SCOPE_GLOBAL);
84             globalProfiles = ProfilesFactory.getInstance().getPropertyProfiles(null, true,
85                             session.getUser().getRealm().getResourceId());
86         } else {
87             profilesForm.setProfileScope(Constants.SCOPE_PERSONAL);
88             globalProfiles = ResourceUtil.filterOwned(ResourceUtil.getGrantedResource(
89                             getSessionInfo(request), getResourceType()));
90             personalProfiles = ResourceUtil.filterResources(session.getUser(), ProfilesFactory.getInstance().getPropertyProfiles(
91                 session.getUser().getPrincipalName(), true, session.getUser().getRealm().getResourceId()), false);
92         }
93         PropertyProfile profile = (PropertyProfile) request.getSession().getAttribute(Constants.SELECTED_PROFILE);
94         if (profile != null) {
95             profilesForm.initialize(globalProfiles, personalProfiles, request.getSession(), profile.getResourceId());
96         } else {
97             profilesForm.initialize(globalProfiles, personalProfiles, request.getSession(), -1);
98         }
99         profilesForm.checkSelectedView(request, response);
100         return fwd;
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see com.sslexplorer.policyframework.actions.AbstractResourcesDispatchAction#create(org.apache.struts.action.ActionMapping,
107      * org.apache.struts.action.ActionForm,
108      * javax.servlet.http.HttpServletRequest,
109      * javax.servlet.http.HttpServletResponse)
110      */

111     public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
112                     throws Exception JavaDoc {
113         if (getSessionInfo(request).getNavigationContext() == SessionInfo.USER_CONSOLE_CONTEXT) {
114             PolicyUtil.checkPermission(PolicyConstants.PERSONAL_PROFILE_RESOURCE_TYPE, PolicyConstants.PERM_MAINTAIN, request);
115         } else {
116             return super.create(mapping, form, request, response);
117         }
118         return mapping.findForward("create");
119     }
120
121     /**
122      * Confirm removal of a resource
123      * @param mapping
124      * @param form
125      * @param request
126      * @param response
127      * @return ActionForward
128      * @throws Exception
129      */

130     public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
131                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
132         ProfilesForm f = (ProfilesForm) form;
133         PropertyProfile profile = (PropertyProfile) getResourceById(f.getSelectedResource());
134         if (profile == null) {
135             throw new Exception JavaDoc("Selected profile '" + f.getSelectedResource() + "' doesn't exist.");
136         }
137         if(profile.getOwnerUsername() != null) {
138             SessionInfo session = getSessionInfo(request);
139             if(!session.getUser().getPrincipalName().equals(profile.getOwnerUsername())) {
140                 throw new Exception JavaDoc("Cannot delete profiles owned by others.");
141             }
142             return mapping.findForward("confirmRemove");
143         }
144         else {
145             return super.confirmRemove(mapping, form, request, response);
146         }
147     }
148
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see com.sslexplorer.policyframework.actions.AbstractResourcesDispatchAction#remove(org.apache.struts.action.ActionMapping,
154      * org.apache.struts.action.ActionForm,
155      * javax.servlet.http.HttpServletRequest,
156      * javax.servlet.http.HttpServletResponse)
157      */

158     public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
159                     throws Exception JavaDoc {
160         ProfilesForm f = (ProfilesForm) form;
161         ActionMessages errors = new ActionMessages();
162         PropertyProfile profile = (PropertyProfile) getResourceById(f.getSelectedResource());
163         if (profile == null) {
164             throw new Exception JavaDoc("Selected profile '" + f.getSelectedResource() + "' doesn't exist.");
165         }
166         if (profile.getOwnerUsername() == null && Constants.SCOPE_PERSONAL.equals(f.getProfileScope())) {
167             errors.add(Globals.ERROR_KEY, new ActionMessage("error.deleteProfile.cantDeleteGlobalProfile"));
168         }
169         if (profile.getResourceName().equalsIgnoreCase("Default")
170                         && (profile.getOwnerUsername() == null || profile.getOwnerUsername().equals(""))) {
171             errors.add(Globals.ERROR_KEY, new ActionMessage("error.deleteProfile.cantDeleteDefaultProfile"));
172         }
173         saveErrors(request, errors);
174         CoreUtil.resetMainNavigation(request.getSession());
175         if (errors.size() > 0) {
176             String JavaDoc returnTo = request.getParameter("returnTo");
177             return returnTo != null ? new ActionForward(returnTo, false) : mapping.getInputForward();
178         } else {
179             return super.remove(mapping, form, request, response);
180         }
181     }
182
183     /**
184      * Dispatch action target called when the user wishes to select a profile.
185      * After checking if the user has access, the profile will be made the
186      * current profile for the session.
187      *
188      * @param mapping mapping
189      * @param form form
190      * @param request request
191      * @param response response
192      * @return forward
193      * @throws Exception on any error
194      */

195     public ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
196                     throws Exception JavaDoc {
197         ProfilesForm f = (ProfilesForm) form;
198         PropertyProfile profile = ProfilesFactory.getInstance().getPropertyProfile(f.getSelectedResource());
199         SessionInfo session = getSessionInfo(request);
200         ResourceUtil.checkResourceAccessRights(profile, session);
201         request.getSession().setAttribute(Constants.SELECTED_PROFILE, profile);
202         ActionMessages messages = new ActionMessages();
203         messages.add(Globals.MESSAGES_KEY, new ActionMessage("message.profileSelected", profile.getResourceName()));
204         saveMessages(request, messages);
205         return mapping.findForward("refresh");
206     }
207
208     /**
209      * An alternative method of selecting a profile, this one forwards to
210      * 'selectPropertyProfile' which also allows the selected to be chosen as a
211      * default
212      *
213      * @param mapping mapping
214      * @param form form
215      * @param request request
216      * @param response response
217      * @return forward
218      * @throws Exception on any error
219      */

220     public ActionForward selectOrDefault(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
221                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
222         return mapping.findForward("selectOrDefault");
223     }
224
225     /*
226      * (non-Javadoc)
227      *
228      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
229      * org.apache.struts.action.ActionForm,
230      * javax.servlet.http.HttpServletRequest,
231      * javax.servlet.http.HttpServletResponse)
232      */

233     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
234         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT;
235     }
236
237     protected void checkValid(Resource r, Permission[] permission, ActionMapping mapping, AbstractResourcesForm form,
238                     HttpServletRequest JavaDoc request) throws NoPermissionException {
239         if (r instanceof OwnedResource && ((OwnedResource) r).getOwnerUsername() != null) {
240             super.checkValid(r, new Permission[] { PolicyConstants.PERM_MAINTAIN }, mapping, form, request);
241         } else {
242             super.checkValid(r, permission, mapping, form, request);
243         }
244     }
245 }
246
Popular Tags