Category Archives: LDAP

Sorting and filtering LDAP data


Lightweight Directory Access Protocol is a mapping between the names of resources in a network and their respective network addresses. It is a shared information infrastructure for locating, managing, administering and organizing everyday items and network resources, which can include volumes, folders, files, printers, users, groups, devices, telephone numbers and other objects. A directory service is a critical component of a network operating system. A directory server or name server is a server which provides such a service. Each resource on the network is considered an object by the directory server. Information about a particular resource is stored as a collection of attributes associated with that resource or a directory content and update requests. LDIF conveys directory content as a set of records, one record for each object (or entry). It also represents update requests, such as Add, Modify, Delete, and Rename, as a set of records, one record for each update request. The code below shows how to access and list Data from an LDAP server:

try {
    LdapContext ctx = new InitialLdapContext(env, null);

    // Create critical Sort that sorts based on CN
    Control[] ctxCtls = new Control[]{
        new SortControl(new String[]{"cn"}, Control.CRITICAL)

    // Sets context request controls; 
    // effect until unset

    // Perform list() with controls in effect
    NamingEnumeration answer = ctx.list("");

    // Enumerate answers
    while (answer.hasMore()) {
        NameClassPair item = (NameClassPair);
} catch (NamingException e) {