>From 00280ea40d9f6c98ee5bdeacc55ab7a731c48068 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Fri, 27 Sep 2013 14:30:45 -0400
Subject: [PATCH] Changes to  TPSConnectorService based on review

---
 .../certsrv/system/TPSConnectorClient.java         |   7 +-
 .../netscape/certsrv/system/TPSConnectorData.java  |   7 +-
 .../certsrv/system/TPSConnectorResource.java       |   6 +-
 .../cms/servlet/csadmin/ConfigurationUtils.java    |   9 +-
 .../cms/servlet/tks/TPSConnectorService.java       | 221 +++++++++++----------
 .../cmstools/system/TPSConnectorAddCLI.java        |  27 ++-
 .../netscape/cmstools/system/TPSConnectorCLI.java  |   2 +-
 .../cmstools/system/TPSConnectorFindCLI.java       |  24 ++-
 .../cmstools/system/TPSConnectorRemoveCLI.java     |  24 ++-
 .../python/pki/server/deployment/pkihelper.py      |   4 +-
 10 files changed, 202 insertions(+), 129 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java b/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java
index 09ba9db45a96f88b08684d1a7f767180679ea20f..fa5ae4f2dc5df9e2e06981552b72e19bd7c37a28 100644
--- a/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java
+++ b/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java
@@ -2,6 +2,8 @@ package com.netscape.certsrv.system;
 
 import java.net.URISyntaxException;
 
+import org.jboss.resteasy.client.ClientResponse;
+
 import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.key.KeyData;
@@ -36,7 +38,10 @@ public class TPSConnectorClient extends Client {
     }
 
     public TPSConnectorData createConnector(String tpsHost, String tpsPort) {
-        return tpsConnectorClient.createConnector(tpsHost, tpsPort);
+        @SuppressWarnings("unchecked")
+        ClientResponse<TPSConnectorData> response = (ClientResponse<TPSConnectorData>)
+                tpsConnectorClient.createConnector(tpsHost, tpsPort);
+        return client.getEntity(response);
     }
 
     public void deleteConnector(String id) {
diff --git a/base/common/src/com/netscape/certsrv/system/TPSConnectorData.java b/base/common/src/com/netscape/certsrv/system/TPSConnectorData.java
index d5e1480eb5961ac4c3f68a293c26b35d4fa16406..5483fa8ed756a96527562463c5ea6c8a849184e6 100644
--- a/base/common/src/com/netscape/certsrv/system/TPSConnectorData.java
+++ b/base/common/src/com/netscape/certsrv/system/TPSConnectorData.java
@@ -44,7 +44,7 @@ public class TPSConnectorData {
         this.id = id;
     }
 
-    @XmlAttribute(name="Host")
+    @XmlElement(name="Host")
     public String getHost() {
         return host;
     }
@@ -53,7 +53,7 @@ public class TPSConnectorData {
         this.host = host;
     }
 
-    @XmlAttribute(name="Port")
+    @XmlElement(name="Port")
     public String getPort() {
         return port;
     }
@@ -71,7 +71,7 @@ public class TPSConnectorData {
         this.userID = userID;
     }
 
-    @XmlAttribute(name="Nickname")
+    @XmlElement(name="Nickname")
     public String getNickname() {
         return nickname;
     }
@@ -80,6 +80,7 @@ public class TPSConnectorData {
         this.nickname = nickname;
     }
 
+    @XmlElement(name="Link")
     public Link getLink() {
         return link;
     }
diff --git a/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java b/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java
index 8a76ee696d9570ba5a2aa20baba4bb7828ef45b4..54519f0ffc77a775e9088768e422e377a8b1dca2 100644
--- a/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java
+++ b/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java
@@ -26,6 +26,9 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.annotations.ClientResponseType;
 
 import com.netscape.certsrv.acls.ACLMapping;
 import com.netscape.certsrv.authentication.AuthMethodMapping;
@@ -53,8 +56,9 @@ public interface TPSConnectorResource {
             @QueryParam("port") String port);
 
     @POST
+    @ClientResponseType(entityType=TPSConnectorData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public TPSConnectorData createConnector(@QueryParam("host") String host,
+    public Response createConnector(@QueryParam("host") String host,
             @QueryParam("port") String port);
 
     @DELETE
diff --git a/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java b/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
index 1e6ee25a3b5487e261c6c69eadfaa17270bee42a..d86aa077023d7bc63f54c07a348655fb0488ba43 100644
--- a/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
+++ b/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
@@ -141,6 +141,7 @@ import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.EPropertyNotFound;
 import com.netscape.certsrv.base.IConfigStore;
 import com.netscape.certsrv.base.ISubsystem;
+import com.netscape.certsrv.base.ResourceNotFoundException;
 import com.netscape.certsrv.ca.ICertificateAuthority;
 import com.netscape.certsrv.client.ClientConfig;
 import com.netscape.certsrv.client.PKIClient;
@@ -3601,7 +3602,13 @@ public class ConfigurationUtils {
         TPSConnectorClient tpsConnectorClient = new TPSConnectorClient(client);
 
         accountClient.login();
-        TPSConnectorData data = tpsConnectorClient.getConnector(host, port);
+        TPSConnectorData data = null;
+        try {
+            data = tpsConnectorClient.getConnector(host, port);
+        } catch (ResourceNotFoundException e) {
+            // no connector exists
+            data = null;
+        }
         KeyData keyData = null;
         if (data == null) {
             data = tpsConnectorClient.createConnector(host, port);
diff --git a/base/common/src/com/netscape/cms/servlet/tks/TPSConnectorService.java b/base/common/src/com/netscape/cms/servlet/tks/TPSConnectorService.java
index 97478d6994fd796a50b1661c4d8ba87cf73e092c..765973e3fe1e12537cb93250990b23c39477cc72 100644
--- a/base/common/src/com/netscape/cms/servlet/tks/TPSConnectorService.java
+++ b/base/common/src/com/netscape/cms/servlet/tks/TPSConnectorService.java
@@ -6,15 +6,17 @@ import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
 import java.security.cert.X509Certificate;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
+import java.util.Collection;
+import java.util.TreeSet;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 import org.mozilla.jss.CryptoManager.NotInitializedException;
@@ -26,6 +28,7 @@ import com.netscape.certsrv.base.BadRequestException;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IConfigStore;
 import com.netscape.certsrv.base.PKIException;
+import com.netscape.certsrv.base.ResourceNotFoundException;
 import com.netscape.certsrv.base.UnauthorizedException;
 import com.netscape.certsrv.key.KeyData;
 import com.netscape.certsrv.system.TPSConnectorCollection;
@@ -40,6 +43,8 @@ import com.netscape.cmsutil.util.Utils;
 
 public class TPSConnectorService implements TPSConnectorResource {
 
+    private static final String TPS_LIST = "tps.list";
+
     IConfigStore cs = CMS.getConfigStore();
 
     @Context
@@ -53,14 +58,11 @@ public class TPSConnectorService implements TPSConnectorResource {
     @Override
     public TPSConnectorCollection listConnectors() {
         try {
-            String tpsList = cs.getString("tps.list", "");
-            if (tpsList.isEmpty()) {
-                return null;
-            }
-
             TPSConnectorCollection ret = new TPSConnectorCollection();
-            for (String tpsID : tpsList.split(",")) {
-                ret.addEntry(createTPSSystemClientData(tpsID));
+            String tpsList = cs.getString(TPS_LIST, "");
+
+            for (String tpsID : StringUtils.split(tpsList,",")) {
+                ret.addEntry(createTPSConnectorData(tpsID));
             }
             return ret;
         } catch (EBaseException e) {
@@ -69,7 +71,7 @@ public class TPSConnectorService implements TPSConnectorResource {
         }
     }
 
-    private TPSConnectorData createTPSSystemClientData(String tpsID) throws EBaseException {
+    private TPSConnectorData createTPSConnectorData(String tpsID) throws EBaseException {
         TPSConnectorData data = new TPSConnectorData();
         data.setID(tpsID);
         data.setHost(cs.getString("tps." + tpsID + ".host", ""));
@@ -84,16 +86,8 @@ public class TPSConnectorService implements TPSConnectorResource {
     @Override
     public TPSConnectorData getConnector(String id) {
         try {
-            String tpsList = cs.getString("tps.list", "");
-            if (tpsList.isEmpty()) {
-                return null;
-            }
-
-            for (String tpsID : tpsList.split(",")) {
-                if (tpsID.equals(id))
-                    return createTPSSystemClientData(tpsID);
-            }
-            return null;
+            if (connectorExists(id)) return createTPSConnectorData(id);
+            throw new ResourceNotFoundException("Connector " + id + " not found.");
         } catch (EBaseException e) {
             e.printStackTrace();
             throw new PKIException("Unable to get TPS connection data" + e);
@@ -103,17 +97,10 @@ public class TPSConnectorService implements TPSConnectorResource {
     @Override
     public TPSConnectorData getConnector(String host, String port) {
         try {
-            String tpsList = cs.getString("tps.list", "");
-            if (tpsList.isEmpty()) {
-                return null;
-            }
-
-            for (String tpsID : tpsList.split(",")) {
-                TPSConnectorData data = createTPSSystemClientData(tpsID);
-                if (data.getHost().equals(host) && data.getPort().equals(port))
-                    return data;
-            }
-            return null;
+            String id = getConnectorID(host, port);
+            if (id != null) return createTPSConnectorData(id);
+            throw new ResourceNotFoundException(
+                    "Connector not found for " + host + ":" + port);
         } catch (EBaseException e) {
             e.printStackTrace();
             throw new PKIException("Unable to get TPS connection data" + e);
@@ -121,41 +108,34 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public TPSConnectorData createConnector(String tpsHost, String tpsPort) {
-        TPSConnectorData newData = new TPSConnectorData();
-        newData.setHost(tpsHost);
-        newData.setPort(tpsPort);
-        newData.setUserID("TPS-" + tpsHost + "-" + tpsPort);
+    public Response createConnector(String tpsHost, String tpsPort) {
         try {
-            int index = 0;
-            boolean indexFound = false;
-            String tpsList = cs.getString("tps.list", "");
-            if (!tpsList.isEmpty()) {
-                List<String> sorted = new ArrayList<String>(Arrays.asList(tpsList.split(",")));
-                Collections.sort(sorted);
-                for (String tpsID : sorted) {
-                    TPSConnectorData data = createTPSSystemClientData(tpsID);
-                    if (data.equals(newData)) {
-                        throw new BadRequestException("TPS connection already exists at " + data.getLink());
-                    }
-                    if (!indexFound && tpsID.equals(index)) {
-                        index++;
-                    } else {
-                        indexFound = true;
-                    }
-                }
+            String id = getConnectorID(tpsHost, tpsPort);
+            if (id != null) {
+                URI uri = uriInfo.getBaseUriBuilder().path(TPSCertResource.class)
+                        .path("{id}").build(id);
+                throw new BadRequestException("TPS connection already exists at " + uri.toString());
             }
-            String newID = Integer.toString(index);
+            String newID = findNextConnectorID();
+
+            TPSConnectorData newData = new TPSConnectorData();
             newData.setID(newID);
+            newData.setHost(tpsHost);
+            newData.setPort(tpsPort);
+            newData.setUserID("TPS-" + tpsHost + "-" + tpsPort);
             URI uri = uriInfo.getBaseUriBuilder().path(TPSCertResource.class).path("{id}").build(newID);
             newData.setLink(new Link("self", uri));
             saveClientData(newData);
 
-            cs.putString("tps.list", tpsList.isEmpty() ? Integer.toString(index) :
-                    tpsList + "," + index);
-            cs.commit(false);
+            addToConnectorList(newID);
+            cs.commit(true);
+
+            return Response
+                    .created(newData.getLink().getHref())
+                    .entity(newData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
 
-            return newData;
         } catch (EBaseException e) {
             e.printStackTrace();
             throw new PKIException("Unable to create  new TPS connection data" + e);
@@ -164,10 +144,9 @@ public class TPSConnectorService implements TPSConnectorResource {
 
     private void saveClientData(TPSConnectorData newData) throws EBaseException {
         String id = newData.getID();
-        if ((id == null) || (id.isEmpty())) {
+        if (StringUtils.isEmpty(id)) {
             CMS.debug("saveClientData: Attempt to save tps connection with null or empty id");
             return;
-            // throw exception here?
         }
         String prefix = "tps." + id + ".";
 
@@ -179,39 +158,20 @@ public class TPSConnectorService implements TPSConnectorResource {
             cs.putString(prefix + "userid", newData.getUserID());
         if (newData.getNickname() != null)
             cs.putString(prefix + "nickname", newData.getNickname());
-
-        cs.commit(false);
     }
 
     @Override
     public void deleteConnector(String id) {
         try {
-            if ((id == null) || id.isEmpty())
+            if (StringUtils.isEmpty(id))
                 throw new BadRequestException("Attempt to delete TPS connection with null or empty id");
 
-            if (getConnector(id) == null) {
-                return;
-                // return 404 here?
-            }
+            if (!connectorExists(id)) return;
 
             deleteSharedSecret(id);
-
-            String prefix = "tps." + id;
-            cs.removeSubStore(prefix);
-
-            String tpsList = cs.getString("tps.list", "");
-            if (tpsList.isEmpty()) {
-                return;
-            }
-
-            List<String> newList = new ArrayList<String>();
-            for (String tpsID : tpsList.split(",")) {
-                if (!tpsID.equals(id)) {
-                    newList.add(tpsID);
-                }
-            }
-            cs.putString("tps.list", StringUtils.join(newList, ","));
-            cs.commit(false);
+            cs.removeSubStore("tps." + id);
+            removeFromConnectorList(id);
+            cs.commit(true);
         } catch (EBaseException e) {
             e.printStackTrace();
             throw new PKIException("Failed to delete TPS connection" + e);
@@ -219,10 +179,22 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
+    public void deleteConnector(String host, String port) {
+        String id;
+        try {
+            id = getConnectorID(host, port);
+            deleteConnector(id);
+        } catch (EBaseException e) {
+            e.printStackTrace();
+            throw new PKIException("Failed to delete TPS connector: " + e);
+        }
+    }
+
+    @Override
     public KeyData createSharedSecret(String id) {
         try {
-            if (getConnector(id) == null) {
-                throw new BadRequestException("TPS Connection does not exist");
+            if (!connectorExists(id)) {
+                throw new ResourceNotFoundException("TPS connection does not exist");
             }
 
             // get and validate user
@@ -240,7 +212,7 @@ public class TPSConnectorService implements TPSConnectorResource {
             CryptoUtil.createSharedSecret(nickname);
 
             cs.putString("tps." + id + ".nickname", nickname);
-            cs.commit(false);
+            cs.commit(true);
 
             byte[] wrappedKey = CryptoUtil.exportSharedSecret(nickname, certs[0]);
             KeyData keyData = new KeyData();
@@ -277,22 +249,22 @@ public class TPSConnectorService implements TPSConnectorResource {
     @Override
     public KeyData replaceSharedSecret(String id) {
         try {
-            if (getConnector(id) == null) {
-                throw new BadRequestException("TPS Connection does not exist");
+            if (!connectorExists(id)) {
+                throw new ResourceNotFoundException("TPS connection does not exist");
             }
 
             // get and validate user
             String userid = validateUser(id);
 
-            // get user cert
-            IUser user = userGroupManager.getUser(userid);
-            X509Certificate[] certs = user.getX509Certificates();
-
             String nickname = userid + " sharedSecret";
             if (!CryptoUtil.sharedSecretExists(nickname)) {
                 throw new BadRequestException("Cannot replace. Shared secret does not exist");
             }
 
+            // get user cert
+            IUser user = userGroupManager.getUser(userid);
+            X509Certificate[] certs = user.getX509Certificates();
+
             CryptoUtil.deleteSharedSecret(nickname);
             CryptoUtil.createSharedSecret(nickname);
             byte[] wrappedKey = CryptoUtil.exportSharedSecret(nickname, certs[0]);
@@ -311,7 +283,7 @@ public class TPSConnectorService implements TPSConnectorResource {
     @Override
     public void deleteSharedSecret(String id) {
         try {
-            if (getConnector(id) == null) {
+            if (!connectorExists(id)) {
                 return;
             }
 
@@ -325,7 +297,7 @@ public class TPSConnectorService implements TPSConnectorResource {
             CryptoUtil.deleteSharedSecret(nickname);
 
             cs.putString("tps." + id + ".nickname", "");
-            cs.commit(false);
+            cs.commit(true);
         } catch (InvalidKeyException | IllegalStateException | EBaseException
                 | NotInitializedException | TokenException e) {
             e.printStackTrace();
@@ -337,21 +309,22 @@ public class TPSConnectorService implements TPSConnectorResource {
     @Override
     public KeyData getSharedSecret(String id) {
         try {
-            if (getConnector(id) == null) {
-                throw new BadRequestException("TPS Connection does not exist");
+            if (!connectorExists(id)) {
+                throw new ResourceNotFoundException("TPS connection does not exist");
             }
 
             // get and validate user
             String userid = validateUser(id);
 
-            // get user cert
-            IUser user = userGroupManager.getUser(userid);
-            X509Certificate[] certs = user.getX509Certificates();
-
             String nickname = userid + " sharedSecret";
             if (!CryptoUtil.sharedSecretExists(nickname)) {
                 return null;
             }
+
+            // get user cert
+            IUser user = userGroupManager.getUser(userid);
+            X509Certificate[] certs = user.getX509Certificates();
+
             byte[] wrappedKey = CryptoUtil.exportSharedSecret(nickname, certs[0]);
             KeyData keyData = new KeyData();
             keyData.setWrappedPrivateData(Utils.base64encode(wrappedKey));
@@ -365,12 +338,44 @@ public class TPSConnectorService implements TPSConnectorResource {
         }
     }
 
-    @Override
-    public void deleteConnector(String host, String port) {
-        TPSConnectorData data = getConnector(host, port);
-        if (data == null) {
-            return;
+    private boolean connectorExists(String id) throws EBaseException {
+        String tpsList = cs.getString(TPS_LIST, "");
+        return ArrayUtils.contains(StringUtils.split(tpsList, ","), id);
+    }
+
+    private String getConnectorID(String host, String port) throws EBaseException {
+        String tpsList = cs.getString(TPS_LIST, "");
+        for (String tpsID : StringUtils.split(tpsList,",")) {
+            TPSConnectorData data = createTPSConnectorData(tpsID);
+            if (data.getHost().equals(host) && data.getPort().equals(port))
+                return tpsID;
         }
-        deleteConnector(data.getID());
+        return null;
+    }
+
+    private void addToConnectorList(String id) throws EBaseException {
+        String tpsList = cs.getString(TPS_LIST, "");
+        Collection<String> sorted = new TreeSet<String>();
+        sorted.addAll(Arrays.asList(StringUtils.split(tpsList, ",")));
+        sorted.add(id);
+        cs.putString(TPS_LIST, StringUtils.join(sorted, ","));
+    }
+
+    private void removeFromConnectorList(String id) throws EBaseException {
+        String tpsList = cs.getString(TPS_LIST, "");
+        Collection<String> sorted = new TreeSet<String>();
+        sorted.addAll(Arrays.asList(StringUtils.split(tpsList, ",")));
+        sorted.remove(id);
+        cs.putString(TPS_LIST, StringUtils.join(sorted, ","));
+    }
+
+    private String findNextConnectorID() throws EBaseException {
+        String tpsList = cs.getString(TPS_LIST, "");
+        Collection<String> sorted = new TreeSet<String>();
+        sorted.addAll(Arrays.asList(StringUtils.split(tpsList, ",")));
+
+        int index = 0;
+        while (sorted.contains(Integer.toString(index))) index++;
+        return Integer.toString(index);
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java
index c9e1df985d37af1df74d417e7dbcf3404620ee48..7a5f19412d3ab20cd38be86c8212578a0a0b3228 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java
@@ -17,6 +17,9 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.cmstools.system;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -27,22 +30,36 @@ public class TPSConnectorAddCLI extends CLI {
     public TPSConnectorCLI tpsConnectorCLI;
 
     public TPSConnectorAddCLI(TPSConnectorCLI tpsConnectorCLI) {
-        super("add", "Add TPS Connector to TKS", tpsConnectorCLI);
+        super("add", "Add TPS connector to TKS", tpsConnectorCLI);
         this.tpsConnectorCLI = tpsConnectorCLI;
     }
 
     public void printHelp() {
-        formatter.printHelp(getFullName() + " <TPS Host> <TPS Port>", options);
+        formatter.printHelp(getFullName() + " [OPTIONS...]", options);
     }
 
     public void execute(String[] args) throws Exception {
-        if (args.length != 2) {
+        Option option = new Option(null, "host", true, "TPS host");
+        option.setArgName("host");
+        options.addOption(option);
+
+        option = new Option(null, "port", true, "TPS port");
+        option.setArgName("port");
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
             printHelp();
             System.exit(1);
         }
 
-        String tpsHost = args[0];
-        String tpsPort = args[1];
+        String tpsHost = cmd.getOptionValue("host");
+        String tpsPort = cmd.getOptionValue("port");
 
         tpsConnectorCLI.tpsConnectorClient.createConnector(tpsHost, tpsPort);
 
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java
index 7a87c34287f9fd9b32e26f0cff7c774ffd601b49..609c08379815741b62b0895a4c922ea5f2b632d7 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java
@@ -33,7 +33,7 @@ public class TPSConnectorCLI extends CLI {
     public TPSConnectorClient tpsConnectorClient;
 
     public TPSConnectorCLI(CLI parent) {
-        super("tpsconnector", "TPS Connector management commands", parent);
+        super("tpsconnector", "TPS connector management commands", parent);
 
         addModule(new TPSConnectorAddCLI(this));
         addModule(new TPSConnectorFindCLI(this));
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java
index 0a42b7d3ff8de23f8f341d1cbcf2654dec83406e..4caab729f73e47a4cfa01d4361a3c0feb7d545f6 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java
@@ -22,9 +22,11 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.base.ResourceNotFoundException;
 import com.netscape.certsrv.system.TPSConnectorCollection;
 import com.netscape.certsrv.system.TPSConnectorData;
 import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
 
 /**
  * @author Ade Lee
@@ -65,14 +67,26 @@ public class TPSConnectorFindCLI extends CLI {
         String tpsPort = cmd.getOptionValue("port");
 
         if (tpsHost != null) {
-            if (tpsPort == null) tpsPort = "443";
-            TPSConnectorData data = tpsConnectorCLI.tpsConnectorClient.getConnector(
-                    tpsHost, tpsPort);
-            TPSConnectorCLI.printConnectorInfo(data);
+            if (tpsPort == null)
+                tpsPort = "443";
+            try {
+                TPSConnectorData data = tpsConnectorCLI.tpsConnectorClient.getConnector(
+                        tpsHost, tpsPort);
+                TPSConnectorCLI.printConnectorInfo(data);
+            } catch (ResourceNotFoundException e) {
+                System.out.println("  TPS connector not found.");
+                return;
+            }
         } else {
             TPSConnectorCollection result = tpsConnectorCLI.tpsConnectorClient.listConnectors();
             Collection<TPSConnectorData> conns = result.getEntries();
 
+            if (conns.isEmpty()) {
+                System.out.println("  No TPS connectors found.");
+                return;
+            }
+
+            MainCLI.printMessage(conns.size() + " TPS connector(s) matched");
             boolean first = true;
             for (TPSConnectorData data: conns) {
                 if (first) {
@@ -83,6 +97,8 @@ public class TPSConnectorFindCLI extends CLI {
 
                 TPSConnectorCLI.printConnectorInfo(data);
             }
+
+            MainCLI.printMessage("Number of entries returned " + conns.size());
         }
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java
index f721fe28f0a3332d80a773ee24f1707c0c0da8b1..875e5958ec7bfdb928160b3a80dd8ca42277133c 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java
@@ -17,6 +17,9 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.cmstools.system;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -32,18 +35,31 @@ public class TPSConnectorRemoveCLI extends CLI {
     }
 
     public void printHelp() {
-        formatter.printHelp(getFullName() + " <TPS Host> <TPS Port>", options);
+        formatter.printHelp(getFullName() + " [OPTIONS...]", options);
     }
 
     public void execute(String[] args) throws Exception {
+        Option option = new Option(null, "host", true, "TPS host");
+        option.setArgName("host");
+        options.addOption(option);
 
-        if (args.length != 2) {
+        option = new Option(null, "port", true, "TPS port");
+        option.setArgName("port");
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
             printHelp();
             System.exit(1);
         }
 
-        String tpsHost = args[0];
-        String tpsPort = args[1];
+        String tpsHost = cmd.getOptionValue("host");
+        String tpsPort = cmd.getOptionValue("port");
 
         tpsConnectorCLI.tpsConnectorClient.deleteConnector(tpsHost, tpsPort);
 
diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py
index ce800471b5957e64a8c92e106c334d3d9b68284f..d98d8ab429260fe303bb256bb4d9cb871cd9ab6c 100644
--- a/base/server/python/pki/server/deployment/pkihelper.py
+++ b/base/server/python/pki/server/deployment/pkihelper.py
@@ -2826,7 +2826,9 @@ class TPSConnector:
                    "-d", self.master_dict['pki_database_path'],
                    "-c", token_pwd,
                    "-t", "tks",
-                   "tks-tpsconnector-del", tpshost, str(tpsport)]
+                   "tks-tpsconnector-del",
+                   "--host", tpshost,
+                   "--port", str(tpsport)]
 
         output = subprocess.check_output(command,
                                          stderr=subprocess.STDOUT,
-- 
1.8.3.1

