1 package org
.argeo
.naming
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.InputStreamReader
;
8 import java
.nio
.charset
.Charset
;
9 import java
.nio
.charset
.StandardCharsets
;
10 import java
.util
.ArrayList
;
11 import java
.util
.Base64
;
12 import java
.util
.List
;
13 import java
.util
.SortedMap
;
14 import java
.util
.TreeMap
;
16 import javax
.naming
.InvalidNameException
;
17 import javax
.naming
.NamingException
;
18 import javax
.naming
.directory
.Attribute
;
19 import javax
.naming
.directory
.Attributes
;
20 import javax
.naming
.directory
.BasicAttribute
;
21 import javax
.naming
.directory
.BasicAttributes
;
22 import javax
.naming
.ldap
.LdapName
;
23 import javax
.naming
.ldap
.Rdn
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.osgi
.useradmin
.UserDirectoryException
;
29 /** Basic LDIF parser. */
30 public class LdifParser
{
31 private final static Log log
= LogFactory
.getLog(LdifParser
.class);
32 private final static Charset DEFAULT_CHARSET
= StandardCharsets
.UTF_8
;
34 protected Attributes
addAttributes(SortedMap
<LdapName
, Attributes
> res
, int lineNumber
, LdapName currentDn
,
35 Attributes currentAttributes
) {
37 Rdn nameRdn
= currentDn
.getRdn(currentDn
.size() - 1);
38 Attribute nameAttr
= currentAttributes
.get(nameRdn
.getType());
40 currentAttributes
.put(nameRdn
.getType(), nameRdn
.getValue());
41 else if (!nameAttr
.get().equals(nameRdn
.getValue()))
42 throw new UserDirectoryException(
43 "Attribute " + nameAttr
.getID() + "=" + nameAttr
.get() + " not consistent with DN " + currentDn
44 + " (shortly before line " + lineNumber
+ " in LDIF file)");
45 Attributes previous
= res
.put(currentDn
, currentAttributes
);
46 if (log
.isTraceEnabled())
47 log
.trace("Added " + currentDn
);
49 } catch (NamingException e
) {
50 throw new UserDirectoryException("Cannot add " + currentDn
, e
);
54 /** With UTF-8 charset */
55 public SortedMap
<LdapName
, Attributes
> read(InputStream in
) throws IOException
{
56 try (Reader reader
= new InputStreamReader(in
, DEFAULT_CHARSET
)) {
61 } catch (IOException e
) {
62 if (log
.isTraceEnabled())
63 log
.error("Cannot close stream", e
);
68 /** Will close the reader. */
69 public SortedMap
<LdapName
, Attributes
> read(Reader reader
) throws IOException
{
70 SortedMap
<LdapName
, Attributes
> res
= new TreeMap
<LdapName
, Attributes
>();
72 List
<String
> lines
= new ArrayList
<>();
73 try (BufferedReader br
= new BufferedReader(reader
)) {
75 while ((line
= br
.readLine()) != null) {
79 if (lines
.size() == 0)
81 // add an empty new line since the last line is not checked
82 if (!lines
.get(lines
.size() - 1).equals(""))
85 LdapName currentDn
= null;
86 Attributes currentAttributes
= null;
87 StringBuilder currentEntry
= new StringBuilder();
89 readLines
: for (int lineNumber
= 0; lineNumber
< lines
.size(); lineNumber
++) {
90 String line
= lines
.get(lineNumber
);
91 boolean isLastLine
= false;
92 if (lineNumber
== lines
.size() - 1)
94 if (line
.startsWith(" ")) {
95 currentEntry
.append(line
.substring(1));
100 if (currentEntry
.length() != 0 || isLastLine
) {
101 // read previous attribute
102 StringBuilder attrId
= new StringBuilder(8);
103 boolean isBase64
= false;
104 readAttrId
: for (int i
= 0; i
< currentEntry
.length(); i
++) {
105 char c
= currentEntry
.charAt(i
);
107 if (i
+ 1 < currentEntry
.length() && currentEntry
.charAt(i
+ 1) == ':')
109 currentEntry
.delete(0, i
+ (isBase64 ?
2 : 1));
116 String attributeId
= attrId
.toString();
117 // TODO should we really trim the end of the string as well?
118 String cleanValueStr
= currentEntry
.toString().trim();
119 Object attributeValue
= isBase64 ? Base64
.getDecoder().decode(cleanValueStr
) : cleanValueStr
;
121 // manage DN attributes
122 if (attributeId
.equals(LdapAttrs
.DN
) || isLastLine
) {
123 if (currentDn
!= null) {
127 Attributes previous
= addAttributes(res
, lineNumber
, currentDn
, currentAttributes
);
128 if (previous
!= null) {
129 log
.warn("There was already an entry with DN " + currentDn
130 + ", which has been discarded by a subsequent one.");
134 if (attributeId
.equals(LdapAttrs
.DN
))
136 currentDn
= new LdapName(attributeValue
.toString());
137 currentAttributes
= new BasicAttributes(true);
138 } catch (InvalidNameException e
) {
139 log
.error(attributeValue
+ " not a valid DN, skipping the entry.");
141 currentAttributes
= null;
146 if (currentAttributes
!= null) {
147 Attribute attribute
= currentAttributes
.get(attributeId
);
148 if (attribute
== null) {
149 attribute
= new BasicAttribute(attributeId
);
150 currentAttributes
.put(attribute
);
152 attribute
.add(attributeValue
);
154 currentEntry
= new StringBuilder();
156 currentEntry
.append(line
);
161 } catch (IOException e
) {
162 if (log
.isTraceEnabled())
163 log
.error("Cannot close stream", e
);