LDIFSearch.java revision b8c6b80da1cb6118167a934daa480eb381c59e0e
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * CDDL HEADER START
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * The contents of this file are subject to the terms of the
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Common Development and Distribution License, Version 1.0 only
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * (the "License"). You may not use this file except in compliance
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * with the License.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * or http://forgerock.org/license/CDDLv1.0.html.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * See the License for the specific language governing permissions
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * and limitations under the License.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * When distributing Covered Code, include this CDDL HEADER in each
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * file and include the License file at legal-notices/CDDLv1_0.txt.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * If applicable, add the following below this CDDL HEADER, with the
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * fields enclosed by brackets "[]" replaced with your own identifying
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * information:
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Portions Copyright [yyyy] [name of copyright owner]
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * CDDL HEADER END
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Copyright 2006-2008 Sun Microsystems, Inc.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Portions Copyright 2013-2015 ForgeRock AS.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport static com.forgerock.opendj.cli.ArgumentConstants.*;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport static org.opends.messages.ToolMessages.*;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport static org.opends.server.util.StaticUtils.*;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport org.opends.server.core.DirectoryServer.DirectoryServerVersionHandler;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport org.opends.server.extensions.ConfigFileHandler;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport org.opends.server.protocols.ldap.LDAPResultCode;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * This class provides a program that may be used to search LDIF files. It is
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * modeled after the LDAPSearch tool, with the primary differencing being that
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * all of its data comes from LDIF rather than communicating over LDAP.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * However, it does have a number of differences that allow it to perform
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * multiple operations in a single pass rather than requiring multiple passes
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * through the LDIF.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /** The fully-qualified name of this class. */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final String CLASS_NAME = "org.opends.server.tools.LDIFSearch";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /** The search scope string that will be used for baseObject searches. */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final String SCOPE_STRING_BASE = "base";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /** The search scope string that will be used for singleLevel searches. */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final String SCOPE_STRING_ONE = "one";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /** The search scope string that will be used for wholeSubtree searches. */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final String SCOPE_STRING_SUB = "sub";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /** The search scope string that will be used for subordinateSubtree searches. */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final String SCOPE_STRING_SUBORDINATE = "subordinate";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Provides the command line arguments to the <CODE>mainSearch</CODE> method
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * so that they can be processed.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @param args The command line arguments provided to this program.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int exitCode = mainSearch(args, true, System.out, System.err);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Parses the provided command line arguments and performs the appropriate
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * search operation.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @param args The command line arguments provided to this
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @param initializeServer True if server initialization should be done.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @param outStream The output stream to use for standard output, or
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * {@code null} if standard output is not needed.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @param errStream The output stream to use for standard error, or
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * {@code null} if standard error is not needed.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @return The return code for this operation. A value of zero indicates
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * that all processing completed successfully. A nonzero value
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * indicates that some problem occurred during processing.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static int mainSearch(String[] args, boolean initializeServer,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk PrintStream out = NullOutputStream.wrapOrNullStream(outStream);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk PrintStream err = NullOutputStream.wrapOrNullStream(errStream);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LinkedHashSet<String> scopeStrings = new LinkedHashSet<>(4);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage toolDescription = INFO_LDIFSEARCH_TOOL_DESCRIPTION.get();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk ArgumentParser argParser = new ArgumentParser(CLASS_NAME, toolDescription,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk "[filter] [attributes ...]");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk argParser.setShortToolDescription(REF_SHORT_DESC_LDIFSEARCH.get());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk argParser.setVersionHandler(new DirectoryServerVersionHandler());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk true, INFO_LDIFFILE_PLACEHOLDER.get(), null, null,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk true, INFO_SCOPE_PLACEHOLDER.get(), SCOPE_STRING_SUB,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk false, true, INFO_CONFIGFILE_PLACEHOLDER.get(), null, null,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk configClass = new StringArgument("configclass", OPTION_SHORT_CONFIG_CLASS,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk false, true, INFO_CONFIGCLASS_PLACEHOLDER.get(),
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk filterFile = new StringArgument("filterfile", 'f', "filterFile", false,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk false, true, INFO_FILTER_FILE_PLACEHOLDER.get(), null, null,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk false, true, INFO_OUTPUT_FILE_PLACEHOLDER.get(), null, null,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk INFO_LDIFSEARCH_DESCRIPTION_OVERWRITE_EXISTING.get());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk false, true, INFO_SIZE_LIMIT_PLACEHOLDER.get(), 0, null,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk false, true, INFO_TIME_LIMIT_PLACEHOLDER.get(), 0, null,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Parse the command-line arguments provided to the program.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // If we should just display usage or version information,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // then print it and exit.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Checks the version - if upgrade required, the tool is unusable
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Make sure that at least one filter was provided. Also get the attribute
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // list at the same time because it may need to be specified in the same
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk boolean allUserAttrs = false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk boolean allOperationalAttrs = false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk //Return objectclass attribute unless analysis of the arguments determines
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk //otherwise.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk final LinkedList<String> attributeNames = new LinkedList<>();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LinkedList<String> objectClassNames = new LinkedList<>();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LinkedList<String> filterStrings = new LinkedList<>();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk in = new BufferedReader(new FileReader(fileNameValue));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // ignore empty lines.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk ArrayList<String> trailingArguments = argParser.getTrailingArguments();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (trailingArguments != null && !trailingArguments.isEmpty())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk ArrayList<String> trailingArguments = argParser.getTrailingArguments();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ((trailingArguments == null) || trailingArguments.isEmpty())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = ERR_LDIFSEARCH_NO_FILTER.get();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk Iterator<String> iterator = trailingArguments.iterator();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk String lowerName = toLowerCase(iterator.next());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // This will be true if no attributes were requested, which is effectively
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // all user attributes. It will also be true if just "*" was included,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // but the net result will be the same.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk //Determine if objectclass attribute should be returned.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk //Single '+', never return objectclass.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if(allOperationalAttrs && objectClassNames.isEmpty() &&
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk //If "objectclass" isn't specified in the attributes to return, then
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk //don't include objectclass attribute.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if(!attributeNames.isEmpty() && objectClassNames.isEmpty() &&
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Bootstrap the Directory Server configuration for use as a client.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk DirectoryServer directoryServer = DirectoryServer.getInstance();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // If we're to use the configuration then initialize it, along with the
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_CANNOT_INITIALIZE_JMX.get(configFile.getValue(), e.getMessage()));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk directoryServer.initializeConfiguration(configClass.getValue(),
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_CANNOT_INITIALIZE_CONFIG.get(configFile.getValue(), e.getMessage()));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_CANNOT_INITIALIZE_SCHEMA.get(configFile.getValue(), e.getMessage()));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Choose the desired search scope.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk String scopeStr = toLowerCase(scopeString.getValue());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk else if (scopeStr.equals(SCOPE_STRING_SUBORDINATE))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Create the list of filters that will be used to process the searches.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LinkedList<SearchFilter> searchFilters = new LinkedList<>();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk searchFilters.add(SearchFilter.createFilterFromString(filterString));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = ERR_LDIFSEARCH_CANNOT_PARSE_FILTER.get(
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Transform the attributes to return from strings to attribute types.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LinkedHashSet<AttributeType> userAttributeTypes = new LinkedHashSet<>();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LinkedHashSet<AttributeType> operationalAttributeTypes = new LinkedHashSet<>();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk AttributeType t = DirectoryServer.getAttributeType(attributeName, true);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk ObjectClass c = DirectoryServer.getObjectClass(objectClassName, true);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (AttributeType t : c.getRequiredAttributeChain())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (AttributeType t : c.getOptionalAttributeChain())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Set the base DNs for the import config.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (String dnString : baseDNString.getValues())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = ERR_LDIFSEARCH_CANNOT_PARSE_BASE_DN.get(
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Get the time limit in milliseconds.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk timeLimitMillis = 1000L * timeLimit.getIntValue();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_CANNOT_PARSE_TIME_LIMIT.get(e));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Convert the size limit to an integer.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_CANNOT_PARSE_SIZE_LIMIT.get(e));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Create the LDIF import configuration that will be used to read the source
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk importConfig = new LDIFImportConfig(ldifFile.getValues());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Create the LDIF export configuration that will be used to write the
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // matching entries.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk exportConfig = new LDIFExportConfig(outputFile.getValue(),
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk exportConfig = new LDIFExportConfig(outputFile.getValue(),
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk exportConfig.setIncludeObjectClasses(includeObjectclassAttrs);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Create the LDIF reader/writer from the import/export config.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_CANNOT_CREATE_READER.get(e));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_CANNOT_CREATE_WRITER.get(e));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Start reading data from the LDIF reader.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk while (true)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // If the time limit has been reached, then stop now.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ((timeLimitMillis > 0) && (System.currentTimeMillis() > stopTime))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk resultCode = LDAPResultCode.TIME_LIMIT_EXCEEDED;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = WARN_LDIFSEARCH_TIME_LIMIT_EXCEEDED.get();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Check to see if the entry has an acceptable base and scope.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk boolean matchesBaseAndScope = false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (entry.matchesBaseAndScope(baseDN, searchScope))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Check to see if the entry matches any of the filters.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk boolean matchesFilter = false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Prepare the entry to return to the client.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (! userAttributeTypes.contains(iterator.next()))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk entry.getOperationalAttributes().keySet().iterator();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (! operationalAttributeTypes.contains(iterator.next()))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Write the entry to the client and increase the count.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // FIXME -- Should we include a comment about which base+filter matched?
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ((sizeLimitValue > 0) && (matchCount >= sizeLimitValue))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk resultCode = LDAPResultCode.SIZE_LIMIT_EXCEEDED;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = WARN_LDIFSEARCH_SIZE_LIMIT_EXCEEDED.get();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = ERR_LDIFSEARCH_CANNOT_READ_ENTRY_RECOVERABLE.get(
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk LocalizableMessage message = ERR_LDIFSEARCH_CANNOT_READ_ENTRY_FATAL.get(
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk resultCode = LDAPResultCode.CLIENT_SIDE_LOCAL_ERROR;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk err.println(ERR_LDIFSEARCH_ERROR_DURING_PROCESSING.get(e));