/**
         LDAP Search Sample
                         for Netscape Directory SDK 4.0

                 Using: ldapSearch host username password filter

**/
import netscape.ldap.*;
import java.util.*;

public class ldapSearch {
         public static void main(String[] args) {

                LDAPConnection ld = null;
                LDAPEntry findEntry = null;
                LDAPSearchResults res;

                try {
                        ld = new LDAPConnection(); // <--------------------- (2)

                        String host = args[0];  //
                        int port = 389;         // <------------------- (3)
                        String buser = args[1]; //
                        String bpass = args[2]; //

                        ld.connect( host, port ,buser ,bpass); // <-------------- (1)

                        String BaseDN = "o=ABC-Syouji, c=JP";           //
                        int scope = LDAPConnection.SCOPE_SUB;           // <----- (5)
                        String Filter = args[3];                        //
                        String Attlib[] = {"DN", "cn;lang-ja", "mail"}; //
                        boolean typesOnly = false;

                        res = ld.search(BaseDN,         // (a)
                                        scope,          // (b)
                                        Filter,         // (c) <--------------------- (4)
                                        Attlib,         // (d)
                                        typesOnly );    // (e)

                        while ( res.hasMoreElements() ) {       // <----------- (6)

                                try {
                                        findEntry = res.next();         // <---------------- (7)
                                } catch ( LDAPException e ) {
                                        System.out.println( ">>>Error: " + e.toString() );
                                        continue;
                                }

                                System.out.println("DN: " + findEntry.getDN() );  // <---------- (8)

                                LDAPAttributeSet findAttrs = findEntry.getAttributeSet();       //
                                Enumeration enumAttrs = findAttrs.getAttributes();              // <- (9)

                                while ( enumAttrs.hasMoreElements() ) { // <--------------- (10)

/*                              */      LDAPAttribute anAttr = (LDAPAttribute)enumAttrs.nextElement();
/*      (11) --------------->   */      String attrName = anAttr.getName();
/*                              */      System.out.print("\t" + attrName + ": ");

/*                              */      Enumeration enumVals = anAttr.getStringValues();
/*                              */      if (enumVals != null) {
/*      (12) --------------->   */              while ( enumVals.hasMoreElements() ) {
/*                              */                      String aVal = (String)enumVals.nextElement();
/*                              */                      System.out.println(aVal);
/*                              */             }
                                        }
                                }
                        }

                } catch( LDAPException e ) {
                        System.out.println( ">>>Error: " + e.toString() );
                }

                if ( (ld != null) && ld.isConnected() ) {
                        try {
                                ld.disconnect();                                    // <------- (13)
                        } catch ( LDAPException e ) {
                                System.out.println( ">>>Error: " + e.toString() );
                        }
                }

        }

}