From 6244968fc1d0956106d0b4f9e17959b2a0761d27 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Thu, 1 Oct 2015 12:12:38 -0400
Subject: [PATCH 273/274] Added Java client and CLI support for Feature
 resource.

---
 .../src/com/netscape/certsrv/ca/CAClient.java      |  4 +-
 .../src/com/netscape/certsrv/system/Feature.java   |  8 +--
 .../com/netscape/certsrv/system/FeatureClient.java | 51 ++++++++++++++
 .../src/com/netscape/cmstools/cli/CACLI.java       |  2 +
 .../com/netscape/cmstools/feature/FeatureCLI.java  | 63 +++++++++++++++++
 .../netscape/cmstools/feature/FeatureFindCLI.java  | 79 +++++++++++++++++++++
 .../netscape/cmstools/feature/FeatureShowCLI.java  | 80 ++++++++++++++++++++++
 .../org/dogtagpki/server/rest/FeatureService.java  |  2 +-
 8 files changed, 283 insertions(+), 6 deletions(-)
 create mode 100644 base/common/src/com/netscape/certsrv/system/FeatureClient.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/feature/FeatureCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java

diff --git a/base/common/src/com/netscape/certsrv/ca/CAClient.java b/base/common/src/com/netscape/certsrv/ca/CAClient.java
index 1fbd2a0b286ed09854373846510c392c5202307a..bb131ecb1daf7342e3a350edc1b23da830225adf 100644
--- a/base/common/src/com/netscape/certsrv/ca/CAClient.java
+++ b/base/common/src/com/netscape/certsrv/ca/CAClient.java
@@ -19,14 +19,15 @@ package com.netscape.certsrv.ca;
 
 import java.net.URISyntaxException;
 
+import com.netscape.certsrv.authority.AuthorityClient;
 import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.client.SubsystemClient;
 import com.netscape.certsrv.group.GroupClient;
 import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.certsrv.selftests.SelfTestClient;
+import com.netscape.certsrv.system.FeatureClient;
 import com.netscape.certsrv.user.UserClient;
-import com.netscape.certsrv.authority.AuthorityClient;
 
 public class CAClient extends SubsystemClient {
 
@@ -38,6 +39,7 @@ public class CAClient extends SubsystemClient {
     public void init() throws URISyntaxException {
         addClient(new AuthorityClient(client, name));
         addClient(new CertClient(client, name));
+        addClient(new FeatureClient(client, name));
         addClient(new GroupClient(client, name));
         addClient(new ProfileClient(client, name));
         addClient(new SelfTestClient(client, name));
diff --git a/base/common/src/com/netscape/certsrv/system/Feature.java b/base/common/src/com/netscape/certsrv/system/Feature.java
index 59b88d2f64b033d4b08ccfce2949ce31cb79189e..c995a2e6de86a2cc63f27ba48fa22c1c92cd4cb6 100644
--- a/base/common/src/com/netscape/certsrv/system/Feature.java
+++ b/base/common/src/com/netscape/certsrv/system/Feature.java
@@ -56,12 +56,12 @@ public class Feature {
     }
 
     @XmlAttribute(name="enabled")
-    public boolean getEnabled() {
+    public boolean isEnabled() {
         return enabled;
     }
 
-    public void setEnabled(String enabled) {
-        this.enabled = enabled.equalsIgnoreCase("true");
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
     }
 
     @XmlAttribute(name="version")
@@ -138,7 +138,7 @@ public class Feature {
     public static void main(String args[]) throws Exception {
         Feature before = new Feature();
         before.setId("authority");
-        before.setEnabled("true");
+        before.setEnabled(true);
         before.setDescription("Subordinate CA Feature");
         before.setVersion("1.0");
 
diff --git a/base/common/src/com/netscape/certsrv/system/FeatureClient.java b/base/common/src/com/netscape/certsrv/system/FeatureClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..c5c9a16424ac1e8725be9e29caa16222432f7d97
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/system/FeatureClient.java
@@ -0,0 +1,51 @@
+//--- BEGIN COPYRIGHT BLOCK ---
+//This program is free software; you can redistribute it and/or modify
+//it under the terms of the GNU General Public License as published by
+//the Free Software Foundation; version 2 of the License.
+//
+//This program is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//GNU General Public License for more details.
+//
+//You should have received a copy of the GNU General Public License along
+//with this program; if not, write to the Free Software Foundation, Inc.,
+//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+//(C) 2015 Red Hat, Inc.
+//All rights reserved.
+//--- END COPYRIGHT BLOCK ---
+package com.netscape.certsrv.system;
+
+import java.net.URISyntaxException;
+import java.util.List;
+
+import javax.ws.rs.core.GenericType;
+import javax.ws.rs.core.Response;
+
+import com.netscape.certsrv.client.Client;
+import com.netscape.certsrv.client.PKIClient;
+
+/**
+* @author Ade Lee <alee@redhat.com>
+*/
+public class FeatureClient extends Client {
+
+  public FeatureResource featureClient;
+
+  public FeatureClient(PKIClient client, String subsystem) throws URISyntaxException {
+      super(client, subsystem, "feature");
+      featureClient = createProxy(FeatureResource.class);
+  }
+
+  public List<Feature> listFeatures() {
+      Response response = featureClient.listFeatures();
+      GenericType<List<Feature>> type = new GenericType<List<Feature>>() {};
+      return client.getEntity(response, type);
+  }
+
+  public Feature getFeature(String featureID) {
+      Response response = featureClient.getFeature(featureID);
+      return client.getEntity(response, Feature.class);
+  }
+}
\ No newline at end of file
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
index 5c41f00c2eb6e393cc95d3b174cb14eefc7307ae..39dd2a2ce723daaf336ba4f3ddc2e2be4afbf407 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
@@ -22,6 +22,7 @@ import com.netscape.certsrv.ca.CAClient;
 import com.netscape.certsrv.client.Client;
 import com.netscape.cmstools.authority.AuthorityCLI;
 import com.netscape.cmstools.cert.CertCLI;
+import com.netscape.cmstools.feature.FeatureCLI;
 import com.netscape.cmstools.group.GroupCLI;
 import com.netscape.cmstools.profile.ProfileCLI;
 import com.netscape.cmstools.selftests.SelfTestCLI;
@@ -40,6 +41,7 @@ public class CACLI extends SubsystemCLI {
 
         addModule(new AuthorityCLI(this));
         addModule(new CertCLI(this));
+        addModule(new FeatureCLI(this));
         addModule(new GroupCLI(this));
         addModule(new KRAConnectorCLI(this));
         addModule(new ProfileCLI(this));
diff --git a/base/java-tools/src/com/netscape/cmstools/feature/FeatureCLI.java b/base/java-tools/src/com/netscape/cmstools/feature/FeatureCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..41c01df8e55969e8644f3383b4e85c3fe2d3ab60
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/feature/FeatureCLI.java
@@ -0,0 +1,63 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2015 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmstools.feature;
+
+import com.netscape.certsrv.system.Feature;
+import com.netscape.certsrv.system.FeatureClient;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+public class FeatureCLI extends CLI {
+
+    public FeatureClient featureClient;
+
+    public FeatureCLI(CLI parent) {
+        super("feature", "Feature management commands", parent);
+
+        addModule(new FeatureFindCLI(this));
+        addModule(new FeatureShowCLI(this));
+    }
+
+    public String getFullName() {
+        if (parent instanceof MainCLI) {
+            // do not include MainCLI's name
+            return name;
+        } else {
+            return parent.getFullName() + "-" + name;
+        }
+    }
+
+    public void execute(String[] args) throws Exception {
+        client = parent.getClient();
+        featureClient = new FeatureClient(client, "ca");
+        super.execute(args);
+    }
+
+    protected static void printFeature(Feature data) {
+        System.out.println("  ID:             " + data.getId());
+        String desc = data.getDescription();
+        if (desc != null)
+            System.out.println("  Description:    " + desc);
+        String version = data.getVersion();
+        if (version != null)
+            System.out.println("  Version:        " + version);
+        System.out.println("  Enabled:        " + data.isEnabled());
+    }
+
+}
+
diff --git a/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java b/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..5bc77071a91901565931b8f3727051a183f8f8f0
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java
@@ -0,0 +1,79 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2015 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmstools.feature;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.ParseException;
+
+import com.netscape.certsrv.system.Feature;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+public class FeatureFindCLI extends CLI {
+
+    public FeatureCLI featureCLI;
+
+    public FeatureFindCLI(FeatureCLI featureCLI) {
+        super("find", "Find features", featureCLI);
+        this.featureCLI = featureCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName(), options);
+    }
+
+    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);
+        }
+
+        @SuppressWarnings("unused")
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+        } catch (ParseException e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(-1);
+        }
+
+        List<Feature> features = featureCLI.featureClient.listFeatures();
+
+        MainCLI.printMessage(features.size() + " entries matched");
+        if (features.size() == 0) return;
+
+        boolean first = true;
+        for (Feature feature : features) {
+            if (first)
+                first = false;
+            else
+                System.out.println();
+            FeatureCLI.printFeature(feature);
+        }
+
+        MainCLI.printMessage("Number of entries returned " + features.size());
+    }
+
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java b/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..b18db388d8eac18450c56327554762bea48b1b9c
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java
@@ -0,0 +1,80 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2015 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmstools.feature;
+
+import java.util.Arrays;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.ParseException;
+
+import com.netscape.certsrv.system.Feature;
+import com.netscape.cmstools.cli.CLI;
+
+public class FeatureShowCLI extends CLI {
+
+    public FeatureCLI featureCLI;
+
+    public FeatureShowCLI(FeatureCLI featureCLI) {
+        super("show", "Show features", featureCLI);
+        this.featureCLI = featureCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <ID>", options);
+    }
+
+    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);
+        }
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+        } catch (ParseException e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(-1);
+        }
+
+        String[] cmdArgs = cmd.getArgs();
+
+        String featureID = null;
+        if (cmdArgs.length > 1) {
+            System.err.println("Error: too many arguments.");
+            printHelp();
+            System.exit(-1);
+        } else if (cmdArgs.length == 1) {
+            featureID = cmdArgs[0];
+        }
+
+        if (featureID == null) {
+            System.err.println("Error: No ID specified.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        Feature data = featureCLI.featureClient.getFeature(featureID);
+        FeatureCLI.printFeature(data);
+    }
+
+}
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/FeatureService.java b/base/server/cms/src/org/dogtagpki/server/rest/FeatureService.java
index 0361d6b0ae3bc97039ec300390b46dcaec9edd44..9e4008b9e5ae16db450ff6c536b354a60ec24d0c 100644
--- a/base/server/cms/src/org/dogtagpki/server/rest/FeatureService.java
+++ b/base/server/cms/src/org/dogtagpki/server/rest/FeatureService.java
@@ -55,7 +55,7 @@ public class FeatureService extends PKIService implements FeatureResource {
             props = cs.getSubStore(tag).getProperties();
             Feature feature = new Feature();
             feature.setId(tag);
-            feature.setEnabled(props.getOrDefault("enabled", "false"));
+            feature.setEnabled(props.getOrDefault("enabled", "false").equalsIgnoreCase("true"));
             feature.setDescription(props.getOrDefault("description", ""));
             feature.setVersion(props.getOrDefault("version", ""));
             return feature;
-- 
2.4.3

