Search in sources :

Example 1 with DNsOnlyLDAPResultWriter

use of com.unboundid.ldap.sdk.unboundidds.tools.DNsOnlyLDAPResultWriter 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);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) OutputFormat(com.unboundid.util.OutputFormat) DN(com.unboundid.ldap.sdk.DN) DNsOnlyLDAPResultWriter(com.unboundid.ldap.sdk.unboundidds.tools.DNsOnlyLDAPResultWriter) ValuesOnlyLDAPResultWriter(com.unboundid.ldap.sdk.unboundidds.tools.ValuesOnlyLDAPResultWriter) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPURL(com.unboundid.ldap.sdk.LDAPURL) Filter(com.unboundid.ldap.sdk.Filter) JSONLDAPResultWriter(com.unboundid.ldap.sdk.unboundidds.tools.JSONLDAPResultWriter) Iterator(java.util.Iterator) SearchScope(com.unboundid.ldap.sdk.SearchScope) List(java.util.List) ArrayList(java.util.ArrayList) ArgumentException(com.unboundid.util.args.ArgumentException) ColumnBasedLDAPResultWriter(com.unboundid.ldap.sdk.unboundidds.tools.ColumnBasedLDAPResultWriter) LDIFLDAPResultWriter(com.unboundid.ldap.sdk.unboundidds.tools.LDIFLDAPResultWriter) File(java.io.File)

Aggregations

DN (com.unboundid.ldap.sdk.DN)1 Filter (com.unboundid.ldap.sdk.Filter)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPURL (com.unboundid.ldap.sdk.LDAPURL)1 SearchScope (com.unboundid.ldap.sdk.SearchScope)1 ColumnBasedLDAPResultWriter (com.unboundid.ldap.sdk.unboundidds.tools.ColumnBasedLDAPResultWriter)1 DNsOnlyLDAPResultWriter (com.unboundid.ldap.sdk.unboundidds.tools.DNsOnlyLDAPResultWriter)1 JSONLDAPResultWriter (com.unboundid.ldap.sdk.unboundidds.tools.JSONLDAPResultWriter)1 LDIFLDAPResultWriter (com.unboundid.ldap.sdk.unboundidds.tools.LDIFLDAPResultWriter)1 ValuesOnlyLDAPResultWriter (com.unboundid.ldap.sdk.unboundidds.tools.ValuesOnlyLDAPResultWriter)1 OutputFormat (com.unboundid.util.OutputFormat)1 ArgumentException (com.unboundid.util.args.ArgumentException)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1