java 如何从 javax.naming.directory.Attribute 中提取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12162898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:51:10  来源:igfitidea点击:

How to extract value from javax.naming.directory.Attribute

javaldapjndi

提问by Tapas Bose

The question says everything. When I am printing an Attribute it is:

问题说明了一切。当我打印一个属性时,它是:

cn: WF-008-DAM-PS

The code snippet is:

代码片段是:

private void searchGroup() throws NamingException {
    NamingEnumeration<SearchResult> searchResults = getLdapDirContext().search(groupDN, "(objectclass=groupOfUniqueNames)", getSearchControls());
    String searchGroupCn = getCNForBrand(m_binder.getLocal("brandId"), m_binder.getLocal("brandName"));
    Log.info(searchGroupCn);
    while (searchResults.hasMore()) {
        SearchResult searchResult = searchResults.next();
        Attributes attributes = searchResult.getAttributes();
        Attribute groupCn = attributes.get("cn");
        if(groupCn != null) {
            Log.info(groupCn.toString());               
        }
    }
}

How can I only get the value that is: WF-008-DAM-PS, that is without the key portion? Regards.

我怎样才能只得到值:WF-008-DAM-PS,没有关键部分?问候。

采纳答案by Terry Gardner

Invoke the getValue()method or the getValue(int)method.

调用getValue()方法或getValue(int)方法。

回答by Tapas Bose

The solution is:

解决办法是:

Attribute groupCn = attributes.get("cn");
String value = groupCn.get();

回答by ROMANIA_engineer

General

一般的

Let's say that we have:

假设我们有:

Attributes attributes;
Attribute a = attributes.get("something");
  • if(a.size() == 1)
    • then you can use a.get()or a.get(0)to get the unique value
  • if(a.size() > 1)

    • iterate through all the values:

      for ( int i = 0 ; i < a.size() ; i++ ) {
          Object currentVal = a.get(i);
          // do something with currentVal
      }
      

      If you use a.get()here, it will return only the first value, because its internal implementation (in BasicAttribute) looks like this:

      public Object get() throws NamingException {
          if (values.size() == 0) {
              throw new NoSuchElementException("Attribute " + getID() + " has no value");
          } else {
              return values.elementAt(0);
          }
      }
      
  • if(a.size() == 1)
    • 然后您可以使用a.get()a.get(0)获取唯一值
  • if(a.size() > 1)

    • 遍历所有值:

      for ( int i = 0 ; i < a.size() ; i++ ) {
          Object currentVal = a.get(i);
          // do something with currentVal
      }
      

      如果你a.get()在这里使用,它只会返回第一个值,因为它的内部实现(in BasicAttribute)看起来像这样:

      public Object get() throws NamingException {
          if (values.size() == 0) {
              throw new NoSuchElementException("Attribute " + getID() + " has no value");
          } else {
              return values.elementAt(0);
          }
      }
      

Both methods (get(int)and get()) throws a NamingException.

两种方法(get(int)get())都会抛出一个NamingException.

Practical example
(when the Attributeinstance has multiple values)

实际示例
(当Attribute实例有多个值时)

LdapContext ctx = new InitialLdapContext(env, null);

Attributes attributes = ctx.getAttributes("", new String[] { "supportedSASLMechanisms" });
System.out.println(attributes); // {supportedsaslmechanisms=supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5}

Attribute a = atts.get("supportedsaslmechanisms");
System.out.println(a); // supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5

System.out.println(a.get()); // GSSAPI

for (int i = 0; i < a.size(); i++) {
    System.out.print(a.get(i) + " "); // GSSAPI EXTERNAL DIGEST-MD5
}

回答by Hpalle

This worked:(checking attribute present or not before fetching attribute value)

这有效:(在获取属性值之前检查属性是否存在)

            Attributes attributes = searchResult.getAttributes();
            Attribute mail = attributes.get("mail");
            if (mail != null)
               {
                   System.out.println(" Mail-id value from LDAP :"+mail.get());
               }