From ad388b56995e7283cf2a505c70ace1d320bae56e Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Wed, 18 Jan 2017 02:13:34 +0100 Subject: Cleaned up error handling in feature and authority CLIs. The feature and authority CLIs have been modified to use Exceptions instead of System.exit() such that errors can be handled consistently. --- .../cmstools/authority/AuthorityCreateCLI.java | 35 ++++---------- .../cmstools/authority/AuthorityDisableCLI.java | 18 ++----- .../cmstools/authority/AuthorityEnableCLI.java | 18 ++----- .../cmstools/authority/AuthorityFindCLI.java | 14 +----- .../cmstools/authority/AuthorityKeyExportCLI.java | 56 +++++++--------------- .../cmstools/authority/AuthorityRemoveCLI.java | 29 ++++------- .../cmstools/authority/AuthorityShowCLI.java | 28 +++-------- .../netscape/cmstools/feature/FeatureFindCLI.java | 14 +----- .../netscape/cmstools/feature/FeatureShowCLI.java | 22 ++------- 9 files changed, 57 insertions(+), 177 deletions(-) (limited to 'base/java-tools/src/com') diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java index 7f40662b6..9cea963bf 100644 --- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java @@ -4,7 +4,6 @@ import java.util.Arrays; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.authority.AuthorityData; import com.netscape.certsrv.ca.AuthorityID; @@ -34,29 +33,19 @@ public class AuthorityCreateCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); String[] cmdArgs = cmd.getArgs(); - if (cmdArgs.length != 1) { - if (cmdArgs.length < 1) - System.err.println("No DN specified."); - else - System.err.println("Too many arguments."); - printHelp(); - System.exit(-1); + + if (cmdArgs.length < 1) { + throw new Exception("No DN specified."); + + } else if (cmdArgs.length > 1) { + throw new Exception("Too many arguments."); } String parentAIDString = null; @@ -65,14 +54,10 @@ public class AuthorityCreateCLI extends CLI { try { new AuthorityID(parentAIDString); } catch (IllegalArgumentException e) { - System.err.println("Bad CA ID: " + parentAIDString); - printHelp(); - System.exit(-1); + throw new Exception("Bad CA ID: " + parentAIDString, e); } } else { - System.err.println("Must specify parent authority"); - printHelp(); - System.exit(-1); + throw new Exception("Must specify parent authority"); } String desc = null; diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java index b1265b503..7c5da13de 100644 --- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java @@ -3,7 +3,6 @@ package com.netscape.cmstools.authority; import java.util.Arrays; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.authority.AuthorityData; import com.netscape.cmstools.cli.CLI; @@ -24,27 +23,16 @@ public class AuthorityDisableCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length < 1) { - System.err.println("Error: No ID specified."); - printHelp(); - System.exit(-1); + throw new Exception("No ID specified."); } AuthorityData data = new AuthorityData( diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java index 5afef455b..7ff25a450 100644 --- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java @@ -3,7 +3,6 @@ package com.netscape.cmstools.authority; import java.util.Arrays; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.authority.AuthorityData; import com.netscape.cmstools.cli.CLI; @@ -24,27 +23,16 @@ public class AuthorityEnableCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length < 1) { - System.err.println("Error: No ID specified."); - printHelp(); - System.exit(-1); + throw new Exception("No ID specified."); } AuthorityData data = new AuthorityData( diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java index c1aa99fc6..2b96e3aab 100644 --- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java @@ -4,7 +4,6 @@ import java.util.Arrays; import java.util.List; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.authority.AuthorityData; import com.netscape.cmstools.cli.CLI; @@ -26,21 +25,12 @@ public class AuthorityFindCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } @SuppressWarnings("unused") - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); List datas = authorityCLI.authorityClient.listCAs(); diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java index a3dee82c8..2fafe5204 100644 --- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java @@ -6,8 +6,6 @@ import java.security.PublicKey; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; - import org.mozilla.jss.CryptoManager; import org.mozilla.jss.crypto.CryptoToken; import org.mozilla.jss.crypto.IVParameterSpec; @@ -46,64 +44,44 @@ public class AuthorityKeyExportCLI extends CLI { } public void execute(String[] args) throws Exception { - CommandLine cmd = null; - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("help")) { - // Display usage printHelp(); - System.exit(0); + return; } String filename = cmd.getOptionValue("output"); if (filename == null) { - System.err.println("Error: No output file specified."); - printHelp(); - System.exit(-1); + throw new Exception("No output file specified."); } String wrapNick = cmd.getOptionValue("wrap-nickname"); if (wrapNick == null) { - System.err.println("Error: no wrapping key nickname specified."); - printHelp(); - System.exit(-1); + throw new Exception("No wrapping key nickname specified."); } String targetNick = cmd.getOptionValue("target-nickname"); if (targetNick == null) { - System.err.println("Error: no target key nickname specified."); - printHelp(); - System.exit(-1); + throw new Exception("No target key nickname specified."); } - try { - CryptoManager cm = CryptoManager.getInstance(); - X509Certificate wrapCert = cm.findCertByNickname(wrapNick); - X509Certificate targetCert = cm.findCertByNickname(targetNick); + CryptoManager cm = CryptoManager.getInstance(); + X509Certificate wrapCert = cm.findCertByNickname(wrapNick); + X509Certificate targetCert = cm.findCertByNickname(targetNick); - PublicKey wrappingKey = wrapCert.getPublicKey(); - PrivateKey toBeWrapped = cm.findPrivKeyByCert(targetCert); - CryptoToken token = cm.getInternalKeyStorageToken(); + PublicKey wrappingKey = wrapCert.getPublicKey(); + PrivateKey toBeWrapped = cm.findPrivKeyByCert(targetCert); + CryptoToken token = cm.getInternalKeyStorageToken(); - byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 }; - IVParameterSpec ivps = new IVParameterSpec(iv); + byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 }; + IVParameterSpec ivps = new IVParameterSpec(iv); - byte[] data = CryptoUtil.createPKIArchiveOptions( - token, wrappingKey, toBeWrapped, - KeyGenAlgorithm.DES3, 0, ivps); - - Files.newOutputStream(Paths.get(filename)).write(data); - } catch (Throwable e) { - e.printStackTrace(); - System.exit(-1); - } + byte[] data = CryptoUtil.createPKIArchiveOptions( + token, wrappingKey, toBeWrapped, + KeyGenAlgorithm.DES3, 0, ivps); + Files.newOutputStream(Paths.get(filename)).write(data); } } diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java index 42265b180..f69948aaf 100644 --- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java @@ -5,7 +5,6 @@ import java.io.InputStreamReader; import java.util.Arrays; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.cmstools.cli.CLI; import com.netscape.cmstools.cli.MainCLI; @@ -28,29 +27,19 @@ public class AuthorityRemoveCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); String[] cmdArgs = cmd.getArgs(); - if (cmdArgs.length != 1) { - if (cmdArgs.length < 1) - System.err.println("No ID specified."); - else - System.err.println("Too many arguments."); - printHelp(); - System.exit(-1); + + if (cmdArgs.length < 1) { + throw new Exception("No ID specified."); + + } else if (cmdArgs.length > 1) { + throw new Exception("Too many arguments."); } if (!cmd.hasOption("force")) { @@ -60,7 +49,7 @@ public class AuthorityRemoveCLI extends CLI { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = reader.readLine(); if (!line.equalsIgnoreCase("Y")) { - System.exit(-1); + return; } } diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java index c95660248..b26371070 100644 --- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java @@ -4,7 +4,6 @@ import java.util.Arrays; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.authority.AuthorityData; import com.netscape.certsrv.authority.AuthorityResource; @@ -30,45 +29,32 @@ public class AuthorityShowCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); String[] cmdArgs = cmd.getArgs(); String caIDString = null; + if (cmdArgs.length > 1) { - System.err.println("Error: too many arguments."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments."); + } else if (cmdArgs.length == 1) { caIDString = cmdArgs[0]; } if (cmd.hasOption("host-authority")) { if (caIDString != null) { - System.err.println("Error: authority ID and --host-authority are mutually exclusive."); - printHelp(); - System.exit(-1); + throw new Exception("Authority ID and --host-authority are mutually exclusive."); } caIDString = AuthorityResource.HOST_AUTHORITY; } if (caIDString == null) { - System.err.println("Error: No ID specified."); - printHelp(); - System.exit(-1); + throw new Exception("No ID specified."); } AuthorityData data = authorityCLI.authorityClient.getCA(caIDString); diff --git a/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java b/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java index 5bc77071a..72f22ec71 100644 --- a/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java @@ -21,7 +21,6 @@ import java.util.Arrays; import java.util.List; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.system.Feature; import com.netscape.cmstools.cli.CLI; @@ -43,21 +42,12 @@ public class FeatureFindCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } @SuppressWarnings("unused") - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); List features = featureCLI.featureClient.listFeatures(); diff --git a/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java b/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java index 59cfb67eb..7eda79951 100644 --- a/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java @@ -20,7 +20,6 @@ package com.netscape.cmstools.feature; import java.util.Arrays; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.system.Feature; import com.netscape.cmstools.cli.CLI; @@ -41,33 +40,20 @@ public class FeatureShowCLI extends CLI { public void execute(String[] args) throws Exception { // Always check for "--help" prior to parsing if (Arrays.asList(args).contains("--help")) { - // Display usage printHelp(); - System.exit(0); + return; } - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args); - } catch (ParseException e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args); String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length > 1) { - System.err.println("Error: too many arguments."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments."); } if (cmdArgs.length == 0) { - System.err.println("Error: No ID specified."); - printHelp(); - System.exit(-1); + throw new Exception("No ID specified."); } String featureID = cmdArgs[0]; -- cgit