/* * 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.Filter; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.LDAPSearchException; import com.unboundid.ldap.sdk.ResultCode; import com.unboundid.ldap.sdk.SearchResult; import com.unboundid.ldap.sdk.SearchResultEntry; import com.unboundid.ldap.sdk.SearchResultListener; import com.unboundid.ldap.sdk.SearchResultReference; 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 SearchResultListener} interface to * write all of the values for a specified attribute to a file. */ public final class WriteAttrToFileUsingListener extends LDAPCommandLineTool implements SearchResultListener { // 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 writer that will be used to write to the output file. private PrintWriter writer; // 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 WriteAttrToFileUsingListener(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-listener"; } /** * 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 search result " + "listener interface 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. 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 the search request. This class will serve as the // search result listener. try { final SearchResult result = conn.search(this, baseDN.getStringValue(), SearchScope.SUB, Filter.createPresenceFilter(attributeName.getValue()), attributeName.getValue()); out("Search processing completed after returning ", result.getEntryCount(), " entries."); return ResultCode.SUCCESS; } catch (final LDAPSearchException lse) { err("An error occurred while processing the search: ", lse.getExceptionMessage()); return lse.getResultCode(); } finally { // Close the connection to the directory server and the // output file. conn.close(); writer.close(); } } /** * Indicates that the provided search result entry has been * returned by the server. If the entry has any values for the * target attribute, then they will be written to the output file. * * @param entry The entry that was returned. */ public void searchEntryReturned(final SearchResultEntry entry) { final String[] values = entry.getAttributeValues(attributeName.getValue()); if (values != null) { for (final String value : values) { writer.println(value); } } } /** * Indicates that the provided search result reference has been * returned by the server. We'll ignore this. * * @param ref The search result reference that was returned. */ public void searchReferenceReturned( final SearchResultReference ref) { // Just print out a message indicating that we got a referral. out("Received a search reference: ", String.valueOf(ref)); } }