use of com.unboundid.ldap.sdk.unboundidds.tools.ValuesOnlyLDAPResultWriter in project ldapsdk by pingidentity.
the class LDIFSearch method doExtendedArgumentValidation.
/**
* {@inheritDoc}
*/
@Override()
public void doExtendedArgumentValidation() throws ArgumentException {
// If the output file exists and either compressOutput or encryptOutput is
// present, then the overwrite argument must also be present.
final File outFile = outputFile.getValue();
if ((outFile != null) && outFile.exists() && (compressOutput.isPresent() || encryptOutput.isPresent()) && (!overwriteExistingOutputFile.isPresent())) {
throw new ArgumentException(ERR_LDIFSEARCH_APPEND_WITH_COMPRESSION_OR_ENCRYPTION.get(compressOutput.getIdentifierString(), encryptOutput.getIdentifierString(), overwriteExistingOutputFile.getIdentifierString()));
}
// Create the set of LDAP URLs to use when issuing the searches.
final List<String> trailingArgs = parser.getTrailingArguments();
final List<String> requestedAttributes = new ArrayList<>();
if (filterFile.isPresent()) {
// valid filter.
if (!trailingArgs.isEmpty()) {
try {
Filter.create(trailingArgs.get(0));
throw new ArgumentException(ERR_LDIFSEARCH_FILTER_FILE_WITH_TRAILING_FILTER.get());
} catch (final LDAPException e) {
// This was expected.
}
}
requestedAttributes.addAll(trailingArgs);
readFilterFile();
} else if (ldapURLFile.isPresent()) {
// Make sure there aren't any trailing arguments.
if (!trailingArgs.isEmpty()) {
throw new ArgumentException(ERR_LDIFSEARCH_LDAP_URL_FILE_WITH_TRAILING_ARGS.get());
}
readLDAPURLFile();
// requested attributes.
if ((searchURLs.size() > 1) && (!separateOutputFilePerSearch.isPresent())) {
final Iterator<LDAPURL> iterator = searchURLs.iterator();
final Set<String> requestedAttrs = new HashSet<>(Arrays.asList(iterator.next().getAttributes()));
while (iterator.hasNext()) {
final Set<String> attrSet = new HashSet<>(Arrays.asList(iterator.next().getAttributes()));
if (!requestedAttrs.equals(attrSet)) {
throw new ArgumentException(ERR_LDIFSEARCH_DIFFERENT_URL_ATTRS_IN_SAME_FILE.get(ldapURLFile.getIdentifierString(), separateOutputFilePerSearch.getIdentifierString()));
}
}
}
} else {
// requested arguments.
if (trailingArgs.isEmpty()) {
throw new ArgumentException(ERR_LDIFSEARCH_NO_FILTER.get());
}
final Filter filter;
try {
final List<String> trailingArgList = new ArrayList<>(trailingArgs);
final Iterator<String> trailingArgIterator = trailingArgList.iterator();
filter = Filter.create(trailingArgIterator.next());
while (trailingArgIterator.hasNext()) {
requestedAttributes.add(trailingArgIterator.next());
}
} catch (final LDAPException e) {
Debug.debugException(e);
throw new ArgumentException(ERR_LDIFSEARCH_FIRST_TRAILING_ARG_NOT_FILTER.get(), e);
}
DN dn = baseDN.getValue();
if (dn == null) {
dn = DN.NULL_DN;
}
SearchScope searchScope = scope.getValue();
if (searchScope == null) {
searchScope = SearchScope.SUB;
}
try {
searchURLs.add(new LDAPURL("ldap", null, null, dn, requestedAttributes.toArray(StaticUtils.NO_STRINGS), searchScope, filter));
} catch (final LDAPException e) {
Debug.debugException(e);
// This should never happen.
throw new ArgumentException(StaticUtils.getExceptionMessage(e), e);
}
}
// Create the result writer.
final String outputFormatStr = StaticUtils.toLowerCase(outputFormat.getValue());
if (outputFormatStr.equals("json")) {
resultWriter = new JSONLDAPResultWriter(getOut());
} else if (outputFormatStr.equals("csv") || outputFormatStr.equals("multi-valued-csv") || outputFormatStr.equals("tab-delimited") || outputFormatStr.equals("multi-valued-tab-delimited")) {
// These output formats cannot be used with the --ldapURLFile argument.
if (ldapURLFile.isPresent()) {
throw new ArgumentException(ERR_LDIFSEARCH_OUTPUT_FORMAT_NOT_SUPPORTED_WITH_URLS.get(outputFormat.getValue(), ldapURLFile.getIdentifierString()));
}
// These output formats require a set of requested attributes.
if (requestedAttributes.isEmpty()) {
throw new ArgumentException(ERR_LDIFSEARCH_OUTPUT_FORMAT_REQUIRES_REQUESTED_ATTRS.get(outputFormat.getValue()));
}
final OutputFormat format;
final boolean includeAllValues;
switch(outputFormatStr) {
case "multi-valued-csv":
format = OutputFormat.CSV;
includeAllValues = true;
break;
case "tab-delimited":
format = OutputFormat.TAB_DELIMITED_TEXT;
includeAllValues = false;
break;
case "multi-valued-tab-delimited":
format = OutputFormat.TAB_DELIMITED_TEXT;
includeAllValues = true;
break;
case "csv":
default:
format = OutputFormat.CSV;
includeAllValues = false;
break;
}
resultWriter = new ColumnBasedLDAPResultWriter(getOut(), format, requestedAttributes, WRAP_COLUMN, includeAllValues);
} else if (outputFormatStr.equals("dns-only")) {
resultWriter = new DNsOnlyLDAPResultWriter(getOut());
} else if (outputFormatStr.equals("values-only")) {
resultWriter = new ValuesOnlyLDAPResultWriter(getOut());
} else {
final int wc;
if (doNotWrap.isPresent()) {
wc = Integer.MAX_VALUE;
} else if (wrapColumn.isPresent()) {
wc = wrapColumn.getValue();
} else {
wc = WRAP_COLUMN;
}
resultWriter = new LDIFLDAPResultWriter(getOut(), wc);
}
}
Aggregations