KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.CoreAttributeConstants;
35 import com.sslexplorer.core.CoreEvent;
36 import com.sslexplorer.core.CoreEventConstants;
37 import com.sslexplorer.core.CoreServlet;
38 import com.sslexplorer.core.UserDatabaseManager;
39 import com.sslexplorer.input.MultiSelectDataSource;
40 import com.sslexplorer.policyframework.DelegatedPoliciesDataSource;
41 import com.sslexplorer.policyframework.Policy;
42 import com.sslexplorer.policyframework.PolicyConstants;
43 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
44 import com.sslexplorer.policyframework.Principal;
45 import com.sslexplorer.policyframework.Resource;
46 import com.sslexplorer.policyframework.forms.PolicyForm;
47 import com.sslexplorer.properties.Property;
48 import com.sslexplorer.properties.attributes.AttributeDefinition;
49 import com.sslexplorer.properties.attributes.AttributeValueItem;
50 import com.sslexplorer.properties.impl.policyattributes.PolicyAttributeKey;
51 import com.sslexplorer.security.LogonControllerFactory;
52 import com.sslexplorer.security.SessionInfo;
53 import com.sslexplorer.security.User;
54 import com.sslexplorer.security.UserDatabase;
55
56 /**
57  * Implementation of a
58  * {@link com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction}
59  * that allows editing of a <i>Policy</i> resource.
60  *
61  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
62  */

63 public class ShowPolicyDispatchAction extends AbstractResourceDispatchAction {
64
65     final static Log log = LogFactory.getLog(ShowPolicyDispatchAction.class);
66
67     /**
68      * Constructor
69      */

70     public ShowPolicyDispatchAction() {
71         super(PolicyConstants.POLICY_RESOURCE_TYPE);
72     }
73
74     public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
75                     throws Exception JavaDoc {
76         ActionForward fwd = super.commit(mapping, form, request, response);
77         PolicyForm policyForm = (PolicyForm) form;
78         Policy pol = (Policy) policyForm.getResource();
79         UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
80         if (pol.getResourceId() != PolicyDatabaseFactory.getInstance().getEveryonePolicyIDForRealm(udb.getRealm())) {
81             List JavaDoc wasAttached = PolicyDatabaseFactory.getInstance().getPrincipalsGrantedPolicy(pol, udb.getRealm()); // objects
82
List JavaDoc nowAttached = policyForm.getSelectedAccountsList();
83             for (Iterator JavaDoc i = wasAttached.iterator(); i.hasNext();) {
84                 Principal p = (Principal) i.next();
85                 try {
86                     if (!nowAttached.contains(p.getPrincipalName())) {
87                         CoreServlet.getServlet().fireCoreEvent(
88                             new CoreEvent(this, CoreEventConstants.REVOKE_POLICY_FROM_PRINCIPAL, null, null,
89                                             CoreEvent.STATE_SUCCESSFUL).addAttribute(
90                                 CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, p instanceof User ? "user" : "group")
91                                             .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName())
92                                             .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
93
94                     }
95                 } catch (Exception JavaDoc e) {
96                     CoreServlet.getServlet().fireCoreEvent(
97                         new CoreEvent(this, CoreEventConstants.REVOKE_POLICY_FROM_PRINCIPAL, null, null, e).addAttribute(
98                             CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, p instanceof User ? "user" : "group").addAttribute(
99                             CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute(
100                             CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
101                     throw e;
102                 }
103             }
104
105             // TODO fire revoking events
106
PolicyDatabaseFactory.getInstance().revokePolicyFromAllPrincipals(pol, udb.getRealm());
107             for (Iterator JavaDoc i = nowAttached.iterator(); i.hasNext();) {
108                 Principal p = udb.getAccount((String JavaDoc) i.next());
109                 try {
110                     PolicyDatabaseFactory.getInstance().grantPolicyToPrincipal(pol, p);
111                     CoreServlet.getServlet().fireCoreEvent(
112                         new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_SUCCESSFUL)
113                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE,
114                                             "user").addAttribute(
115                                             CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute(
116                                             CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
117                 } catch (Exception JavaDoc e) {
118                     CoreServlet.getServlet().fireCoreEvent(
119                         new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, e).addAttribute(
120                             CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "user").addAttribute(
121                             CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute(
122                             CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
123                     throw e;
124                 }
125             }
126             for (Iterator JavaDoc i = policyForm.getSelectedRolesList().iterator(); i.hasNext();) {
127                 Principal p = udb.getRole((String JavaDoc) i.next());
128                 try {
129                     PolicyDatabaseFactory.getInstance().grantPolicyToPrincipal(pol, p);
130                     CoreServlet.getServlet().fireCoreEvent(
131                         new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_SUCCESSFUL)
132                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "group")
133                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName())
134                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
135                 } catch (Exception JavaDoc e) {
136                     CoreServlet.getServlet().fireCoreEvent(
137                         new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_UNSUCCESSFUL)
138                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "group")
139                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName())
140                                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
141                     throw e;
142                 }
143             }
144         }
145         // Update the attributes
146
for(AttributeValueItem v : policyForm.getAttributeValueItems()) {
147            if(v.getDefinition().getVisibility() != AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) {
148                Property.setProperty(new PolicyAttributeKey(policyForm.getResourceId(), v.getDefinition().getName()), v.getDefinition().formatAttributeValue(v.getValue()), getSessionInfo(request));
149            }
150         }
151         
152         return fwd;
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
159      * org.apache.struts.action.ActionForm,
160      * javax.servlet.http.HttpServletRequest,
161      * javax.servlet.http.HttpServletResponse)
162      */

163     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
164         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction#createResource(org.apache.struts.action.ActionMapping,
171      * org.apache.struts.action.ActionForm,
172      * javax.servlet.http.HttpServletRequest,
173      * javax.servlet.http.HttpServletResponse)
174      */

175     public Resource createResource(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
176                     throws Exception JavaDoc {
177         throw new Exception JavaDoc("Create resource not supported.");
178     }
179
180     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
181         ActionForward fwd = super.edit(mapping, form, request, response);
182         ((PolicyForm)form).initAttributes(request);
183         return fwd;
184     }
185
186     /*
187      * (non-Javadoc)
188      *
189      * @see com.sslexplorer.policyframework.actions.AbstractResourceDispatchAction#createAvailablePoliciesDataSource(com.sslexplorer.policyframework.Resource,
190      * org.apache.struts.action.ActionMapping,
191      * org.apache.struts.action.ActionForm,
192      * javax.servlet.http.HttpServletRequest,
193      * javax.servlet.http.HttpServletResponse)
194      */

195     protected MultiSelectDataSource createAvailablePoliciesDataSource(Resource resource, ActionMapping mapping, ActionForm form,
196                                                                       HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
197                     throws Exception JavaDoc {
198         return new DelegatedPoliciesDataSource((Policy) resource, PolicyConstants.POLICY_RESOURCE_TYPE,
199                         PolicyConstants.SYSTEM_CLASS, getSessionInfo(request).getUser());
200     }
201     
202     @Override JavaDoc
203     protected void doUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
204         super.doUpdate(mapping, form, request, response);
205         // we now need to rebuild any menus, as more or less could be visable.
206
LogonControllerFactory.getInstance().applyMenuItemChanges(request);
207     }
208
209 }
Popular Tags