>From 72b24d62b52b208aee589d767bab71af4624cfeb Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 23 Mar 2017 23:33:03 +0100
Subject: [PATCH] Refactored CA CertCLI.

The CA CertCLI and its submodules have been modified to use lazy
initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/cert/CertCLI.java    | 23 +++++++++++-----------
 .../com/netscape/cmstools/cert/CertFindCLI.java    |  4 +++-
 .../com/netscape/cmstools/cert/CertHoldCLI.java    |  8 +++++---
 .../netscape/cmstools/cert/CertReleaseHoldCLI.java |  8 +++++---
 .../netscape/cmstools/cert/CertRequestFindCLI.java |  4 +++-
 .../cmstools/cert/CertRequestProfileFindCLI.java   |  4 +++-
 .../cmstools/cert/CertRequestProfileShowCLI.java   |  4 +++-
 .../cmstools/cert/CertRequestReviewCLI.java        | 20 ++++++++++---------
 .../netscape/cmstools/cert/CertRequestShowCLI.java |  4 +++-
 .../cmstools/cert/CertRequestSubmitCLI.java        |  7 +++++--
 .../com/netscape/cmstools/cert/CertRevokeCLI.java  | 10 ++++++----
 .../com/netscape/cmstools/cert/CertShowCLI.java    |  4 +++-
 12 files changed, 61 insertions(+), 39 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
index 14e4a53d6d604e21f06cdf791e27b0bf34a10955..96870844002f23d61d567c2a5647030317ea9f47 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
@@ -29,6 +29,7 @@ import com.netscape.certsrv.cert.CertDataInfo;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertRequestInfos;
 import com.netscape.certsrv.cert.CertReviewResponse;
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -73,22 +74,20 @@ public class CertCLI extends CLI {
         return "pki-cert";
     }
 
-    public void execute(String[] args) throws Exception {
+    public CertClient getCertClient() throws Exception {
 
-        client = parent.getClient();
-        certClient = (CertClient)parent.getClient("cert");
+        if (certClient != null) return certClient;
 
-        // if this is a top-level command
-        if (certClient == null) {
-            // determine the subsystem
-            String subsystem = client.getSubsystem();
-            if (subsystem == null) subsystem = "ca";
+        PKIClient client = getClient();
 
-            // create new cert client
-            certClient = new CertClient(client, subsystem);
-        }
+        // determine the subsystem
+        String subsystem = client.getSubsystem();
+        if (subsystem == null) subsystem = "ca";
 
-        super.execute(args);
+        // create new cert client
+        certClient = new CertClient(client, subsystem);
+
+        return certClient;
     }
 
     public static String getAlgorithmNameFromOID(String oid) {
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java
index 1a9e4de1f48caa3b7e7675e546ec565c1f23d82c..8320619f2670fb3b8bae787eb0668ea4417f2cc8 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java
@@ -27,6 +27,7 @@ import java.util.Date;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertDataInfo;
 import com.netscape.certsrv.cert.CertDataInfos;
 import com.netscape.certsrv.cert.CertSearchRequest;
@@ -244,7 +245,8 @@ public class CertFindCLI extends CLI {
 
         addSearchAttribute(cmd, searchData);
 
-        CertDataInfos certs = certCLI.certClient.findCerts(searchData, start, size);
+        CertClient certClient = certCLI.getCertClient();
+        CertDataInfos certs = certClient.findCerts(searchData, start, size);
 
         MainCLI.printMessage(certs.getTotal() + " entries found");
         if (certs.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
index 9c30ee0039d01cf6e5e4dc265032d0e675f1a058..28a2da092197a5c6a185d19c1ad60b754849fd9d 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertData;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertRevokeRequest;
@@ -77,7 +78,8 @@ public class CertHoldCLI extends CLI {
         }
 
         CertId certID = new CertId(cmdArgs[0]);
-        CertData certData = certCLI.certClient.reviewCert(certID);
+        CertClient certClient = certCLI.getCertClient();
+        CertData certData = certClient.reviewCert(certID);
 
         if (!cmd.hasOption("force")) {
 
@@ -101,7 +103,7 @@ public class CertHoldCLI extends CLI {
         request.setComments(cmd.getOptionValue("comments"));
         request.setNonce(certData.getNonce());
 
-        CertRequestInfo certRequestInfo = certCLI.certClient.revokeCert(certID, request);
+        CertRequestInfo certRequestInfo = certClient.revokeCert(certID, request);
 
         if (verbose) {
             CertCLI.printCertRequestInfo(certRequestInfo);
@@ -116,7 +118,7 @@ public class CertHoldCLI extends CLI {
                 MainCLI.printMessage("Could not place certificate \"" + certID.toHexString() + "\" on-hold");
             } else {
                 MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" on-hold");
-                certData = certCLI.certClient.getCert(certID);
+                certData = certClient.getCert(certID);
                 CertCLI.printCertData(certData, false, false);
             }
         } else {
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
index a767282551754483f7277b15f9ec1e726a7b51d5..3166d4ce057c8a4dea8ac4b2c21d3a86f66bcb88 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
@@ -24,6 +24,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertData;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.dbs.certdb.CertId;
@@ -69,10 +70,11 @@ public class CertReleaseHoldCLI extends CLI {
         }
 
         CertId certID = new CertId(cmdArgs[0]);
+        CertClient certClient = certCLI.getCertClient();
 
         if (!cmd.hasOption("force")) {
 
-            CertData certData = certCLI.certClient.getCert(certID);
+            CertData certData = certClient.getCert(certID);
 
             System.out.println("Placing certificate off-hold:");
 
@@ -88,7 +90,7 @@ public class CertReleaseHoldCLI extends CLI {
             }
         }
 
-        CertRequestInfo certRequestInfo = certCLI.certClient.unrevokeCert(certID);
+        CertRequestInfo certRequestInfo = certClient.unrevokeCert(certID);
 
         if (verbose) {
             CertCLI.printCertRequestInfo(certRequestInfo);
@@ -103,7 +105,7 @@ public class CertReleaseHoldCLI extends CLI {
                 MainCLI.printMessage("Could not place certificate \"" + certID.toHexString() + "\" off-hold");
             } else {
                 MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" off-hold");
-                CertData certData = certCLI.certClient.getCert(certID);
+                CertData certData = certClient.getCert(certID);
                 CertCLI.printCertData(certData, false, false);
             }
         } else {
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
index 71d669e50591e8885b16ecbc6d697e5bbf2f1cbf..460b1a8d3d7569b8c15de5407853e8ecaddbfa12 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
@@ -24,6 +24,7 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertRequestInfos;
 import com.netscape.certsrv.request.RequestId;
@@ -113,7 +114,8 @@ public class CertRequestFindCLI extends CLI {
         String requestType = cmd.getOptionValue("type");
         if (requestType != null && requestType.equals("all")) requestType = null;
 
-        CertRequestInfos response = certCLI.certClient.listRequests(requestState, requestType, start, size, maxResults, maxTime);
+        CertClient certClient = certCLI.getCertClient();
+        CertRequestInfos response = certClient.listRequests(requestState, requestType, start, size, maxResults, maxTime);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java
index 220ee7c18b17c819f6eade29539c9e1976448783..c9be315ae129695038f11cae15d916d48dfae6aa 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java
@@ -6,6 +6,7 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.profile.ProfileDataInfo;
 import com.netscape.certsrv.profile.ProfileDataInfos;
 import com.netscape.cmstools.cli.CLI;
@@ -58,7 +59,8 @@ public class CertRequestProfileFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        ProfileDataInfos response = certCLI.certClient.listEnrollmentTemplates(start, size);
+        CertClient certClient = certCLI.getCertClient();
+        ProfileDataInfos response = certClient.listEnrollmentTemplates(start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java
index cb3a79bfb6f788642f9ae4a2a5db67c853a1fd20..ba33bc1c29ea8f779cff02b2c7341f71a4f58555 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java
@@ -5,6 +5,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertEnrollmentRequest;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -57,7 +58,8 @@ public class CertRequestProfileShowCLI extends CLI {
             }
         }
 
-        CertEnrollmentRequest request = certCLI.certClient.getEnrollmentTemplate(profileId);
+        CertClient certClient = certCLI.getCertClient();
+        CertEnrollmentRequest request = certClient.getEnrollmentTemplate(profileId);
 
         MainCLI.printMessage("Enrollment Template for Profile \"" + profileId + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java
index bc3df309a1e98c876370276a572d122c1f16347c..fcfefa8472c8e4039c6c8d66236559a6a5a1dd5f 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java
@@ -15,6 +15,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 import org.apache.commons.lang.StringUtils;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertReviewResponse;
 import com.netscape.certsrv.request.RequestId;
@@ -98,7 +99,8 @@ public class CertRequestReviewCLI extends CLI {
         }
 
         // Retrieve certificate request.
-        CertReviewResponse reviewInfo = certCLI.certClient.reviewRequest(requestId);
+        CertClient certClient = certCLI.getCertClient();
+        CertReviewResponse reviewInfo = certClient.reviewRequest(requestId);
 
         if (action == null) {
             // Store certificate request in a file.
@@ -133,38 +135,38 @@ public class CertRequestReviewCLI extends CLI {
         }
 
         if (action.equalsIgnoreCase("approve")) {
-            certCLI.certClient.approveRequest(reviewInfo.getRequestId(), reviewInfo);
+            certClient.approveRequest(reviewInfo.getRequestId(), reviewInfo);
             MainCLI.printMessage("Approved certificate request " + requestId);
 
         } else if (action.equalsIgnoreCase("reject")) {
-            certCLI.certClient.rejectRequest(reviewInfo.getRequestId(), reviewInfo);
+            certClient.rejectRequest(reviewInfo.getRequestId(), reviewInfo);
             MainCLI.printMessage("Rejected certificate request " + requestId);
 
         } else if (action.equalsIgnoreCase("cancel")) {
-            certCLI.certClient.cancelRequest(reviewInfo.getRequestId(), reviewInfo);
+            certClient.cancelRequest(reviewInfo.getRequestId(), reviewInfo);
             MainCLI.printMessage("Canceled certificate request " + requestId);
 
         } else if (action.equalsIgnoreCase("update")) {
-            certCLI.certClient.updateRequest(reviewInfo.getRequestId(), reviewInfo);
+            certClient.updateRequest(reviewInfo.getRequestId(), reviewInfo);
             MainCLI.printMessage("Updated certificate request " + requestId);
 
         } else if (action.equalsIgnoreCase("validate")) {
-            certCLI.certClient.validateRequest(reviewInfo.getRequestId(), reviewInfo);
+            certClient.validateRequest(reviewInfo.getRequestId(), reviewInfo);
             MainCLI.printMessage("Validated certificate request " + requestId);
 
         } else if (action.equalsIgnoreCase("assign")) {
-            certCLI.certClient.assignRequest(reviewInfo.getRequestId(), reviewInfo);
+            certClient.assignRequest(reviewInfo.getRequestId(), reviewInfo);
             MainCLI.printMessage("Assigned certificate request " + requestId);
 
         } else if (action.equalsIgnoreCase("unassign")) {
-            certCLI.certClient.unassignRequest(reviewInfo.getRequestId(), reviewInfo);
+            certClient.unassignRequest(reviewInfo.getRequestId(), reviewInfo);
             MainCLI.printMessage("Unassigned certificate request " + requestId);
 
         } else {
             throw new Exception("Invalid action: " + action);
         }
 
-        CertRequestInfo certRequest = certCLI.certClient.getRequest(requestId);
+        CertRequestInfo certRequest = certClient.getRequest(requestId);
         CertCLI.printCertRequestInfo(certRequest);
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
index 6df333eae074243aa0654c85d817549d45557e48..079cf604990f695aa5ccc206346f516ba502b0a2 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
@@ -4,6 +4,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.request.RequestId;
 import com.netscape.cmstools.cli.CLI;
@@ -47,7 +48,8 @@ public class CertRequestShowCLI extends CLI {
             throw new Exception("Invalid certificate request ID " + cmdArgs[0] + ".", e);
         }
 
-        CertRequestInfo certRequest = certCLI.certClient.getRequest(requestId);
+        CertClient certClient = certCLI.getCertClient();
+        CertRequestInfo certRequest = certClient.getRequest(requestId);
 
         MainCLI.printMessage("Certificate request \"" + requestId + "\"");
         CertCLI.printCertRequestInfo(certRequest);
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java
index e49dba2406a07697468855c93b71735849527256..fbccc4ce4855f51acfff7faecf00542db04c6ddb 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java
@@ -14,6 +14,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.ca.AuthorityID;
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertEnrollmentRequest;
 import com.netscape.certsrv.cert.CertRequestInfos;
 import com.netscape.certsrv.dbs.certdb.CertId;
@@ -133,7 +134,8 @@ public class CertRequestSubmitCLI extends CLI {
                 System.out.println("Retrieving " + profileID + " profile.");
             }
 
-            request = certCLI.certClient.getEnrollmentTemplate(profileID);
+            CertClient certClient = certCLI.getCertClient();
+            request = certClient.getEnrollmentTemplate(profileID);
 
             // set default request type for new request
             if (requestType == null) requestType = "pkcs10";
@@ -258,7 +260,8 @@ public class CertRequestSubmitCLI extends CLI {
             System.out.println(request);
         }
 
-        CertRequestInfos cri = certCLI.certClient.enrollRequest(request, aid, adn);
+        CertClient certClient = certCLI.getCertClient();
+        CertRequestInfos cri = certClient.enrollRequest(request, aid, adn);
         MainCLI.printMessage("Submitted certificate request");
         CertCLI.printCertRequestInfos(cri);
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
index e61401b903a765e6dbec2da9e8776ef85403e591..ecc3f801a8492285180896056430a70b36a3f9b2 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertData;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertRevokeRequest;
@@ -102,7 +103,8 @@ public class CertRevokeCLI extends CLI {
             throw new Exception("Invalid revocation reason: " + string);
         }
 
-        CertData certData = certCLI.certClient.reviewCert(certID);
+        CertClient certClient = certCLI.getCertClient();
+        CertData certData = certClient.reviewCert(certID);
 
         if (!cmd.hasOption("force")) {
 
@@ -135,9 +137,9 @@ public class CertRevokeCLI extends CLI {
         CertRequestInfo certRequestInfo;
 
         if (cmd.hasOption("ca")) {
-            certRequestInfo = certCLI.certClient.revokeCACert(certID, request);
+            certRequestInfo = certClient.revokeCACert(certID, request);
         } else {
-            certRequestInfo = certCLI.certClient.revokeCert(certID, request);
+            certRequestInfo = certClient.revokeCert(certID, request);
         }
 
         if (verbose) {
@@ -160,7 +162,7 @@ public class CertRevokeCLI extends CLI {
                     MainCLI.printMessage("Revoked certificate \"" + certID.toHexString() + "\"");
                 }
 
-                certData = certCLI.certClient.getCert(certID);
+                certData = certClient.getCert(certID);
                 CertCLI.printCertData(certData, false, false);
             }
         } else {
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java
index a31b02253fa41176ff64b1c950e532576a94d277..cf3802705dbdc06a4af98c65bd652236d64fc62c 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertData;
 import com.netscape.certsrv.dbs.certdb.CertId;
 import com.netscape.cmstools.cli.CLI;
@@ -78,7 +79,8 @@ public class CertShowCLI extends CLI {
         CertId certID = new CertId(cmdArgs[0]);
         String file = cmd.getOptionValue("output");
 
-        CertData certData = certCLI.certClient.getCert(certID);
+        CertClient certClient = certCLI.getCertClient();
+        CertData certData = certClient.getCert(certID);
 
         String encoded = certData.getEncoded();
         if (encoded != null && file != null) {
-- 
2.9.3

