1 package org
.argeo
.osgi
.useradmin
;
3 import static org
.argeo
.osgi
.useradmin
.LdifName
.dn
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
8 import java
.util
.SortedMap
;
9 import java
.util
.TreeMap
;
11 import javax
.naming
.InvalidNameException
;
12 import javax
.naming
.NamingException
;
13 import javax
.naming
.directory
.Attribute
;
14 import javax
.naming
.directory
.Attributes
;
15 import javax
.naming
.directory
.BasicAttribute
;
16 import javax
.naming
.directory
.BasicAttributes
;
17 import javax
.naming
.ldap
.LdapName
;
18 import javax
.naming
.ldap
.Rdn
;
20 import org
.apache
.commons
.codec
.binary
.Base64
;
21 import org
.apache
.commons
.io
.IOUtils
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
25 /** Basic LDIF parser. */
27 private final static Log log
= LogFactory
.getLog(LdifParser
.class);
29 protected Attributes
addAttributes(SortedMap
<LdapName
, Attributes
> res
,
30 int lineNumber
, LdapName currentDn
, Attributes currentAttributes
) {
32 Rdn nameRdn
= currentDn
.getRdn(currentDn
.size() - 1);
33 Attribute nameAttr
= currentAttributes
.get(nameRdn
.getType());
35 currentAttributes
.put(nameRdn
.getType(), nameRdn
.getValue());
36 else if (!nameAttr
.get().equals(nameRdn
.getValue()))
37 throw new UserDirectoryException("Attribute "
38 + nameAttr
.getID() + "=" + nameAttr
.get()
39 + " not consistent with DN " + currentDn
40 + " (shortly before line " + lineNumber
42 Attributes previous
= res
.put(currentDn
, currentAttributes
);
43 if (log
.isTraceEnabled())
44 log
.trace("Added " + currentDn
);
46 } catch (NamingException e
) {
47 throw new UserDirectoryException("Cannot add " + currentDn
, e
);
51 static void checkDnConsistency() {
55 SortedMap
<LdapName
, Attributes
> read(InputStream in
) throws IOException
{
56 SortedMap
<LdapName
, Attributes
> res
= new TreeMap
<LdapName
, Attributes
>();
58 List
<String
> lines
= IOUtils
.readLines(in
);
59 // add an empty new line since the last line is not checked
60 if (!lines
.get(lines
.size() - 1).equals(""))
63 LdapName currentDn
= null;
64 Attributes currentAttributes
= null;
65 StringBuilder currentEntry
= new StringBuilder();
67 readLines
: for (int lineNumber
= 0; lineNumber
< lines
.size(); lineNumber
++) {
68 String line
= lines
.get(lineNumber
);
69 boolean isLastLine
= false;
70 if (lineNumber
== lines
.size() - 1)
72 if (line
.startsWith(" ")) {
73 currentEntry
.append(line
.substring(1));
78 if (currentEntry
.length() != 0 || isLastLine
) {
79 // read previous attribute
80 StringBuilder attrId
= new StringBuilder(8);
81 boolean isBase64
= false;
82 readAttrId
: for (int i
= 0; i
< currentEntry
.length(); i
++) {
83 char c
= currentEntry
.charAt(i
);
85 if (i
+ 1 < currentEntry
.length()
86 && currentEntry
.charAt(i
+ 1) == ':')
88 currentEntry
.delete(0, i
+ (isBase64 ?
2 : 1));
95 String attributeId
= attrId
.toString();
96 String cleanValueStr
= currentEntry
.toString().trim();
97 Object attributeValue
= isBase64 ? Base64
98 .decodeBase64(cleanValueStr
) : cleanValueStr
;
100 // manage DN attributes
101 if (attributeId
.equals(dn
.name()) || isLastLine
) {
102 if (currentDn
!= null) {
106 Attributes previous
= addAttributes(res
,
107 lineNumber
, currentDn
, currentAttributes
);
108 if (previous
!= null) {
109 log
.warn("There was already an entry with DN "
111 + ", which has been discarded by a subsequent one.");
115 if (attributeId
.equals(dn
.name()))
117 currentDn
= new LdapName(
118 attributeValue
.toString());
119 currentAttributes
= new BasicAttributes(true);
120 } catch (InvalidNameException e
) {
121 log
.error(attributeValue
122 + " not a valid DN, skipping the entry.");
124 currentAttributes
= null;
129 if (currentAttributes
!= null) {
130 Attribute attribute
= currentAttributes
132 if (attribute
== null) {
133 attribute
= new BasicAttribute(attributeId
);
134 currentAttributes
.put(attribute
);
136 attribute
.add(attributeValue
);
138 currentEntry
= new StringBuilder();
140 currentEntry
.append(line
);
143 IOUtils
.closeQuietly(in
);