Example usage for javax.naming.directory SearchResult getName

List of usage examples for javax.naming.directory SearchResult getName

Introduction

In this page you can find the example usage for javax.naming.directory SearchResult getName.

Prototype

public String getName() 

Source Link

Document

Retrieves the name of this binding.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    String filter = "(&(sn=YourName)(mail=*))";

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    NamingEnumeration e = ctx.search("", filter, ctls);

    while (e.hasMore()) {
        SearchResult entry = (SearchResult) e.next();
        System.out.println(entry.getName());
    }//  ww w.j a  v a2  s .  co  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] attrIDs = { "sn", "number", "value", "mail" };

    Attributes matchAttrs = new BasicAttributes(true);
    matchAttrs.put(new BasicAttribute("sn", "YourName"));
    matchAttrs.put(new BasicAttribute("mail"));

    Object obj = "yourObject";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    NamingEnumeration e = ctx.search("ou=People", matchAttrs, attrIDs);

    while (e.hasMore()) {
        SearchResult entry = (SearchResult) e.next();
        System.out.println(entry.getName());
    }/*from   w  ww  . ja  va 2 s  .  c om*/
}

From source file:FilterExample.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);

    String filter = "(&(cn=E*)(account>1005))";

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

    while (result.hasMore()) {
        SearchResult sr = (SearchResult) result.next();
        System.out.println("Result = " + sr.getName());
    }//from  w w  w . jav  a  2s . co  m

}

From source file:SearchControlsExample.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);

    String filter = "(&(cn=S*)(account>1005))";

    String[] attrIDs = { "cn", "email" };
    SearchControls sc = new SearchControls();
    sc.setReturningAttributes(attrIDs);/* w  w  w  .j a va 2 s  .  c  om*/

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

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

}

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 ww  .j ava 2 s. c o  m
        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:SearchScopeExample.java

public static void main(String[] argc) {
    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");

    try {// ww  w.  j av a 2 s.  c  om
        DirContext dctx = new InitialDirContext(env);

        String filter = "(&(cn=S*)(account>1000))";

        String[] attrIDs = { "cn", "email" };
        SearchControls sc = new SearchControls();
        sc.setReturningAttributes(attrIDs);
        sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

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

        while (result.hasMore()) {
            SearchResult sr = (SearchResult) result.next();
            System.out.println("Result = " + sr.getName());
        }
    } catch (NamingException e) {
        System.out.println(e);
    }
}

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);
            }// w  ww  .  j a v  a  2s .com
        }
    }
}

From source file:SortedResults.java

public static void main(String[] args) throws IOException {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=People,o=JNDITutorial");

    try {//from   w w w  . jav  a  2 s  .c om
        // Create initial context with no connection request controls
        LdapContext ctx = new InitialLdapContext(env, null);

        // Create a sort control that sorts based on CN
        String sortKey = "cn";
        ctx.setRequestControls(new Control[] { new SortControl(sortKey, Control.CRITICAL) });

        // Perform a search
        NamingEnumeration results = ctx.search("", "(objectclass=*)", new SearchControls());

        // Iterate over search results
        System.out.println("---->sort by cn");
        while (results != null && results.hasMore()) {
            // Display an entry
            SearchResult entry = (SearchResult) results.next();
            System.out.println(entry.getName());

            // Handle the entry's response controls (if any)
            if (entry instanceof HasControls) {
                // ((HasControls)entry).getControls();
            }
        }
        // Examine the sort control response
        Control[] controls = ctx.getResponseControls();
        if (controls != null) {
            for (int i = 0; i < controls.length; i++) {
                if (controls[i] instanceof SortResponseControl) {
                    SortResponseControl src = (SortResponseControl) controls[i];
                    if (!src.isSorted()) {
                        throw src.getException();
                    }
                } else {
                    // Handle other response controls (if any)
                }
            }
        }

        // Close when no longer needed
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:PagedSearch.java

public static void main(String[] args) {

    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    /* Specify host and port to use for directory service */
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=People,o=JNDITutorial");

    try {//w w  w  .  j  a v a 2  s.c om
        LdapContext ctx = new InitialLdapContext(env, null);

        // Activate paged results
        int pageSize = 5;
        byte[] cookie = null;
        ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
        int total;

        do {
            /* perform the search */
            NamingEnumeration results = ctx.search("", "(objectclass=*)", new SearchControls());

            /* for each entry print out name + all attrs and values */
            while (results != null && results.hasMore()) {
                SearchResult entry = (SearchResult) results.next();
                System.out.println(entry.getName());
            }

            // Examine the paged results control response
            Control[] controls = ctx.getResponseControls();
            if (controls != null) {
                for (int i = 0; i < controls.length; i++) {
                    if (controls[i] instanceof PagedResultsResponseControl) {
                        PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                        total = prrc.getResultSize();
                        if (total != 0) {
                            System.out.println("(total : " + total);
                        } else {
                            System.out.println("(total: unknown)");
                        }
                        cookie = prrc.getCookie();
                    }
                }
            } else {
                System.out.println("No controls were sent from the server");
            }
            ctx.setRequestControls(
                    new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });

        } while (cookie != null);

        ctx.close();

    } catch (NamingException e) {
        System.err.println("PagedSearch failed.");
        e.printStackTrace();
    } catch (IOException ie) {
        System.err.println("PagedSearch failed.");
        ie.printStackTrace();
    }
}

From source file:PagedSearch.java

public static void main(String[] args) {

    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    /* Specify host and port to use for directory service */
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=People,o=JNDITutorial");

    try {/*from w  ww  . j a  v  a 2s . c om*/
        LdapContext ctx = new InitialLdapContext(env, null);

        // Activate paged results
        int pageSize = 5;
        byte[] cookie = null;
        ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
        int total;

        do {
            /* perform the search */
            NamingEnumeration results = ctx.search("", "(objectclass=*)", new SearchControls());

            /* for each entry print out name + all attrs and values */
            while (results != null && results.hasMore()) {
                SearchResult entry = (SearchResult) results.next();
                System.out.println(entry.getName());
            }

            // Examine the paged results control response
            Control[] controls = ctx.getResponseControls();
            if (controls != null) {
                for (int i = 0; i < controls.length; i++) {
                    if (controls[i] instanceof PagedResultsResponseControl) {
                        PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                        total = prrc.getResultSize();
                        if (total != 0) {
                            System.out.println("***************** END-OF-PAGE " + "(total : " + total
                                    + ") *****************\n");
                        } else {
                            System.out.println(
                                    "***************** END-OF-PAGE " + "(total: unknown) ***************\n");
                        }
                        cookie = prrc.getCookie();
                    }
                }
            } else {
                System.out.println("No controls were sent from the server");
            }
            // Re-activate paged results
            ctx.setRequestControls(
                    new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });

        } while (cookie != null);

        ctx.close();

    } catch (NamingException e) {
        System.err.println("PagedSearch failed.");
        e.printStackTrace();
    } catch (IOException ie) {
        System.err.println("PagedSearch failed.");
        ie.printStackTrace();
    }
}