Fix: LDIF parser manages last entry properly.
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 24 Aug 2015 15:29:13 +0000 (15:29 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 24 Aug 2015 15:29:13 +0000 (15:29 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8333 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.security.core/ext/test/org/argeo/osgi/useradmin/LdifParserTest.java
org.argeo.security.core/ext/test/org/argeo/osgi/useradmin/test.ldif
org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java

index dd32dca171632641a5c15359e696dc7b028389d8..6a579668a6db4fcdb7d8d06c0a7a2c6840012fda 100644 (file)
@@ -1,15 +1,19 @@
 package org.argeo.osgi.useradmin;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.SortedMap;
 
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.ldap.LdapName;
 
+import junit.framework.TestCase;
+
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.digest.DigestUtils;
 
-import junit.framework.TestCase;
-
 public class LdifParserTest extends TestCase {
        public void testSimpleLdif() throws Exception {
                LdifParser ldifParser = new LdifParser();
@@ -27,5 +31,19 @@ public class LdifParserTest extends TestCase {
                assertEquals("{SHA}" + Base64.encodeBase64String(hashedPassword),
                                new String(rawPwEntry));
 
+               LdapName adminDn = new LdapName(
+                               "cn=admin,ou=Roles,dc=demo,dc=example,dc=org");
+               Attributes adminAttributes = res.get(adminDn);
+               assertNotNull(adminAttributes);
+               Attribute memberAttribute = adminAttributes.get("member");
+               assertNotNull(memberAttribute);
+               NamingEnumeration<?> members = memberAttribute.getAll();
+               List<String> users = new ArrayList<String>();
+               while (members.hasMore()) {
+                       Object value = members.next();
+                       users.add(value.toString());
+               }
+               assertEquals(1, users.size());
+               assertEquals(rootDn, new LdapName(users.get(0)));
        }
 }
index 0d2e8ba8fea748f9679beee1c0ce7efe8f4ba41e..a5526a0564dd2bcb4e7f87d0169b439450bbe473 100644 (file)
@@ -44,4 +44,4 @@ dn: cn=admin,ou=Roles,dc=demo,dc=example,dc=org
 objectClass: groupOfNames
 objectClass: top
 cn: admin
-member: uid=root,ou=People,dc=demo,dc=example,dc=org
+member: uid=root,ou=People,dc=demo,dc=example,dc=org
\ No newline at end of file
index 1e9390a5c494dae04b148d7ebdd654171e80914d..38c37312f23b65d006d0e2fe86595dc6023b917e 100644 (file)
@@ -26,50 +26,62 @@ class LdifParser {
                SortedMap<LdapName, Attributes> res = new TreeMap<LdapName, Attributes>();
                try {
                        List<String> lines = IOUtils.readLines(in);
+                       // add an empty new line since the last line is not checked
+                       if (!lines.get(lines.size() - 1).equals(""))
+                               lines.add("");
 
                        LdapName currentDn = null;
                        Attributes currentAttributes = null;
                        StringBuilder currentEntry = new StringBuilder();
 
-                       readLines: for (String line : lines) {
+                       readLines: for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) {
+                               String line = lines.get(lineNumber);
+                               boolean isLastLine = false;
+                               if (lineNumber == lines.size() - 1)
+                                       isLastLine = true;
                                if (line.startsWith(" ")) {
                                        currentEntry.append(line.substring(1));
-                                       continue readLines;
-                               } else {
-                                       if (currentEntry.length() != 0) {
-                                               // read previous attribute
-                                               StringBuilder attrId = new StringBuilder(8);
-                                               boolean isBase64 = false;
-                                               readAttrId: for (int i = 0; i < currentEntry.length(); i++) {
-                                                       char c = currentEntry.charAt(i);
-                                                       if (c == ':') {
-                                                               if (i + 1 < currentEntry.length()
-                                                                               && currentEntry.charAt(i + 1) == ':')
-                                                                       isBase64 = true;
-                                                               currentEntry.delete(0, i + (isBase64 ? 2 : 1));
-                                                               break readAttrId;
-                                                       } else {
-                                                               attrId.append(c);
-                                                       }
+                                       if (!isLastLine)
+                                               continue readLines;
+                               }
+
+                               if (currentEntry.length() != 0 || isLastLine) {
+                                       // read previous attribute
+                                       StringBuilder attrId = new StringBuilder(8);
+                                       boolean isBase64 = false;
+                                       readAttrId: for (int i = 0; i < currentEntry.length(); i++) {
+                                               char c = currentEntry.charAt(i);
+                                               if (c == ':') {
+                                                       if (i + 1 < currentEntry.length()
+                                                                       && currentEntry.charAt(i + 1) == ':')
+                                                               isBase64 = true;
+                                                       currentEntry.delete(0, i + (isBase64 ? 2 : 1));
+                                                       break readAttrId;
+                                               } else {
+                                                       attrId.append(c);
                                                }
+                                       }
 
-                                               String attributeId = attrId.toString();
-                                               String cleanValueStr = currentEntry.toString().trim();
-                                               Object attributeValue = isBase64 ? Base64
-                                                               .decodeBase64(cleanValueStr) : cleanValueStr;
+                                       String attributeId = attrId.toString();
+                                       String cleanValueStr = currentEntry.toString().trim();
+                                       Object attributeValue = isBase64 ? Base64
+                                                       .decodeBase64(cleanValueStr) : cleanValueStr;
 
-                                               // manage DN attributes
-                                               if (attributeId.equals("dn")) {
-                                                       if (currentDn != null) {
-                                                               Attributes previous = res.put(currentDn,
-                                                                               currentAttributes);
-                                                               if (previous != null) {
-                                                                       log.warn("There was already an entry with DN "
-                                                                                       + currentDn
-                                                                                       + ", which has been discarded by a subsequent one.");
-                                                               }
+                                       // manage DN attributes
+                                       if (attributeId.equals("dn") || isLastLine) {
+                                               if (currentDn != null) {
+                                                       Attributes previous = res.put(currentDn,
+                                                                       currentAttributes);
+                                                       if (log.isDebugEnabled())
+                                                               log.debug("Added " + currentDn);
+                                                       if (previous != null) {
+                                                               log.warn("There was already an entry with DN "
+                                                                               + currentDn
+                                                                               + ", which has been discarded by a subsequent one.");
                                                        }
+                                               }
 
+                                               if (attributeId.equals("dn"))
                                                        try {
                                                                currentDn = new LdapName(
                                                                                attributeValue.toString());
@@ -80,22 +92,21 @@ class LdifParser {
                                                                currentDn = null;
                                                                currentAttributes = null;
                                                        }
-                                               }
+                                       }
 
-                                               // store attribute
-                                               if (currentAttributes != null) {
-                                                       Attribute attribute = currentAttributes
-                                                                       .get(attributeId);
-                                                       if (attribute == null) {
-                                                               attribute = new BasicAttribute(attributeId);
-                                                               currentAttributes.put(attribute);
-                                                       }
-                                                       attribute.add(attributeValue);
+                                       // store attribute
+                                       if (currentAttributes != null) {
+                                               Attribute attribute = currentAttributes
+                                                               .get(attributeId);
+                                               if (attribute == null) {
+                                                       attribute = new BasicAttribute(attributeId);
+                                                       currentAttributes.put(attribute);
                                                }
-                                               currentEntry = new StringBuilder();
+                                               attribute.add(attributeValue);
                                        }
-                                       currentEntry.append(line);
+                                       currentEntry = new StringBuilder();
                                }
+                               currentEntry.append(line);
                        }
                } finally {
                        IOUtils.closeQuietly(in);