简体   繁体   English

Java NamingEnumeration-从NamingEnumeration中获取单个元素

[英]Java NamingEnumeration - Getting a single element out of a NamingEnumeration

I am trying to fetch some users from Active Directory group and update them in another one of our site. 我正在尝试从Active Directory组中获取一些用户,并在我们网站的另一个站点中对其进行更新。 The task is almost done except for the part where I need to fetch the user ID from NamingEnumeration and pass it onto another method which will update it through a REST API call. 除了需要从NamingEnumeration提取用户ID并将其传递到另一个将通过REST API调用更新它的方法之外,该任务几乎已完成。 Below is a part of the code where I am fetching users from AD group: 以下是我从AD组获取用户的代码的一部分:

DirContext myContext = new InitialDirContext(envVars);

    SearchControls searchCtrls = new SearchControls();
    searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    String[] attributes = { "cn", "member"};
    searchCtrls.setReturningAttributes(attributes);

    String filter = "(&(objectClass=group)(cn=GROUP_NAME))";
    NamingEnumeration values = myContext.search("DC=XXXX,DC=XXXX",filter,searchCtrls);

    while (values.hasMoreElements())
    {
        SearchResult result = (SearchResult) values.next();
        Attributes attribs = result.getAttributes();
        if (null != attribs)
        {
            for (NamingEnumeration ae = attribs.getAll(); ae.hasMoreElements();)
            {
                Attribute atr = (Attribute) ae.next();
                String attributeID = atr.getID();
                for (
                    Enumeration vals = atr.getAll(); 
                    vals.hasMoreElements(); 
                    System.out.println(attributeID+": "+vals.nextElement())                     
                );
            }
        }
    }

When I run this, the output is something like below: 当我运行此命令时,输出如下所示:

member: CN=USERNAME,OU=XXX,OU=XXX,OU=XXX,DC=XXX,DC=XXX,DC=XXX 成员:CN = USERNAME,OU = XXX,OU = XXX,OU = XXX,DC = XXX,DC = XXX,DC = XXX
member: CN=USERNAME,OU=XXX,OU=XXX,OU=XXX,DC=XXX,DC=XXX,DC=XXX 成员:CN = USERNAME,OU = XXX,OU = XXX,OU = XXX,DC = XXX,DC = XXX,DC = XXX

Basically, I need to fetch this CN ie USERNAME alone, which I will pass onto another method. 基本上,我需要单独获取此CN,即USERNAME,我将其传递给另一种方法。

I did try to get them in a String array and process them, to no avail and I am running short of time. 我确实尝试过将它们放入String数组并进行处理,但无济于事,而且我的时间很短。 Any ideas would be greatly appreciated. 任何想法将不胜感激。 Thanks. 谢谢。

Well, I did get around this problem. 好吧,我确实解决了这个问题。 Below approach worked for me. 下面的方法对我有用。

if (null != attribs)
        {
            for (NamingEnumeration ae = attribs.getAll(); ae.hasMoreElements();)
            {

                Attribute atr = (Attribute) ae.next();
                for (
                    Enumeration vals = atr.getAll(); 
                    vals.hasMoreElements(); 
                ) {
                        list.add(vals.nextElement().toString());
                }
            }
        }

I let the output inside a list and clipped the unwanted parts to obtain the CN alone. 我将输出放在列表中,并剪切掉不需要的部分以单独获得CN。 Would be glad if this helps someone in the future. 如果将来能对某人有所帮助,我们将非常高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM