diff options
| author | Endi S. Dewata <edewata@redhat.com> | 2017-01-18 01:10:37 +0100 |
|---|---|---|
| committer | Endi S. Dewata <edewata@redhat.com> | 2017-01-30 21:51:29 +0100 |
| commit | ab05e6b094be0547c04092cd087bfe161546ac5e (patch) | |
| tree | 27f81477fe452f07b8c499c465b6539a216871d2 /base/java-tools | |
| parent | a9a02ae0b8b136fbf5e044e881f65c1c63b5653a (diff) | |
| download | pki-ab05e6b094be0547c04092cd087bfe161546ac5e.tar.gz pki-ab05e6b094be0547c04092cd087bfe161546ac5e.tar.xz pki-ab05e6b094be0547c04092cd087bfe161546ac5e.zip | |
Cleaned up error handling in key CLIs.
The key CLIs have been modified to use Exceptions instead of
System.exit() such that errors can be handled consistently.
Diffstat (limited to 'base/java-tools')
12 files changed, 70 insertions, 304 deletions
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java index e9ce7f2ec..c3116a673 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java @@ -10,7 +10,6 @@ import javax.xml.bind.Unmarshaller; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.key.KeyArchivalRequest; import com.netscape.certsrv.key.KeyRequestResponse; @@ -51,31 +50,19 @@ public class KeyArchiveCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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 != 0) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } String requestFile = cmd.getOptionValue("input"); @@ -104,15 +91,10 @@ public class KeyArchiveCLI extends CLI { } } catch (JAXBException e) { - System.err.println("Error: Cannot parse the request file."); - if (verbose) - e.printStackTrace(); - System.exit(-1); + throw new Exception("Cannot parse the request file.", e); + } catch (FileNotFoundException e) { - System.err.println("Error: Cannot locate file at path: " + requestFile); - if (verbose) - e.printStackTrace(); - System.exit(-1); + throw new Exception("Cannot locate file at path: " + requestFile, e); } } else { @@ -120,25 +102,14 @@ public class KeyArchiveCLI extends CLI { String clientKeyId = cmd.getOptionValue("clientKeyID"); String passphrase = cmd.getOptionValue("passphrase"); if (clientKeyId == null) { - System.err.println("Error: Client Key Id is not specified."); - printHelp(); - System.exit(-1); + throw new Exception("Client Key Id is not specified."); } if (passphrase == null) { - System.err.println("Error: No passphrase provided to archive."); - printHelp(); - System.exit(-1); + throw new Exception("No passphrase provided to archive."); } String realm = cmd.getOptionValue("realm"); - try { - response = keyCLI.keyClient.archivePassphrase(clientKeyId, passphrase, realm); - } catch (Exception e) { - System.err.println(e.getMessage()); - if (verbose) - e.printStackTrace(); - System.exit(-1); - } + response = keyCLI.keyClient.archivePassphrase(clientKeyId, passphrase, realm); } MainCLI.printMessage("Archival request details"); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java index 954246f7e..87d6b2f98 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java @@ -23,7 +23,6 @@ import java.util.Collection; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.key.KeyInfo; import com.netscape.certsrv.key.KeyInfoCollection; @@ -78,31 +77,19 @@ public class KeyFindCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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 != 0) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } String clientKeyID = cmd.getOptionValue("clientKeyID"); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java index 4149ee677..312fbf078 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java @@ -5,7 +5,6 @@ import java.util.List; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.key.KeyRequestResource; import com.netscape.certsrv.key.KeyRequestResponse; @@ -58,31 +57,19 @@ public class KeyGenerateCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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: Missing Client Key Id."); - printHelp(); - System.exit(-1); + throw new Exception("Missing Client Key Id."); } String clientKeyId = cmdArgs[0]; @@ -104,13 +91,9 @@ public class KeyGenerateCLI extends CLI { case KeyRequestResource.RC2_ALGORITHM: case KeyRequestResource.RSA_ALGORITHM: case KeyRequestResource.DSA_ALGORITHM: - System.err.println("Error: Key size must be specified for the algorithm used."); - printHelp(); - System.exit(-1); + throw new Exception("Key size must be specified for the algorithm used."); default: - System.err.println("Error: Algorithm not supported."); - printHelp(); - System.exit(-1); + throw new Exception("Algorithm not supported."); } } @@ -118,9 +101,7 @@ public class KeyGenerateCLI extends CLI { try { size = Integer.parseInt(keySize); } catch (NumberFormatException e) { - System.err.println("Error: Key size must be an integer."); - printHelp(); - System.exit(-1); + throw new Exception("Key size must be an integer.", e); } List<String> usages = null; String givenUsages = cmd.getOptionValue("usages"); @@ -145,9 +126,7 @@ public class KeyGenerateCLI extends CLI { clientKeyId, keyAlgorithm, size, usages, null, realm); break; default: - System.err.println("Error: Algorithm not supported."); - printHelp(); - System.exit(-1); + throw new Exception("Algorithm not supported."); } MainCLI.printMessage("Key generation request info"); KeyCLI.printKeyRequestInfo(response.getRequestInfo()); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java index b16bc41d1..1778fef47 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java @@ -22,7 +22,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.dbs.keydb.KeyId; import com.netscape.certsrv.key.KeyInfo; @@ -49,31 +48,19 @@ public class KeyModifyCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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 Key ID specified."); - printHelp(); - System.exit(-1); + throw new Exception("No Key ID specified."); } String status = cmd.getOptionValue("status"); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java index b23577f53..0bb2a2d3e 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java @@ -10,7 +10,6 @@ import javax.xml.bind.Unmarshaller; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.dbs.keydb.KeyId; import com.netscape.certsrv.key.KeyRecoveryRequest; @@ -43,31 +42,19 @@ public class KeyRecoverCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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 != 0) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } String requestFile = cmd.getOptionValue("input"); @@ -86,24 +73,16 @@ public class KeyRecoverCLI extends CLI { Utils.base64decode(req.getTransWrappedSessionKey()), Utils.base64decode(req.getNonceData()), req.getCertificate()); } catch (JAXBException e) { - System.err.println("Error: Cannot parse the request file."); - if (verbose) - e.printStackTrace(); - System.exit(-1); + throw new Exception("Cannot parse the request file.", e); } catch (FileNotFoundException e) { - System.err.println("Error: Cannot locate file at path: " + requestFile); - if (verbose) - e.printStackTrace(); - System.exit(-1); + throw new Exception("Cannot locate file at path: " + requestFile, e); } } else if (keyID != null) { String keyId = cmd.getOptionValue("keyID"); response = keyCLI.keyClient.recoverKey(new KeyId(keyId), null, null, null, null); } else { - System.err.println("Error: Neither a key ID nor a request file's path is specified."); - printHelp(); - System.exit(-1); + throw new Exception("Neither a key ID nor a request file's path is specified."); } MainCLI.printMessage("Key Recovery Request Information"); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java index de061d630..69730f9b3 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java @@ -23,7 +23,6 @@ import java.util.Collection; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.key.KeyRequestInfo; import com.netscape.certsrv.key.KeyRequestInfoCollection; @@ -83,31 +82,19 @@ public class KeyRequestFindCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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 != 0) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } String status = cmd.getOptionValue("status"); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java index 15ca4f804..d3ab01b70 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.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.key.KeyRequestInfo; import com.netscape.certsrv.request.RequestId; @@ -33,31 +32,19 @@ public class KeyRequestReviewCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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: Incorrect number of arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Incorrect number of arguments specified."); } RequestId reqId = new RequestId(cmdArgs[0]); @@ -74,9 +61,7 @@ public class KeyRequestReviewCLI extends CLI { keyCLI.keyClient.cancelRequest(reqId); break; default: - System.err.println("Error: Invalid action."); - printHelp(); - System.exit(-1); + throw new Exception("Invalid action."); } KeyRequestInfo keyRequestInfo = keyCLI.keyClient.getRequestInfo(reqId); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java index fe3ef1446..82aabfc42 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java @@ -21,7 +21,6 @@ package com.netscape.cmstools.key; import java.util.Arrays; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.key.KeyRequestInfo; import com.netscape.certsrv.request.RequestId; @@ -40,32 +39,20 @@ public class KeyRequestShowCLI extends CLI { formatter.printHelp(getFullName() + " <Request ID> [OPTIONS...]", options); } - public void execute(String[] args) { + public void execute(String[] args) throws Exception { // Check for "--help" 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 Request ID specified."); - printHelp(); - System.exit(-1); + throw new Exception("No Request ID specified."); } RequestId requestId = new RequestId(args[0].trim()); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java index 5a2b77e50..23e220a9a 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java @@ -10,7 +10,6 @@ import javax.xml.bind.Unmarshaller; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.dbs.keydb.KeyId; import com.netscape.certsrv.key.Key; @@ -60,34 +59,20 @@ public class KeyRetrieveCLI 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 != 0) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } if (cmd.getOptions().length == 0) { - System.err.println("Error: Incorrect number of parameters provided."); - printHelp(); - System.exit(-1); + throw new Exception("Incorrect number of parameters provided."); } String requestFile = cmd.getOptionValue("input"); @@ -101,8 +86,7 @@ public class KeyRetrieveCLI extends CLI { KeyRecoveryRequest req = (KeyRecoveryRequest) unmarshaller.unmarshal(fis); if (req.getKeyId() == null) { - System.err.println("Error: Key ID must be specified in the request file."); - System.exit(-1); + throw new Exception("Key ID must be specified in the request file."); } if (req.getCertificate() != null) { @@ -133,8 +117,7 @@ public class KeyRetrieveCLI extends CLI { String requestId = cmd.getOptionValue("requestID"); if ((requestId == null) && (keyId == null)) { - System.out.println("Either requestID or keyID must be specified"); - System.exit(1); + throw new Exception("Either requestID or keyID must be specified"); } if (passphrase != null) { diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java index ebc5741b6..d877222dc 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java @@ -22,7 +22,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.dbs.keydb.KeyId; import com.netscape.certsrv.key.KeyInfo; @@ -49,24 +48,14 @@ public class KeyShowCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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 clientKeyId = cmd.getOptionValue("clientKeyID"); @@ -80,10 +69,7 @@ public class KeyShowCLI extends CLI { keyInfo = keyCLI.keyClient.getActiveKeyInfo(clientKeyId); } else { - System.err.println("Error: Missing Key ID or Client Key ID."); - printHelp(); - System.exit(-1); - return; + throw new Exception("Missing Key ID or Client Key ID."); } KeyCLI.printKeyInfo(keyInfo); diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java index 3303560a6..32a3f02fa 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java @@ -1,14 +1,10 @@ package com.netscape.cmstools.key; import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; -import javax.xml.bind.JAXBException; - import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.base.ResourceMessage; import com.netscape.certsrv.key.KeyTemplate; @@ -30,41 +26,23 @@ public class KeyTemplateFindCLI extends CLI { formatter.printHelp(getFullName() + " [OPTIONS...]", options); } - public void execute(String[] args) { + 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 != 0) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } - try { - createTemplateList(); - } catch (FileNotFoundException | JAXBException e) { - System.err.println("Error: " + e.getMessage()); - if (verbose) - e.printStackTrace(); - System.exit(-1); - } + createTemplateList(); + MainCLI.printMessage(templates.size() + " entries matched"); for (KeyTemplate template : templates) { template.printTemplateInfo(); @@ -73,12 +51,11 @@ public class KeyTemplateFindCLI extends CLI { MainCLI.printMessage("Number of entries returned " + templates.size()); } - public void createTemplateList() throws FileNotFoundException, JAXBException { + public void createTemplateList() throws Exception { String templateDir = "/usr/share/pki/key/templates/"; File file = new File(templateDir); if (!file.exists()) { - System.err.println("Error: Missing template files."); - System.exit(-1); + throw new Exception("Missing template files."); } KeyTemplate template = null; ResourceMessage data = null; diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java index 30a40390a..c9ef4b19b 100644 --- a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java @@ -1,15 +1,10 @@ package com.netscape.cmstools.key; -import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.IOException; import java.util.Arrays; -import javax.xml.bind.JAXBException; - import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import com.netscape.certsrv.base.ResourceMessage; import com.netscape.certsrv.key.KeyArchivalRequest; @@ -36,71 +31,34 @@ public class KeyTemplateShowCLI extends CLI { options.addOption(option); } - public void execute(String[] args) { + 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 Template ID specified."); - printHelp(); - System.exit(-1); + throw new Exception("No Template ID specified."); } String templateId = cmdArgs[0]; String writeToFile = cmd.getOptionValue("output"); String templateDir = "/usr/share/pki/key/templates/"; String templatePath = templateDir + templateId + ".xml"; - ResourceMessage data = null; - try { - data = ResourceMessage.unmarshall(KeyArchivalRequest.class, templatePath); - } catch (FileNotFoundException | JAXBException e2) { - System.err.println("Error: " + e2.getMessage()); - if (verbose) - e2.printStackTrace(); - System.exit(-1); - } + ResourceMessage data = ResourceMessage.unmarshall(KeyArchivalRequest.class, templatePath); if (writeToFile != null) { try (FileOutputStream fOS = new FileOutputStream(writeToFile)) { data.marshall(fOS); - } catch (JAXBException e) { - System.err.println("Error: Cannot write the file"); - if (verbose) - e.printStackTrace(); - } catch (FileNotFoundException e) { - System.err.println("Error: Cannot write the file"); - if (verbose) - e.printStackTrace(); - } catch (IOException e1) { - System.err.println("Error: " + e1.getMessage()); - if (verbose) - e1.printStackTrace(); } } else { MainCLI.printMessage(data.getAttribute("description")); - try { - data.marshall(System.out); - } catch (JAXBException e) { - System.err.println(e.getMessage()); - if (verbose) - e.printStackTrace(); - } + data.marshall(System.out); } } } |
