/* * Copyright 2008-2010 UnboundID Corp. * All Rights Reserved. */ /* * Copyright (C) 2008-2010 UnboundID Corp. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * (GPLv2 only) or the terms of the GNU Lesser General Public License * (LGPLv2.1 only) as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see * . */ package com.unboundid.ldap.sdk.examples; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import com.unboundid.ldap.sdk.Entry; import com.unboundid.ldap.sdk.EntrySourceException; import com.unboundid.ldap.sdk.Filter; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPEntrySource; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.ResultCode; import com.unboundid.ldap.sdk.SearchRequest; import com.unboundid.ldap.sdk. SearchResultReferenceEntrySourceException; import com.unboundid.ldap.sdk.SearchScope; import com.unboundid.util.LDAPCommandLineTool; import com.unboundid.util.args.ArgumentException; import com.unboundid.util.args.ArgumentParser; import com.unboundid.util.args.DNArgument; import com.unboundid.util.args.FileArgument; import com.unboundid.util.args.StringArgument; /** * This class provides a simple tool that demonstrates how to perform * a search that uses the {@link LDAPEntrySource} class to iterate * through the results. */ public final class WriteAttrToFileUsingEntrySource extends LDAPCommandLineTool { // The argument to use to obtain the base DN for the search. private DNArgument baseDN; // The argument to use to specify the output file to write. private FileArgument outputFile; // The argument to use to obtain the name of the attribute to be // written. private StringArgument attributeName; /** * Creates and runs the tool using the provided set of command-line * arguments. * * @param args The command-line arguments provided to this * program. */ public static void main(final String[] args) { final WriteAttrToFileUsingListener tool = new WriteAttrToFileUsingListener(System.out, System.err); final ResultCode resultCode = tool.runTool(args); if (resultCode != ResultCode.SUCCESS) { System.exit(resultCode.intValue()); } } /** * Creates a new instance of this tool that will use the provided * streams for standard output and standard error. * * @param outStream The output stream to use for standard output, * or {@code null} if no standard output should * be generated. * @param errStream The output stream to use for standard error, * or {@code null} if no standard error should * be generated. */ public WriteAttrToFileUsingEntrySource( final OutputStream outStream, final OutputStream errStream) { super(outStream, errStream); } /** * Retrieves the name for this tool. * * @return The name for this tool. */ @Override() public String getToolName() { return "write-attr-to-file-using-entry-source"; } /** * Retrieves the description for this tool. * * @return The description for this tool. */ @Override() public String getToolDescription() { return "Perform a search to find all values for a specified " + "attribute and write them to a file. The LDAP entry " + "source class will be used to avoid the need to keep a " + "large result set in memory."; } /** * Adds the arguments needed by this command-line tool to the * provided argument parser which are not related to connecting or * authenticating to the directory server. * * @param parser The argument parser to which the arguments * should be added. * * @throws ArgumentException If a problem occurs while adding the * arguments. */ @Override() public void addNonLDAPArguments(final ArgumentParser parser) throws ArgumentException { baseDN = new DNArgument('b', "baseDN", true, 1, "{dn}", "Specifies the base DN that will be used for the search."); parser.addArgument(baseDN); attributeName = new StringArgument('A', "attribute", true, 1, "{attr}", "The name of the attribute to be retrieved."); parser.addArgument(attributeName); outputFile = new FileArgument('f', "outputFile", true, 1, "{path}", "The path to the output file to be written.", false, true, true, false); parser.addArgument(outputFile); } /** * Performs the appropriate processing for this tool. It will * obtain a connection to the directory server and perform the * search. * * @return A result code that indicates the result of the * processing. */ @Override() public ResultCode doToolProcessing() { // Open the output file for writing. final PrintWriter writer; try { writer = new PrintWriter(outputFile.getValue()); } catch (final IOException ioe) { err("Unable to open output file ", outputFile.getValue().getAbsolutePath(), " for writing: ", String.valueOf(ioe)); return ResultCode.LOCAL_ERROR; } // Get a connection to the directory server. final LDAPConnection conn; try { conn = getConnection(); } catch (final LDAPException le) { err("Unable to connect to the directory server: ", le.getExceptionMessage()); writer.close(); return le.getResultCode(); } // Create and process the search. try { final SearchRequest searchRequest = new SearchRequest( baseDN.getStringValue(), SearchScope.SUB, Filter.createPresenceFilter(attributeName.getValue()), attributeName.getValue()); final LDAPEntrySource entrySource = new LDAPEntrySource(conn, searchRequest, false); while (true) { try { final Entry entry = entrySource.nextEntry(); if (entry == null) { // There are no more entries to return. break; } // Look at the entry and write any appropriate values to // the output file. final String[] values = entry.getAttributeValues(attributeName.getValue()); if (values != null) { for (final String value : values) { writer.println(value); } } } catch (final SearchResultReferenceEntrySourceException e) { // We got a search result reference. Just print it out. out("Received a search reference: ", String.valueOf(e.getSearchReference())); } catch (final EntrySourceException e) { err("An error occurred during processing: ", e.getExceptionMessage()); if (! e.mayContinueReading()) { final Throwable cause = e.getCause(); if ((cause != null) && (cause instanceof LDAPException)) { return ((LDAPException) cause).getResultCode(); } else { return ResultCode.LOCAL_ERROR; } } } } return ResultCode.SUCCESS; } catch (final LDAPException le) { err("An error occurred while processing the search: ", le.getExceptionMessage()); return le.getResultCode(); } finally { // Close the connection to the directory server and the // output file. conn.close(); writer.close(); } } }