diff options
author | Endi S. Dewata <edewata@redhat.com> | 2017-06-03 02:07:04 +0200 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2017-06-03 03:08:55 +0200 |
commit | af41896f083e1101b1ba62f6cc8c9be6064c6786 (patch) | |
tree | 1c82e8ccafee39b66c81dc2407f4a25ceb3fee73 /base/java-tools/src | |
parent | a614eb15476adb00df571d3ea05fdd8ea282141d (diff) | |
download | pki-af41896f083e1101b1ba62f6cc8c9be6064c6786.tar.gz pki-af41896f083e1101b1ba62f6cc8c9be6064c6786.tar.xz pki-af41896f083e1101b1ba62f6cc8c9be6064c6786.zip |
Refactored MainCLI.loadPassword() (part 1).
The method that loads password from a file in MainCLI has been
renamed into loadPassword() and modified to return early for
clarity.
https://pagure.io/dogtagpki/issue/2717
Change-Id: I9b031c31040c2d00f04d9997abcdae38163bf6d5
Diffstat (limited to 'base/java-tools/src')
-rw-r--r-- | base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java index 1b9c56906..2402196e0 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java @@ -229,7 +229,7 @@ public class MainCLI extends CLI { options.addOption(null, "version", false, "Show version number."); } - public String[] readPlaintextPasswordFromFile(String pwfile) throws Exception { + public String[] loadPassword(String pwfile) throws Exception { String[] tokenPassword = { null, null }; BufferedReader br = null; String delimiter = "="; @@ -238,11 +238,16 @@ public class MainCLI extends CLI { br = new BufferedReader(new FileReader(pwfile)); String line = br.readLine(); - if (line != null) { - if (line.isEmpty()) { - throw new Exception("File '" + pwfile + "' does not define a token or a password!"); - } else if (line.contains(delimiter)) { + if (line == null) { + throw new Exception("File '" + pwfile + "' is empty!"); + } + + if (line.isEmpty()) { + throw new Exception("File '" + pwfile + "' does not define a token or a password!"); + } + + if (line.contains(delimiter)) { // Process 'token=password' format: // // Token: tokenPassword[0] @@ -270,10 +275,7 @@ public class MainCLI extends CLI { // Set simple 'password' (do not trim leading/trailing whitespace) tokenPassword[1] = line; } - } else { - // Case of an empty password file - throw new Exception("File '" + pwfile + "' is empty!"); - } + } finally { if (br != null) { br.close(); @@ -397,7 +399,7 @@ public class MainCLI extends CLI { if (certPasswordFile != null) { // read client security database password from specified file - tokenPasswordPair = readPlaintextPasswordFromFile(certPasswordFile); + tokenPasswordPair = loadPassword(certPasswordFile); // XXX TBD set client security database token certPassword = tokenPasswordPair[1]; @@ -411,7 +413,7 @@ public class MainCLI extends CLI { if (passwordFile != null) { // read user password from specified file - tokenPasswordPair = readPlaintextPasswordFromFile(passwordFile); + tokenPasswordPair = loadPassword(passwordFile); // XXX TBD set user token password = tokenPasswordPair[1]; |