KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > AccessRightsResourceType


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;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.struts.util.MessageResources;
26
27 import com.sslexplorer.core.CoreAttributeConstants;
28 import com.sslexplorer.core.CoreEvent;
29 import com.sslexplorer.core.CoreEventConstants;
30 import com.sslexplorer.core.CoreServlet;
31 import com.sslexplorer.core.CoreUtil;
32 import com.sslexplorer.security.SessionInfo;
33
34
35 /**
36  * Implementation of a {@link com.sslexplorer.policyframework.ResourceType}
37  * for <i>Resource Permission</i> resources.
38  *
39  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
40  */

41 public class AccessRightsResourceType extends DefaultResourceType {
42
43     /**
44      * Constructor
45      */

46     public AccessRightsResourceType() {
47         super(PolicyConstants.ACCESS_RIGHTS_RESOURCE_TYPE_ID, "policyframework", PolicyConstants.SYSTEM_CLASS);
48     }
49
50     /* (non-Javadoc)
51      * @see com.sslexplorer.navigation.FavoriteResourceType#getResourceById(int)
52      */

53     public Resource getResourceById(int resourceId) throws Exception JavaDoc {
54         return PolicyDatabaseFactory.getInstance().getAccessRight(resourceId);
55     }
56
57     /* (non-Javadoc)
58      * @see com.sslexplorer.policyframework.DefaultResourceType#getResourceByName(java.lang.String, com.sslexplorer.security.SessionInfo)
59      */

60     public Resource getResourceByName(String JavaDoc resourceName, SessionInfo session) throws Exception JavaDoc {
61         return PolicyDatabaseFactory.getInstance().getAccessRightsByName(resourceName, session.getUser().getRealm().getRealmID());
62     }
63
64     /* (non-Javadoc)
65      * @see com.sslexplorer.boot.policyframework.ResourceType#removeResource(int, com.sslexplorer.security.SessionInfo)
66      */

67     public Resource removeResource(int resourceId, SessionInfo session) throws Exception JavaDoc {
68         try {
69             AccessRights resource = PolicyDatabaseFactory.getInstance().deleteAccessRights(resourceId);
70             AccessRights accessRights = (AccessRights)resource;
71             ResourceDeleteEvent event = new ResourceDeleteEvent(this, CoreEventConstants.DELETE_ACCESS_RIGHT, resource, session, CoreEvent.STATE_SUCCESSFUL);
72             event.addAttribute(CoreAttributeConstants.EVENT_ATTR_TYPE_ACCESS_RIGHT, getAccessRightType(session, accessRights.getAccessRightsClass()));
73             CoreServlet.getServlet().fireCoreEvent(addAccessRightsAttributes(event, accessRights));
74             return resource;
75         } catch (Exception JavaDoc e) {
76             CoreServlet.getServlet().fireCoreEvent(
77                 new ResourceDeleteEvent(this, CoreEventConstants.DELETE_ACCESS_RIGHT, session, e));
78             throw e;
79         }
80     }
81
82     /* (non-Javadoc)
83      * @see com.sslexplorer.boot.policyframework.ResourceType#updateResource(com.sslexplorer.boot.policyframework.Resource, com.sslexplorer.security.SessionInfo)
84      */

85     public void updateResource(Resource resource, SessionInfo session) throws Exception JavaDoc {
86         try {
87             AccessRights accessRights = (AccessRights)resource;
88             PolicyDatabaseFactory.getInstance().updateAccessRights(accessRights);
89             CoreEvent coreEvent = addAccessRightsAttributes(new ResourceChangeEvent(this, CoreEventConstants.UPDATE_ACCESS_RIGHT, resource, session, CoreEvent.STATE_SUCCESSFUL), accessRights);
90             
91             List JavaDoc permissionsList = accessRights.getAccessRights();
92              if (permissionsList != null) {
93                  int j =0;
94                  for (Iterator JavaDoc i = permissionsList.iterator(); i.hasNext();) {
95                      j++;
96                      AccessRight permission = (AccessRight)i.next();
97
98                      MessageResources mrPermission = CoreUtil.getMessageResources(session.getHttpSession(), permission.getPermission().getBundle());
99                      String JavaDoc permissionName = mrPermission.getMessage("permission."+permission.getPermission().getId()+".title");
100
101                      MessageResources mrResourceType = CoreUtil.getMessageResources(session.getHttpSession(), permission.getResourceType().getBundle());
102                      String JavaDoc resourceTypeName = mrResourceType.getMessage("resourceType."+permission.getResourceType().getResourceTypeId()+".title");
103                      
104                      coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_TYPE_PERMISSION + Integer.toString(j), permissionName + " " + resourceTypeName);
105                  }
106              }
107              coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_TYPE_ACCESS_RIGHT, getAccessRightType(session, accessRights.getAccessRightsClass()));
108              CoreServlet.getServlet().fireCoreEvent(coreEvent);
109         } catch (Exception JavaDoc e) {
110             CoreServlet.getServlet().fireCoreEvent(
111                 new ResourceChangeEvent(this, CoreEventConstants.UPDATE_ACCESS_RIGHT, session, e));
112             throw e;
113         }
114     }
115     
116     CoreEvent addAccessRightsAttributes(CoreEvent evt, AccessRights resource) {
117         return evt;
118     }
119
120     private static String JavaDoc getAccessRightType(SessionInfo session, String JavaDoc permissionClass) {
121         MessageResources messageResources = CoreUtil.getMessageResources(session.getHttpSession(), "policyframework");
122         String JavaDoc accessRightType = messageResources.getMessage("permission.type." + permissionClass);
123         return accessRightType;
124     }
125 }
Popular Tags