Saturday, 10 November 2012

Example of  search code in LDAP


package com.demo.beans;

import com.novell.ldap.*;
import com.demo.services.LdapConnection;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;

public class SearchResult {
   
    String searchBase = "o=myOrg";
    int searchScope = LDAPConnection.SCOPE_SUB;
    String searchFilter = "(objectClass=inetOrgPerson)";
    private static LDAPConnection connection;
    private static LDAPConnection lc;
    private List<String[]> objectList = null;
    List objectList1 = new ArrayList();
   
    public static LDAPConnection getLc() {
        return lc;
    }
    public static void setLc(LDAPConnection lc) {
        SearchResult.lc = lc;
    }
   
    public static LDAPConnection getConnection() {
        return connection;
           
        }
   
    public List<String> serachData(){
        objectList1.clear();
        String str[] = new String[10];
        LdapConnection connection=new LdapConnection();
        objectList = new ArrayList<String[]>();
         try {
            
             lc=connection.getLDAPConnection();
             LDAPSearchResults searchResults = lc.search(searchBase,searchScope,searchFilter,null,false );
             while (searchResults.hasMore()) {
                    LDAPEntry nextEntry = null;
                    try {
                        nextEntry = searchResults.next();
                    } catch(LDAPException e ) {
                        System.out.println("Error: " + e.toString());
                        continue;
                    }
                    LDAPAttributeSet attributeSet1 = nextEntry.getAttributeSet();
                    Iterator allAttributes = attributeSet1.iterator();
                    while ( allAttributes.hasNext() ) {
                        LDAPAttribute attribute1 = (LDAPAttribute)allAttributes.next();
                        String attributeName = attribute1.getName();
                        Enumeration allValues = attribute1.getStringValues();
                        if ( allValues != null ) {
                            while ( allValues.hasMoreElements() ){
                                String value = (String)allValues.nextElement();
                                String strName1="cn";
                                String strName2="givenName";
                                String strName3="sn";
                                if(attributeName.equals(strName1)){
                                    objectList1.add(value);
                                    }
                                if(attributeName.equals(strName2)){
                                    objectList1.add(value);
                                   }
                                if(attributeName.equals(strName3)){
                                    objectList1.add(value);
                                  }
                            }
                        }
                        //objectList.add(str);
                    }
                }
             System.out.println("size of the object list::"+objectList1.size());
             lc.disconnect();
             }
         catch( LDAPException e ) {
             System.out.println( "Error:  " + e.toString());
             }
        
         for(Iterator iterator = objectList1.iterator();iterator.hasNext();){
                Object element = iterator.next();
                System.out.println("The elements are"+element);
        }
        
        return objectList1;
    }
   
    public List getObjectListToShow() {
        return objectList1;
    }
   
    public String showData(){
        for(Iterator iterator = objectList1.iterator();iterator.hasNext();){
            Object element = iterator.next();
            System.out.println("The elements are"+element);
        }
        return null;
    }

}

Thursday, 23 August 2012

What is LDAP?

The LDAP (Lightweight Directory Access Protocol) is an application protocol for accessing and maintaining distributed directory information services over an Internate Protocol (IP) network.
         Directory Services may provide any organized set of records, often with a hierarchical structure, such as a corporate mail directory. Similarly, a addressbook is a list of subscribers with an address and a phone number.

Default Port for LDAP Connection :

The default port for LDAP over SSL is 636 and over TCP is 389


LDAP connection through Java code:

LDAPConnection getNewLdapConnection(){


       int ldapPort = LDAPConnection.DEFAULT_PORT;

        String ldapHost       = "xxx.services.sytem";

        String loginDN        = "cn=yyy,ou=users,ou=services,o=sytem";


        String password       = "private";


    int ldapVersion  = LDAPConnection.LDAP_V3;

      


        LDAPConnection connection = new LDAPConnection();


        try {


           // connect to the server

            connection.connect( ldapHost, ldapPort );


           // authenticate to the server

            connection.bind( ldapVersion, loginDN, password.getBytes("UTF8") );

                   


        } catch(LDAPException e){}

return connection;

}