/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2008 Sun Microsystems, Inc.
* Portions copyright 2013 ForgeRock AS
*/
/**
* This class provides a program that may be used to search LDIF files. It is
* modeled after the LDAPSearch tool, with the primary differencing being that
* all of its data comes from LDIF rather than communicating over LDAP.
* However, it does have a number of differences that allow it to perform
* multiple operations in a single pass rather than requiring multiple passes
* through the LDIF.
*/
public class LDIFSearch
{
/**
* The fully-qualified name of this class.
*/
/**
* The search scope string that will be used for baseObject searches.
*/
/**
* The search scope string that will be used for singleLevel searches.
*/
/**
* The search scope string that will be used for wholeSubtree searches.
*/
/**
* The search scope string that will be used for subordinateSubtree searches.
*/
/**
* Provides the command line arguments to the <CODE>mainSearch</CODE> method
* so that they can be processed.
*
* @param args The command line arguments provided to this program.
*/
{
if (exitCode != 0)
{
}
}
/**
* Parses the provided command line arguments and performs the appropriate
* search operation.
*
* @param args The command line arguments provided to this
* program.
* @param initializeServer True if server initialization should be done.
* @param outStream The output stream to use for standard output, or
* {@code null} if standard output is not needed.
* @param errStream The output stream to use for standard error, or
* {@code null} if standard error is not needed.
*
* @return The return code for this operation. A value of zero indicates
* that all processing completed successfully. A nonzero value
* indicates that some problem occurred during processing.
*/
{
{
}
else
{
}
{
}
else
{
}
false, true, 0, 0,
"[filter] [attributes ...]");
try
{
ldifFile = new StringArgument(
"ldiffile", 'l', "ldifFile", false, true,
baseDNString = new StringArgument(
"basedn", OPTION_SHORT_BASEDN,
OPTION_LONG_BASEDN, false, true,
scopeString = new MultiChoiceArgument(
"scope", 's', "searchScope", false, false,
null, scopeStrings, false,
configFile = new StringArgument(
"configfile", 'c', "configFile", false,
configFile.setHidden(true);
OPTION_LONG_CONFIG_CLASS, false,
false, true, INFO_CONFIGCLASS_PLACEHOLDER.get(),
configClass.setHidden(true);
outputFile = new StringArgument(
"outputfile", 'o', "outputFile", false,
new BooleanArgument(
"overwriteexisting", 'O',"overwriteExisting",
dontWrap = new BooleanArgument(
"dontwrap", 'T', "dontWrap",
sizeLimit = new IntegerArgument(
"sizelimit", 'z', "sizeLimit", false,
true, 0, false, 0,
timeLimit = new IntegerArgument(
"timelimit", 't', "timeLimit", false,
true, 0, false, 0,
showUsage = new BooleanArgument(
"help", OPTION_SHORT_HELP,
}
catch (ArgumentException ae)
{
return 1;
}
// Parse the command-line arguments provided to the program.
try
{
}
catch (ArgumentException ae)
{
}
// If we should just display usage or version information,
// then print it and exit.
if (argParser.usageOrVersionDisplayed())
{
return 0;
}
// Checks the version - if upgrade required, the tool is unusable
try
{
}
catch (InitializationException e)
{
return 1;
}
// Make sure that at least one filter was provided. Also get the attribute
// list at the same time because it may need to be specified in the same
// way.
boolean allUserAttrs = false;
boolean allOperationalAttrs = false;
//Return objectclass attribute unless analysis of the arguments determines
//otherwise.
boolean includeObjectclassAttrs = true;
if (filterFile.isPresent())
{
try
{
{
{
// ignore empty lines.
continue;
}
}
} catch(Exception e)
{
return 1;
}
finally
{
{
try
{
} catch (IOException ioe) {}
}
}
{
}
else
{
{
{
allUserAttrs = true;
}
{
allOperationalAttrs = true;
}
{
}
else
{
}
}
}
}
else
{
{
return 1;
}
else
{
{
{
allUserAttrs = true;
}
{
allOperationalAttrs = true;
}
{
}
else
{
}
}
}
}
(! allOperationalAttrs))
{
// This will be true if no attributes were requested, which is effectively
// all user attributes. It will also be true if just "*" was included,
// but the net result will be the same.
allUserAttrs = true;
}
//Determine if objectclass attribute should be returned.
if(!allUserAttrs) {
//Single '+', never return objectclass.
includeObjectclassAttrs=false;
//If "objectclass" isn't specified in the attributes to return, then
//don't include objectclass attribiute.
includeObjectclassAttrs=false;
}
// Bootstrap the Directory Server configuration for use as a client.
// If we're to use the configuration then initialize it, along with the
// schema.
if(initializeServer) {
if (checkSchema)
{
try
{
}
catch (Exception e)
{
e.getMessage());
return 1;
}
try
{
configFile.getValue());
}
catch (Exception e)
{
e.getMessage());
return 1;
}
try
{
}
catch (Exception e)
{
e.getMessage());
return 1;
}
}
}
// Choose the desired search scope.
if (scopeString.isPresent())
{
{
}
{
}
{
}
else
{
}
}
else
{
}
// Create the list of filters that will be used to process the searches.
{
try
{
}
catch (Exception e)
{
filterString, e.getMessage());
return 1;
}
}
// Transform the attributes to return from strings to attribute types.
new LinkedHashSet<AttributeType>();
new LinkedHashSet<AttributeType>();
{
if (t.isOperational())
{
}
else
{
}
}
{
for (AttributeType t : c.getRequiredAttributeChain())
{
if (t.isOperational())
{
}
else
{
}
}
for (AttributeType t : c.getOptionalAttributeChain())
{
if (t.isOperational())
{
}
else
{
}
}
}
// Set the base DNs for the import config.
if (baseDNString.isPresent())
{
{
try
{
}
catch (Exception e)
{
dnString, e.getMessage());
return 1;
}
}
}
else
{
}
// Get the time limit in milliseconds.
long timeLimitMillis;
try
{
{
}
else
{
timeLimitMillis = 0;
}
}
catch (Exception e)
{
return 1;
}
// Convert the size limit to an integer.
int sizeLimitValue;
try
{
{
}
else
{
sizeLimitValue =0;
}
}
catch (Exception e)
{
return 1;
}
// Create the LDIF import configuration that will be used to read the source
// data.
{
}
else
{
}
// Create the LDIF export configuration that will be used to write the
// matching entries.
if (outputFile.isPresent())
{
if (overwriteExisting.isPresent())
{
}
else
{
}
}
else
{
}
{
}
else
{
}
try
{
}
catch (Exception e)
{
return 1;
}
try
{
}
catch (Exception e)
{
try
{
return 1;
}
// Start reading data from the LDIF reader.
long matchCount = 0;
while (true)
{
// If the time limit has been reached, then stop now.
{
break;
}
try
{
{
break;
}
// Check to see if the entry has an acceptable base and scope.
boolean matchesBaseAndScope = false;
{
{
matchesBaseAndScope = true;
break;
}
}
if (! matchesBaseAndScope)
{
continue;
}
// Check to see if the entry matches any of the filters.
boolean matchesFilter = false;
{
{
matchesFilter = true;
break;
}
}
if (! matchesFilter)
{
continue;
}
// Prepare the entry to return to the client.
if (! allUserAttrs)
{
{
{
}
}
}
if (! allOperationalAttrs)
{
{
{
}
}
}
// Write the entry to the client and increase the count.
// FIXME -- Should we include a comment about which base+filter matched?
matchCount++;
{
break;
}
}
catch (LDIFException le)
{
if (le.canContinueReading())
{
le.getMessage());
}
else
{
le.getMessage());
break;
}
}
catch (Exception e)
{
break;
}
}
// Close the reader and writer.
try
{
} catch (Exception e) {}
try
{
} catch (Exception e) {}
return resultCode;
}
}