Search in sources :

Example 1 with OIDRegistry

use of com.unboundid.util.OIDRegistry 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

LDAPException (com.unboundid.ldap.sdk.LDAPException)1 ColumnFormatter (com.unboundid.util.ColumnFormatter)1 FormattableColumn (com.unboundid.util.FormattableColumn)1 NotNull (com.unboundid.util.NotNull)1 OIDRegistry (com.unboundid.util.OIDRegistry)1 OIDRegistryItem (com.unboundid.util.OIDRegistryItem)1 File (java.io.File)1