{
ClientConnection clientConnection = operation.getClientConnection();
if (! clientConnection.hasPrivilege(Privilege.LDIF_IMPORT, operation))
{
Message message = ERR_TASK_LDIFIMPORT_INSUFFICIENT_PRIVILEGES.get();
throw new DirectoryException(ResultCode.INSUFFICIENT_ACCESS_RIGHTS,
message);
}
}
Entry taskEntry = getTaskEntry();
AttributeType typeLdifFile;
AttributeType typeTemplateFile;
AttributeType typeAppend;
AttributeType typeReplaceExisting;
AttributeType typeBackendID;
AttributeType typeIncludeBranch;
AttributeType typeExcludeBranch;
AttributeType typeIncludeAttribute;
AttributeType typeExcludeAttribute;
AttributeType typeIncludeFilter;
AttributeType typeExcludeFilter;
AttributeType typeRejectFile;
AttributeType typeSkipFile;
AttributeType typeOverwrite;
AttributeType typeSkipSchemaValidation;
AttributeType typeIsCompressed;
AttributeType typeIsEncrypted;
AttributeType typeClearBackend;
AttributeType typeRandomSeed;
AttributeType typeThreadCount;
AttributeType typeTmpDirectory;
AttributeType typeDNCheckPhase2;
typeLdifFile =
getAttributeType(ATTR_IMPORT_LDIF_FILE, true);
typeTemplateFile =
getAttributeType(ATTR_IMPORT_TEMPLATE_FILE, true);
typeAppend =
getAttributeType(ATTR_IMPORT_APPEND, true);
typeReplaceExisting =
getAttributeType(ATTR_IMPORT_REPLACE_EXISTING, true);
typeBackendID =
getAttributeType(ATTR_IMPORT_BACKEND_ID, true);
typeIncludeBranch =
getAttributeType(ATTR_IMPORT_INCLUDE_BRANCH, true);
typeExcludeBranch =
getAttributeType(ATTR_IMPORT_EXCLUDE_BRANCH, true);
typeIncludeAttribute =
getAttributeType(ATTR_IMPORT_INCLUDE_ATTRIBUTE, true);
typeExcludeAttribute =
getAttributeType(ATTR_IMPORT_EXCLUDE_ATTRIBUTE, true);
typeIncludeFilter =
getAttributeType(ATTR_IMPORT_INCLUDE_FILTER, true);
typeExcludeFilter =
getAttributeType(ATTR_IMPORT_EXCLUDE_FILTER, true);
typeRejectFile =
getAttributeType(ATTR_IMPORT_REJECT_FILE, true);
typeSkipFile =
getAttributeType(ATTR_IMPORT_SKIP_FILE, true);
typeOverwrite =
getAttributeType(ATTR_IMPORT_OVERWRITE, true);
typeSkipSchemaValidation =
getAttributeType(ATTR_IMPORT_SKIP_SCHEMA_VALIDATION, true);
typeIsCompressed =
getAttributeType(ATTR_IMPORT_IS_COMPRESSED, true);
typeIsEncrypted =
getAttributeType(ATTR_IMPORT_IS_ENCRYPTED, true);
typeClearBackend =
getAttributeType(ATTR_IMPORT_CLEAR_BACKEND, true);
typeRandomSeed =
getAttributeType(ATTR_IMPORT_RANDOM_SEED, true);
typeThreadCount =
getAttributeType(ATTR_IMPORT_THREAD_COUNT, true);
typeTmpDirectory =
getAttributeType(ATTR_IMPORT_TMP_DIRECTORY, true);
typeDNCheckPhase2 =
getAttributeType(ATTR_IMPORT_SKIP_DN_VALIDATION, true);
List<Attribute> attrList;
attrList = taskEntry.getAttribute(typeLdifFile);
ArrayList<String> ldifFilestmp = TaskUtils.getMultiValueString(attrList);
ldifFiles = new ArrayList<String>(ldifFilestmp.size());
for (String s : ldifFilestmp)
{
File f = new File (s);
if (!f.isAbsolute())
{
f = new File(DirectoryServer.getInstanceRoot(), s);
try
{
s = f.getCanonicalPath();
}
catch (Exception ex)
{
s = f.getAbsolutePath();
}
ldifFiles.add(s);
}
else
{
ldifFiles.add(s);
}
}
attrList = taskEntry.getAttribute(typeTemplateFile);
templateFile = TaskUtils.getSingleValueString(attrList);
if (templateFile != null)
{
File f = new File(templateFile);
if (!f.isAbsolute())
{
templateFile = new File(DirectoryServer.getInstanceRoot(), templateFile)
.getAbsolutePath();
}
}
attrList = taskEntry.getAttribute(typeAppend);
append = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeDNCheckPhase2);
skipDNValidation = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeTmpDirectory);
tmpDirectory = TaskUtils.getSingleValueString(attrList);
attrList = taskEntry.getAttribute(typeReplaceExisting);
replaceExisting = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeBackendID);
backendID = TaskUtils.getSingleValueString(attrList);
attrList = taskEntry.getAttribute(typeIncludeBranch);
includeBranchStrings = TaskUtils.getMultiValueString(attrList);
attrList = taskEntry.getAttribute(typeExcludeBranch);
excludeBranchStrings = TaskUtils.getMultiValueString(attrList);
attrList = taskEntry.getAttribute(typeIncludeAttribute);
includeAttributeStrings = TaskUtils.getMultiValueString(attrList);
attrList = taskEntry.getAttribute(typeExcludeAttribute);
excludeAttributeStrings = TaskUtils.getMultiValueString(attrList);
attrList = taskEntry.getAttribute(typeIncludeFilter);
includeFilterStrings = TaskUtils.getMultiValueString(attrList);
attrList = taskEntry.getAttribute(typeExcludeFilter);
excludeFilterStrings = TaskUtils.getMultiValueString(attrList);
attrList = taskEntry.getAttribute(typeRejectFile);
rejectFile = TaskUtils.getSingleValueString(attrList);
attrList = taskEntry.getAttribute(typeSkipFile);
skipFile = TaskUtils.getSingleValueString(attrList);
attrList = taskEntry.getAttribute(typeOverwrite);
overwrite = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeSkipSchemaValidation);
skipSchemaValidation = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeIsCompressed);
isCompressed = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeIsEncrypted);
isEncrypted = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeClearBackend);
clearBackend = TaskUtils.getBoolean(attrList, false);
attrList = taskEntry.getAttribute(typeRandomSeed);
randomSeed = TaskUtils.getSingleValueInteger(attrList, 0);
attrList = taskEntry.getAttribute(typeThreadCount);
threadCount = TaskUtils.getSingleValueInteger(attrList, 0);
// Make sure that either the "includeBranchStrings" argument or the
// "backendID" argument was provided.
if(includeBranchStrings.isEmpty() && backendID == null)
{
Message message = ERR_LDIFIMPORT_MISSING_BACKEND_ARGUMENT.get(
typeIncludeBranch.getNameOrOID(), typeBackendID.getNameOrOID());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
Backend backend = null;
ArrayList<DN> defaultIncludeBranches;
ArrayList<DN> excludeBranches =
new ArrayList<DN>(excludeBranchStrings.size());
ArrayList<DN> includeBranches =
new ArrayList<DN>(includeBranchStrings.size());
for (String s : includeBranchStrings)
{
DN includeBranch;
try
{
includeBranch = DN.decode(s);
}
catch (DirectoryException de)
{
Message message = ERR_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE.get(
s, de.getMessageObject());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
catch (Exception e)
{
Message message = ERR_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE.get(
s, getExceptionMessage(e));
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
if(! includeBranches.contains(includeBranch))
{
includeBranches.add(includeBranch);
}
}
for (String s : excludeBranchStrings)
{
DN excludeBranch;
try
{
excludeBranch = DN.decode(s);
}
catch (DirectoryException de)
{
Message message = ERR_LDIFIMPORT_CANNOT_DECODE_EXCLUDE_BASE.get(
s, de.getMessageObject());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
catch (Exception e)
{
Message message = ERR_LDIFIMPORT_CANNOT_DECODE_EXCLUDE_BASE.get(
s, getExceptionMessage(e));
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
if (! excludeBranches.contains(excludeBranch))
{
excludeBranches.add(excludeBranch);
}
}
for (String filterString : excludeFilterStrings)
{
try
{
SearchFilter.createFilterFromString(filterString);
}
catch (DirectoryException de)
{
Message message = ERR_LDIFIMPORT_CANNOT_PARSE_EXCLUDE_FILTER.get(
filterString, de.getMessageObject());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
}
for (String filterString : includeFilterStrings)
{
try
{
SearchFilter.createFilterFromString(filterString);
}
catch (DirectoryException de)
{
Message message = ERR_LDIFIMPORT_CANNOT_PARSE_INCLUDE_FILTER.get(
filterString, de.getMessageObject());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
}
if(backendID != null)
{
backend = DirectoryServer.getBackend(backendID);
if (backend == null)
{
Message message = ERR_LDIFIMPORT_NO_BACKENDS_FOR_ID.get();
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
else if (! backend.supportsLDIFImport())
{
Message message = ERR_LDIFIMPORT_CANNOT_IMPORT.get(backendID);
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
// Make sure that if the "backendID" argument was provided, no include
// base was included, and the "append" option was not provided, the
// "clearBackend" argument was also provided if there are more then one
// baseDNs for the backend being imported.
else if(!append && includeBranchStrings.isEmpty() &&
backend.getBaseDNs().length > 1 && !clearBackend)
{
StringBuilder builder = new StringBuilder();
for(DN dn : backend.getBaseDNs())
{
builder.append(dn.toNormalizedString());
builder.append(" ");
}
Message message = ERR_LDIFIMPORT_MISSING_CLEAR_BACKEND.get(
builder.toString(), typeClearBackend.getNameOrOID());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
}
else
{
// Find the backend that includes all the branches.
for(DN includeBranch : includeBranches)
{
Backend locatedBackend = DirectoryServer.getBackend(includeBranch);
if(locatedBackend != null)
{
if(backend == null)
{
backend = locatedBackend;
}
else if(backend != locatedBackend)
{
// The include branches span across multiple backends.
Message message = ERR_LDIFIMPORT_INVALID_INCLUDE_BASE.get(
includeBranch.toNormalizedString(), backend.getBackendID());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
message);
}
}
else
{
// The include branch is not associated with any backend.
Message message =
ERR_NO_BACKENDS_FOR_BASE.get(includeBranch
.toNormalizedString());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
message);
}
}
}
// Make sure the selected backend will handle all the include branches
defaultIncludeBranches = new ArrayList<DN>(backend.getBaseDNs().length);
for (DN dn : backend.getBaseDNs())
{
defaultIncludeBranches.add(dn);
}
for(DN includeBranch : includeBranches)
{
if (! Backend.handlesEntry(includeBranch, defaultIncludeBranches,
excludeBranches))
{
Message message = ERR_LDIFIMPORT_INVALID_INCLUDE_BASE.get(
includeBranch.toNormalizedString(), backend.getBackendID());
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
}
}