LDIFReader.java revision 1177
/*
* 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
*
*
* Portions Copyright 2006-2007 Sun Microsystems, Inc.
*/
/**
* This class provides the ability to read information from an LDIF file. It
* provides support for both standard entries and change entries (as would be
* used with a tool like ldapmodify).
*/
public final class LDIFReader
{
// The reader that will be used to read the data.
private BufferedReader reader;
// The buffer to use to read data from a URL.
private byte[] buffer;
// The import configuration that specifies what should be imported.
private LDIFImportConfig importConfig;
// The lines that comprise the body of the last entry read.
// The lines that comprise the header (DN and any comments) for the last entry
// read.
// The number of entries that have been ignored by this LDIF reader because
// they didn't match the criteria.
private long entriesIgnored;
// The number of entries that have been read by this LDIF reader, including
// those that were ignored because they didn't match the criteria, and
// including those that were rejected because they were invalid in some way.
private long entriesRead;
// The number of entries that have been rejected by this LDIF reader.
private long entriesRejected;
// The line number on which the last entry started.
private long lastEntryLineNumber;
// The line number of the last line read from the LDIF file, starting with 1.
private long lineNumber;
// The plugin config manager that will be used if we are to invoke plugins
// on the entries as they are read.
private PluginConfigManager pluginConfigManager;
/**
* Creates a new LDIF reader that will read information from the specified
* file.
*
* @param importConfig The import configuration for this LDIF reader. It
* must not be <CODE>null</CODE>.
*
* @throws IOException If a problem occurs while opening the LDIF file for
* reading.
*/
throws IOException
{
this.importConfig = importConfig;
buffer = new byte[4096];
entriesRead = 0;
entriesIgnored = 0;
entriesRejected = 0;
lineNumber = 0;
lastEntryLineNumber = -1;
}
/**
* Reads the next entry from the LDIF source.
*
* @return The next entry read from the LDIF source, or <CODE>null</CODE> if
* the end of the LDIF data is reached.
*
* @throws IOException If an I/O problem occurs while reading from the file.
*
* @throws LDIFException If the information read cannot be parsed as an LDIF
* entry.
*/
throws IOException, LDIFException
{
}
/**
* Reads the next entry from the LDIF source.
*
* @param checkSchema Indicates whether this reader should perform schema
* checking on the entry before returning it to the
* caller. Note that some basic schema checking (like
* refusing multiple values for a single-valued
* attribute) may always be performed.
*
*
* @return The next entry read from the LDIF source, or <CODE>null</CODE> if
* the end of the LDIF data is reached.
*
* @throws IOException If an I/O problem occurs while reading from the file.
*
* @throws LDIFException If the information read cannot be parsed as an LDIF
* entry.
*/
throws IOException, LDIFException
{
while (true)
{
// Read the set of lines that make up the next entry.
{
return null;
}
// Read the DN of the entry and see if it is one that should be included
// in the import.
{
// This should only happen if the LDIF starts with the "version:" line
// and has a blank line immediately after that. In that case, simply
// read and return the next entry.
continue;
}
{
if (debugEnabled())
{
debugInfo("Skipping entry %s because the DN is not one that should " +
"be included based on the include and exclude branches.",
entryDN);
}
entriesRead++;
continue;
}
else
{
entriesRead++;
}
// Read the set of attributes from the entry.
try
{
{
}
}
catch (LDIFException e)
{
throw e;
}
// Create the entry and see if it is one that should be included in the
// import.
try
{
{
if (debugEnabled())
{
debugInfo("Skipping entry %s because the DN is not one that " +
"should be included based on the include and exclude filters.",
entryDN);
}
continue;
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
}
// If we should invoke import plugins, then do so.
if (importConfig.invokeImportPlugins())
{
if (! pluginResult.continueEntryProcessing())
{
continue;
}
}
// Make sure that the entry is valid as per the server schema if it is
// appropriate to do so.
if (checkSchema)
{
{
int msgID = MSGID_LDIF_SCHEMA_VIOLATION;
}
}
// The entry should be included in the import, so return it.
return entry;
}
}
/**
* Reads the next change record from the LDIF source.
*
* @param defaultAdd Indicates whether the change type should default to
* "add" if none is explicitly provided.
*
* @return The next change record from the LDIF source, or <CODE>null</CODE>
* if the end of the LDIF data is reached.
*
* @throws IOException If an I/O problem occurs while reading from the file.
*
* @throws LDIFException If the information read cannot be parsed as an LDIF
* entry.
*/
throws IOException, LDIFException
{
while (true)
{
// Read the set of lines that make up the next entry.
{
return null;
}
// Read the DN of the entry and see if it is one that should be included
// in the import.
{
// This should only happen if the LDIF starts with the "version:" line
// and has a blank line immediately after that. In that case, simply
// read and return the next entry.
continue;
}
if(changeType != null)
{
{
{
{
{
{
} else
{
"add, delete, modify, moddn, modrdn");
}
} else
{
// default to "add"?
if(defaultAdd)
{
} else
{
"add, delete, modify, moddn, modrdn");
}
}
return entry;
}
}
/**
* Reads a set of lines from the next entry in the LDIF source.
*
* @return A set of lines from the next entry in the LDIF source.
*
* @throws IOException If a problem occurs while reading from the LDIF
* source.
*
* @throws LDIFException If the information read is not valid LDIF.
*/
throws IOException, LDIFException
{
// Read the entry lines into a buffer.
int lastLine = -1;
while (true)
{
lineNumber++;
{
// This must mean that we have reached the end of the LDIF source.
// If the set of lines read so far is empty, then move onto the next
// file or return null. Otherwise, break out of this loop.
{
{
return null;
}
else
{
return readEntryLines();
}
}
else
{
break;
}
}
{
// This is a blank line. If the set of lines read so far is empty,
// then just skip over it. Otherwise, break out of this loop.
{
continue;
}
else
{
break;
}
}
{
// This is a comment. Ignore it.
continue;
}
{
// This is a continuation of the previous line. If there is no
// previous line, then that's a problem. Note that while RFC 2849
// technically only allows a space in this position, both OpenLDAP and
// the Sun Java System Directory Server allow a tab as well, so we will
// too for compatibility reasons. See issue #852 for details.
if (lastLine >= 0)
{
}
else
{
}
}
else
{
// This is a new line.
{
}
lastLine++;
}
}
return lines;
}
/**
* Reads the DN of the entry from the provided list of lines. The DN must be
* the first line in the list, unless the first line starts with "version",
* in which case the DN should be the second line.
*
* @param lines The set of lines from which the DN should be read.
*
* @return The decoded entry DN.
*
* @throws LDIFException If DN is not the first element in the list (or the
* second after the LDIF version), or if a problem
* occurs while trying to parse it.
*/
throws LDIFException
{
{
// This is possible if the contents of the first "entry" were just
// the version identifier. If that is the case, then return null and
// use that as a signal to the caller to go ahead and read the next entry.
return null;
}
if (colonPos <= 0)
{
int msgID = MSGID_LDIF_NO_ATTR_NAME;
}
{
// This is the version line, and we can skip it.
}
{
int msgID = MSGID_LDIF_NO_DN;
}
// Look at the character immediately after the colon. If there is none,
// then assume the null DN. If it is another colon, then the DN must be
// base64-encoded. Otherwise, it may be one or more spaces.
{
}
{
// The DN is base64-encoded. Find the first non-blank character and
// take the rest of the line, base64-decode it, and parse it as a DN.
{
pos++;
}
try
{
}
catch (Exception e)
{
// The value did not have a valid base64-encoding.
if (debugEnabled())
{
}
}
try
{
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
de.getErrorMessage());
}
catch (Exception e)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
}
}
else
{
// The rest of the value should be the DN. Skip over any spaces and
// attempt to decode the rest of the line as the DN.
{
pos++;
}
try
{
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
de.getErrorMessage());
}
catch (Exception e)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
}
}
}
/**
* Reads the changetype of the entry from the provided list of lines. If
* there is no changetype attribute then an add is assumed.
*
* @param lines The set of lines from which the DN should be read.
*
* @return The decoded entry DN.
*
* @throws LDIFException If DN is not the first element in the list (or the
* second after the LDIF version), or if a problem
* occurs while trying to parse it.
*/
throws LDIFException
{
{
// Error. There must be other entries.
return null;
}
if (colonPos <= 0)
{
int msgID = MSGID_LDIF_NO_ATTR_NAME;
}
{
// No changetype attribute - return null
return null;
} else
{
// Remove the line
}
// Look at the character immediately after the colon. If there is none,
// then no value was specified. Throw an exception
{
"add, delete, modify, moddn, modrdn");
}
{
// The change type is base64-encoded. Find the first non-blank
// character and
// take the rest of the line, and base64-decode it.
{
pos++;
}
try
{
"UTF-8");
}
catch (Exception e)
{
// The value did not have a valid base64-encoding.
if (debugEnabled())
{
}
}
return changeTypeStr;
}
else
{
// The rest of the value should be the changetype.
// Skip over any spaces and
// attempt to decode the rest of the line as the changetype string.
{
pos++;
}
return changeTypeString;
}
}
/**
* Decodes the provided line as an LDIF attribute and adds it to the
* appropriate hash.
*
* @param lines The full set of lines that comprise the
* entry (used for writing reject information).
* @param line The line to decode.
* @param entryDN The DN of the entry being decoded.
* @param objectClasses The set of objectclasses decoded so far for
* the current entry.
* @param userAttributes The set of user attributes decoded so far
* for the current entry.
* @param operationalAttributes The set of operational attributes decoded so
* far for the current entry.
* @param checkSchema Indicates whether to perform schema
* validation for the attribute.
*
* @throws LDIFException If a problem occurs while trying to decode the
* attribute contained in the provided entry.
*/
boolean checkSchema)
throws LDIFException
{
// Parse the attribute type description.
// Now parse the attribute value.
// See if this is an objectclass or an attribute. Then get the
// corresponding definition and add the value to the appropriate hash.
{
if (! importConfig.includeObjectClasses())
{
if (debugEnabled())
{
debugVerbose("Skipping objectclass %s for entry %s due to the " +
}
return;
}
if (objectClass == null)
{
}
{
}
else
{
}
}
else
{
{
}
{
if (debugEnabled())
{
debugVerbose("Skipping attribute %s for entry %s due to the import " +
}
return;
}
if (attrType.isOperational())
{
{
new LinkedHashSet<AttributeValue>();
return;
}
}
else
{
{
new LinkedHashSet<AttributeValue>();
return;
}
}
// Check to see if any of the attributes in the list have the same set of
// options. If so, then try to add a value to that attribute.
{
if (a.optionsEqual(options))
{
{
if (! checkSchema)
{
// If we're not doing schema checking, then it is possible that
// the attribute type should use case-sensitive matching and the
// values differ in capitalization. Only reject the proposed
// value if we find another value that is exactly the same as the
// one that was provided.
for (AttributeValue v : valueSet)
{
{
int msgID = MSGID_LDIF_DUPLICATE_ATTR;
value.stringValue());
true);
}
}
}
else
{
int msgID = MSGID_LDIF_DUPLICATE_ATTR;
value.stringValue());
true);
}
}
{
}
return;
}
}
// No set of matching options was found, so create a new one and add it to
// the list.
new LinkedHashSet<AttributeValue>();
return;
}
}
/**
* Decodes the provided line as an LDIF attribute and returns the
* Attribute (name and values) for the specified attribute name.
*
* @param lines The full set of lines that comprise the
* entry (used for writing reject information).
* @param line The line to decode.
* @param entryDN The DN of the entry being decoded.
* @param attributeName The name and options of the attribute to
* return the values for.
*
* @return The attribute in octet string form.
* @throws LDIFException If a problem occurs while trying to decode
* the attribute contained in the provided
* entry or if the parsed attribute name does
* not match the specified attribute name.
*/
private Attribute readSingleValueAttribute(
{
// Parse the attribute type description.
if (attributeName != null)
{
{
}
}
// Now parse the attribute value.
return attribute;
}
/**
* Retrieves the starting line number for the last entry read from the LDIF
* source.
*
* @return The starting line number for the last entry read from the LDIF
* source.
*/
public long getLastEntryLineNumber()
{
return lastEntryLineNumber;
}
/**
* Rejects the last entry read from the LDIF. This method is intended for use
* by components that perform their own validation of entries (e.g., backends
* during import processing) in which the entry appeared valid to the LDIF
* reader but some other problem was encountered.
*
* @param message A human-readable message providing the reason that the
* last entry read was not acceptable.
*/
{
if (rejectWriter != null)
{
try
{
{
}
{
}
{
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
}
}
}
/**
* Closes this LDIF reader and the underlying file or input stream.
*/
public void close()
{
}
/**
* Parse an AttributeDescription (an attribute type name and its options).
* @param attrDescr The attribute description to be parsed.
* @return A new attribute with no values, representing the attribute type
* and its options.
*/
{
if (semicolonPos > 0)
{
while (nextPos > 0)
{
{
}
}
{
}
}
else
{
}
{
}
}
/**
* Retrieves the total number of entries read so far by this LDIF reader,
* including those that have been ignored or rejected.
*
* @return The total number of entries read so far by this LDIF reader.
*/
public long getEntriesRead()
{
return entriesRead;
}
/**
* Retrieves the total number of entries that have been ignored so far by this
* LDIF reader because they did not match the import criteria.
*
* @return The total number of entries ignored so far by this LDIF reader.
*/
public long getEntriesIgnored()
{
return entriesIgnored;
}
/**
* Retrieves the total number of entries rejected so far by this LDIF reader.
* This includes both entries that were rejected because of internal
* validation failure (e.g., they didn't conform to the defined server
* schema) or an external validation failure (e.g., the component using this
* LDIF reader didn't accept the entry because it didn't have a parent).
*
* @return The total number of entries rejected so far by this LDIF reader.
*/
public long getEntriesRejected()
{
return entriesRejected;
}
/**
* Parse a modifyDN change record entry from LDIF.
*
* @param entryDN
* The name of the entry being modified.
* @param lines
* The lines to parse.
* @return Returns the parsed modifyDN change record entry.
* @throws LDIFException
* If there was an error when parsing the change record.
*/
boolean deleteOldRDN = false;
{
}
try
{
} catch (DirectoryException de)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
de.getErrorMessage());
} catch (Exception e)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
e.getMessage());
}
{
}
lineNumber++;
entryDN, "deleteoldrdn");
{
deleteOldRDN = false;
{
deleteOldRDN = true;
} else
{
}
{
lineNumber++;
entryDN, "newsuperior");
try
{
} catch (DirectoryException de)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
de.getErrorMessage());
} catch (Exception e)
{
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_DN;
e.getMessage());
}
}
}
/**
* Return the string value for the specified attribute name which only
* has one value.
*
* @param lines
* The set of lines for this change record entry.
* @param line
* The line currently being examined.
* @param entryDN
* The name of the entry being modified.
* @param attributeName
* The attribute name
* @return the string value for the attribute name.
* @throws LDIFException
* If a problem occurs while attempting to determine the
* attribute value.
*/
{
// Get the attribute value
}
/**
* Parse a modify change record entry from LDIF.
*
* @param entryDN
* The name of the entry being modified.
* @param lines
* The lines to parse.
* @return Returns the parsed modify change record entry.
* @throws LDIFException
* If there was an error when parsing the change record.
*/
{
// Get the attribute description
{
{
{
{
} else
{
// Invalid attribute name.
"add, delete, replace, increment");
}
// Now go through the rest of the attributes till the "-" line is
// reached.
{
{
break;
}
Attribute a =
}
}
}
/**
* Parse a delete change record entry from LDIF.
*
* @param entryDN
* The name of the entry being deleted.
* @param lines
* The lines to parse.
* @return Returns the parsed delete change record entry.
* @throws LDIFException
* If there was an error when parsing the change record.
*/
{
}
return new DeleteChangeRecordEntry(entryDN);
}
/**
* Parse an add change record entry from LDIF.
*
* @param entryDN
* The name of the entry being added.
* @param lines
* The lines to parse.
* @return Returns the parsed add change record entry.
* @throws LDIFException
* If there was an error when parsing the change record.
*/
{
}
// Reconstruct the object class attribute.
}
}
/**
* Parse colon position in an attribute description.
*
* @param lines
* The current set of lines.
* @param line
* The current line.
* @return The colon position.
* @throws LDIFException
* If the colon was badly placed or not found.
*/
if (colonPos <= 0)
{
int msgID = MSGID_LDIF_NO_ATTR_NAME;
}
return colonPos;
}
/**
* Parse a single attribute value from a line of LDIF.
*
* @param lines
* The current set of lines.
* @param line
* The current line.
* @param entryDN
* The DN of the entry being parsed.
* @param colonPos
* The position of the separator colon in the line.
* @param attrName
* The name of the attribute being parsed.
* @return The parsed attribute value.
* @throws LDIFException
* If an error occurred when parsing the attribute value.
*/
private ASN1OctetString parseSingleValue(
int colonPos,
// Look at the character immediately after the colon. If there is
// none, then assume an attribute with an empty value. If it is another
// colon, then the value must be base64-encoded. If it is a less-than
// sign, then assume that it is a URL. Otherwise, it is a regular value.
{
value = new ASN1OctetString();
}
else
{
if (c == ':')
{
// The value is base64-encoded. Find the first non-blank
// character, take the rest of the line, and base64-decode it.
{
pos++;
}
try
{
}
catch (Exception e)
{
// The value did not have a valid base64-encoding.
if (debugEnabled())
{
}
}
}
else if (c == '<')
{
// Find the first non-blank character, decode the rest of the
// line as a URL, and read its contents.
{
pos++;
}
try
{
}
catch (Exception e)
{
// The URL was malformed or had an invalid protocol.
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_INVALID_URL;
}
try
{
outputStream = new ByteArrayOutputStream();
int bytesRead;
{
}
}
catch (Exception e)
{
// We were unable to read the contents of that URL for some
// reason.
if (debugEnabled())
{
}
int msgID = MSGID_LDIF_URL_IO_ERROR;
}
finally
{
if (outputStream != null)
{
try
{
} catch (Exception e) {}
}
if (inputStream != null)
{
try
{
inputStream.close();
} catch (Exception e) {}
}
}
}
else
{
// The rest of the line should be the value. Skip over any
// spaces and take the rest of the line as the value.
{
pos++;
}
}
}
return value;
}
/**
* Log a message to the reject writer if one is configured.
*
* @param lines
* The set of rejected lines.
* @param message
* The associated error message.
*/
if (rejectWriter != null)
{
try
{
{
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
}
}
}
}