Make writing of member and uniqueMember nicer in LDIF writer.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 27 Oct 2019 11:03:55 +0000 (12:03 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 27 Oct 2019 11:03:55 +0000 (12:03 +0100)
org.argeo.enterprise/src/org/argeo/naming/LdifWriter.java

index 6a3fea12f29fa8b438700ddc72d6cc225a59a880..98d2df055719a3f04e443183f00f26211be07c5d 100644 (file)
@@ -1,5 +1,10 @@
 package org.argeo.naming;
 
+import static org.argeo.naming.LdapAttrs.DN;
+import static org.argeo.naming.LdapAttrs.member;
+import static org.argeo.naming.LdapAttrs.objectClass;
+import static org.argeo.naming.LdapAttrs.uniqueMember;
+
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
@@ -8,6 +13,8 @@ import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.Base64;
 import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
@@ -42,16 +49,24 @@ public class LdifWriter {
                                throw new UserDirectoryException(
                                                "Attribute " + nameAttr.getID() + "=" + nameAttr.get() + " not consistent with DN " + name);
 
-                       writer.append(LdapAttrs.DN + ": ").append(name.toString()).append('\n');
-                       Attribute objectClassAttr = attributes.get("objectClass");
+                       writer.append(DN + ": ").append(name.toString()).append('\n');
+                       Attribute objectClassAttr = attributes.get(objectClass.name());
                        if (objectClassAttr != null)
                                writeAttribute(objectClassAttr);
-                       for (NamingEnumeration<? extends Attribute> attrs = attributes.getAll(); attrs.hasMore();) {
+                       attributes: for (NamingEnumeration<? extends Attribute> attrs = attributes.getAll(); attrs.hasMore();) {
                                Attribute attribute = attrs.next();
-                               if (attribute.getID().equals(LdapAttrs.DN) || attribute.getID().equals("objectClass"))
-                                       continue;// skip DN attribute
+                               if (attribute.getID().equals(DN) || attribute.getID().equals(objectClass.name()))
+                                       continue attributes;// skip DN attribute
+                               if (attribute.getID().equals(member.name()) || attribute.getID().equals(uniqueMember.name()))
+                                       continue attributes;// skip member and uniqueMember attributes, so that they are always written last
                                writeAttribute(attribute);
                        }
+                       // write member and uniqueMember attributes last
+                       for (NamingEnumeration<? extends Attribute> attrs = attributes.getAll(); attrs.hasMore();) {
+                               Attribute attribute = attrs.next();
+                               if (attribute.getID().equals(member.name()) || attribute.getID().equals(uniqueMember.name()))
+                                       writeMemberAttribute(attribute);
+                       }
                        writer.append('\n');
                        writer.flush();
                } catch (NamingException e) {
@@ -75,4 +90,17 @@ public class LdifWriter {
                        }
                }
        }
+
+       protected void writeMemberAttribute(Attribute attribute) throws NamingException, IOException {
+               // Note: duplicate entries will be swallowed
+               SortedSet<String> values = new TreeSet<>();
+               for (NamingEnumeration<?> attrValues = attribute.getAll(); attrValues.hasMore();) {
+                       String value = attrValues.next().toString();
+                       values.add(value);
+               }
+
+               for (String value : values) {
+                       writer.append(attribute.getID()).append(": ").append(value).append('\n');
+               }
+       }
 }