From bd3c805059c23dcf3f5832485332f19cb933a477 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Thu, 21 Feb 2013 16:49:50 -0500
Subject: [PATCH] Added cert-request-show command.

A new cert-request-show command has been added to allow EE users to
check certificate request status.

Ticket #511
---
 .../src/com/netscape/certsrv/cert/CertClient.java  |  4 ++
 .../com/netscape/certsrv/cert/CertRequestInfo.java | 26 +++++++---
 .../cms/servlet/cert/CertRequestInfoFactory.java   | 29 ++++-------
 .../src/com/netscape/cmstools/cert/CertCLI.java    |  7 ++-
 .../netscape/cmstools/cert/CertRequestShowCLI.java | 57 ++++++++++++++++++++++
 5 files changed, 97 insertions(+), 26 deletions(-)
 create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java

diff --git a/base/common/src/com/netscape/certsrv/cert/CertClient.java b/base/common/src/com/netscape/certsrv/cert/CertClient.java
index 53118aba1c4a7122a37189fa9d8b5d0ac6eddf5e..215153fd18a46c30fe2cb868dc5fc9d400076f15 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertClient.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertClient.java
@@ -76,6 +76,10 @@ public class CertClient extends PKIClient {
         return certRequestResource.enrollCert(data);
     }
 
+    public CertRequestInfo getRequest(RequestId id) {
+        return certRequestResource.getRequestInfo(id);
+    }
+
     public CertReviewResponse reviewRequest(RequestId id) {
         return certRequestResource.reviewRequest(id);
     }
diff --git a/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java b/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java
index 0d887a871e69c440c6fb5dc928f6f3588f8457b2..430f4772691ce1f7cebaa744a89605d793391965 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertRequestInfo.java
@@ -28,8 +28,10 @@ import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
 import com.netscape.certsrv.dbs.certdb.CertId;
+import com.netscape.certsrv.dbs.certdb.CertIdAdapter;
 import com.netscape.certsrv.request.CMSRequestInfo;
 import com.netscape.certsrv.request.RequestStatus;
 
@@ -40,6 +42,10 @@ public class CertRequestInfo extends CMSRequestInfo {
     public static final String REQ_COMPLETE = "complete";
 
     @XmlElement
+    @XmlJavaTypeAdapter(CertIdAdapter.class)
+    protected CertId certId;
+
+    @XmlElement
     protected String certURL;
 
     @XmlElement
@@ -82,17 +88,19 @@ public class CertRequestInfo extends CMSRequestInfo {
     /**
      * @return the certId
      */
-
     public CertId getCertId() {
-        if (certURL == null) return null;
-        String id = certURL.substring(certURL.lastIndexOf("/") + 1);
-        return new CertId(id);
+        return certId;
+    }
+
+    public void setCertId(CertId certId) {
+        this.certId = certId;
     }
 
     @Override
     public int hashCode() {
         final int prime = 31;
-        int result = 1;
+        int result = super.hashCode();
+        result = prime * result + ((certId == null) ? 0 : certId.hashCode());
         result = prime * result + ((certRequestType == null) ? 0 : certRequestType.hashCode());
         result = prime * result + ((certURL == null) ? 0 : certURL.hashCode());
         return result;
@@ -102,11 +110,16 @@ public class CertRequestInfo extends CMSRequestInfo {
     public boolean equals(Object obj) {
         if (this == obj)
             return true;
-        if (obj == null)
+        if (!super.equals(obj))
             return false;
         if (getClass() != obj.getClass())
             return false;
         CertRequestInfo other = (CertRequestInfo) obj;
+        if (certId == null) {
+            if (other.certId != null)
+                return false;
+        } else if (!certId.equals(other.certId))
+            return false;
         if (certRequestType == null) {
             if (other.certRequestType != null)
                 return false;
@@ -148,6 +161,7 @@ public class CertRequestInfo extends CMSRequestInfo {
         before.setRequestType("enrollment");
         before.setRequestStatus(RequestStatus.COMPLETE);
         before.setCertRequestType("pkcs10");
+        before.setCertId(new CertId("5"));
 
         String string = before.toString();
         System.out.println(string);
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 fc16bd5f0851de9df14b8433d7331fac8b7351f2..2fbc2096cb9985e13a3190cbff86bc4275ebe714 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/CertRequestInfoFactory.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/CertRequestInfoFactory.java
@@ -26,11 +26,10 @@ import javax.ws.rs.core.UriInfo;
 
 import netscape.security.x509.X509CertImpl;
 
-import org.apache.commons.lang.StringUtils;
-
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertRequestResource;
 import com.netscape.certsrv.cert.CertResource;
+import com.netscape.certsrv.dbs.certdb.CertId;
 import com.netscape.certsrv.profile.IEnrollProfile;
 import com.netscape.certsrv.request.IRequest;
 import com.netscape.certsrv.request.RequestId;
@@ -57,27 +56,19 @@ public class CertRequestInfoFactory {
         reqBuilder.path(certRequestPath.value() + "/" + requestId);
         info.setRequestURL(reqBuilder.build().toString());
 
-        //Get Cert info if issued.
+        if (requestType == null || requestStatus != RequestStatus.COMPLETE) return info;
 
-        String serialNoStr = null;
+        X509CertImpl impl = request.getExtDataInCert(IEnrollProfile.REQUEST_ISSUED_CERT);
+        if (impl == null) return info;
 
-        if (requestType != null && requestStatus == RequestStatus.COMPLETE) {
-            X509CertImpl impl[] = new X509CertImpl[1];
-            impl[0] = request.getExtDataInCert(IEnrollProfile.REQUEST_ISSUED_CERT);
+        BigInteger serialNo = impl.getSerialNumber();
+        info.setCertId(new CertId(serialNo));
 
-            BigInteger serialNo;
-            if (impl[0] != null) {
-                serialNo = impl[0].getSerialNumber();
-                serialNoStr = serialNo.toString();
-            }
-        }
+        Path certPath = CertResource.class.getAnnotation(Path.class);
+        UriBuilder certBuilder = uriInfo.getBaseUriBuilder();
+        certBuilder.path(certPath.value() + "/" + serialNo);
 
-        if (!StringUtils.isEmpty(serialNoStr)) {
-            Path certPath = CertResource.class.getAnnotation(Path.class);
-            UriBuilder certBuilder = uriInfo.getBaseUriBuilder();
-            certBuilder.path(certPath.value() + "/" + serialNoStr);
-            info.setCertURL(certBuilder.build().toString());
-        }
+        info.setCertURL(certBuilder.build().toString());
 
         return info;
     }
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 f43ef9702241cb978087ab8ee1233afd4a42ec4f..9f47b20c60a9a5d3f28d7bd1a1fa1967fccb700c 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
@@ -48,11 +48,12 @@ public class CertCLI extends CLI {
 
         addModule(new CertFindCLI(this));
         addModule(new CertShowCLI(this));
-
         addModule(new CertRevokeCLI(this));
         addModule(new CertHoldCLI(this));
         addModule(new CertReleaseHoldCLI(this));
+
         addModule(new CertRequestFindCLI(this));
+        addModule(new CertRequestShowCLI(this));
         addModule(new CertRequestSubmitCLI(this));
         addModule(new CertRequestReviewCLI(this));
     }
@@ -184,6 +185,10 @@ public class CertCLI extends CLI {
         System.out.println("  Request ID: " + info.getRequestId());
         System.out.println("  Type: " + info.getRequestType());
         System.out.println("  Status: " + info.getRequestStatus());
+
+        if (info.getCertId() != null) {
+            System.out.println("  Certificate ID: " + info.getCertId().toHexString());
+        }
     }
 
     public static void printCertReviewResponse(CertReviewResponse response) {
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..bee9f841a557b4b768e17777ebcf51fa6bb31016
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
@@ -0,0 +1,57 @@
+package com.netscape.cmstools.cert;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.ParseException;
+
+import com.netscape.certsrv.cert.CertRequestInfo;
+import com.netscape.certsrv.request.RequestId;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+public class CertRequestShowCLI extends CLI {
+
+    CertCLI parent;
+
+    public CertRequestShowCLI(CertCLI parent) {
+        super("request-show", "Show certificate request");
+        this.parent = parent;
+    }
+
+    @Override
+    public void printHelp() {
+        formatter.printHelp(parent.name + "-" + name + " <Request ID> [OPTIONS...]", options);
+    }
+
+    @Override
+    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);
+        }
+
+        String[] cLineArgs = cmd.getArgs();
+        if (cLineArgs.length < 1) {
+            System.err.println("Error: Missing certificate request ID.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        RequestId requestId = null;
+        try {
+            requestId = new RequestId(cLineArgs[0]);
+        } catch (NumberFormatException e) {
+            System.err.println("Error: Invalid certificate request ID " + cLineArgs[0] + ".");
+            System.exit(-1);
+        }
+
+        CertRequestInfo certRequest = parent.client.getRequest(requestId);
+
+        MainCLI.printMessage("Certificate request \"" + requestId + "\"");
+        CertCLI.printCertRequestInfo(certRequest);
+    }
+}
-- 
1.7.12.1

