From db1a3826ba28aa6231756ebd224f16677440ed2e Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Tue, 01 Mar 2016 09:43:01 +0000
Subject: [PATCH] OPENDJ-2701 status command using -n fails if no password is provided

---
 opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommonArguments.java    |   20 +++++++++
 opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java |   26 ------------
 opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommandBuilder.java     |   18 +++++++++
 opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java              |   33 ++++++++++++++++
 4 files changed, 71 insertions(+), 26 deletions(-)

diff --git a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommandBuilder.java b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommandBuilder.java
index 2e2cfb8..6f379e8 100644
--- a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommandBuilder.java
+++ b/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
diff --git a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommonArguments.java b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommonArguments.java
index 63f0a7d..ce34201 100644
--- a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/CommonArguments.java
+++ b/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();
diff --git a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java
index 6384a43..3e22ef7 100644
--- a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConsoleApplication.java
+++ b/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);
     }
 
     /**
diff --git a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
index aa8c578..5d6cf7d 100644
--- a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
+++ b/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;
+                }
+            }
+        };
+    }
 }

--
Gitblit v1.10.0