| | |
| | | |
| | | |
| | | |
| | | import static org.opends.server.loggers.Debug.*; |
| | | import static org.opends.server.loggers.debug.DebugLogger.debugEnabled; |
| | | import static org.opends.server.loggers.debug.DebugLogger.debugVerbose; |
| | | import static org.opends.server.loggers.debug.DebugLogger.debugInfo; |
| | | import static org.opends.server.loggers.debug.DebugLogger.debugCought; |
| | | import static org.opends.server.loggers.debug.DebugLogger.debugProtocolElement; |
| | | import static org.opends.server.loggers.Error.logError; |
| | | import static org.opends.server.messages.MessageHandler.getMessage; |
| | | import static org.opends.server.messages.UtilityMessages.*; |
| | |
| | | import org.opends.server.types.AttributeValue; |
| | | import org.opends.server.types.DirectoryException; |
| | | import org.opends.server.types.DN; |
| | | import org.opends.server.types.DebugLogCategory; |
| | | import org.opends.server.types.DebugLogSeverity; |
| | | import org.opends.server.types.DebugLogLevel; |
| | | import org.opends.server.types.Entry; |
| | | import org.opends.server.types.ErrorLogCategory; |
| | | import org.opends.server.types.ErrorLogSeverity; |
| | |
| | | import org.opends.server.types.RDN; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | */ |
| | | public final class LDIFReader |
| | | { |
| | | /** |
| | | * The fully-qualified name of this class for debugging purposes. |
| | | */ |
| | | private static final String CLASS_NAME = "org.opends.server.util.LDIFReader"; |
| | | |
| | | |
| | | |
| | |
| | | public LDIFReader(LDIFImportConfig importConfig) |
| | | throws IOException |
| | | { |
| | | assert debugConstructor(CLASS_NAME, String.valueOf(importConfig)); |
| | | |
| | | ensureNotNull(importConfig); |
| | | this.importConfig = importConfig; |
| | |
| | | public Entry readEntry() |
| | | throws IOException, LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readEntry"); |
| | | |
| | | return readEntry(importConfig.validateSchema()); |
| | | } |
| | |
| | | public Entry readEntry(boolean checkSchema) |
| | | throws IOException, LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readEntry"); |
| | | |
| | | |
| | | while (true) |
| | |
| | | // read and return the next entry. |
| | | continue; |
| | | } |
| | | else if (! importConfig.includeEntry(entryDN)) |
| | | else if (!importConfig.includeEntry(entryDN)) |
| | | { |
| | | assert debugMessage(DebugLogCategory.PROTOCOL_READ, |
| | | DebugLogSeverity.INFO, CLASS_NAME, "readEntry", |
| | | "Skipping entry " + String.valueOf(entryDN) + |
| | | " because the DN is not one that should be " + |
| | | "included based on the include and exclude " + |
| | | "branches."); |
| | | 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++; |
| | | entriesIgnored++; |
| | | continue; |
| | |
| | | // import. |
| | | Entry entry = new Entry(entryDN, objectClasses, userAttributes, |
| | | operationalAttributes); |
| | | assert debugProtocolElementRead(CLASS_NAME, "readEntry", entry); |
| | | debugProtocolElement(DebugLogLevel.VERBOSE, entry); |
| | | |
| | | try |
| | | { |
| | | if (! importConfig.includeEntry(entry)) |
| | | { |
| | | assert debugMessage(DebugLogCategory.PROTOCOL_READ, |
| | | DebugLogSeverity.INFO, CLASS_NAME, "readEntry", |
| | | "Skipping entry " + String.valueOf(entryDN) + |
| | | " because the DN is not one that should be " + |
| | | "included based on the include and exclude " + |
| | | "filters."); |
| | | if (debugEnabled()) |
| | | { |
| | | debugInfo("Skipping entry %s because the DN is not one that " + |
| | | "should be included based on the include and exclude filters.", |
| | | entryDN); |
| | | } |
| | | entriesIgnored++; |
| | | continue; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "readEntry", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_COULD_NOT_EVALUATE_FILTERS_FOR_IMPORT; |
| | | String message = getMessage(msgID, String.valueOf(entry.getDN()), |
| | |
| | | public ChangeRecordEntry readChangeRecord(boolean defaultAdd) |
| | | throws IOException, LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readChangeRecord"); |
| | | |
| | | while (true) |
| | | { |
| | |
| | | private LinkedList<StringBuilder> readEntryLines() |
| | | throws IOException, LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readEntryLines"); |
| | | |
| | | // Read the entry lines into a buffer. |
| | | LinkedList<StringBuilder> lines = new LinkedList<StringBuilder>(); |
| | |
| | | private DN readDN(LinkedList<StringBuilder> lines) |
| | | throws LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readDN", String.valueOf(lines)); |
| | | |
| | | if (lines.isEmpty()) |
| | | { |
| | |
| | | catch (Exception e) |
| | | { |
| | | // The value did not have a valid base64-encoding. |
| | | assert debugException(CLASS_NAME, "readDN", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_COULD_NOT_BASE64_DECODE_DN; |
| | | String message = getMessage(msgID, lastEntryLineNumber, line, |
| | |
| | | } |
| | | catch (DirectoryException de) |
| | | { |
| | | assert debugException(CLASS_NAME, "readDN", de); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, de); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lastEntryLineNumber, line.toString(), |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "readDN", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lastEntryLineNumber, line.toString(), |
| | |
| | | } |
| | | catch (DirectoryException de) |
| | | { |
| | | assert debugException(CLASS_NAME, "readDN", de); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, de); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lastEntryLineNumber, line.toString(), |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "readDN", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lastEntryLineNumber, line.toString(), |
| | |
| | | private String readChangeType(LinkedList<StringBuilder> lines) |
| | | throws LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readChangeType", String.valueOf(lines)); |
| | | |
| | | if (lines.isEmpty()) |
| | | { |
| | |
| | | catch (Exception e) |
| | | { |
| | | // The value did not have a valid base64-encoding. |
| | | assert debugException(CLASS_NAME, "readChangeType", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_COULD_NOT_BASE64_DECODE_DN; |
| | | String message = getMessage(msgID, lastEntryLineNumber, line, |
| | |
| | | HashMap<AttributeType,List<Attribute>> operationalAttributes) |
| | | throws LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readAttribute", |
| | | String.valueOf(line), |
| | | String.valueOf(entryDN), |
| | | String.valueOf(objectClasses), |
| | | String.valueOf(userAttributes), |
| | | String.valueOf(operationalAttributes)); |
| | | |
| | | // Parse the attribute type description. |
| | | int colonPos = parseColonPosition(lines, line); |
| | |
| | | { |
| | | if (! importConfig.includeObjectClasses()) |
| | | { |
| | | assert debugMessage(DebugLogCategory.PROTOCOL_READ, |
| | | DebugLogSeverity.VERBOSE, CLASS_NAME, |
| | | "readAttribute", |
| | | "Skipping objectclass " + String.valueOf(value) + |
| | | " for entry " + String.valueOf(entryDN) + |
| | | " due to the import configuration."); |
| | | if (debugEnabled()) |
| | | { |
| | | debugVerbose("Skipping objectclass %s for entry %s due to the " + |
| | | "import configuration.", value, entryDN); |
| | | } |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | if (! importConfig.includeAttribute(attrType)) |
| | | { |
| | | assert debugMessage(DebugLogCategory.PROTOCOL_READ, |
| | | DebugLogSeverity.VERBOSE, CLASS_NAME, |
| | | "readAttribute", |
| | | "Skipping attribute " + String.valueOf(attrName) + |
| | | " for entry " + String.valueOf(entryDN) + |
| | | " due to the import configuration."); |
| | | if (debugEnabled()) |
| | | { |
| | | debugVerbose("Skipping attribute %s for entry %s due to the import " + |
| | | "configuration.", attrName, entryDN); |
| | | } |
| | | return; |
| | | } |
| | | |
| | |
| | | LinkedList<StringBuilder> lines, StringBuilder line, DN entryDN, |
| | | String attributeName) throws LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "readSingleValueAttribute", |
| | | String.valueOf(lines), |
| | | String.valueOf(line), |
| | | String.valueOf(entryDN), |
| | | String.valueOf(attributeName)); |
| | | |
| | | // Parse the attribute type description. |
| | | int colonPos = parseColonPosition(lines, line); |
| | |
| | | */ |
| | | public long getLastEntryLineNumber() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "getLastEntryLineNumber"); |
| | | |
| | | return lastEntryLineNumber; |
| | | } |
| | |
| | | */ |
| | | public void rejectLastEntry(String message) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "rejectLastEntry", String.valueOf(message)); |
| | | |
| | | entriesRejected++; |
| | | |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "rejectLastEntry", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | */ |
| | | public void close() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "close"); |
| | | |
| | | importConfig.close(); |
| | | } |
| | |
| | | */ |
| | | private static Attribute parseAttrDescription(String attrDescr) |
| | | { |
| | | assert debugEnter(CLASS_NAME, "parseAttributeDescription", |
| | | String.valueOf(attrDescr)); |
| | | |
| | | String attrName; |
| | | String lowerName; |
| | |
| | | */ |
| | | public long getEntriesRead() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "getEntriesRead"); |
| | | |
| | | return entriesRead; |
| | | } |
| | |
| | | */ |
| | | public long getEntriesIgnored() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "getEntriesIgnored"); |
| | | |
| | | return entriesIgnored; |
| | | } |
| | |
| | | */ |
| | | public long getEntriesRejected() |
| | | { |
| | | assert debugEnter(CLASS_NAME, "getEntriesRejected"); |
| | | |
| | | return entriesRejected; |
| | | } |
| | |
| | | */ |
| | | private ChangeRecordEntry parseModifyDNChangeRecordEntry(DN entryDN, |
| | | LinkedList<StringBuilder> lines) throws LDIFException { |
| | | assert debugEnter(CLASS_NAME, "parseModifyDNChangeRecordEntry", |
| | | String.valueOf(entryDN), |
| | | String.valueOf(lines), |
| | | String.valueOf(lineNumber)); |
| | | |
| | | DN newSuperiorDN = null; |
| | | RDN newRDN = null; |
| | |
| | | newRDN = RDN.decode(rdnStr); |
| | | } catch (DirectoryException de) |
| | | { |
| | | assert debugException(CLASS_NAME, "parse", de); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, de); |
| | | } |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lineNumber, line.toString(), |
| | | de.getErrorMessage()); |
| | | throw new LDIFException(msgID, message, lineNumber, true); |
| | | } catch (Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "parse", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lineNumber, line.toString(), |
| | | e.getMessage()); |
| | |
| | | newSuperiorDN = DN.decode(dnStr); |
| | | } catch (DirectoryException de) |
| | | { |
| | | assert debugException(CLASS_NAME, "parse", de); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, de); |
| | | } |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lineNumber, line.toString(), |
| | | de.getErrorMessage()); |
| | | throw new LDIFException(msgID, message, lineNumber, true); |
| | | } catch (Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "parse", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | int msgID = MSGID_LDIF_INVALID_DN; |
| | | String message = getMessage(msgID, lineNumber, line.toString(), |
| | | e.getMessage()); |
| | |
| | | DN entryDN, |
| | | String attributeName) throws LDIFException |
| | | { |
| | | assert debugEnter(CLASS_NAME, "getModifyDNAttributeValue", |
| | | String.valueOf(lines), |
| | | String.valueOf(line), |
| | | String.valueOf(entryDN), |
| | | String.valueOf(attributeName)); |
| | | |
| | | Attribute attr = |
| | | readSingleValueAttribute(lines, line, entryDN, attributeName); |
| | |
| | | */ |
| | | private ChangeRecordEntry parseModifyChangeRecordEntry(DN entryDN, |
| | | LinkedList<StringBuilder> lines) throws LDIFException { |
| | | assert debugEnter(CLASS_NAME, "parseModifyChangeRecordEntry", |
| | | String.valueOf(entryDN), |
| | | String.valueOf(lines), |
| | | String.valueOf(lineNumber)); |
| | | |
| | | List<LDAPModification> modifications = new ArrayList<LDAPModification>(); |
| | | while(!lines.isEmpty()) |
| | |
| | | */ |
| | | private ChangeRecordEntry parseDeleteChangeRecordEntry(DN entryDN, |
| | | LinkedList<StringBuilder> lines) throws LDIFException { |
| | | assert debugEnter(CLASS_NAME, "parseDeleteChangeRecordEntry", |
| | | String.valueOf(entryDN), |
| | | String.valueOf(lines), |
| | | String.valueOf(lineNumber)); |
| | | |
| | | if(!lines.isEmpty()) |
| | | if (!lines.isEmpty()) |
| | | { |
| | | int msgID = MSGID_LDIF_INVALID_DELETE_ATTRIBUTES; |
| | | String message = getMessage(msgID); |
| | |
| | | */ |
| | | private ChangeRecordEntry parseAddChangeRecordEntry(DN entryDN, |
| | | LinkedList<StringBuilder> lines) throws LDIFException { |
| | | assert debugEnter(CLASS_NAME, "parseAddChangeRecordEntry", |
| | | String.valueOf(entryDN), |
| | | String.valueOf(lines), |
| | | String.valueOf(lineNumber)); |
| | | |
| | | HashMap<ObjectClass,String> objectClasses = |
| | | new HashMap<ObjectClass,String>(); |
| | |
| | | */ |
| | | private int parseColonPosition(LinkedList<StringBuilder> lines, |
| | | StringBuilder line) throws LDIFException { |
| | | assert debugEnter(CLASS_NAME, "parseColonPosition", |
| | | String.valueOf(lines), |
| | | String.valueOf(lineNumber)); |
| | | |
| | | int colonPos = line.indexOf(":"); |
| | | if (colonPos <= 0) |
| | |
| | | DN entryDN, |
| | | int colonPos, |
| | | String attrName) throws LDIFException { |
| | | assert debugEnter(CLASS_NAME, "parseSingleValue", |
| | | String.valueOf(lines), |
| | | String.valueOf(line), |
| | | String.valueOf(entryDN), |
| | | String.valueOf(colonPos), |
| | | String.valueOf(attrName)); |
| | | |
| | | // Look at the character immediately after the colon. If there is |
| | | // none, then assume an attribute with an empty value. If it is another |
| | |
| | | catch (Exception e) |
| | | { |
| | | // The value did not have a valid base64-encoding. |
| | | assert debugException(CLASS_NAME, "readAttribute", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_COULD_NOT_BASE64_DECODE_ATTR; |
| | | String message = getMessage(msgID, String.valueOf(entryDN), |
| | |
| | | catch (Exception e) |
| | | { |
| | | // The URL was malformed or had an invalid protocol. |
| | | assert debugException(CLASS_NAME, "readAttribute", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_INVALID_URL; |
| | | String message = getMessage(msgID, String.valueOf(entryDN), |
| | |
| | | { |
| | | // We were unable to read the contents of that URL for some |
| | | // reason. |
| | | assert debugException(CLASS_NAME, "readAttribute", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_LDIF_URL_IO_ERROR; |
| | | String message = getMessage(msgID, String.valueOf(entryDN), |
| | |
| | | */ |
| | | private void logToRejectWriter(LinkedList<StringBuilder> lines, |
| | | String message) { |
| | | assert debugEnter(CLASS_NAME, "logToRejectWriter", |
| | | String.valueOf(lines), |
| | | String.valueOf(message)); |
| | | |
| | | BufferedWriter rejectWriter = importConfig.getRejectWriter(); |
| | | if (rejectWriter != null) |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "logToRejectWriter", e); |
| | | if (debugEnabled()) |
| | | { |
| | | debugCought(DebugLogLevel.ERROR, e); |
| | | } |
| | | } |
| | | } |
| | | } |