Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

FELIX-3777:

- let the RoleRepositoryStore use a filter string instead of a Filter implementation.



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1412590 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit a449633bb3c6ae3691a8ac6eb59d19b8c16cddf8 1 parent 62d7334
authored November 22, 2012
11  useradmin/filestore/src/main/java/org/apache/felix/useradmin/filestore/RoleRepositoryMemoryStore.java
@@ -26,6 +26,8 @@
26 26
 import org.apache.felix.useradmin.RoleFactory;
27 27
 import org.apache.felix.useradmin.RoleRepositoryStore;
28 28
 import org.osgi.framework.Filter;
  29
+import org.osgi.framework.FrameworkUtil;
  30
+import org.osgi.framework.InvalidSyntaxException;
29 31
 import org.osgi.service.useradmin.Role;
30 32
 
31 33
 
@@ -45,9 +47,14 @@ public Role addRole(String roleName, int type) {
45 47
         return (result == null) ? role : null;
46 48
     }
47 49
 
48  
-    public Role[] getRoles(Filter filter) {
  50
+    public Role[] getRoles(String filterValue) throws InvalidSyntaxException {
49 51
         Collection roles = m_entries.values();
50  
-        
  52
+
  53
+        Filter filter = null;
  54
+        if (filterValue != null) {
  55
+            filter = FrameworkUtil.createFilter(filterValue);
  56
+        }
  57
+
51 58
         List matchingRoles = new ArrayList();
52 59
         Iterator rolesIter = roles.iterator();
53 60
         while (rolesIter.hasNext()) {
17  useradmin/mongodb/src/main/java/org/apache/felix/useradmin/mongodb/MongoDBStore.java
@@ -26,6 +26,8 @@
26 26
 
27 27
 import org.apache.felix.useradmin.RoleRepositoryStore;
28 28
 import org.osgi.framework.Filter;
  29
+import org.osgi.framework.FrameworkUtil;
  30
+import org.osgi.framework.InvalidSyntaxException;
29 31
 import org.osgi.service.cm.ConfigurationException;
30 32
 import org.osgi.service.cm.ManagedService;
31 33
 import org.osgi.service.log.LogService;
@@ -160,15 +162,24 @@ public void close() {
160 162
     }
161 163
 
162 164
     @Override
163  
-    public Role[] getRoles(Filter filter) throws MongoException {
  165
+    public Role[] getRoles(String filterValue) throws InvalidSyntaxException, MongoException {
164 166
         List<Role> roles = new ArrayList<Role>();
165  
-        
  167
+
  168
+        Filter filter = null;
  169
+        if (filterValue != null) {
  170
+            filter = FrameworkUtil.createFilter(filterValue);
  171
+        }
  172
+
166 173
         DBCollection coll = getCollection();
167 174
 
168 175
         DBCursor cursor = coll.find();
169 176
         try {
170 177
             while (cursor.hasNext()) {
171  
-                roles.add(m_helper.deserialize(cursor.next()));
  178
+                // Hmm, there might be a more clever way of doing this...
  179
+                Role role = m_helper.deserialize(cursor.next());
  180
+                if ((filter == null) || filter.match(role.getProperties())) {
  181
+                    roles.add(role);
  182
+                }
172 183
             }
173 184
         } finally {
174 185
             cursor.close();
5  useradmin/useradmin/src/main/java/org/apache/felix/useradmin/RoleRepositoryStore.java
@@ -16,7 +16,6 @@
16 16
  */
17 17
 package org.apache.felix.useradmin;
18 18
 
19  
-import org.osgi.framework.Filter;
20 19
 import org.osgi.service.useradmin.Role;
21 20
 
22 21
 /**
@@ -49,11 +48,11 @@
49 48
     /**
50 49
      * Returns all roles in this backend matching the given filter criteria.
51 50
      * 
52  
-     * @param filter the optional filter to apply, can be <code>null</code> in which case all roles are to be returned.
  51
+     * @param filter the (optional) filter to apply, can be <code>null</code> in which case all roles will be returned.
53 52
      * @return an array with all roles, never <code>null</code>, but can be empty.
54 53
      * @throws Exception in case of problems retrieving the set of roles.
55 54
      */
56  
-    Role[] getRoles(Filter filter) throws Exception;
  55
+    Role[] getRoles(String filter) throws Exception;
57 56
 
58 57
     /**
59 58
      * Returns a {@link Role} by its name.
22  useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/RoleRepository.java
@@ -25,8 +25,6 @@
25 25
 import org.apache.felix.useradmin.RoleFactory;
26 26
 import org.apache.felix.useradmin.RoleRepositoryStore;
27 27
 import org.apache.felix.useradmin.impl.role.ObservableRole;
28  
-import org.osgi.framework.Filter;
29  
-import org.osgi.framework.FrameworkUtil;
30 28
 import org.osgi.service.useradmin.Group;
31 29
 import org.osgi.service.useradmin.Role;
32 30
 import org.osgi.service.useradmin.UserAdminPermission;
@@ -181,11 +179,11 @@ public Role getRoleByName(String roleName) {
181 179
      * @param filter the filter to match the individual roles against, can be <code>null</code> if all roles should be returned.
182 180
      * @return a list with all matching roles, can be empty, but never <code>null</code>.
183 181
      */
184  
-    public List getRoles(Filter filter) {
  182
+    public List getRoles(String filter) {
185 183
         List matchingRoles = new ArrayList();
186 184
 
187 185
         try {
188  
-            Role[] roles = m_store.getRoles(filter);
  186
+            Role[] roles = m_store.getRoles(sanitizeFilter(filter));
189 187
             for (int i = 0; i < roles.length; i++) {
190 188
                 Role role = roles[i];
191 189
                 if (!isPredefinedRole(role.getName())) {
@@ -219,9 +217,8 @@ public List getRoles(String key, String value) {
219 217
 
220 218
         try {
221 219
             String criteria = "(".concat(key).concat("=").concat(value).concat(")");
222  
-            Filter filter = FrameworkUtil.createFilter(criteria);
223 220
 
224  
-            Role[] roles = m_store.getRoles(filter);
  221
+            Role[] roles = m_store.getRoles(criteria);
225 222
             for (int i = 0; i < roles.length; i++) {
226 223
                 Role role = roles[i];
227 224
                 if (!isPredefinedRole(role.getName())) {
@@ -354,6 +351,19 @@ private void removeRoleFromAllGroups(Role removedRole) {
354 351
             throw new BackendException("Failed to get all roles!", e);
355 352
         }
356 353
 	}
  354
+	
  355
+	/**
  356
+	 * Sanitizes the given filter string.
  357
+	 * 
  358
+	 * @param filter the filter string to sanitize, can be <code>null</code>.
  359
+	 * @return the sanitized filter, or <code>null</code> if the given filter was <code>null</code> or empty.
  360
+	 */
  361
+	private String sanitizeFilter(String filter) {
  362
+	    if (filter == null || "".equals(filter.trim())) {
  363
+	        return null;
  364
+	    }
  365
+	    return filter.trim();
  366
+	}
357 367
 
358 368
     /**
359 369
      * Unwires the given role to this repository so it no longer listens for its changes.
22  useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/UserAdminImpl.java
@@ -20,7 +20,6 @@
20 20
 import java.util.List;
21 21
 
22 22
 import org.osgi.framework.Bundle;
23  
-import org.osgi.framework.Filter;
24 23
 import org.osgi.framework.FrameworkUtil;
25 24
 import org.osgi.framework.InvalidSyntaxException;
26 25
 import org.osgi.framework.ServiceFactory;
@@ -89,7 +88,12 @@ public Role getRole(String name) {
89 88
      * {@inheritDoc}
90 89
      */
91 90
     public Role[] getRoles(String filter) throws InvalidSyntaxException {
92  
-        List roles = m_roleRepository.getRoles(createFilter(filter));
  91
+        // Do a sanity check on the given filter...
  92
+        if (filter != null && !"".equals(filter.trim())) {
  93
+            FrameworkUtil.createFilter(filter);
  94
+        }
  95
+
  96
+        List roles = m_roleRepository.getRoles(filter);
93 97
         if (roles.isEmpty()) {
94 98
             return null;
95 99
         }
@@ -169,20 +173,6 @@ public void roleRemoved(Role role) {
169 173
     public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
170 174
         // Nop; we leave the service as-is...
171 175
     }
172  
-    
173  
-    /**
174  
-     * Creates a {@link Filter} instance for the given OSGi/LDAP filter.
175  
-     * 
176  
-     * @param filter the filter to convert to a {@link Filter} instance.
177  
-     * @return a {@link Filter} instance corresponding to the given filter string, never <code>null</code>.
178  
-     * @throws InvalidSyntaxException in case the given filter was invalid.
179  
-     */
180  
-    protected Filter createFilter(String filter) throws InvalidSyntaxException {
181  
-        if (filter == null || "".equals(filter.trim())) {
182  
-            return null;
183  
-        }
184  
-        return FrameworkUtil.createFilter(filter);
185  
-    }
186 176
 
187 177
     /**
188 178
      * Creates a new {@link UserAdminEvent} instance for the given type and role.
3  useradmin/useradmin/src/main/java/org/apache/felix/useradmin/osgi/RoleRepositoryStoreHelper.java
@@ -18,7 +18,6 @@
18 18
 
19 19
 import org.apache.felix.useradmin.RoleRepositoryStore;
20 20
 import org.osgi.framework.BundleContext;
21  
-import org.osgi.framework.Filter;
22 21
 import org.osgi.service.useradmin.Role;
23 22
 import org.osgi.util.tracker.ServiceTracker;
24 23
 
@@ -49,7 +48,7 @@ public Role addRole(String roleName, int type) throws Exception {
49 48
         return null;
50 49
     }
51 50
 
52  
-    public Role[] getRoles(Filter filter) throws Exception {
  51
+    public Role[] getRoles(String filter) throws Exception {
53 52
         RoleRepositoryStore store = getStore();
54 53
         if (store != null) {
55 54
             return store.getRoles(filter);
20  useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/CustomRoleImplTest.java
@@ -34,6 +34,7 @@
34 34
 
35 35
 import org.apache.felix.useradmin.RoleRepositoryStore;
36 36
 import org.osgi.framework.Filter;
  37
+import org.osgi.framework.FrameworkUtil;
37 38
 import org.osgi.service.useradmin.Group;
38 39
 import org.osgi.service.useradmin.Role;
39 40
 import org.osgi.service.useradmin.User;
@@ -130,19 +131,20 @@ public Role addRole(String roleName, int type) throws IOException {
130 131
             return (result == null) ? role : null;
131 132
         }
132 133
 
133  
-        public void close() throws IOException {
134  
-            // Nop
135  
-        }
136  
-
137  
-        public Role getRoleByName(String roleName) throws IOException {
  134
+        public Role getRoleByName(String roleName) throws Exception {
138 135
             if (roleName == null) {
139 136
                 throw new IllegalArgumentException("Role name cannot be null!");
140 137
             }
141 138
             return (Role) m_entries.get(roleName);
142 139
         }
143 140
 
144  
-        public Role[] getRoles(Filter filter) throws IOException {
  141
+        public Role[] getRoles(String filterValue) throws Exception {
145 142
             Collection roles = m_entries.values();
  143
+
  144
+            Filter filter = null;
  145
+            if (filterValue != null) {
  146
+                filter = FrameworkUtil.createFilter(filterValue);
  147
+            }
146 148
             
147 149
             List matchingRoles = new ArrayList();
148 150
             Iterator rolesIter = roles.iterator();
@@ -156,12 +158,8 @@ public Role getRoleByName(String roleName) throws IOException {
156 158
             Role[] result = new Role[matchingRoles.size()];
157 159
             return (Role[]) matchingRoles.toArray(result);
158 160
         }
159  
-        
160  
-        public void initialize() throws IOException {
161  
-            // Nop
162  
-        }
163 161
 
164  
-        public Role removeRole(String roleName) throws IOException {
  162
+        public Role removeRole(String roleName) throws Exception {
165 163
             if (roleName == null) {
166 164
                 throw new IllegalArgumentException("Name cannot be null!");
167 165
             }
25  useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/MemoryRoleRepositoryStore.java
@@ -16,7 +16,6 @@
16 16
  */
17 17
 package org.apache.felix.useradmin.impl;
18 18
 
19  
-import java.io.IOException;
20 19
 import java.util.ArrayList;
21 20
 import java.util.Collection;
22 21
 import java.util.Iterator;
@@ -27,6 +26,7 @@
27 26
 import org.apache.felix.useradmin.RoleFactory;
28 27
 import org.apache.felix.useradmin.RoleRepositoryStore;
29 28
 import org.osgi.framework.Filter;
  29
+import org.osgi.framework.FrameworkUtil;
30 30
 import org.osgi.service.useradmin.Role;
31 31
 
32 32
 
@@ -37,7 +37,7 @@
37 37
     
38 38
     private final ConcurrentMap m_entries = new ConcurrentHashMap();
39 39
 
40  
-    public Role addRole(String roleName, int type) throws IOException {
  40
+    public Role addRole(String roleName, int type) throws Exception {
41 41
         if (roleName == null) {
42 42
             throw new IllegalArgumentException("Name cannot be null!");
43 43
         }
@@ -46,13 +46,14 @@ public Role addRole(String roleName, int type) throws IOException {
46 46
         return (result == null) ? role : null;
47 47
     }
48 48
 
49  
-    public void close() throws IOException {
50  
-        // Nop
51  
-    }
52  
-
53  
-    public Role[] getRoles(Filter filter) throws IOException {
  49
+    public Role[] getRoles(String filterValue) throws Exception {
54 50
         Collection roles = m_entries.values();
55  
-        
  51
+
  52
+        Filter filter = null;
  53
+        if (filterValue != null) {
  54
+            filter = FrameworkUtil.createFilter(filterValue);
  55
+        }
  56
+
56 57
         List matchingRoles = new ArrayList();
57 58
         Iterator rolesIter = roles.iterator();
58 59
         while (rolesIter.hasNext()) {
@@ -66,18 +67,14 @@ public void close() throws IOException {
66 67
         return (Role[]) matchingRoles.toArray(result);
67 68
     }
68 69
 
69  
-    public Role getRoleByName(String roleName) throws IOException {
  70
+    public Role getRoleByName(String roleName) throws Exception {
70 71
         if (roleName == null) {
71 72
             throw new IllegalArgumentException("Role name cannot be null!");
72 73
         }
73 74
         return (Role) m_entries.get(roleName);
74 75
     }
75  
-    
76  
-    public void initialize() throws IOException {
77  
-        // Nop
78  
-    }
79 76
 
80  
-    public Role removeRole(String roleName) throws IOException {
  77
+    public Role removeRole(String roleName) throws Exception {
81 78
         if (roleName == null) {
82 79
             throw new IllegalArgumentException("Name cannot be null!");
83 80
         }
3  useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositorySecurityTest.java
@@ -21,7 +21,6 @@
21 21
 
22 22
 import junit.framework.TestCase;
23 23
 
24  
-import org.apache.felix.framework.FilterImpl;
25 24
 import org.osgi.service.useradmin.Role;
26 25
 
27 26
 /**
@@ -74,7 +73,7 @@ public void testGetRolesWithKeyValueOk() throws SecurityException {
74 73
      * Tests that getting roles with filters works without any permissions.
75 74
      */
76 75
     public void testGetRolesWithFilterOk() throws Exception {
77  
-        m_roleManager.getRoles(new FilterImpl("(key=value)"));
  76
+        m_roleManager.getRoles("(key=value)");
78 77
     }
79 78
 
80 79
     /**
12  useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositoryTest.java
@@ -26,8 +26,6 @@
26 26
 
27 27
 import junit.framework.TestCase;
28 28
 
29  
-import org.apache.felix.framework.FilterImpl;
30  
-import org.osgi.framework.Filter;
31 29
 import org.osgi.service.useradmin.Group;
32 30
 import org.osgi.service.useradmin.Role;
33 31
 import org.osgi.service.useradmin.User;
@@ -274,18 +272,18 @@ public void testGetRolesWithFilterOk() throws Exception {
274 272
         role2.getProperties().put("key", "value2");
275 273
         role2.getProperties().put("keyB", "value1");
276 274
         
277  
-        Filter filter;
  275
+        String filter;
278 276
 
279  
-        filter = new FilterImpl("(key=value1)");
  277
+        filter = "(key=value1)";
280 278
         assertSameRoles(new Role[]{ role1 }, m_roleRepository.getRoles(filter));
281 279
 
282  
-        filter = new FilterImpl("(key=value2)");
  280
+        filter = "(key=value2)";
283 281
         assertSameRoles(new Role[]{ role2 }, m_roleRepository.getRoles(filter));
284 282
 
285  
-        filter = new FilterImpl("(key=value*)");
  283
+        filter = "(key=value*)";
286 284
         assertSameRoles(new Role[]{ role1, role2 }, m_roleRepository.getRoles(filter));
287 285
 
288  
-        filter = new FilterImpl("(|(key=value1)(keyB=value1))");
  286
+        filter = "(|(key=value1)(keyB=value1))";
289 287
         assertSameRoles(new Role[]{ role1, role2 }, m_roleRepository.getRoles(filter));
290 288
     }
291 289
 
11  useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java
@@ -21,9 +21,7 @@
21 21
 
22 22
 import junit.framework.TestCase;
23 23
 
24  
-import org.apache.felix.framework.FilterImpl;
25 24
 import org.apache.felix.useradmin.RoleFactory;
26  
-import org.osgi.framework.Filter;
27 25
 import org.osgi.framework.InvalidSyntaxException;
28 26
 import org.osgi.service.event.Event;
29 27
 import org.osgi.service.event.EventAdmin;
@@ -858,14 +856,7 @@ protected void setUp() throws Exception {
858 856
         m_roleRepository = new RoleRepository(new MemoryRoleRepositoryStore());
859 857
         m_dispatcher = new EventDispatcher(new StubEventAdmin(), new StubUserAdminListenerList());
860 858
 
861  
-        m_userAdmin = new UserAdminImpl(m_roleRepository, m_dispatcher) {
862  
-            protected Filter createFilter(String filter) throws InvalidSyntaxException {
863  
-                if (filter == null || "".equals(filter.trim())) {
864  
-                    return null;
865  
-                }
866  
-                return new FilterImpl(filter);
867  
-            }
868  
-        };
  859
+        m_userAdmin = new UserAdminImpl(m_roleRepository, m_dispatcher);
869 860
         m_dispatcher.start();
870 861
     }
871 862
     

0 notes on commit a449633

Please sign in to comment.
Something went wrong with that request. Please try again.