1 package org
.argeo
.osgi
.useradmin
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.SortedMap
;
7 import java
.util
.TreeMap
;
9 import javax
.naming
.InvalidNameException
;
10 import javax
.naming
.directory
.Attribute
;
11 import javax
.naming
.directory
.Attributes
;
12 import javax
.naming
.directory
.BasicAttribute
;
13 import javax
.naming
.directory
.BasicAttributes
;
14 import javax
.naming
.ldap
.LdapName
;
16 import org
.apache
.commons
.codec
.binary
.Base64
;
17 import org
.apache
.commons
.io
.IOUtils
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
21 /** Basic LDIF parser. */
23 private final static Log log
= LogFactory
.getLog(LdifParser
.class);
25 SortedMap
<LdapName
, Attributes
> read(InputStream in
) throws IOException
{
26 SortedMap
<LdapName
, Attributes
> res
= new TreeMap
<LdapName
, Attributes
>();
28 List
<String
> lines
= IOUtils
.readLines(in
);
29 // add an empty new line since the last line is not checked
30 if (!lines
.get(lines
.size() - 1).equals(""))
33 LdapName currentDn
= null;
34 Attributes currentAttributes
= null;
35 StringBuilder currentEntry
= new StringBuilder();
37 readLines
: for (int lineNumber
= 0; lineNumber
< lines
.size(); lineNumber
++) {
38 String line
= lines
.get(lineNumber
);
39 boolean isLastLine
= false;
40 if (lineNumber
== lines
.size() - 1)
42 if (line
.startsWith(" ")) {
43 currentEntry
.append(line
.substring(1));
48 if (currentEntry
.length() != 0 || isLastLine
) {
49 // read previous attribute
50 StringBuilder attrId
= new StringBuilder(8);
51 boolean isBase64
= false;
52 readAttrId
: for (int i
= 0; i
< currentEntry
.length(); i
++) {
53 char c
= currentEntry
.charAt(i
);
55 if (i
+ 1 < currentEntry
.length()
56 && currentEntry
.charAt(i
+ 1) == ':')
58 currentEntry
.delete(0, i
+ (isBase64 ?
2 : 1));
65 String attributeId
= attrId
.toString();
66 String cleanValueStr
= currentEntry
.toString().trim();
67 Object attributeValue
= isBase64 ? Base64
68 .decodeBase64(cleanValueStr
) : cleanValueStr
;
70 // manage DN attributes
71 if (attributeId
.equals("dn") || isLastLine
) {
72 if (currentDn
!= null) {
73 Attributes previous
= res
.put(currentDn
,
75 if (log
.isTraceEnabled())
76 log
.trace("Added " + currentDn
);
77 if (previous
!= null) {
78 log
.warn("There was already an entry with DN "
80 + ", which has been discarded by a subsequent one.");
84 if (attributeId
.equals("dn"))
86 currentDn
= new LdapName(
87 attributeValue
.toString());
88 currentAttributes
= new BasicAttributes();
89 } catch (InvalidNameException e
) {
90 log
.error(attributeValue
91 + " not a valid DN, skipping the entry.");
93 currentAttributes
= null;
98 if (currentAttributes
!= null) {
99 Attribute attribute
= currentAttributes
101 if (attribute
== null) {
102 attribute
= new BasicAttribute(attributeId
);
103 currentAttributes
.put(attribute
);
105 attribute
.add(attributeValue
);
107 currentEntry
= new StringBuilder();
109 currentEntry
.append(line
);
112 IOUtils
.closeQuietly(in
);