X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.security.core%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fuseradmin%2FLdifParser.java;h=56bdb54e39adbc992ca14bda6df1ce59397cf6b6;hb=4fe1d843a422b0b616683c15b7b1cfb9c0538ee7;hp=1e9390a5c494dae04b148d7ebdd654171e80914d;hpb=700f034e0772a4d3e33a31227b85ef6a8486e78d;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java b/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java index 1e9390a5c..56bdb54e3 100644 --- a/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java +++ b/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java @@ -26,50 +26,62 @@ class LdifParser { SortedMap res = new TreeMap(); try { List 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.isTraceEnabled()) + log.trace("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);