Search in sources :

Example 6 with FormattableColumn

use of com.unboundid.util.FormattableColumn in project ldapsdk by pingidentity.

the class OIDLookup method doToolProcessing.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Get a reference to the default OID registry.
    OIDRegistry oidRegistry = OIDRegistry.getDefault();
    // If any schema paths were provided, then read the schema(s) and use that
    // to augment the default OID registry.  If not, and if this tool is running
    // from a Ping Identity Directory Server installation, then see if we can
    // use its default schema.
    List<File> schemaPaths = Collections.emptyList();
    if ((schemaPathArg != null) && (schemaPathArg.isPresent())) {
        schemaPaths = schemaPathArg.getValues();
    } else {
        try {
            final File instanceRoot = InternalSDKHelper.getPingIdentityServerRoot();
            if (instanceRoot != null) {
                final File instanceRootSchemaDir = StaticUtils.constructPath(instanceRoot, "config", "schema");
                if (new File(instanceRootSchemaDir, "00-core.ldif").exists()) {
                    schemaPaths = Collections.singletonList(instanceRootSchemaDir);
                }
            }
        } catch (final Throwable t) {
        // This is fine.  We're just not running with access to a Ping Identity
        // Directory Server.
        }
    }
    if (!schemaPaths.isEmpty()) {
        try {
            oidRegistry = augmentOIDRegistry(oidRegistry, schemaPaths);
        } catch (final LDAPException e) {
            Debug.debugException(e);
            wrapErr(0, WRAP_COLUMN, e.getMessage());
            return e.getResultCode();
        }
    }
    // See if there is a search string.  If so, then identify the appropriate
    // set of matching OID registry items.  Otherwise, just grab everything in
    // the OID registry.
    final Collection<OIDRegistryItem> matchingItems;
    if ((parser != null) && (!parser.getTrailingArguments().isEmpty())) {
        matchingItems = new ArrayList<>();
        final String lowerSearchString = StaticUtils.toLowerCase(parser.getTrailingArguments().get(0));
        for (final OIDRegistryItem item : oidRegistry.getItems().values()) {
            if (itemMatchesSearchString(item, lowerSearchString, exactMatchArg.isPresent())) {
                matchingItems.add(item);
            }
        }
    } else {
        matchingItems = oidRegistry.getItems().values();
    }
    // If there weren't any matches, then display a message if appropriate.
    boolean json = false;
    ColumnFormatter columnFormatter = null;
    if ((outputFormatArg != null) && outputFormatArg.isPresent()) {
        final String outputFormat = outputFormatArg.getValue();
        if (outputFormat != null) {
            if (outputFormat.equalsIgnoreCase(OUTPUT_FORMAT_CSV)) {
                columnFormatter = new ColumnFormatter(false, null, OutputFormat.CSV, null, new FormattableColumn(1, HorizontalAlignment.LEFT, "OID"), new FormattableColumn(1, HorizontalAlignment.LEFT, "Name"), new FormattableColumn(1, HorizontalAlignment.LEFT, "Type"), new FormattableColumn(1, HorizontalAlignment.LEFT, "Origin"), new FormattableColumn(1, HorizontalAlignment.LEFT, "URL"));
            } else if (outputFormat.equalsIgnoreCase(OUTPUT_FORMAT_JSON)) {
                json = true;
            } else if (outputFormat.equalsIgnoreCase(OUTPUT_FORMAT_TAB_DELIMITED)) {
                columnFormatter = new ColumnFormatter(false, null, OutputFormat.TAB_DELIMITED_TEXT, null, new FormattableColumn(1, HorizontalAlignment.LEFT, "OID"), new FormattableColumn(1, HorizontalAlignment.LEFT, "Name"), new FormattableColumn(1, HorizontalAlignment.LEFT, "Type"), new FormattableColumn(1, HorizontalAlignment.LEFT, "Origin"), new FormattableColumn(1, HorizontalAlignment.LEFT, "URL"));
            }
        }
    }
    final int numMatches = matchingItems.size();
    switch(numMatches) {
        case 0:
            wrapComment(WARN_OID_LOOKUP_NO_MATCHES.get());
            return ResultCode.NO_RESULTS_RETURNED;
        case 1:
            wrapComment(INFO_OID_LOOKUP_ONE_MATCH.get());
            break;
        default:
            wrapComment(INFO_OID_LOOKUP_MULTIPLE_MATCHES.get(numMatches));
            break;
    }
    if (columnFormatter != null) {
        for (final String line : columnFormatter.getHeaderLines(false)) {
            out(line);
        }
    }
    for (final OIDRegistryItem item : matchingItems) {
        if (json) {
            out(item.asJSONObject().toSingleLineString());
        } else if (columnFormatter != null) {
            out(columnFormatter.formatRow(item.getOID(), item.getName(), item.getType(), item.getOrigin(), item.getURL()));
        } else {
            out();
            out(INFO_OID_LOOKUP_OUTPUT_LINE_OID.get(item.getOID()));
            out(INFO_OID_LOOKUP_OUTPUT_LINE_NAME.get(item.getName()));
            out(INFO_OID_LOOKUP_OUTPUT_LINE_TYPE.get(item.getType()));
            final String origin = item.getOrigin();
            if (origin != null) {
                out(INFO_OID_LOOKUP_OUTPUT_LINE_ORIGIN.get(origin));
            }
            final String url = item.getURL();
            if (url != null) {
                out(INFO_OID_LOOKUP_OUTPUT_LINE_URL.get(url));
            }
        }
    }
    return ResultCode.SUCCESS;
}
Also used : OIDRegistryItem(com.unboundid.util.OIDRegistryItem) OIDRegistry(com.unboundid.util.OIDRegistry) LDAPException(com.unboundid.ldap.sdk.LDAPException) FormattableColumn(com.unboundid.util.FormattableColumn) File(java.io.File) ColumnFormatter(com.unboundid.util.ColumnFormatter) NotNull(com.unboundid.util.NotNull)

Aggregations

ColumnFormatter (com.unboundid.util.ColumnFormatter)6 FormattableColumn (com.unboundid.util.FormattableColumn)6 NotNull (com.unboundid.util.NotNull)6 LDAPException (com.unboundid.ldap.sdk.LDAPException)5 ResultCode (com.unboundid.ldap.sdk.ResultCode)5 OutputFormat (com.unboundid.util.OutputFormat)5 Control (com.unboundid.ldap.sdk.Control)4 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)4 FixedRateBarrier (com.unboundid.util.FixedRateBarrier)4 ObjectPair (com.unboundid.util.ObjectPair)4 RateAdjustor (com.unboundid.util.RateAdjustor)4 ResultCodeCounter (com.unboundid.util.ResultCodeCounter)4 ValuePattern (com.unboundid.util.ValuePattern)4 ArgumentException (com.unboundid.util.args.ArgumentException)4 IOException (java.io.IOException)4 ParseException (java.text.ParseException)4 ArrayList (java.util.ArrayList)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 AssertionRequestControl (com.unboundid.ldap.sdk.controls.AssertionRequestControl)3