Example usage for javax.naming.directory Attribute getAll

List of usage examples for javax.naming.directory Attribute getAll

Introduction

In this page you can find the example usage for javax.naming.directory Attribute getAll.

Prototype

NamingEnumeration<?> getAll() throws NamingException;

Source Link

Document

Retrieves an enumeration of the attribute's values.

Usage

From source file:AttributeExample.java

public static void main(String[] argc) throws Exception {
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
    DirContext dctx = new InitialDirContext(env);
    Attributes attrs = new BasicAttributes(true);
    attrs.put(new BasicAttribute("email"));
    attrs.put(new BasicAttribute("website", "www.pri.com"));

    NamingEnumeration result = dctx.search("ou=People", attrs);

    while (result.hasMore()) {
        SearchResult sr = (SearchResult) result.next();
        System.out.println("Result = " + sr.getName());
        Attributes srchAttrs = sr.getAttributes();

        NamingEnumeration attributes = srchAttrs.getAll();

        while (attributes.hasMore()) {
            Attribute attr = (Attribute) attributes.next();
            System.out.println("Attribute: " + attr.getID());
            NamingEnumeration values = attr.getAll();
            while (values.hasMore()) {
                Object value = values.next();
                System.out.println("Value = " + value);
            }/*from  www  . j ava 2  s . c om*/
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    env.put("java.naming.provider.url", "dns://123.123.123.123/");

    String dns_attributes[] = { "MX", "A", "HINFO" };

    DirContext ctx = new InitialDirContext(env);
    Attributes attrsl = ctx.getAttributes("http://www.yourserver.com", dns_attributes);
    for (int z = 0; z < dns_attributes.length; z++) {
        Attribute attr = attrsl.get(dns_attributes[z]);

        if (attr != null) {
            System.out.print(dns_attributes[z] + ": ");
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                System.out.println(vals.nextElement());
            }//from  ww w.j ava2s .  c  o m
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
    env.put(Context.PROVIDER_URL, MY_HOST);
    DirContext ctx = new InitialDirContext(env);
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);
    while (results != null && results.hasMore()) {
        SearchResult sr = (SearchResult) results.next();
        String dn = sr.getName() + ", " + MY_SEARCHBASE;

        System.out.println("Distinguished Name is " + dn);
        Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
        if (ar == null) {
            System.out.println("Entry " + dn + " has none of the specified attributes\n");
            return;
        }//from  w  w w.j a  v  a  2s .  com
        for (int i = 0; i < MY_ATTRS.length; i++) {
            Attribute attr = ar.get(MY_ATTRS[i]);
            if (attr == null) {
                continue;
            }
            System.out.println(MY_ATTRS[i] + ":");
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                System.out.println("\t" + vals.nextElement());

            }
        }
    }
}

From source file:org.jsecurity.realm.ldap.LdapUtils.java

/**
 * Helper method used to retrieve all attribute values from a particular context attribute.
 *
 * @param attr the LDAP attribute.//from   w ww.  j  av a2s  . com
 * @return the values of the attribute.
 * @throws javax.naming.NamingException if there is an LDAP error while reading the values.
 */
public static Collection<String> getAllAttributeValues(Attribute attr) throws NamingException {
    Set<String> values = new HashSet<String>();
    for (NamingEnumeration e = attr.getAll(); e.hasMore();) {
        String value = (String) e.next();
        values.add(value);
    }
    return values;
}

From source file:ModAttrs.java

static void printAttrs(Attributes attrs) {
    if (attrs == null) {
        System.out.println("No attributes");
    } else {//  ww  w .  j  ava2 s . c  o  m
        /* Print each attribute */
        try {
            for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
                Attribute attr = (Attribute) ae.next();
                System.out.println("attribute: " + attr.getID());

                /* print each value */
                for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out.println("value: " + e.next()))
                    ;
            }
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java

public static void collectMemberOf(Attributes attrs, Collection<ADGroup> groups) throws NamingException {
    Attribute tga = attrs.get("memberOf");
    if (tga == null)
        return;/*from w  ww  .  j a v  a  2s. co  m*/
    NamingEnumeration<?> membersOf = tga.getAll();
    while (membersOf.hasMore()) {
        Object memberObject = membersOf.next();
        groups.add(new ADGroup(memberObject.toString()));
    }
    membersOf.close();
}

From source file:org.swordess.ldap.util.AttrUtils.java

/**
 * Get values from the given <tt>attr</tt>.
 * //from   www  .ja v  a2s.  c  om
 * @param attr
 * @return
 * @throws NamingException
 *             If a naming exception was encountered while retrieving the value.
 */
public static List<Object> values(Attribute attr) throws NamingException {
    List<Object> values = new ArrayList<Object>();
    for (NamingEnumeration<?> all = attr.getAll(); all.hasMore();) {
        values.add(all.next());
    }
    return values;
}

From source file:org.projectforge.business.ldap.LdapUtils.java

public static String[] getAttributeStringValues(final Attributes attributes, final String attrId)
        throws NamingException {
    final Attribute attr = attributes.get(attrId);
    if (attr == null) {
        return null;
    }/*w w  w  .  j  a v a2s.c  o m*/
    final NamingEnumeration<?> enumeration = attr.getAll();
    final List<String> list = new ArrayList<String>();
    while (enumeration.hasMore() == true) {
        final Object attrValue = enumeration.next();
        if (attrValue == null) {
            list.add(null);
        }
        list.add(String.valueOf(attrValue));
    }
    return list.toArray(new String[list.size()]);
}

From source file:org.ballerinalang.stdlib.ldap.nativeimpl.GetLdapScopesOfUser.java

private static List<String> getListOfNames(List<String> searchBases, String searchFilter,
        SearchControls searchCtls, String property, DirContext ldapConnectionContext) throws NamingException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Result for searchBase: " + searchBases + " searchFilter: " + searchFilter + " property:"
                + property + " appendDN: false");
    }/*  www  .  j a  v a2s . co  m*/

    List<String> names = new ArrayList<>();
    NamingEnumeration<SearchResult> answer = null;
    try {
        // handle multiple search bases
        for (String searchBase : searchBases) {
            answer = ldapConnectionContext.search(LdapUtils.escapeDNForSearch(searchBase), searchFilter,
                    searchCtls);
            while (answer.hasMoreElements()) {
                SearchResult searchResult = answer.next();
                if (searchResult.getAttributes() == null) {
                    continue;
                }
                Attribute attr = searchResult.getAttributes().get(property);
                if (attr == null) {
                    continue;
                }
                for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                    String name = (String) vals.nextElement();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Found user: " + name);
                    }
                    names.add(name);
                }
            }

            if (LOG.isDebugEnabled()) {
                for (String name : names) {
                    LOG.debug("Result  :  " + name);
                }
            }
        }
    } finally {
        LdapUtils.closeNamingEnumeration(answer);
    }
    return names;
}

From source file:net.spfbl.core.Reverse.java

public static boolean hasValidNameServers(String hostname) throws NamingException {
    if ((hostname = Domain.normalizeHostname(hostname, false)) == null) {
        return false;
    } else {/*  ww w.j  av  a  2 s  . c o m*/
        try {
            Attributes attributesNS = Server.getAttributesDNS(hostname, new String[] { "NS" });
            if (attributesNS != null) {
                Enumeration enumerationNS = attributesNS.getAll();
                while (enumerationNS.hasMoreElements()) {
                    Attribute attributeNS = (Attribute) enumerationNS.nextElement();
                    NamingEnumeration enumeration = attributeNS.getAll();
                    while (enumeration.hasMoreElements()) {
                        String ns = (String) enumeration.next();
                        if (Domain.isHostname(ns)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        } catch (NameNotFoundException ex) {
            return false;
        }
    }
}