2013-02-13 45 views
3

我想使用Spring将ActiveDirectory记录导出为LDIF格式的文件。我如何获得LdapAttributes

我找到很多关于解析 LDIF文件的信息,但是对于输出到LDIF文件却相对较少。在Spring中,有一个LdapAttributes类,它的toString()方法返回一个LDIF格式的字符串,但我没有看到从哪里得到LdapAttributes实例。我在LdapTemplate上看不到任何东西。

希望框架提供了一个简单的方法来获得这个,而不是我必须自己构建LdapAttributes对象。

回答

0

嗯,我想出了这个:

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; 
    } 
} 

我不禁觉得必须有一些多出来的最现成的方法来做到这一点,虽然上述作品。

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; 
}