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
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Base64
;
10 import java
.util
.SortedMap
;
11 import java
.util
.TreeMap
;
13 import javax
.naming
.InvalidNameException
;
14 import javax
.naming
.NamingException
;
15 import javax
.naming
.directory
.Attribute
;
16 import javax
.naming
.directory
.Attributes
;
17 import javax
.naming
.directory
.BasicAttribute
;
18 import javax
.naming
.directory
.BasicAttributes
;
19 import javax
.naming
.ldap
.LdapName
;
20 import javax
.naming
.ldap
.Rdn
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.osgi
.useradmin
.UserDirectoryException
;
26 /** Basic LDIF parser. */
27 public class LdifParser
{
28 private final static Log log
= LogFactory
.getLog(LdifParser
.class);
30 protected Attributes
addAttributes(SortedMap
<LdapName
, Attributes
> res
, int lineNumber
, LdapName currentDn
,
31 Attributes currentAttributes
) {
33 Rdn nameRdn
= currentDn
.getRdn(currentDn
.size() - 1);
34 Attribute nameAttr
= currentAttributes
.get(nameRdn
.getType());
36 currentAttributes
.put(nameRdn
.getType(), nameRdn
.getValue());
37 else if (!nameAttr
.get().equals(nameRdn
.getValue()))
38 throw new UserDirectoryException(
39 "Attribute " + nameAttr
.getID() + "=" + nameAttr
.get() + " not consistent with DN " + currentDn
40 + " (shortly before line " + lineNumber
+ " in LDIF file)");
41 Attributes previous
= res
.put(currentDn
, currentAttributes
);
42 if (log
.isTraceEnabled())
43 log
.trace("Added " + currentDn
);
45 } catch (NamingException e
) {
46 throw new UserDirectoryException("Cannot add " + currentDn
, e
);
50 public SortedMap
<LdapName
, Attributes
> read(InputStream in
) throws IOException
{
51 SortedMap
<LdapName
, Attributes
> res
= new TreeMap
<LdapName
, Attributes
>();
53 List
<String
> lines
= new ArrayList
<>();
54 try (BufferedReader br
= new BufferedReader(new InputStreamReader(in
))) {
56 while ((line
= br
.readLine()) != null) {
60 if (lines
.size() == 0)
62 // add an empty new line since the last line is not checked
63 if (!lines
.get(lines
.size() - 1).equals(""))
66 LdapName currentDn
= null;
67 Attributes currentAttributes
= null;
68 StringBuilder currentEntry
= new StringBuilder();
70 readLines
: for (int lineNumber
= 0; lineNumber
< lines
.size(); lineNumber
++) {
71 String line
= lines
.get(lineNumber
);
72 boolean isLastLine
= false;
73 if (lineNumber
== lines
.size() - 1)
75 if (line
.startsWith(" ")) {
76 currentEntry
.append(line
.substring(1));
81 if (currentEntry
.length() != 0 || isLastLine
) {
82 // read previous attribute
83 StringBuilder attrId
= new StringBuilder(8);
84 boolean isBase64
= false;
85 readAttrId
: for (int i
= 0; i
< currentEntry
.length(); i
++) {
86 char c
= currentEntry
.charAt(i
);
88 if (i
+ 1 < currentEntry
.length() && currentEntry
.charAt(i
+ 1) == ':')
90 currentEntry
.delete(0, i
+ (isBase64 ?
2 : 1));
97 String attributeId
= attrId
.toString();
98 String cleanValueStr
= currentEntry
.toString().trim();
99 Object attributeValue
= isBase64 ? Base64
.getDecoder().decode(cleanValueStr
) : cleanValueStr
;
101 // manage DN attributes
102 if (attributeId
.equals(LdapAttrs
.DN
) || isLastLine
) {
103 if (currentDn
!= null) {
107 Attributes previous
= addAttributes(res
, lineNumber
, currentDn
, currentAttributes
);
108 if (previous
!= null) {
109 log
.warn("There was already an entry with DN " + currentDn
110 + ", which has been discarded by a subsequent one.");
114 if (attributeId
.equals(LdapAttrs
.DN
))
116 currentDn
= new LdapName(attributeValue
.toString());
117 currentAttributes
= new BasicAttributes(true);
118 } catch (InvalidNameException e
) {
119 log
.error(attributeValue
+ " not a valid DN, skipping the entry.");
121 currentAttributes
= null;
126 if (currentAttributes
!= null) {
127 Attribute attribute
= currentAttributes
.get(attributeId
);
128 if (attribute
== null) {
129 attribute
= new BasicAttribute(attributeId
);
130 currentAttributes
.put(attribute
);
132 attribute
.add(attributeValue
);
134 currentEntry
= new StringBuilder();
136 currentEntry
.append(line
);