]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java
Use SWT operation mode
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / LdifParser.java
1 package org.argeo.osgi.useradmin;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.List;
6 import java.util.SortedMap;
7 import java.util.TreeMap;
8
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;
15
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;
20
21 /** Basic LDIF parser. */
22 class LdifParser {
23 private final static Log log = LogFactory.getLog(LdifParser.class);
24
25 SortedMap<LdapName, Attributes> read(InputStream in) throws IOException {
26 SortedMap<LdapName, Attributes> res = new TreeMap<LdapName, Attributes>();
27 try {
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(""))
31 lines.add("");
32
33 LdapName currentDn = null;
34 Attributes currentAttributes = null;
35 StringBuilder currentEntry = new StringBuilder();
36
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)
41 isLastLine = true;
42 if (line.startsWith(" ")) {
43 currentEntry.append(line.substring(1));
44 if (!isLastLine)
45 continue readLines;
46 }
47
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);
54 if (c == ':') {
55 if (i + 1 < currentEntry.length()
56 && currentEntry.charAt(i + 1) == ':')
57 isBase64 = true;
58 currentEntry.delete(0, i + (isBase64 ? 2 : 1));
59 break readAttrId;
60 } else {
61 attrId.append(c);
62 }
63 }
64
65 String attributeId = attrId.toString();
66 String cleanValueStr = currentEntry.toString().trim();
67 Object attributeValue = isBase64 ? Base64
68 .decodeBase64(cleanValueStr) : cleanValueStr;
69
70 // manage DN attributes
71 if (attributeId.equals("dn") || isLastLine) {
72 if (currentDn != null) {
73 Attributes previous = res.put(currentDn,
74 currentAttributes);
75 if (log.isTraceEnabled())
76 log.trace("Added " + currentDn);
77 if (previous != null) {
78 log.warn("There was already an entry with DN "
79 + currentDn
80 + ", which has been discarded by a subsequent one.");
81 }
82 }
83
84 if (attributeId.equals("dn"))
85 try {
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.");
92 currentDn = null;
93 currentAttributes = null;
94 }
95 }
96
97 // store attribute
98 if (currentAttributes != null) {
99 Attribute attribute = currentAttributes
100 .get(attributeId);
101 if (attribute == null) {
102 attribute = new BasicAttribute(attributeId);
103 currentAttributes.put(attribute);
104 }
105 attribute.add(attributeValue);
106 }
107 currentEntry = new StringBuilder();
108 }
109 currentEntry.append(line);
110 }
111 } finally {
112 IOUtils.closeQuietly(in);
113 }
114 return res;
115 }
116 }