>From 97efa3e8e82c5884dfd5bfc21b95c52727ad899e Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Tue, 31 Jul 2012 11:17:15 -0400
Subject: [PATCH] Feature : Search certificate request interface in
 CLI.(Ticket 150)

pki-cert-find <filename> [OPTIONS]
Available search options
  pki-cert-find - lists all the certificates.
  pki-cert-find <filename> - reads the search criteria from the file (Unmarshalled CertSearchData object)
  pki-cert-find [Options] - custom build of search criteria
  pki-cert-find <filename> [Options] - for quick change in search criteria.

  pki-cert-find --help - shows all the available options.
---
 .../com/netscape/cms/client/cert/CertFindCLI.java  |  227 +++++++++++++++++++-
 .../cms/servlet/cert/model/CertSearchData.java     |   18 +-
 2 files changed, 238 insertions(+), 7 deletions(-)

diff --git a/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java b/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java
index 32457d6dfb17317482091976e9c6445931851772..548651303643415ef9a26fe69aef7b5f862fe997 100644
--- a/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java
+++ b/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java
@@ -18,8 +18,19 @@
 
 package com.netscape.cms.client.cert;
 
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+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.cms.client.cli.CLI;
 import com.netscape.cms.client.cli.MainCLI;
+import com.netscape.cms.servlet.base.CMSException;
 import com.netscape.cms.servlet.cert.model.CertDataInfo;
 import com.netscape.cms.servlet.cert.model.CertDataInfos;
 import com.netscape.cms.servlet.cert.model.CertSearchData;
@@ -37,16 +48,73 @@ public class CertFindCLI extends CLI {
     }
 
     public void printHelp() {
-        formatter.printHelp(parent.name + "-" + name + " [OPTIONS...]", options);
+        formatter.printHelp(parent.name + "-" + name + "<filename>(Optional) [OPTIONS...]", options);
     }
 
-    public void execute(String[] args) throws Exception {
+    public void execute(String[] args) {
 
-        CertSearchData searchData = new CertSearchData();
-        searchData.setSerialNumberRangeInUse(true);
+        addOptions();
 
-        CertDataInfos certs = parent.client.findCerts(searchData);
+        CommandLine cmd = null;
+        CertSearchData searchData = null;
+        try {
+            cmd = parser.parse(options, args);
+        } catch (ParseException e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(-1);
+        }
 
+        if(cmd.hasOption("help")){
+            printHelp();
+            System.exit(-1);
+        }
+
+        String[] cLineArgs = cmd.getArgs();
+
+        if (cLineArgs.length < 1 && cmd.getOptions().length < 1) {
+            searchData = new CertSearchData();
+            searchData.setSerialNumberRangeInUse(true);
+        } else if (cLineArgs.length > 0) {
+            FileInputStream fis = null;
+            try {
+                fis = new FileInputStream(cLineArgs[0]);
+                searchData = CertSearchData.valueOf(fis);
+            } catch (FileNotFoundException e) {
+                System.err.println("Error: " + e.getMessage());
+                System.exit(-1);
+            } catch (JAXBException e) {
+                System.err.println("Error: " + e.getMessage());
+                System.exit(-1);
+            } finally {
+                if (fis != null)
+                    try {
+                        fis.close();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+            }
+        }
+
+        if (searchData == null) {
+            searchData = new CertSearchData();
+        }
+
+        Option[] allOptions = cmd.getOptions();
+        for (int i = 0; i < allOptions.length; i++) {
+            addSearchAttribute(allOptions[i], searchData);
+        }
+        CertDataInfos certs = null;
+        try {
+            certs = parent.client.findCerts(searchData);
+        } catch (CMSException e) {
+            System.err.println("Error: Cannot list certificates.");
+            System.exit(-1);
+        }
+        if (certs.getCertInfos() == null) {
+            System.err.println("Error: No matches found.");
+            System.exit(-1);
+        }
         MainCLI.printMessage(certs.getCertInfos().size() + " certificate(s) matched");
 
         boolean first = true;
@@ -63,4 +131,153 @@ public class CertFindCLI extends CLI {
 
         MainCLI.printMessage("Number of entries returned " + certs.getCertInfos().size());
     }
+
+    public void addOptions() {
+
+        //help
+        options.addOption(null, "help", false, "Show help options");
+
+        //serialNumberinUse
+        options.addOption(null, "minSerialNumber", true, "Minimum Serial Number");
+        options.addOption(null, "maxSerialNumber", true, "Maximum Serial Number");
+
+        //subjectNameinUse
+        options.addOption(null, "name", true, "Common Name");
+        options.addOption(null, "email", true, "Email Address");
+        options.addOption(null, "id", true, "UserId");
+        options.addOption(null, "org", true, "Organization");
+        options.addOption(null, "orgUnit", true, "Organization Unit");
+        options.addOption(null, "locality", true, "Locality");
+        options.addOption(null, "state", true, "State");
+        options.addOption(null, "country", true, "Country");
+        options.addOption(null, "matchExactly", false, "Match exactly with the details provided");
+
+        //revokedByInUse
+        options.addOption(null, "revokedBy", true, "Certificate Revoked By");
+
+        //revocationPeriod
+        options.addOption(null, "revokedFrom", true, "Revocation Start Date");
+        options.addOption(null, "revokedTill", true, "Revocation End Date");
+
+        //revocationReason
+        options.addOption(null, "revocationReason", true, "Reason for revocation");
+
+        //issuedBy
+        options.addOption(null, "issuedBy", true, "Issued By");
+
+        //issuedFor(period)
+        options.addOption(null, "issuedOnFrom", true, "Issued Start Date");
+        options.addOption(null, "issuedOnTo", true, "Issued End Date");
+
+        //certTypeinUse
+        options.addOption(null, "certTypeSubEmailCA", true, "Certifiate Type : Subject Email CA");
+        options.addOption(null, "certTypeSubSSLCA", true, "Certificate type : Subject SSL CA");
+        options.addOption(null, "certTypeSecureEmail", true, "Certifiate Type: Secure Email");
+        options.addOption(null, "certTypeSSLClient", true, "Certifiate Type: SSL Client");
+        options.addOption(null, "certTypeSSLServer", true, "Certifiate Type: SSL Server");
+
+        //validationNotBeforeInUse
+        options.addOption(null, "validNotBeforeFrom", true, "Valid Not Before Start Date");
+        options.addOption(null, "validNotBeforeTo", true, "Valid Not Before End Date");
+
+        //validityNotAfterinUse
+        options.addOption(null, "validNotAfterFrom", true, "Valid Not After Start Date");
+        options.addOption(null, "validNotAfterTo", true, "Valid Not After End Date");
+
+        //validityLengthinUse
+        options.addOption(null, "validityOperation", true, "Validity Operation");
+        options.addOption(null, "validityCount", true, "Validity Count");
+        options.addOption(null, "validityUnit", true, "Validity Unit");
+    }
+
+    public void addSearchAttribute(Option option, CertSearchData csd) {
+        String optionName = option.getLongOpt();
+        if (optionName.equalsIgnoreCase("minSerialNumber")) {
+            csd.setSerialNumberRangeInUse(true);
+            csd.setSerialFrom(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("maxSerialNumber")) {
+            csd.setSerialNumberRangeInUse(true);
+            csd.setSerialTo(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("name")) {
+            csd.setSubjectInUse(true);
+            csd.setCommonName(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("email")) {
+            csd.setSubjectInUse(true);
+            csd.setEmail(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("id")) {
+            csd.setSubjectInUse(true);
+            csd.setUserID(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("org")) {
+            csd.setSubjectInUse(true);
+            csd.setOrg(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("orgUnit")) {
+            csd.setSubjectInUse(true);
+            csd.setOrgUnit(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("locality")) {
+            csd.setSubjectInUse(true);
+            csd.setLocality(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("state")) {
+            csd.setSubjectInUse(true);
+            csd.setState(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("country")) {
+            csd.setSubjectInUse(true);
+            csd.setCountry(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("matchExactly")) {
+            csd.setMatchExactly(true);
+        } else if (optionName.equalsIgnoreCase("revokedBy")) {
+            csd.setRevokedByInUse(true);
+            csd.setRevokedBy(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("revokedFrom")) {
+            csd.setRevokedOnInUse(true);
+            csd.setRevokedOnFrom(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("revokedTill")) {
+            csd.setRevokedOnInUse(true);
+            csd.setRevokedOnTo(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("revocationReason")) {
+            csd.setRevocationReasonInUse(true);
+            csd.setRevocationReason(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("issuedBy")) {
+            csd.setIssuedByInUse(true);
+            csd.setIssuedBy(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("issuedOnFrom")) {
+            csd.setIssuedOnInUse(true);
+            csd.setIssuedOnFrom(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("certTypeSubEmailCA")) {
+            csd.setCertTypeInUse(true);
+            csd.setCertTypeSubEmailCA(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("certTypeSubSSLCA")) {
+            csd.setCertTypeInUse(true);
+            csd.setCertTypeSubSSLCA(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("certTypeSecureEmail")) {
+            csd.setCertTypeInUse(true);
+            csd.setCertTypeSecureEmail(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("certTypeSSLClient")) {
+            csd.setCertTypeInUse(true);
+            csd.setCertTypeSSLClient(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("certTypeSSLServer")) {
+            csd.setCertTypeInUse(true);
+            csd.setCertTypeSSLServer(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("validNotBeforeFrom")) {
+            csd.setValidNotBeforeInUse(true);
+            csd.setValidNotBeforeFrom(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("validNotBeforeTo")) {
+            csd.setValidNotBeforeInUse(true);
+            csd.setValidNotBeforeTo(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("validNotAfterFrom")) {
+            csd.setValidNotAfterInUse(true);
+            csd.setValidNotAfterFrom(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("validNotAfterTo")) {
+            csd.setValidNotAfterInUse(true);
+            csd.setValidNotAfterTo(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("validityOperation")) {
+            csd.setValidityLengthInUse(true);
+            csd.setValidityOperation(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("validityCount")) {
+            csd.setValidityLengthInUse(true);
+            csd.setValidityCount(option.getValue().trim());
+        } else if (optionName.equalsIgnoreCase("validityUnit")) {
+            csd.setValidityLengthInUse(true);
+            csd.setValidityUnit(option.getValue().trim());
+        }
+    }
 }
diff --git a/base/common/src/com/netscape/cms/servlet/cert/model/CertSearchData.java b/base/common/src/com/netscape/cms/servlet/cert/model/CertSearchData.java
index b474ddf79dbacc779971848c32229e6acab80396..4e1c85a9e99a83645fd75a4b552f214de005924a 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/model/CertSearchData.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/model/CertSearchData.java
@@ -20,11 +20,15 @@
 // smaller classes
 package com.netscape.cms.servlet.cert.model;
 
+import java.io.FileInputStream;
 import java.util.Calendar;
 import java.util.StringTokenizer;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.MultivaluedMap;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -493,11 +497,11 @@ public class CertSearchData {
 
     //Cert Type
 
-    String getCertTypeSubEmailCA() {
+    public String getCertTypeSubEmailCA() {
         return certTypeSubEmailCA;
     }
 
-    void setCertTypeSubEmailCA(String certTypeSubEmailCA) {
+    public void setCertTypeSubEmailCA(String certTypeSubEmailCA) {
         this.certTypeSubEmailCA = certTypeSubEmailCA;
     }
 
@@ -513,6 +517,10 @@ public class CertSearchData {
         return certTypeSecureEmail;
     }
 
+    public void setCertTypeSecureEmail(String certTypeSecureEmail) {
+        this.certTypeSecureEmail = certTypeSecureEmail;
+    }
+
     public String getCertTypeSSLClient() {
         return certTypeSSLClient;
     }
@@ -845,4 +853,10 @@ public class CertSearchData {
     public void setSearchFilter(String searchFilter) {
         this.searchFilter = searchFilter;
     }
+
+    public static CertSearchData valueOf(FileInputStream fis) throws JAXBException {
+        JAXBContext context = JAXBContext.newInstance(CertSearchData.class);
+        Unmarshaller unmarshaller = context.createUnmarshaller();
+        return (CertSearchData) unmarshaller.unmarshal(fis);
+    }
 }
-- 
1.7.10.2

