How to get LdapAttributes
I am trying to export ActiveDirectory records to LDIF files using Spring.
I find a lot of information about parsing LDIF files, but relatively little about exporting to LDIF. With Spring there is a class LdapAttributes
, the method toString()
returns a string in LDIF format, but I can't see where to get the instance LdapAttributes
in the first place. I don't see anything on LdapTemplate
.
Hopefully the structure provides an easy way to get this, instead of having to build the object myself LdapAttributes
.
source to share
Check out something like unboundid LDAP SDK https://www.unboundid.com/products/ldap-sdk/docs/javadoc/com/unboundid/ldif/package-summary.html
source to share
Hm, I came up with this:
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapAttributes;
public class PersonMapper implements AttributesMapper {
@Override
public Object mapFromAttributes(Attributes attrs) throws NamingException {
String dnValue = (String) attrs.get("distinguishedName").get();
DistinguishedName dn = new DistinguishedName(dnValue);
LdapAttributes ldapAttrs = new LdapAttributes(dn);
for (NamingEnumeration<? extends Attribute> ne = attrs.getAll(); ne.hasMore(); ) {
ldapAttrs.put(ne.next());
}
return ldapAttrs;
}
}
I can't help but feel that there must be another ready-made way to do this, although it works.
source to share
LdapAttributes ldapAttributes = basic2LdapAttributes(result.getNameInNamespace(), result.getAttributes());
public static LdapAttributes basic2LdapAttributes(String distinguishedName, Attributes attributes) throws NamingException{
LdapAttributes ldapAttributes = new LdapAttributes();
ldapAttributes.setName(LdapUtils.newLdapName(distinguishedName));
for (NamingEnumeration<? extends Attribute> nameEnumeration = attributes.getAll(); nameEnumeration.hasMore();) {
ldapAttributes.put(nameEnumeration.next());
}
return ldapAttributes;
}
source to share