Make testing more generic.
[lgpl/argeo-commons.git] / org.argeo.enterprise / src / org / argeo / naming / LdifParser.java
index e47d8133ed2fda27415068f3c5ff0e7d187a5947..cc19570588f2eb9c9dcc19f124e28fe0d30444e8 100644 (file)
@@ -1,11 +1,12 @@
 package org.argeo.naming;
 
-import static org.argeo.osgi.useradmin.LdifName.dn;
-
 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;
@@ -21,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<LdapName, Attributes> res, int lineNumber, LdapName currentDn,
                        Attributes currentAttributes) {
@@ -41,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<LdapName, Attributes> 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<LdapName, Attributes> read(Reader reader) throws IOException {
                SortedMap<LdapName, Attributes> res = new TreeMap<LdapName, Attributes>();
                try {
                        List<String> 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);
@@ -97,28 +108,29 @@ 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;
 
                                        // manage DN attributes
-                                       if (attributeId.equals(dn.name()) || isLastLine) {
+                                       if (attributeId.equals(LdapAttrs.DN) || isLastLine) {
                                                if (currentDn != null) {
                                                        //
                                                        // ADD
                                                        //
                                                        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.");
                                                        }
                                                }
 
-                                               if (attributeId.equals(dn.name()))
+                                               if (attributeId.equals(LdapAttrs.DN))
                                                        try {
                                                                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;
                                                        }
@@ -138,7 +150,11 @@ public class LdifParser {
                                currentEntry.append(line);
                        }
                } finally {
-                       in.close();
+                       try {
+                               reader.close();
+                       } catch (IOException e) {
+                               // silent
+                       }
                }
                return res;
        }