1 package org
.argeo
.cms
.directory
.ldap
;
3 import static org
.argeo
.api
.acr
.ldap
.LdapAttr
.DN
;
4 import static org
.argeo
.api
.acr
.ldap
.LdapAttr
.member
;
5 import static org
.argeo
.api
.acr
.ldap
.LdapAttr
.objectClass
;
6 import static org
.argeo
.api
.acr
.ldap
.LdapAttr
.uniqueMember
;
8 import java
.io
.IOException
;
9 import java
.io
.OutputStream
;
10 import java
.io
.OutputStreamWriter
;
11 import java
.io
.Writer
;
12 import java
.nio
.charset
.Charset
;
13 import java
.nio
.charset
.StandardCharsets
;
14 import java
.util
.Base64
;
16 import java
.util
.SortedSet
;
17 import java
.util
.TreeSet
;
19 import javax
.naming
.NamingEnumeration
;
20 import javax
.naming
.NamingException
;
21 import javax
.naming
.directory
.Attribute
;
22 import javax
.naming
.directory
.Attributes
;
23 import javax
.naming
.ldap
.LdapName
;
24 import javax
.naming
.ldap
.Rdn
;
26 /** Basic LDIF writer */
27 public class LdifWriter
{
28 private final static Charset DEFAULT_CHARSET
= StandardCharsets
.UTF_8
;
29 private final Writer writer
;
31 /** Writer must be closed by caller */
32 public LdifWriter(Writer writer
) {
36 /** Stream must be closed by caller */
37 public LdifWriter(OutputStream out
) {
38 this(new OutputStreamWriter(out
, DEFAULT_CHARSET
));
41 public void writeEntry(LdapName name
, Attributes attributes
) throws IOException
{
44 Rdn nameRdn
= name
.getRdn(name
.size() - 1);
45 Attribute nameAttr
= attributes
.get(nameRdn
.getType());
46 if (!nameAttr
.get().equals(nameRdn
.getValue()))
47 throw new IllegalArgumentException(
48 "Attribute " + nameAttr
.getID() + "=" + nameAttr
.get() + " not consistent with DN " + name
);
50 writer
.append(DN
+ ": ").append(name
.toString()).append('\n');
51 Attribute objectClassAttr
= attributes
.get(objectClass
.name());
52 if (objectClassAttr
!= null)
53 writeAttribute(objectClassAttr
);
54 attributes
: for (NamingEnumeration
<?
extends Attribute
> attrs
= attributes
.getAll(); attrs
.hasMore();) {
55 Attribute attribute
= attrs
.next();
56 if (attribute
.getID().equals(DN
) || attribute
.getID().equals(objectClass
.name()))
57 continue attributes
;// skip DN attribute
58 if (attribute
.getID().equals(member
.name()) || attribute
.getID().equals(uniqueMember
.name()))
59 continue attributes
;// skip member and uniqueMember attributes, so that they are always written last
60 writeAttribute(attribute
);
62 // write member and uniqueMember attributes last
63 for (NamingEnumeration
<?
extends Attribute
> attrs
= attributes
.getAll(); attrs
.hasMore();) {
64 Attribute attribute
= attrs
.next();
65 if (attribute
.getID().equals(member
.name()) || attribute
.getID().equals(uniqueMember
.name()))
66 writeMemberAttribute(attribute
);
70 } catch (NamingException e
) {
71 throw new IllegalStateException("Cannot write LDIF", e
);
75 public void write(Map
<LdapName
, Attributes
> entries
) throws IOException
{
76 for (LdapName dn
: entries
.keySet())
77 writeEntry(dn
, entries
.get(dn
));
80 protected void writeAttribute(Attribute attribute
) throws NamingException
, IOException
{
81 for (NamingEnumeration
<?
> attrValues
= attribute
.getAll(); attrValues
.hasMore();) {
82 Object value
= attrValues
.next();
83 if (value
instanceof byte[]) {
84 String encoded
= Base64
.getEncoder().encodeToString((byte[]) value
);
85 writer
.append(attribute
.getID()).append(":: ").append(encoded
).append('\n');
87 writer
.append(attribute
.getID()).append(": ").append(value
.toString()).append('\n');
92 protected void writeMemberAttribute(Attribute attribute
) throws NamingException
, IOException
{
93 // Note: duplicate entries will be swallowed
94 SortedSet
<String
> values
= new TreeSet
<>();
95 for (NamingEnumeration
<?
> attrValues
= attribute
.getAll(); attrValues
.hasMore();) {
96 String value
= attrValues
.next().toString();
100 for (String value
: values
) {
101 writer
.append(attribute
.getID()).append(": ").append(value
).append('\n');