Package com.unboundid.ldap.sdk.examples
Class LDAPSearch
- java.lang.Object
-
- com.unboundid.util.CommandLineTool
-
- com.unboundid.util.LDAPCommandLineTool
-
- com.unboundid.ldap.sdk.examples.LDAPSearch
-
- All Implemented Interfaces:
SearchResultListener
,java.io.Serializable
@ThreadSafety(level=NOT_THREADSAFE) public final class LDAPSearch extends LDAPCommandLineTool implements SearchResultListener
This class provides a simple tool that can be used to search an LDAP directory server. Some of the APIs demonstrated by this example include:- Argument Parsing (from the
com.unboundid.util.args
package) - LDAP Command-Line Tool (from the
com.unboundid.util
package) - LDAP Communication (from the
com.unboundid.ldap.sdk
package)
All of the necessary information is provided using command line arguments. Supported arguments include those allowed by theLDAPCommandLineTool
class, as well as the following additional arguments:- "-b {baseDN}" or "--baseDN {baseDN}" -- specifies the base DN to use for the search. This must be provided.
- "-s {scope}" or "--scope {scope}" -- specifies the scope to use for the search. The scope value should be one of "base", "one", "sub", or "subord". If this isn't specified, then a scope of "sub" will be used.
- "-R" or "--followReferrals" -- indicates that the tool should follow any referrals encountered while searching.
- "-t" or "--terse" -- indicates that the tool should generate minimal output beyond the search results.
- "-i {millis}" or "--repeatIntervalMillis {millis}" -- indicates that the search should be periodically repeated with the specified delay (in milliseconds) between requests.
- "-n {count}" or "--numSearches {count}" -- specifies the total number of times that the search should be performed. This may only be used in conjunction with the "--repeatIntervalMillis" argument. If "--repeatIntervalMillis" is used without "--numSearches", then the searches will continue to be repeated until the tool is interrupted.
- "--bindControl {control}" -- specifies a control that should be included in the bind request sent by this tool before performing any search operations.
- "-J {control}" or "--control {control}" -- specifies a control that should be included in the search request(s) sent by this tool.
Note that this class implements the SearchResultListener interface, which will be notified whenever a search result entry or reference is returned from the server. Whenever an entry is received, it will simply be printed displayed in LDIF.- See Also:
LDAPSearch
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description LDAPSearch(java.io.OutputStream outStream, java.io.OutputStream errStream)
Creates a new instance of this tool.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addNonLDAPArguments(ArgumentParser parser)
Adds the arguments used by this program that aren't already provided by the genericLDAPCommandLineTool
framework.boolean
defaultsToInteractiveMode()
Indicates whether this tool defaults to launching in interactive mode if the tool is invoked without any command-line arguments.protected boolean
defaultToPromptForBindPassword()
Indicates whether this tool should default to interactively prompting for the bind password if a password is required but no argument was provided to indicate how to get the password.void
doExtendedNonLDAPArgumentValidation()
Performs any necessary processing that should be done to ensure that the provided set of command-line arguments were valid.ResultCode
doToolProcessing()
Performs the actual processing for this tool.protected java.util.List<Control>
getBindControls()
Retrieves a set of controls that should be included in any bind request generated by this tool.java.util.LinkedHashMap<java.lang.String[],java.lang.String>
getExampleUsages()
Retrieves a set of information that may be used to generate example usage information.int
getMaxTrailingArguments()
Retrieves the maximum number of unnamed trailing arguments that are allowed.int
getMinTrailingArguments()
Retrieves the minimum number of unnamed trailing arguments that are required.java.lang.String
getToolDescription()
Retrieves the description for this tool.java.lang.String
getToolName()
Retrieves the name for this tool.java.lang.String
getToolVersion()
Retrieves the version string for this tool.java.lang.String
getTrailingArgumentsPlaceholder()
Retrieves a placeholder string that may be used to indicate what kinds of trailing arguments are allowed.protected boolean
includeAlternateLongIdentifiers()
Indicates whether the LDAP-specific arguments should include alternate versions of all long identifiers that consist of multiple words so that they are available in both camelCase and dash-separated versions.static void
main(java.lang.String[] args)
Parse the provided command line arguments and make the appropriate set of changes.static ResultCode
main(java.lang.String[] args, java.io.OutputStream outStream, java.io.OutputStream errStream)
Parse the provided command line arguments and make the appropriate set of changes.void
searchEntryReturned(SearchResultEntry entry)
Indicates that the provided search result entry was returned from the associated search operation.void
searchReferenceReturned(SearchResultReference reference)
Indicates that the provided search result reference was returned from the associated search operation.protected boolean
supportsDebugLogging()
Indicates whether this tool supports the ability to generate a debug log file.boolean
supportsInteractiveMode()
Indicates whether this tool should provide support for an interactive mode, in which the tool offers a mode in which the arguments can be provided in a text-driven menu rather than requiring them to be given on the command line.protected boolean
supportsOutputFile()
Indicates whether this tool should provide arguments for redirecting output to a file.boolean
supportsPropertiesFile()
Indicates whether this tool supports the use of a properties file for specifying default values for arguments that aren't specified on the command line.protected boolean
supportsSSLDebugging()
Indicates whether this tool should provide a command-line argument that allows for low-level SSL debugging.-
Methods inherited from class com.unboundid.util.LDAPCommandLineTool
addToolArguments, anyLDAPArgumentsProvided, createBindRequest, createServerSet, createSSLUtil, createSSLUtil, doExtendedArgumentValidation, getConnection, getConnectionOptions, getConnectionOptionsWithRequestedSettings, getConnectionPool, getConnectionPool, getSuppressedShortIdentifiers, getUnauthenticatedConnection, supportsAuthentication, supportsMultipleServers, supportsSASLHelp
-
Methods inherited from class com.unboundid.util.CommandLineTool
addEnableSSLDebuggingArgument, createArgumentParser, doShutdownHookProcessing, err, getAdditionalDescriptionParagraphs, getErr, getOriginalErr, getOriginalOut, getOut, getPasswordFileReader, getToolCompletionMessage, logToolInvocationByDefault, out, registerShutdownHook, requestToolArgumentsInteractively, runTool, wrapErr, wrapOut
-
-
-
-
Constructor Detail
-
LDAPSearch
public LDAPSearch(@Nullable java.io.OutputStream outStream, @Nullable java.io.OutputStream errStream)
Creates a new instance of this tool.- Parameters:
outStream
- The output stream to which standard out should be written. It may benull
if output should be suppressed.errStream
- The output stream to which standard error should be written. It may benull
if error messages should be suppressed.
-
-
Method Detail
-
main
public static void main(@NotNull java.lang.String[] args)
Parse the provided command line arguments and make the appropriate set of changes.- Parameters:
args
- The command line arguments provided to this program.
-
main
@NotNull public static ResultCode main(@NotNull java.lang.String[] args, @Nullable java.io.OutputStream outStream, @Nullable java.io.OutputStream errStream)
Parse the provided command line arguments and make the appropriate set of changes.- Parameters:
args
- The command line arguments provided to this program.outStream
- The output stream to which standard out should be written. It may benull
if output should be suppressed.errStream
- The output stream to which standard error should be written. It may benull
if error messages should be suppressed.- Returns:
- A result code indicating whether the processing was successful.
-
getToolName
@NotNull public java.lang.String getToolName()
Retrieves the name for this tool.- Specified by:
getToolName
in classCommandLineTool
- Returns:
- The name for this tool.
-
getToolDescription
@NotNull public java.lang.String getToolDescription()
Retrieves the description for this tool.- Specified by:
getToolDescription
in classCommandLineTool
- Returns:
- The description for this tool.
-
getToolVersion
@NotNull public java.lang.String getToolVersion()
Retrieves the version string for this tool.- Overrides:
getToolVersion
in classCommandLineTool
- Returns:
- The version string for this tool.
-
getMinTrailingArguments
public int getMinTrailingArguments()
Retrieves the minimum number of unnamed trailing arguments that are required.- Overrides:
getMinTrailingArguments
in classCommandLineTool
- Returns:
- One, to indicate that at least one trailing argument (representing the search filter) must be provided.
-
getMaxTrailingArguments
public int getMaxTrailingArguments()
Retrieves the maximum number of unnamed trailing arguments that are allowed.- Overrides:
getMaxTrailingArguments
in classCommandLineTool
- Returns:
- A negative value to indicate that any number of trailing arguments may be provided.
-
getTrailingArgumentsPlaceholder
@NotNull public java.lang.String getTrailingArgumentsPlaceholder()
Retrieves a placeholder string that may be used to indicate what kinds of trailing arguments are allowed.- Overrides:
getTrailingArgumentsPlaceholder
in classCommandLineTool
- Returns:
- A placeholder string that may be used to indicate what kinds of trailing arguments are allowed.
-
supportsInteractiveMode
public boolean supportsInteractiveMode()
Indicates whether this tool should provide support for an interactive mode, in which the tool offers a mode in which the arguments can be provided in a text-driven menu rather than requiring them to be given on the command line. If interactive mode is supported, it may be invoked using the "--interactive" argument. Alternately, if interactive mode is supported anddefaultsToInteractiveMode()
returnstrue
, then interactive mode may be invoked by simply launching the tool without any arguments.- Overrides:
supportsInteractiveMode
in classCommandLineTool
- Returns:
true
if this tool supports interactive mode, orfalse
if not.
-
defaultsToInteractiveMode
public boolean defaultsToInteractiveMode()
Indicates whether this tool defaults to launching in interactive mode if the tool is invoked without any command-line arguments. This will only be used ifsupportsInteractiveMode()
returnstrue
.- Overrides:
defaultsToInteractiveMode
in classCommandLineTool
- Returns:
true
if this tool defaults to using interactive mode if launched without any command-line arguments, orfalse
if not.
-
supportsOutputFile
protected boolean supportsOutputFile()
Indicates whether this tool should provide arguments for redirecting output to a file. If this method returnstrue
, then the tool will offer an "--outputFile" argument that will specify the path to a file to which all standard output and standard error content will be written, and it will also offer a "--teeToStandardOut" argument that can only be used if the "--outputFile" argument is present and will cause all output to be written to both the specified output file and to standard output.- Overrides:
supportsOutputFile
in classCommandLineTool
- Returns:
true
if this tool should provide arguments for redirecting output to a file, orfalse
if not.
-
supportsPropertiesFile
public boolean supportsPropertiesFile()
Indicates whether this tool supports the use of a properties file for specifying default values for arguments that aren't specified on the command line.- Overrides:
supportsPropertiesFile
in classCommandLineTool
- Returns:
true
if this tool supports the use of a properties file for specifying default values for arguments that aren't specified on the command line, orfalse
if not.
-
supportsDebugLogging
protected boolean supportsDebugLogging()
Indicates whether this tool supports the ability to generate a debug log file. If this method returnstrue
, then the tool will expose additional arguments that can control debug logging.- Overrides:
supportsDebugLogging
in classCommandLineTool
- Returns:
true
if this tool supports the ability to generate a debug log file, orfalse
if not.
-
defaultToPromptForBindPassword
protected boolean defaultToPromptForBindPassword()
Indicates whether this tool should default to interactively prompting for the bind password if a password is required but no argument was provided to indicate how to get the password.- Overrides:
defaultToPromptForBindPassword
in classLDAPCommandLineTool
- Returns:
true
if this tool should default to interactively prompting for the bind password, orfalse
if not.
-
includeAlternateLongIdentifiers
protected boolean includeAlternateLongIdentifiers()
Indicates whether the LDAP-specific arguments should include alternate versions of all long identifiers that consist of multiple words so that they are available in both camelCase and dash-separated versions.- Overrides:
includeAlternateLongIdentifiers
in classLDAPCommandLineTool
- Returns:
true
if this tool should provide multiple versions of long identifiers for LDAP-specific arguments, orfalse
if not.
-
supportsSSLDebugging
protected boolean supportsSSLDebugging()
Indicates whether this tool should provide a command-line argument that allows for low-level SSL debugging. If this returnstrue
, then an "--enableSSLDebugging}" argument will be added that sets the "javax.net.debug" system property to "all" before attempting any communication.- Overrides:
supportsSSLDebugging
in classLDAPCommandLineTool
- Returns:
true
if this tool should offer an "--enableSSLDebugging" argument, orfalse
if not.
-
addNonLDAPArguments
public void addNonLDAPArguments(@NotNull ArgumentParser parser) throws ArgumentException
Adds the arguments used by this program that aren't already provided by the genericLDAPCommandLineTool
framework.- Specified by:
addNonLDAPArguments
in classLDAPCommandLineTool
- Parameters:
parser
- The argument parser to which the arguments should be added.- Throws:
ArgumentException
- If a problem occurs while adding the arguments.
-
doExtendedNonLDAPArgumentValidation
public void doExtendedNonLDAPArgumentValidation() throws ArgumentException
Performs any necessary processing that should be done to ensure that the provided set of command-line arguments were valid. This method will be called after the basic argument parsing has been performed and after all LDAP-specific argument validation has been processed, and immediately before theCommandLineTool.doToolProcessing()
method is invoked.- Overrides:
doExtendedNonLDAPArgumentValidation
in classLDAPCommandLineTool
- Throws:
ArgumentException
- If there was a problem with the command-line arguments provided to this program.
-
getBindControls
@NotNull protected java.util.List<Control> getBindControls()
Retrieves a set of controls that should be included in any bind request generated by this tool.- Overrides:
getBindControls
in classLDAPCommandLineTool
- Returns:
- A set of controls that should be included in any bind request
generated by this tool. It may be
null
or empty if no controls should be included in the bind request.
-
doToolProcessing
@NotNull public ResultCode doToolProcessing()
Performs the actual processing for this tool. In this case, it gets a connection to the directory server and uses it to perform the requested search.- Specified by:
doToolProcessing
in classCommandLineTool
- Returns:
- The result code for the processing that was performed.
-
searchEntryReturned
public void searchEntryReturned(@NotNull SearchResultEntry entry)
Indicates that the provided search result entry was returned from the associated search operation.- Specified by:
searchEntryReturned
in interfaceSearchResultListener
- Parameters:
entry
- The entry that was returned from the search.
-
searchReferenceReturned
public void searchReferenceReturned(@NotNull SearchResultReference reference)
Indicates that the provided search result reference was returned from the associated search operation.- Specified by:
searchReferenceReturned
in interfaceSearchResultListener
- Parameters:
reference
- The reference that was returned from the search.
-
getExampleUsages
@NotNull public java.util.LinkedHashMap<java.lang.String[],java.lang.String> getExampleUsages()
Retrieves a set of information that may be used to generate example usage information. Each element in the returned map should consist of a map between an example set of arguments and a string that describes the behavior of the tool when invoked with that set of arguments.- Overrides:
getExampleUsages
in classCommandLineTool
- Returns:
- A set of information that may be used to generate example usage
information. It may be
null
or empty if no example usage information is available.
-
-