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;
    }

}

No comments:

Post a Comment