X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.security.core%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fuseradmin%2FAttributeDictionary.java;h=99e5edb6292dc821b0f6d5fd167179f6681f4b19;hb=d8b62960ec3c9d991840348c63dc0c8ce980233e;hp=7691d8a94d7dfe4c09e1a89437d7b4a637f53298;hpb=e96c7f26228b70f604e41b7a56ce6c5836da9e12;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.security.core/src/org/argeo/osgi/useradmin/AttributeDictionary.java b/org.argeo.security.core/src/org/argeo/osgi/useradmin/AttributeDictionary.java index 7691d8a94..99e5edb62 100644 --- a/org.argeo.security.core/src/org/argeo/osgi/useradmin/AttributeDictionary.java +++ b/org.argeo.security.core/src/org/argeo/osgi/useradmin/AttributeDictionary.java @@ -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 { private final Attributes attributes; + private final List effectiveKeys = new ArrayList(); + private final List attrFilter; + private final Boolean includeFilter; - public AttributeDictionary(Attributes attributes) { + public AttributeDictionary(Attributes attributes, List attrFilter, + Boolean includeFilter) { this.attributes = attributes; + this.attrFilter = attrFilter; + this.includeFilter = includeFilter; + try { + NamingEnumeration 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 keys() { - return attributes.getIDs(); + return Collections.enumeration(effectiveKeys); } @Override public Enumeration elements() { - throw new UnsupportedOperationException(); + final Iterator it = effectiveKeys.iterator(); + return new Enumeration() { + + @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); + } + } }