Wednesday, 25 May 2016

Search for an entry in LDAP using JNDI

Let me explain basic format of LDAP search query.

= (EQUAL TO)
By using = operator, you can get the entries, if an attribute is matched to given value.

Ex: "sn=Kumaran": Return all the entries, where sn is equal to Kumaran.

& (logical AND)
& is used to select all the entries, that satisifies all the conditions. For example, following statement finds all the entries whose sn=Kumar and description=Software Engineer.

(&(sn=Kumaran)(description=Software Engineer))

! (logical NOT)
Exclude objects that have a certain attribute.

(!(sn=Kumaran))
Above statement return all entries, whose sn is not equal to Kumaran.

* (wildcard)
sn=*
Above statement gets all the persons, who has attribute sn.

| (Logical OR)
Logical OR is used to get all entries that satisfy any of the condition.

Following search query return all the persons, whose sn is Kumaran (or) Gurram.

(|((sn=Kumaran) (sn=Gurram)))
Following is my complete working application.
import java.util.Objects;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class DirectoryUtil {
 private final static String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
 private final static String PROVIDER_URL = "ldap://localhost:10389";
 private static DirContext context = null;

 public static DirContext getContext() throws NamingException {
  if (!Objects.isNull(context)) {
   return context;
  }

  Properties properties = new Properties();
  properties.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  properties.put(Context.PROVIDER_URL, PROVIDER_URL);
  DirContext context = new InitialDirContext(properties);
  return context;
 }
}

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class Test {
 private static DirContext context;

 public static void printResults(NamingEnumeration<SearchResult> results)
   throws NamingException {
  while (results.hasMoreElements()) {
   SearchResult res = results.next();
   Attributes atbs = res.getAttributes();
   Attribute cnAttr = atbs.get("cn");
   String cn = (String) cnAttr.get();

   Attribute snAttr = atbs.get("sn");
   String sn = (String) snAttr.get();

   Attribute descriptionAtr = atbs.get("description");
   String description = (String) descriptionAtr.get();

   System.out.println("cn :" + cn + " sn : " + sn
     + " description is :" + description);
  }
 }

 /* Get all people whose sn=Kumaran */
 public static void getPeopleSnIsKumaran() throws NamingException {
  SearchControls searcCon = new SearchControls();
  searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
  NamingEnumeration<SearchResult> results = context.search(
    "ou=people,dc=example,dc=com", "sn=Kumaran", searcCon);
  printResults(results);
 }

 /* Get all people whose sn=Kumaran, cn=Krishna */
 public static void getPeopleSnIsKumaranCnIsKrishna() throws NamingException {
  SearchControls searcCon = new SearchControls();
  searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
  NamingEnumeration<SearchResult> results = context.search(
    "dc=example,dc=com",
    "(&(sn=Kumaran)(description=Software Engineer))", searcCon);
  printResults(results);
 }

 /* Get all people whose sn is not equal to Kumaran */
 public static void getAllExcludeKumaran() throws NamingException {
  SearchControls searcCon = new SearchControls();
  searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
  NamingEnumeration<SearchResult> results = context.search(
    "ou=people,dc=example,dc=com", "(!(sn=Kumaran))", searcCon);
  printResults(results);
 }

 /* Get all persons in ou=people,dc=example,dc=com */
 public static void getAllPersons() throws NamingException {
  SearchControls searcCon = new SearchControls();
  searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
  NamingEnumeration<SearchResult> results = context.search(
    "ou=people,dc=example,dc=com", "sn=*", searcCon);
  printResults(results);
 }

 /* Get all persons whose sn is Gurram (or) Kumaran */
 public static void getAllPersonSnIsGuuramOrKumaran() throws NamingException {
  SearchControls searcCon = new SearchControls();
  searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
  NamingEnumeration<SearchResult> results = context.search(
    "ou=people,dc=example,dc=com", "(|((sn=Kumaran) (sn=Gurram)))",
    searcCon);
  printResults(results);
 }

 public static void main(String[] args) throws NamingException {
  context = DirectoryUtil.getContext();

  System.out.println("All people in ou=people,dc=example,dc=com");
  getAllPersons();
  System.out.println("------------------------------");

  System.out.println("------------------------------");
  System.out.println("All people where sn=Kumaran is");
  getPeopleSnIsKumaran();
  System.out.println("------------------------------");

  System.out
    .println("All people where sn=Kumaran and description=Software Engineer");
  getPeopleSnIsKumaranCnIsKrishna();
  System.out.println("------------------------------");

  System.out
    .println("All people in ou=people,dc=example,dc=com, whose sn is Kumaran or Gurram");
  getAllPersonSnIsGuuramOrKumaran();
  System.out.println("------------------------------");
 }
}


Output
All people in ou=people,dc=example,dc=com
cn :Senthil sn : Kumaran description is :Tech Lead
cn :Hari sn : Gurram description is :Software professional, code for Company XYZ
cn :Krishna sn : Kumaran description is :Software Engineer
cn :Sailu sn : Navakotla description is :Director
------------------------------
------------------------------
All people where sn=Kumaran is
cn :Senthil sn : Kumaran description is :Tech Lead
cn :Krishna sn : Kumaran description is :Software Engineer
------------------------------
All people where sn=Kumaran and description=Software Engineer
cn :Krishna sn : Kumaran description is :Software Engineer
------------------------------
All people in ou=people,dc=example,dc=com, whose sn is Kumaran or Gurram
cn :Senthil sn : Kumaran description is :Tech Lead
cn :Hari sn : Gurram description is :Software professional, code for Company XYZ
cn :Krishna sn : Kumaran description is :Software Engineer
------------------------------




Previous                                                 Next                                                 Home

No comments:

Post a Comment