X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Fnaming%2FLdifParser.java;h=9595b57f0b0d030e866b44856131ea3a6cfe5105;hb=9c1e0062044a1dcf34d34c7cda840334e56a289c;hp=e47d8133ed2fda27415068f3c5ff0e7d187a5947;hpb=85688af22a77f82e7762e11e6eb38c6367eddb6c;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 e47d8133e..9595b57f0 100644 --- a/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java +++ b/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java @@ -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; @@ -28,6 +29,7 @@ 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) { @@ -49,11 +51,26 @@ public class LdifParser { } } + /** 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) { + if (log.isTraceEnabled()) + log.error("Cannot close stream", e); + } + } + } + + /** 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); @@ -101,7 +118,7 @@ public class LdifParser { 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 @@ -113,7 +130,7 @@ public class LdifParser { } } - if (attributeId.equals(dn.name())) + if (attributeId.equals(LdapAttrs.DN)) try { currentDn = new LdapName(attributeValue.toString()); currentAttributes = new BasicAttributes(true); @@ -138,7 +155,12 @@ public class LdifParser { currentEntry.append(line); } } finally { - in.close(); + try { + reader.close(); + } catch (IOException e) { + if (log.isTraceEnabled()) + log.error("Cannot close stream", e); + } } return res; }