From 88496c3a54b4c6e969cb0dce5cf67e5da6846740 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Mon, 31 Oct 2016 13:59:39 +0000
Subject: [PATCH] OPENDJ-2772 Code cleanup

---
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java |  137 +++++++++++++--------------------------------
 1 files changed, 41 insertions(+), 96 deletions(-)

diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java
index 1871364..3904290 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java
@@ -18,24 +18,26 @@
 
 import static com.forgerock.opendj.cli.CliMessages.INFO_FILE_PLACEHOLDER;
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
+import static com.forgerock.opendj.cli.Utils.throwIfArgumentsConflict;
+import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
 import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.cli.CommonArguments.*;
+import static com.forgerock.opendj.ldap.tools.Utils.addControlsToRequest;
+import static com.forgerock.opendj.ldap.tools.Utils.ensureLdapProtocolVersionIsSupported;
+import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage;
+import static com.forgerock.opendj.ldap.tools.Utils.readControls;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.Connection;
-import org.forgerock.opendj.ldap.ConnectionFactory;
-import org.forgerock.opendj.ldap.DecodeException;
 import org.forgerock.opendj.ldap.LdapException;
 import org.forgerock.opendj.ldap.ResultCode;
-import org.forgerock.opendj.ldap.controls.Control;
 import org.forgerock.opendj.ldap.requests.PasswordModifyExtendedRequest;
 import org.forgerock.opendj.ldap.requests.Requests;
 import org.forgerock.opendj.ldap.responses.PasswordModifyExtendedResult;
 
 import com.forgerock.opendj.cli.ArgumentException;
-import com.forgerock.opendj.cli.ArgumentParser;
 import com.forgerock.opendj.cli.BooleanArgument;
 import com.forgerock.opendj.cli.ConnectionFactoryProvider;
 import com.forgerock.opendj.cli.ConsoleApplication;
@@ -65,7 +67,14 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        final int retCode = new LDAPPasswordModify().run(args);
+        final LDAPPasswordModify ldapPasswordModify = new LDAPPasswordModify();
+        int retCode;
+        try {
+            retCode = ldapPasswordModify.run(args);
+        } catch (final LDAPToolException e) {
+            e.printErrorMessage(ldapPasswordModify);
+            retCode = e.getResultCode();
+        }
         System.exit(filterExitCode(retCode));
     }
 
@@ -85,21 +94,22 @@
         return verbose.isPresent();
     }
 
-    private int run(final String[] args) {
+    private int run(final String[] args) throws LDAPToolException {
         // Create the command-line argument parser for use with this program.
         final LocalizableMessage toolDescription = INFO_LDAPPWMOD_TOOL_DESCRIPTION.get();
-        final ArgumentParser argParser =
-                new ArgumentParser(LDAPPasswordModify.class.getName(), toolDescription, false);
+        final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPPasswordModify.class.getName())
+                .toolDescription(toolDescription)
+                .needAuthenticatedConnectionFactory()
+                .build();
         argParser.setVersionHandler(newSdkVersionHandler());
         argParser.setShortToolDescription(REF_SHORT_DESC_LDAPPASSWORDMODIFY.get());
 
         ConnectionFactoryProvider connectionFactoryProvider;
-        ConnectionFactory connectionFactory;
 
         FileBasedArgument currentPWFile;
         FileBasedArgument newPWFile;
         BooleanArgument showUsage;
-        IntegerArgument version;
+        IntegerArgument ldapProtocolVersion;
         StringArgument currentPW;
         StringArgument controlStr;
         StringArgument newPW;
@@ -148,16 +158,11 @@
                             .description(INFO_LDAPPWMOD_DESCRIPTION_AUTHZID.get())
                             .valuePlaceholder(INFO_PROXYAUTHID_PLACEHOLDER.get())
                             .buildAndAddToParser(argParser);
-            controlStr =
-                    StringArgument.builder("control")
-                            .shortIdentifier('J')
-                            .description(INFO_DESCRIPTION_CONTROLS.get())
-                            .multiValued()
-                            .valuePlaceholder(INFO_LDAP_CONTROL_PLACEHOLDER.get())
-                            .buildAndAddToParser(argParser);
+            controlStr = controlArgument();
+            argParser.addArgument(controlStr);
 
-            version = ldapVersionArgument();
-            argParser.addArgument(version);
+            ldapProtocolVersion = ldapVersionArgument();
+            argParser.addArgument(ldapProtocolVersion);
 
             verbose = verboseArgument();
             argParser.addArgument(verbose);
@@ -170,66 +175,27 @@
             return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
         }
 
-        // Parse the command-line arguments provided to this program.
-        try {
-            argParser.parseArguments(args);
-
-            // If we should just display usage or version information, then print it and exit.
-            if (argParser.usageOrVersionDisplayed()) {
-                return 0;
-            }
-
-            connectionFactory = connectionFactoryProvider.getAuthenticatedConnectionFactory();
-        } catch (final ArgumentException ae) {
-            argParser.displayMessageAndUsageReference(getErrStream(), ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
-            return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
+        argParser.parseArgumentsNoBindRequest(args, getErrStream(), connectionFactoryProvider);
+        if (argParser.usageOrVersionDisplayed()) {
+            return ResultCode.SUCCESS.intValue();
         }
+        ensureLdapProtocolVersionIsSupported(ldapProtocolVersion);
 
         final PasswordModifyExtendedRequest request = Requests.newPasswordModifyExtendedRequest();
+        addControlsToRequest(request, readControls(controlStr));
+
         try {
-            final int versionNumber = version.getIntValue();
-            if (versionNumber != 2 && versionNumber != 3) {
-                errPrintln(ERR_DESCRIPTION_INVALID_VERSION.get(String.valueOf(versionNumber)));
-                return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
-            }
-        } catch (final ArgumentException ae) {
-            errPrintln(ERR_DESCRIPTION_INVALID_VERSION.get(version.getValue()));
-            return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
-        }
-
-        if (controlStr.isPresent()) {
-            for (final String ctrlString : controlStr.getValues()) {
-                try {
-                    final Control ctrl = Utils.getControl(ctrlString);
-                    request.addControl(ctrl);
-                } catch (final DecodeException de) {
-                    errPrintln(ERR_TOOL_INVALID_CONTROL_STRING.get(ctrlString));
-                    ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
-                }
-            }
-        }
-
-        if (newPW.isPresent() && newPWFile.isPresent()) {
-            final LocalizableMessage message =
-                    ERR_LDAPPWMOD_CONFLICTING_ARGS.get(newPW.getLongIdentifier(), newPWFile
-                            .getLongIdentifier());
-            errPrintln(message);
-            return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
-        }
-
-        if (currentPW.isPresent() && currentPWFile.isPresent()) {
-            final LocalizableMessage message =
-                    ERR_LDAPPWMOD_CONFLICTING_ARGS.get(currentPW.getLongIdentifier(), currentPWFile
-                            .getLongIdentifier());
-            errPrintln(message);
-            return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
+            throwIfArgumentsConflict(newPW, newPWFile);
+            throwIfArgumentsConflict(currentPW, currentPWFile);
+        } catch (final ArgumentException e) {
+            throw newToolParamException(e, e.getMessageObject());
         }
 
         Connection connection;
         try {
-            connection = connectionFactory.getConnection();
+            connection = argParser.getConnectionFactory().getConnection();
         } catch (final LdapException ere) {
-            return Utils.printErrorMessage(this, ere);
+            return printErrorMessage(this, ere, ERR_LDAPPWMOD_FAILED);
         }
 
         if (proxyAuthzID.isPresent()) {
@@ -252,37 +218,16 @@
         try {
             result = connection.extendedRequest(request);
         } catch (final LdapException e) {
-            LocalizableMessage message =
-                    ERR_LDAPPWMOD_FAILED.get(e.getResult().getResultCode().intValue(), e
-                            .getResult().getResultCode().toString());
-            errPrintln(message);
-
-            final String errorMessage = e.getResult().getDiagnosticMessage();
-            if (errorMessage != null && errorMessage.length() > 0) {
-                message = ERR_LDAPPWMOD_FAILURE_ERROR_MESSAGE.get(errorMessage);
-                errPrintln(message);
-            }
-
-            final String matchedDN = e.getResult().getMatchedDN();
-            if (matchedDN != null && matchedDN.length() > 0) {
-                message = ERR_LDAPPWMOD_FAILURE_MATCHED_DN.get(matchedDN);
-                errPrintln(message);
-            }
-            return e.getResult().getResultCode().intValue();
+            return printErrorMessage(this, e, ERR_LDAPPWMOD_FAILED);
         }
 
         println(INFO_LDAPPWMOD_SUCCESSFUL.get());
-
-        final String additionalInfo = result.getDiagnosticMessage();
-        if (additionalInfo != null && additionalInfo.length() > 0) {
-            println(INFO_LDAPPWMOD_ADDITIONAL_INFO.get(additionalInfo));
-        }
-
+        Utils.printlnTextMsg(this, INFO_LDAPPWMOD_ADDITIONAL_INFO, result.getDiagnosticMessage());
         if (result.getGeneratedPassword() != null) {
-            println(INFO_LDAPPWMOD_GENERATED_PASSWORD.get(ByteString.valueOfBytes(
-                    result.getGeneratedPassword()).toString()));
+            println(INFO_LDAPPWMOD_GENERATED_PASSWORD.get(
+                    ByteString.valueOfBytes(result.getGeneratedPassword()).toString()));
         }
 
-        return 0;
+        return ResultCode.SUCCESS.intValue();
     }
 }

--
Gitblit v1.10.0