[英]Extract data from NamingEnumeration
My application searches an LDAP server for people. 我的应用程序在LDAP服务器中搜索人员。
return ldapTemplate.search("", "(objectclass=person)", new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
return attrs.get("cn").getAll();
}
});
It returns list of NamingEnumeration
object, which contains vectors in it. 它返回NamingEnumeration
对象的列表,其中包含向量。 Each vector may contain one or more values. 每个向量可以包含一个或多个值。 I can print person names by this code 我可以通过此代码打印人名
for(NamingEnumeration ne : list){
while (ne.hasMore()) {
System.out.println("name is : " + ne.next().toString());
}
}
As my ldap search can contain mutiple values so that comes in vector inside NamingEnumeration
object. 由于我的ldap搜索可以包含多个值,因此它位于NamingEnumeration
对象内的向量中。 How can I get multiple values out of it. 如何从中获取多个值。
As you are using a java.util.List
of javax.naming.NamingEnumeration<java.util.Vector>
such as this, 当您使用javax.naming.NamingEnumeration<java.util.Vector>
的java.util.List
,如此
List<NamingEnumeration<Vector>> list
You should be able to iterate over the Vector
in each NamingEnumeration
: 您应该能够在每个NamingEnumeration
迭代Vector
:
for (NamingEnumeration<Vector> ne : list) {
while (ne.hasMore()) {
Vector vector = ne.next();
for (Object object : vector) {
System.out.println(object);
}
}
}
Note that Vector
is considered by many to be obsolescent, although not deprecated. 请注意,许多人认为Vector
已过时,但并未弃用。 Also, the enclosed collection could use a type parameter. 此外,封闭的集合可以使用类型参数。 If you have a choice, consider one of these alternatives: 如果您有选择,请考虑以下替代方案之一:
List<NamingEnumeration<Vector<T>>>
List<NamingEnumeration<List<T>>>
While iterating a list using the for
syntax introduced with Java5 使用Java5引入的for
语法迭代列表时
You shouldn't call hasMore()
你不应该调用hasMore()
for(NamingEnumeration ne : list){
System.out.println("name is : " + ne.toString());
}
In case your list does not support the Iterator interface you need to use the old form: 如果您的列表不支持Iterator接口,则需要使用旧表单:
for ( Enumeration e = v.elements() ; e.hasMoreElements() ; ) {
String a = (String) e.nextElement();
System.out.println( a );
}
This code snippet will do the work for unknown attribute names and single or multiple values (such as multiple object classes): 此代码段将处理未知属性名称以及单个或多个值(例如多个对象类):
Using spring-ldap 2.3.1 and AttributesMapper implementation: 使用spring-ldap 2.3.1和AttributesMapper实现:
<!-- https://mvnrepository.com/artifact/org.springframework.ldap/spring-ldap-core -->
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
In this sample code the searchResultList contains all entries, each one is represented as a map of attributes (with one or more values): 在此示例代码中,searchResultList包含所有条目,每个条目都表示为属性映射(具有一个或多个值):
List<Map<String, List<String>>> searchResultList = sourceLdapTemplate.search(searchBase, filter.encode(), SearchControls.ONELEVEL_SCOPE, new AttributesMapper<Map<String, List<String>>>() {
@Override
public Map<String, List<String>> mapFromAttributes(Attributes attributes) throws NamingException {
Map<String, List<String>> attrsMap = new HashMap<>();
NamingEnumeration<String> attrIdEnum = attributes.getIDs();
while (attrIdEnum.hasMoreElements()) {
// Get attribute id:
String attrId = attrIdEnum.next();
// Get all attribute values:
Attribute attr = attributes.get(attrId);
NamingEnumeration<?> attrValuesEnum = attr.getAll();
while (attrValuesEnum.hasMore()) {
if (!attrsMap.containsKey(attrId))
attrsMap.put(attrId, new ArrayList<String>());
attrsMap.get(attrId).add(attrValuesEnum.next().toString());
}
}
return attrsMap;
}
});
Now, working with the searchResultList looks like this: 现在,使用searchResultList看起来像这样:
for (Map<String, List<String>> attrsMap : searchResultList) {
for (String objectClass : m.get("objectClass")) {
// do something with this objectClass...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.