// Create an LDIF writer which will write the search results to stdout. final LDIFEntryWriter writer = new LDIFEntryWriter(System.out); Connection connection = null; try { // Connect and bind to the server. final LDAPConnectionFactory factory = new LDAPConnectionFactory("localhost", 1389); connection = factory.getConnection(); connection.bind(userName, password); // Read the entries and output them as LDIF. final ConnectionEntryReader reader = connection.search(baseDN, scope, filter, attributes); while (reader.hasNext()) { if (reader.isEntry()) { // Got an entry. final SearchResultEntry entry = reader.readEntry(); writer.writeComment("Search result entry: " + entry.getName().toString()); writer.writeEntry(entry); } else { // Got a continuation reference. final SearchResultReference ref = reader.readReference(); writer.writeComment("Search result reference: " + ref.getURIs().toString()); } } writer.flush(); } catch (final Exception e) { // Handle exceptions... System.err.println(e.getMessage()); } finally { if (connection != null) { connection.close(); } } |