mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
15.35.2016 db1a3826ba28aa6231756ebd224f16677440ed2e
OPENDJ-2701 status command using -n fails if no password is provided

Changes needed for refactoring in server.
* Moves user validation input for port number in Utils to allow
server code to use it.
* Little enhancements in CommandBuilder and in CommonsArguments
4 files modified
97 ■■■■ changed files
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommandBuilder.java 18 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommonArguments.java 20 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java 26 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java 33 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommandBuilder.java
@@ -60,6 +60,11 @@
        }
    }
    /** Creates a {@link CommandBuilder} with {@code null} command and subcommand names. */
    public CommandBuilder() {
        this(null, null);
    }
    /**
     * The constructor for the CommandBuilder.
     *
@@ -111,6 +116,19 @@
    }
    /**
     * Removes the provided arguments from this CommandBuilder.
     * Arguments which are not in this {@link CommandBuilder} will be ignored.
     *
     * @param arguments
     *            Arguments to be removed.
     */
    public void removeArguments(final Argument... arguments) {
        for (final Argument argument : arguments) {
            removeArgument(argument);
        }
    }
    /**
     * Appends the arguments of another command builder to this command builder.
     *
     * @param builder
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommonArguments.java
@@ -418,9 +418,27 @@
     *             If there is a problem with any of the parameters used to create this argument.
     */
    public static StringArgument bindDNArgument(final String defaultBindDN) throws ArgumentException {
        return bindDNArgument(defaultBindDN, INFO_DESCRIPTION_BINDDN.get());
    }
    /**
     * Returns the "bindDN" string argument. <br/>
     * <i> N.B : the 'D' short option is also used by rootUserDN.</i>
     *
     * @param defaultBindDN
     *            The default bind DN.
     * @param description
     *            The localized description to print in help messages.
     * @return The "bindDN" argument.
     * @throws ArgumentException
     *             If there is a problem with any of the parameters used to create this argument.
     */
    public static StringArgument bindDNArgument(final String defaultBindDN, final LocalizableMessage description)
            throws ArgumentException {
        return StringArgument.builder(OPTION_LONG_BINDDN)
                .shortIdentifier(OPTION_SHORT_BINDDN)
                .description(INFO_DESCRIPTION_BINDDN.get())
                .description(description)
                .defaultValue(defaultBindDN)
                .valuePlaceholder(INFO_BINDDN_PLACEHOLDER.get())
                .buildArgument();
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java
@@ -556,35 +556,11 @@
     *             If the port could not be retrieved for some reason.
     */
    public final int readPort(LocalizableMessage prompt, final int defaultValue) throws ClientException {
        final ValidationCallback<Integer> callback = new ValidationCallback<Integer>() {
            @Override
            public Integer validate(ConsoleApplication app, String input) throws ClientException {
                final String ninput = input.trim();
                if (ninput.length() == 0) {
                    return defaultValue;
                }
                try {
                    int i = Integer.parseInt(ninput);
                    if (i < 1 || i > 65535) {
                        throw new NumberFormatException();
                    }
                    return i;
                } catch (NumberFormatException e) {
                    // Try again...
                    app.println();
                    app.println(ERR_BAD_PORT_NUMBER.get(ninput));
                    app.println();
                    return null;
                }
            }
        };
        if (defaultValue != -1) {
            prompt = INFO_PROMPT_SINGLE_DEFAULT.get(prompt, defaultValue);
        }
        return readValidatedInput(prompt, callback, CONFIRMATION_MAX_TRIES);
        return readValidatedInput(prompt, Utils.portValidationCallback(defaultValue), CONFIRMATION_MAX_TRIES);
    }
    /**
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
@@ -625,4 +625,37 @@
        Arrays.fill(str, charToRepeat);
        return new String(str);
    }
    /**
     * Return a {@link ValidationCallback<Integer>} which can be used to validate a port number.
     *
     * @param defaultPort
     *        The default value to suggest to the user.
     * @return a {@link ValidationCallback<Integer>} which can be used to validate a port number.
     */
    public static ValidationCallback<Integer> portValidationCallback(final int defaultPort) {
        return new ValidationCallback<Integer>() {
            @Override
            public Integer validate(ConsoleApplication app, String rawInput) throws ClientException {
                final String input = rawInput.trim();
                if (input.length() == 0) {
                    return defaultPort;
                }
                try {
                    int i = Integer.parseInt(input);
                    if (i < 1 || i > 65535) {
                        throw new NumberFormatException();
                    }
                    return i;
                } catch (NumberFormatException e) {
                    // Try again...
                    app.println();
                    app.println(ERR_BAD_PORT_NUMBER.get(input));
                    app.println();
                    return null;
                }
            }
        };
    }
}