Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
Jan Willem Janssen authored
View
11 ...lestore/src/main/java/org/apache/felix/useradmin/filestore/RoleRepositoryMemoryStore.java
@@ -26,6 +26,8 @@
import org.apache.felix.useradmin.RoleFactory;
import org.apache.felix.useradmin.RoleRepositoryStore;
import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.useradmin.Role;
@@ -45,9 +47,14 @@ public Role addRole(String roleName, int type) {
return (result == null) ? role : null;
}
- public Role[] getRoles(Filter filter) {
+ public Role[] getRoles(String filterValue) throws InvalidSyntaxException {
Collection roles = m_entries.values();
-
+
+ Filter filter = null;
+ if (filterValue != null) {
+ filter = FrameworkUtil.createFilter(filterValue);
+ }
+
List matchingRoles = new ArrayList();
Iterator rolesIter = roles.iterator();
while (rolesIter.hasNext()) {
View
17 useradmin/mongodb/src/main/java/org/apache/felix/useradmin/mongodb/MongoDBStore.java
@@ -26,6 +26,8 @@
import org.apache.felix.useradmin.RoleRepositoryStore;
import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.log.LogService;
@@ -160,15 +162,24 @@ public void close() {
}
@Override
- public Role[] getRoles(Filter filter) throws MongoException {
+ public Role[] getRoles(String filterValue) throws InvalidSyntaxException, MongoException {
List<Role> roles = new ArrayList<Role>();
-
+
+ Filter filter = null;
+ if (filterValue != null) {
+ filter = FrameworkUtil.createFilter(filterValue);
+ }
+
DBCollection coll = getCollection();
DBCursor cursor = coll.find();
try {
while (cursor.hasNext()) {
- roles.add(m_helper.deserialize(cursor.next()));
+ // Hmm, there might be a more clever way of doing this...
+ Role role = m_helper.deserialize(cursor.next());
+ if ((filter == null) || filter.match(role.getProperties())) {
+ roles.add(role);
+ }
}
} finally {
cursor.close();
View
5 useradmin/useradmin/src/main/java/org/apache/felix/useradmin/RoleRepositoryStore.java
@@ -16,7 +16,6 @@
*/
package org.apache.felix.useradmin;
-import org.osgi.framework.Filter;
import org.osgi.service.useradmin.Role;
/**
@@ -49,11 +48,11 @@
/**
* Returns all roles in this backend matching the given filter criteria.
*
- * @param filter the optional filter to apply, can be <code>null</code> in which case all roles are to be returned.
+ * @param filter the (optional) filter to apply, can be <code>null</code> in which case all roles will be returned.
* @return an array with all roles, never <code>null</code>, but can be empty.
* @throws Exception in case of problems retrieving the set of roles.
*/
- Role[] getRoles(Filter filter) throws Exception;
+ Role[] getRoles(String filter) throws Exception;
/**
* Returns a {@link Role} by its name.
View
22 useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/RoleRepository.java
@@ -25,8 +25,6 @@
import org.apache.felix.useradmin.RoleFactory;
import org.apache.felix.useradmin.RoleRepositoryStore;
import org.apache.felix.useradmin.impl.role.ObservableRole;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkUtil;
import org.osgi.service.useradmin.Group;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.UserAdminPermission;
@@ -181,11 +179,11 @@ public Role getRoleByName(String roleName) {
* @param filter the filter to match the individual roles against, can be <code>null</code> if all roles should be returned.
* @return a list with all matching roles, can be empty, but never <code>null</code>.
*/
- public List getRoles(Filter filter) {
+ public List getRoles(String filter) {
List matchingRoles = new ArrayList();
try {
- Role[] roles = m_store.getRoles(filter);
+ Role[] roles = m_store.getRoles(sanitizeFilter(filter));
for (int i = 0; i < roles.length; i++) {
Role role = roles[i];
if (!isPredefinedRole(role.getName())) {
@@ -219,9 +217,8 @@ public List getRoles(String key, String value) {
try {
String criteria = "(".concat(key).concat("=").concat(value).concat(")");
- Filter filter = FrameworkUtil.createFilter(criteria);
- Role[] roles = m_store.getRoles(filter);
+ Role[] roles = m_store.getRoles(criteria);
for (int i = 0; i < roles.length; i++) {
Role role = roles[i];
if (!isPredefinedRole(role.getName())) {
@@ -354,6 +351,19 @@ private void removeRoleFromAllGroups(Role removedRole) {
throw new BackendException("Failed to get all roles!", e);
}
}
+
+ /**
+ * Sanitizes the given filter string.
+ *
+ * @param filter the filter string to sanitize, can be <code>null</code>.
+ * @return the sanitized filter, or <code>null</code> if the given filter was <code>null</code> or empty.
+ */
+ private String sanitizeFilter(String filter) {
+ if (filter == null || "".equals(filter.trim())) {
+ return null;
+ }
+ return filter.trim();
+ }
/**
* Unwires the given role to this repository so it no longer listens for its changes.
View
22 useradmin/useradmin/src/main/java/org/apache/felix/useradmin/impl/UserAdminImpl.java
@@ -20,7 +20,6 @@
import java.util.List;
import org.osgi.framework.Bundle;
-import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceFactory;
@@ -89,7 +88,12 @@ public Role getRole(String name) {
* {@inheritDoc}
*/
public Role[] getRoles(String filter) throws InvalidSyntaxException {
- List roles = m_roleRepository.getRoles(createFilter(filter));
+ // Do a sanity check on the given filter...
+ if (filter != null && !"".equals(filter.trim())) {
+ FrameworkUtil.createFilter(filter);
+ }
+
+ List roles = m_roleRepository.getRoles(filter);
if (roles.isEmpty()) {
return null;
}
@@ -169,20 +173,6 @@ public void roleRemoved(Role role) {
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
// Nop; we leave the service as-is...
}
-
- /**
- * Creates a {@link Filter} instance for the given OSGi/LDAP filter.
- *
- * @param filter the filter to convert to a {@link Filter} instance.
- * @return a {@link Filter} instance corresponding to the given filter string, never <code>null</code>.
- * @throws InvalidSyntaxException in case the given filter was invalid.
- */
- protected Filter createFilter(String filter) throws InvalidSyntaxException {
- if (filter == null || "".equals(filter.trim())) {
- return null;
- }
- return FrameworkUtil.createFilter(filter);
- }
/**
* Creates a new {@link UserAdminEvent} instance for the given type and role.
View
3  ...in/useradmin/src/main/java/org/apache/felix/useradmin/osgi/RoleRepositoryStoreHelper.java
@@ -18,7 +18,6 @@
import org.apache.felix.useradmin.RoleRepositoryStore;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.Filter;
import org.osgi.service.useradmin.Role;
import org.osgi.util.tracker.ServiceTracker;
@@ -49,7 +48,7 @@ public Role addRole(String roleName, int type) throws Exception {
return null;
}
- public Role[] getRoles(Filter filter) throws Exception {
+ public Role[] getRoles(String filter) throws Exception {
RoleRepositoryStore store = getStore();
if (store != null) {
return store.getRoles(filter);
View
20 useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/CustomRoleImplTest.java
@@ -34,6 +34,7 @@
import org.apache.felix.useradmin.RoleRepositoryStore;
import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
import org.osgi.service.useradmin.Group;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;
@@ -130,19 +131,20 @@ public Role addRole(String roleName, int type) throws IOException {
return (result == null) ? role : null;
}
- public void close() throws IOException {
- // Nop
- }
-
- public Role getRoleByName(String roleName) throws IOException {
+ public Role getRoleByName(String roleName) throws Exception {
if (roleName == null) {
throw new IllegalArgumentException("Role name cannot be null!");
}
return (Role) m_entries.get(roleName);
}
- public Role[] getRoles(Filter filter) throws IOException {
+ public Role[] getRoles(String filterValue) throws Exception {
Collection roles = m_entries.values();
+
+ Filter filter = null;
+ if (filterValue != null) {
+ filter = FrameworkUtil.createFilter(filterValue);
+ }
List matchingRoles = new ArrayList();
Iterator rolesIter = roles.iterator();
@@ -156,12 +158,8 @@ public Role getRoleByName(String roleName) throws IOException {
Role[] result = new Role[matchingRoles.size()];
return (Role[]) matchingRoles.toArray(result);
}
-
- public void initialize() throws IOException {
- // Nop
- }
- public Role removeRole(String roleName) throws IOException {
+ public Role removeRole(String roleName) throws Exception {
if (roleName == null) {
throw new IllegalArgumentException("Name cannot be null!");
}
View
25 ...in/useradmin/src/test/java/org/apache/felix/useradmin/impl/MemoryRoleRepositoryStore.java
@@ -16,7 +16,6 @@
*/
package org.apache.felix.useradmin.impl;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
@@ -27,6 +26,7 @@
import org.apache.felix.useradmin.RoleFactory;
import org.apache.felix.useradmin.RoleRepositoryStore;
import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
import org.osgi.service.useradmin.Role;
@@ -37,7 +37,7 @@
private final ConcurrentMap m_entries = new ConcurrentHashMap();
- public Role addRole(String roleName, int type) throws IOException {
+ public Role addRole(String roleName, int type) throws Exception {
if (roleName == null) {
throw new IllegalArgumentException("Name cannot be null!");
}
@@ -46,13 +46,14 @@ public Role addRole(String roleName, int type) throws IOException {
return (result == null) ? role : null;
}
- public void close() throws IOException {
- // Nop
- }
-
- public Role[] getRoles(Filter filter) throws IOException {
+ public Role[] getRoles(String filterValue) throws Exception {
Collection roles = m_entries.values();
-
+
+ Filter filter = null;
+ if (filterValue != null) {
+ filter = FrameworkUtil.createFilter(filterValue);
+ }
+
List matchingRoles = new ArrayList();
Iterator rolesIter = roles.iterator();
while (rolesIter.hasNext()) {
@@ -66,18 +67,14 @@ public void close() throws IOException {
return (Role[]) matchingRoles.toArray(result);
}
- public Role getRoleByName(String roleName) throws IOException {
+ public Role getRoleByName(String roleName) throws Exception {
if (roleName == null) {
throw new IllegalArgumentException("Role name cannot be null!");
}
return (Role) m_entries.get(roleName);
}
-
- public void initialize() throws IOException {
- // Nop
- }
- public Role removeRole(String roleName) throws IOException {
+ public Role removeRole(String roleName) throws Exception {
if (roleName == null) {
throw new IllegalArgumentException("Name cannot be null!");
}
View
3  ...n/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositorySecurityTest.java
@@ -21,7 +21,6 @@
import junit.framework.TestCase;
-import org.apache.felix.framework.FilterImpl;
import org.osgi.service.useradmin.Role;
/**
@@ -74,7 +73,7 @@ public void testGetRolesWithKeyValueOk() throws SecurityException {
* Tests that getting roles with filters works without any permissions.
*/
public void testGetRolesWithFilterOk() throws Exception {
- m_roleManager.getRoles(new FilterImpl("(key=value)"));
+ m_roleManager.getRoles("(key=value)");
}
/**
View
12 useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/RoleRepositoryTest.java
@@ -26,8 +26,6 @@
import junit.framework.TestCase;
-import org.apache.felix.framework.FilterImpl;
-import org.osgi.framework.Filter;
import org.osgi.service.useradmin.Group;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;
@@ -274,18 +272,18 @@ public void testGetRolesWithFilterOk() throws Exception {
role2.getProperties().put("key", "value2");
role2.getProperties().put("keyB", "value1");
- Filter filter;
+ String filter;
- filter = new FilterImpl("(key=value1)");
+ filter = "(key=value1)";
assertSameRoles(new Role[]{ role1 }, m_roleRepository.getRoles(filter));
- filter = new FilterImpl("(key=value2)");
+ filter = "(key=value2)";
assertSameRoles(new Role[]{ role2 }, m_roleRepository.getRoles(filter));
- filter = new FilterImpl("(key=value*)");
+ filter = "(key=value*)";
assertSameRoles(new Role[]{ role1, role2 }, m_roleRepository.getRoles(filter));
- filter = new FilterImpl("(|(key=value1)(keyB=value1))");
+ filter = "(|(key=value1)(keyB=value1))";
assertSameRoles(new Role[]{ role1, role2 }, m_roleRepository.getRoles(filter));
}
View
11 useradmin/useradmin/src/test/java/org/apache/felix/useradmin/impl/UserAdminImplTest.java
@@ -21,9 +21,7 @@
import junit.framework.TestCase;
-import org.apache.felix.framework.FilterImpl;
import org.apache.felix.useradmin.RoleFactory;
-import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
@@ -858,14 +856,7 @@ protected void setUp() throws Exception {
m_roleRepository = new RoleRepository(new MemoryRoleRepositoryStore());
m_dispatcher = new EventDispatcher(new StubEventAdmin(), new StubUserAdminListenerList());
- m_userAdmin = new UserAdminImpl(m_roleRepository, m_dispatcher) {
- protected Filter createFilter(String filter) throws InvalidSyntaxException {
- if (filter == null || "".equals(filter.trim())) {
- return null;
- }
- return new FilterImpl(filter);
- }
- };
+ m_userAdmin = new UserAdminImpl(m_roleRepository, m_dispatcher);
m_dispatcher.start();
}
Please sign in to comment.
Something went wrong with that request. Please try again.