/* * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * Perform a LDAP Search using VLVControl * Fetch all entries from all pages * This is to simulate the Simple Page Control when used * all the entries are returned with a page size. * VLV Control is more powerful than the Simple Paged Control * * * * @author Srikanth Konjarla */ import java.util.*; import netscape.ldap.*; import netscape.ldap.util.*; import netscape.ldap.controls.*; public class VLVFetchAll { public static void main( String[] args ) { String ldapHost = "localhost"; Integer ldapPort = 389; String BindDN = ""; String password = ""; String searchBase = ""; String scope = "LDAPv3.SCOPE_ONE"; String filter = ""; String sortAttr = "sn"; String[] attrs = null; Integer pageSize = 2000; // Read Command line arguments String usage = "Usage: java VLVFetchAll [-h] [-p] [-b] " + "[-D ] [-w ] [-S] [-s] [-P] [-f] \n" + "-h - LDAP Server host. Default: localhost \n" + "-p - LDAP Server port. Default: 389 \n" + "-b - Base DN for search. Default: rootDSE \n" + "-D - Bind DN. Default: anonymous\n" + "-w - Bind Password. Defualt: blank\n" + "-s - Scope fo the search. Default: one\n" + " Allowed values: base,one,sub\n" + "-S - Attribute(s) to sort. Default: sn\n" + "-f - Search filter. Mandatory option\n" + "-A - Space delimited list of attributes. Default: all attributes\n" + "-P - VLV pages size. Default: 2000\n" + "Example:\n" + "java -jar VLVFetchAll -h sundir.example.com -b \"ou=people,dc=example,dc=com\" " + "-D \"uid=admin,ou=poeple,dc=example,dc=com\" -w password -f \"(objectclass=posixAccount)\"" + "-S \"cn uid\" -P 1000\n" + "In the above example all the entries are fetched that match the filter (objectClass=posixAccount)\n" + "Please note that the VLV configuration should already exist. If the Directory Server is configured for\n" + "LDAP naming services for Unix then the above example would work without any issues\n"; GetOpt options = new GetOpt( "h:p:D:w:b:P:S:s:f:A:H", args ); // Get the arguments specified for each option. // Print the usage message if -H option is specified if ( (options.getOptionParam('f') == null) || options.hasOption( 'H' ) ) { System.out.println( usage ); System.exit( 1 ); } // Read the -h option to determing the LDAP host // if -h option is not used then default to localhost if (options.getOptionParam( 'h' ) != null ) { ldapHost = options.getOptionParam('h'); } // Read the -p option to set the port. Default: 389 // If a port number was specified, convert the port value // to an integer. if ( options.getOptionParam( 'p' ) != null ) { try { ldapPort = java.lang.Integer.parseInt( options.getOptionParam( 'p' ) ); } catch ( java.lang.Exception e ) { System.out.println( "Invalid port number: " + options.getOptionParam( 'p' ) ); System.out.println( usage ); System.exit( 1 ); } } // Get the Bind DN. Read -D switch. Default to anonymous if (options.getOptionParam( 'D' ) != null ) { BindDN = options.getOptionParam('D'); } // Get the Bind Password. Default none. if (options.getOptionParam( 'w' ) != null ) { password = options.getOptionParam('w'); } if (BindDN.equals("")) { password = ""; } // Get the Base DN if (options.getOptionParam( 'b' ) != null ) { searchBase = options.getOptionParam('b'); } // Get the page size. Default 2000 if ( options.getOptionParam( 'P' ) != null ) { try { pageSize = java.lang.Integer.parseInt( options.getOptionParam( 'P' ) ); } catch ( java.lang.Exception e ) { System.out.println( "Invalid Page Size: " + options.getOptionParam( 'P' ) ); System.out.println( usage ); System.exit( 1 ); } } // Get sort attribute. Default: sn if (options.getOptionParam( 'S' ) != null ) { sortAttr = options.getOptionParam('S'); } // Get the search scope: Default: LDAPv3.SCOPE_ONE if (options.getOptionParam( 's' ) != null ) { scope = options.getOptionParam('s'); if (scope.equalsIgnoreCase("one")) { scope = "LDAPv3.SCOPE_ONE"; } if (scope.equalsIgnoreCase("base")) { scope = "LDAPv3.SCOPE_BASE"; } if (scope.equalsIgnoreCase("sub")) { scope = "LDAPv3.SCOPE_SUB"; } } // Get the search filter filter = options.getOptionParam('f'); // Read the list of attributes to return if (options.getOptionParam('A') != null){ // Break the string into individual attributes // add them to attr array to pass on to the search method String Attributes = options.getOptionParam('A'); // Create a arrayList. Easy to manage. ArrayList al = new ArrayList(); StringTokenizer st = new StringTokenizer(Attributes); while (st.hasMoreElements()){ al.add(st.nextToken()); } // Convert the arrayList into array attrs = (String[]) al.toArray (new String[al.size()]); } int ldapVersion = 3; // VLV Index. Element to offset to int index = 1; // Number of entries before the index int before = 0; // Number of entries after the index int after = pageSize -1; int count = 0; // Perform a LDAP Connection LDAPConnection ldapConn = null; try { ldapConn = new LDAPConnection(); ldapConn.connect( ldapHost, ldapPort ); } catch(Exception e) { System.err.println("Error: client connection failed!"); System.exit(0); } /* perform an LDAP bind operation */ try { ldapConn.authenticate( ldapVersion, BindDN, password ); } catch (Exception e) { System.err.println( e.toString() ); System.exit(0); } LDAPSearchResults res = dosearch(attrs,sortAttr,index,before,after,count,ldapConn,searchBase,filter,scope); //Print Results printResults(res); // Get the total number of entries LDAPVirtualListResponse nextCont = null; LDAPControl[] resControl = res.getResponseControls(); if (resControl != null) { for ( int i = 0; i < resControl.length; i++ ) { if ( resControl[i] instanceof LDAPVirtualListResponse ) { nextCont = (LDAPVirtualListResponse)resControl[i]; break; } } } if ( nextCont != null ) { // Now we know the total size of the virtual list box count = nextCont.getContentCount(); //System.out.println("Total Number of Entries: "+count); } else { //System.out.println( "Null response control" ); } // Close the writer index = pageSize + 1; while (count >= index ) { res = dosearch(attrs,sortAttr,index,before,after,count,ldapConn,searchBase,filter,scope); printResults(res); index = pageSize + index; } /* Be Kind, Unbind */ try { ldapConn.disconnect(); } catch (Exception e) { System.err.println( e.toString() ); } System.exit(0); } private static LDAPSearchResults dosearch(String[] attrs,String sortAttr,int index,int before,int after,int count, LDAPConnection ldapConn,String searchBase,String filter,String scope) { LDAPVirtualListControl vlvControl = new LDAPVirtualListControl(index,before,after,count); LDAPSortKey sortOrder = new LDAPSortKey( sortAttr ); LDAPSortControl vlvSort = new LDAPSortControl(sortOrder,true); LDAPControl[] controls = new LDAPControl[2]; controls[0] = vlvSort; controls[1] = vlvControl; /* perform an LDAP Search Operation */ LDAPSearchResults res = null; try { LDAPSearchConstraints cons = ldapConn.getSearchConstraints(); cons.setMaxResults( 0 ); cons.setServerControls(controls); res = ldapConn.search(searchBase,LDAPv3.SCOPE_ONE,filter,attrs,false, cons); } catch (Exception e) { System.err.println( e.toString() ); System.exit(1); } //printResults(res,pw); //pw.flush(); //pw.close(); return res; } private static void printResults( LDAPSearchResults res) { /* print out the retrieved entries */ try { /* Loop on results until finished */ while ( res.hasMoreElements() ) { LDAPEntry entry = null; try { /* Next directory entry */ entry = (LDAPEntry)res.next(); } catch (LDAPReferralException ee) { LDAPUrl[] urls= ee.getURLs(); System.err.println("Referral entries: "); for (int i=0; i