4

I'm trying to export ActiveDirectory records into LDIF-formatted files using Spring.

I'm finding lots of information about parsing LDIF files, but relatively little about exporting to LDIF. With Spring there is an LdapAttributes class whose toString() method returns a string in LDIF format, but I don't see where to get the LdapAttributes instance in the first place. I don't see anything on the LdapTemplate.

Hopefully the framework provides a simple way to get this, rather than my having to build the LdapAttributes object myself.

3 Answers3

4

Check out something like the unboundid LDAP SDK https://www.unboundid.com/products/ldap-sdk/docs/javadoc/com/unboundid/ldif/package-summary.html

jwilleke
  • 10,467
  • 1
  • 30
  • 51
0

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 some more out-of-the-box way to do this, though the above works.

0
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;
}
Mike S
  • 41,895
  • 11
  • 89
  • 84