X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Fnaming%2FLdifParser.java;h=cc19570588f2eb9c9dcc19f124e28fe0d30444e8;hb=e4b44be13dde0ddc9acb794add1c9459e2483513;hp=4aefc9a83b8c099564d7a74ec219f8828eb98c4f;hpb=b334cd41b64d0658aae9125c58d6a1194eccb087;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java b/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java index 4aefc9a83..cc1957058 100644 --- a/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java +++ b/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java @@ -4,6 +4,9 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; import java.util.List; @@ -19,13 +22,11 @@ import javax.naming.directory.BasicAttributes; import javax.naming.ldap.LdapName; import javax.naming.ldap.Rdn; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.argeo.osgi.useradmin.UserDirectoryException; /** Basic LDIF parser. */ public class LdifParser { - private final static Log log = LogFactory.getLog(LdifParser.class); + private final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; protected Attributes addAttributes(SortedMap res, int lineNumber, LdapName currentDn, Attributes currentAttributes) { @@ -39,19 +40,31 @@ public class LdifParser { "Attribute " + nameAttr.getID() + "=" + nameAttr.get() + " not consistent with DN " + currentDn + " (shortly before line " + lineNumber + " in LDIF file)"); Attributes previous = res.put(currentDn, currentAttributes); - if (log.isTraceEnabled()) - log.trace("Added " + currentDn); return previous; } catch (NamingException e) { throw new UserDirectoryException("Cannot add " + currentDn, e); } } + /** With UTF-8 charset */ public SortedMap read(InputStream in) throws IOException { + try (Reader reader = new InputStreamReader(in, DEFAULT_CHARSET)) { + return read(reader); + } finally { + try { + in.close(); + } catch (IOException e) { + // silent + } + } + } + + /** Will close the reader. */ + public SortedMap read(Reader reader) throws IOException { SortedMap res = new TreeMap(); try { List lines = new ArrayList<>(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) { + try (BufferedReader br = new BufferedReader(reader)) { String line; while ((line = br.readLine()) != null) { lines.add(line); @@ -95,6 +108,7 @@ public class LdifParser { } String attributeId = attrId.toString(); + // TODO should we really trim the end of the string as well? String cleanValueStr = currentEntry.toString().trim(); Object attributeValue = isBase64 ? Base64.getDecoder().decode(cleanValueStr) : cleanValueStr; @@ -106,8 +120,8 @@ public class LdifParser { // Attributes previous = addAttributes(res, lineNumber, currentDn, currentAttributes); if (previous != null) { - log.warn("There was already an entry with DN " + currentDn - + ", which has been discarded by a subsequent one."); +// log.warn("There was already an entry with DN " + currentDn +// + ", which has been discarded by a subsequent one."); } } @@ -116,7 +130,7 @@ public class LdifParser { currentDn = new LdapName(attributeValue.toString()); currentAttributes = new BasicAttributes(true); } catch (InvalidNameException e) { - log.error(attributeValue + " not a valid DN, skipping the entry."); +// log.error(attributeValue + " not a valid DN, skipping the entry."); currentDn = null; currentAttributes = null; } @@ -136,7 +150,11 @@ public class LdifParser { currentEntry.append(line); } } finally { - in.close(); + try { + reader.close(); + } catch (IOException e) { + // silent + } } return res; }