LDIF user admin read-only features working (adding properties and credentials)
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / AttributeDictionary.java
index 7691d8a94d7dfe4c09e1a89437d7b4a637f53298..99e5edb6292dc821b0f6d5fd167179f6681f4b19 100644 (file)
@@ -1,44 +1,90 @@
 package org.argeo.osgi.useradmin;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
 
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttribute;
 
-class AttributeDictionary extends Dictionary {
+class AttributeDictionary extends Dictionary<String,Object> {
        private final Attributes attributes;
+       private final List<String> effectiveKeys = new ArrayList<String>();
+       private final List<String> attrFilter;
+       private final Boolean includeFilter;
 
-       public AttributeDictionary(Attributes attributes) {
+       public AttributeDictionary(Attributes attributes, List<String> attrFilter,
+                       Boolean includeFilter) {
                this.attributes = attributes;
+               this.attrFilter = attrFilter;
+               this.includeFilter = includeFilter;
+               try {
+                       NamingEnumeration<String> ids = attributes.getIDs();
+                       while (ids.hasMore()) {
+                               String id = ids.next();
+                               if (includeFilter && attrFilter.contains(id))
+                                       effectiveKeys.add(id);
+                               else if (!includeFilter && !attrFilter.contains(id))
+                                       effectiveKeys.add(id);
+                       }
+               } catch (NamingException e) {
+                       throw new ArgeoUserAdminException(
+                                       "Cannot initialise attribute dictionary", e);
+               }
        }
 
        @Override
        public int size() {
-               return attributes.size();
+               return effectiveKeys.size();
        }
 
        @Override
        public boolean isEmpty() {
-               return attributes.size() == 0;
+               return effectiveKeys.size() == 0;
        }
 
        @Override
        public Enumeration<String> keys() {
-               return attributes.getIDs();
+               return Collections.enumeration(effectiveKeys);
        }
 
        @Override
        public Enumeration<Object> elements() {
-               throw new UnsupportedOperationException();
+               final Iterator<String> it = effectiveKeys.iterator();
+               return new Enumeration<Object>() {
+
+                       @Override
+                       public boolean hasMoreElements() {
+                               return it.hasNext();
+                       }
+
+                       @Override
+                       public Object nextElement() {
+                               String key = it.next();
+                               try {
+                                       return attributes.get(key).get();
+                               } catch (NamingException e) {
+                                       throw new ArgeoUserAdminException(
+                                                       "Cannot get value for key " + key, e);
+                               }
+                       }
+
+               };
        }
 
        @Override
        public Object get(Object key) {
                try {
-                       return attributes.get(key.toString()).get();
+                       Attribute attr = attributes.get(key.toString());
+                       if (attr == null)
+                               return null;
+                       return attr.get();
                } catch (NamingException e) {
                        throw new ArgeoUserAdminException("Cannot get value for attribute "
                                        + key, e);
@@ -46,10 +92,15 @@ class AttributeDictionary extends Dictionary {
        }
 
        @Override
-       public Object put(Object key, Object value) {
+       public Object put(String key, Object value) {
                if (!(value instanceof String || value instanceof byte[]))
-                       throw new IllegalArgumentException(
-                                       "Value muste be String or byte[]");
+                       throw new IllegalArgumentException("Value must be String or byte[]");
+
+               if (includeFilter && !attrFilter.contains(key))
+                       throw new IllegalArgumentException("Key " + key + " not included");
+               else if (!includeFilter && attrFilter.contains(key))
+                       throw new IllegalArgumentException("Key " + key + " excluded");
+
                try {
                        Attribute attribute = attributes.get(key.toString());
                        attribute = new BasicAttribute(key.toString());
@@ -67,7 +118,20 @@ class AttributeDictionary extends Dictionary {
 
        @Override
        public Object remove(Object key) {
-               throw new UnsupportedOperationException();
-       }
+               if (includeFilter && !attrFilter.contains(key))
+                       throw new IllegalArgumentException("Key " + key + " not included");
+               else if (!includeFilter && attrFilter.contains(key))
+                       throw new IllegalArgumentException("Key " + key + " excluded");
 
+               try {
+                       Attribute attr = attributes.remove(key.toString());
+                       if (attr != null)
+                               return attr.get();
+                       else
+                               return null;
+               } catch (NamingException e) {
+                       throw new ArgeoUserAdminException("Cannot remove attribute " + key,
+                                       e);
+               }
+       }
 }