>From 1038b7e02fa9087e468e16ab34dcf0530dc6e26f Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Fri, 19 Apr 2013 14:13:03 -0400
Subject: [PATCH] Check the actual result of operations cert revoke/unrevoke.

Output the actual result of a revoke/unrevoke operation in CLI. Since
the actual result of the operation can be different from the cert request
status.

Ticket #217
---
 .../com/netscape/certsrv/cert/CertRequestInfo.java |   24 +++++++++++++++++
 .../cms/servlet/cert/CertRequestInfoFactory.java   |   21 +++++++++++++--
 .../src/com/netscape/cmstools/cert/CertCLI.java    |    8 +++++-
 .../com/netscape/cmstools/cert/CertHoldCLI.java    |   18 +++++++++----
 .../netscape/cmstools/cert/CertReleaseHoldCLI.java |   18 +++++++++----
 .../com/netscape/cmstools/cert/CertRevokeCLI.java  |   28 +++++++++++++-------
 6 files changed, 94 insertions(+), 23 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java b/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java
index 430f4772691ce1f7cebaa744a89605d793391965..9ff5f6cc82dad61a7d2428ac507236af9cb116d0 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java
@@ -40,6 +40,8 @@ import com.netscape.certsrv.request.RequestStatus;
 public class CertRequestInfo extends CMSRequestInfo {
 
     public static final String REQ_COMPLETE = "complete";
+    public static final String REQ_SUCCESS = "SUCCESS";
+    public static final String REQ_ERROR = "ERROR";
 
     @XmlElement
     @XmlJavaTypeAdapter(CertIdAdapter.class)
@@ -51,6 +53,12 @@ public class CertRequestInfo extends CMSRequestInfo {
     @XmlElement
     protected String certRequestType;
 
+    @XmlElement
+    protected String operationResult;
+
+    @XmlElement
+    protected String errorMessage;
+
     public CertRequestInfo() {
         // required to be here for JAXB (defaults)
     }
@@ -96,6 +104,22 @@ public class CertRequestInfo extends CMSRequestInfo {
         this.certId = certId;
     }
 
+    public String getOperationResult() {
+        return operationResult;
+    }
+
+    public void setOperationResult(String operationResult) {
+        this.operationResult = operationResult;
+    }
+
+    public String getErrorMessage() {
+        return errorMessage;
+    }
+
+    public void setErrorMessage(String errorMessage) {
+        this.errorMessage = errorMessage;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
diff --git a/base/common/src/com/netscape/cms/servlet/cert/CertRequestInfoFactory.java b/base/common/src/com/netscape/cms/servlet/cert/CertRequestInfoFactory.java
index 2fbc2096cb9985e13a3190cbff86bc4275ebe714..55af4216cd246eecce5132ca375e00d0fd6016d9 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/CertRequestInfoFactory.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/CertRequestInfoFactory.java
@@ -56,10 +56,27 @@ public class CertRequestInfoFactory {
         reqBuilder.path(certRequestPath.value() + "/" + requestId);
         info.setRequestURL(reqBuilder.build().toString());
 
-        if (requestType == null || requestStatus != RequestStatus.COMPLETE) return info;
+        Integer result = request.getExtDataInInteger(IRequest.RESULT);
+        if (result == null) {
+            info.setOperationResult(CertRequestInfo.REQ_SUCCESS);
+        } else {
+            if (result.equals(IRequest.RES_SUCCESS)) {
+                info.setOperationResult(CertRequestInfo.REQ_SUCCESS);
+            } else {
+                info.setOperationResult(CertRequestInfo.REQ_ERROR);
+                String error = request.getExtDataInString(IRequest.ERROR);
+                if (error != null) {
+                    info.setErrorMessage(error);
+                }
+            }
+        }
+
+        if (requestType == null || requestStatus != RequestStatus.COMPLETE)
+            return info;
 
         X509CertImpl impl = request.getExtDataInCert(IEnrollProfile.REQUEST_ISSUED_CERT);
-        if (impl == null) return info;
+        if (impl == null)
+            return info;
 
         BigInteger serialNo = impl.getSerialNumber();
         info.setCertId(new CertId(serialNo));
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 9f47b20c60a9a5d3f28d7bd1a1fa1967fccb700c..02f973910787e0484fd59814832bc370b3509eff 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
@@ -184,7 +184,13 @@ public class CertCLI extends CLI {
     public static void printCertRequestInfo(CertRequestInfo info) {
         System.out.println("  Request ID: " + info.getRequestId());
         System.out.println("  Type: " + info.getRequestType());
-        System.out.println("  Status: " + info.getRequestStatus());
+        System.out.println("  Request Status: " + info.getRequestStatus());
+        System.out.println("  Operation Result: " + info.getOperationResult());
+
+        String error = info.getErrorMessage();
+        if (error != null) {
+            System.out.println("  Reason: " + error);
+        }
 
         if (info.getCertId() != null) {
             System.out.println("  Certificate ID: " + info.getCertId().toHexString());
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 a112d3616cfe70f612171dfb0a6576630c3df4b2..e0f501f2cc9ff8546599053c03998ec85d7acc2d 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
@@ -108,12 +108,20 @@ public class CertHoldCLI extends CLI {
         }
 
         if (certRequestInfo.getRequestStatus() == RequestStatus.COMPLETE) {
-            MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" on-hold");
-            certData = parent.client.getCert(certID);
-            CertCLI.printCertData(certData, false, false);
-
+            if (certRequestInfo.getOperationResult().equals(CertRequestInfo.REQ_ERROR)) {
+                String error = certRequestInfo.getErrorMessage();
+                if (error != null) {
+                    System.out.println(error);
+                }
+                MainCLI.printMessage("Could not place certificate \"" + certID.toHexString() + "\" on-hold");
+            } else {
+                MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" on-hold");
+                certData = parent.client.getCert(certID);
+                CertCLI.printCertData(certData, false, false);
+            }
         } else {
-            MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": " + certRequestInfo.getRequestStatus());
+            MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": "
+                    + certRequestInfo.getRequestStatus());
         }
     }
 }
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 184976f1ea6256c6d126870b9d7ae18c48954917..1252a89fd1e5a71fd39945052b4a888acf702b59 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
@@ -98,12 +98,20 @@ public class CertReleaseHoldCLI extends CLI {
         }
 
         if (certRequestInfo.getRequestStatus() == RequestStatus.COMPLETE) {
-            MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" off-hold");
-            CertData certData = parent.client.getCert(certID);
-            CertCLI.printCertData(certData, false, false);
-
+            if (certRequestInfo.getOperationResult().equals(CertRequestInfo.REQ_ERROR)) {
+                String error = certRequestInfo.getErrorMessage();
+                if (error != null) {
+                    System.out.println(error);
+                }
+                MainCLI.printMessage("Could not place certificate \"" + certID.toHexString() + "\" off-hold");
+            } else {
+                MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" off-hold");
+                CertData certData = parent.client.getCert(certID);
+                CertCLI.printCertData(certData, false, false);
+            }
         } else {
-            MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": " + certRequestInfo.getRequestStatus());
+            MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": "
+                    + certRequestInfo.getRequestStatus());
         }
     }
 }
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 1a867d079b843e80acf018316d887b426363dea9..1768093072c920ae09865ec48f8b88f35cbcde7d 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
@@ -148,19 +148,27 @@ public class CertRevokeCLI extends CLI {
         }
 
         if (certRequestInfo.getRequestStatus() == RequestStatus.COMPLETE) {
-            if (reason == RevocationReason.CERTIFICATE_HOLD) {
-                MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" on-hold");
-            } else if (reason == RevocationReason.REMOVE_FROM_CRL) {
-                MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" off-hold");
+            if (certRequestInfo.getOperationResult().equals(CertRequestInfo.REQ_ERROR)) {
+                String error = certRequestInfo.getErrorMessage();
+                if (error != null) {
+                    System.out.println(error);
+                }
+                MainCLI.printMessage("Could not revoke certificate \"" + certID.toHexString());
             } else {
-                MainCLI.printMessage("Revoked certificate \"" + certID.toHexString() + "\"");
+                if (reason == RevocationReason.CERTIFICATE_HOLD) {
+                    MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" on-hold");
+                } else if (reason == RevocationReason.REMOVE_FROM_CRL) {
+                    MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" off-hold");
+                } else {
+                    MainCLI.printMessage("Revoked certificate \"" + certID.toHexString() + "\"");
+                }
+
+                certData = parent.client.getCert(certID);
+                CertCLI.printCertData(certData, false, false);
             }
-
-            certData = parent.client.getCert(certID);
-            CertCLI.printCertData(certData, false, false);
-
         } else {
-            MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": " + certRequestInfo.getRequestStatus());
+            MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": "
+                    + certRequestInfo.getRequestStatus());
         }
     }
 }
-- 
1.7.10.2

