From 2aa389d65e4c122f6f5199a079689979f8345ecb Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 14 Feb 2014 15:02:17 -0500
Subject: [PATCH] Updated REST interface for TPS connectors.

The REST interface for TPS connectors has been modified to return
Response objects to allow better handling of server responses.

Ticket #554
---
 .../certsrv/system/TPSConnectorClient.java         | 28 +++++---
 .../certsrv/system/TPSConnectorResource.java       | 27 +++++---
 .../cms/servlet/tks/TPSConnectorService.java       | 81 +++++++++++++---------
 3 files changed, 86 insertions(+), 50 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java b/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java
index 8662159ffbbeb51f9cd25c9c0b2d25a5756013ce..87323e92b96705362d887195e20957b6579d1bef 100644
--- a/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java
+++ b/base/common/src/com/netscape/certsrv/system/TPSConnectorClient.java
@@ -22,15 +22,18 @@ public class TPSConnectorClient extends Client {
     }
 
     public TPSConnectorCollection findConnectors(Integer start, Integer size) {
-        return tpsConnectorClient.findConnectors(start, size);
+        Response response = tpsConnectorClient.findConnectors(start, size);
+        return client.getEntity(response, TPSConnectorCollection.class);
     }
 
     public TPSConnectorData getConnector(String id) {
-        return tpsConnectorClient.getConnector(id);
+        Response response = tpsConnectorClient.getConnector(id);
+        return client.getEntity(response, TPSConnectorData.class);
     }
 
     public TPSConnectorData getConnector(String host, String port) {
-        return tpsConnectorClient.getConnector(host, port);
+        Response response = tpsConnectorClient.getConnector(host, port);
+        return client.getEntity(response, TPSConnectorData.class);
     }
 
     public TPSConnectorData createConnector(String tpsHost, String tpsPort) {
@@ -44,27 +47,32 @@ public class TPSConnectorClient extends Client {
     }
 
     public void deleteConnector(String id) {
-        tpsConnectorClient.deleteConnector(id);
+        Response response = tpsConnectorClient.deleteConnector(id);
+        client.getEntity(response, Void.class);
     }
 
     public KeyData createSharedSecret(String id) {
-        return tpsConnectorClient.createSharedSecret(id);
+        Response response = tpsConnectorClient.createSharedSecret(id);
+        return client.getEntity(response, KeyData.class);
     }
 
     public KeyData replaceSharedSecret(String id) {
-        return tpsConnectorClient.replaceSharedSecret(id);
+        Response response = tpsConnectorClient.replaceSharedSecret(id);
+        return client.getEntity(response, KeyData.class);
     };
 
     public void deleteSharedSecret(String id) {
-        tpsConnectorClient.deleteSharedSecret(id);
+        Response response = tpsConnectorClient.deleteSharedSecret(id);
+        client.getEntity(response, Void.class);
     }
 
     public KeyData getSharedSecret(String id) {
-        return tpsConnectorClient.getSharedSecret(id);
+        Response response = tpsConnectorClient.getSharedSecret(id);
+        return client.getEntity(response, KeyData.class);
     }
 
     public void deleteConnector(String host, String port) {
-        tpsConnectorClient.deleteConnector(host, port);
+        Response response = tpsConnectorClient.deleteConnector(host, port);
+        client.getEntity(response, Void.class);
     }
-
 }
diff --git a/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java b/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java
index 441e6f3033dd7d9682bda07065774531b2cb26b2..43213d0f401988eb49718d72fda363e9c0f1018c 100644
--- a/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java
+++ b/base/common/src/com/netscape/certsrv/system/TPSConnectorResource.java
@@ -42,20 +42,23 @@ import com.netscape.certsrv.key.KeyData;
 @AuthMethodMapping("tpsconnectors")
 public interface TPSConnectorResource {
     @GET
+    @ClientResponseType(entityType=TPSConnectorCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public TPSConnectorCollection findConnectors(
+    public Response findConnectors(
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
 
     @GET
     @Path("{id}")
+    @ClientResponseType(entityType=TPSConnectorData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public TPSConnectorData getConnector(@PathParam("id") String id);
+    public Response getConnector(@PathParam("id") String id);
 
     @GET
     @Path("search")
+    @ClientResponseType(entityType=TPSConnectorData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public TPSConnectorData getConnector(@QueryParam("host") String host,
+    public Response getConnector(@QueryParam("host") String host,
             @QueryParam("port") String port);
 
     @POST
@@ -73,33 +76,39 @@ public interface TPSConnectorResource {
 
     @DELETE
     @Path("{id}")
-    public void deleteConnector(@PathParam("id") String id);
+    @ClientResponseType(entityType=Void.class)
+    public Response deleteConnector(@PathParam("id") String id);
 
     @POST
     @Path("{id}/shared-secret")
+    @ClientResponseType(entityType=KeyData.class)
     @ACLMapping("admin.sharedsecret")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public KeyData createSharedSecret(@PathParam("id") String id);
+    public Response createSharedSecret(@PathParam("id") String id);
 
     @PUT
     @Path("{id}/shared-secret")
+    @ClientResponseType(entityType=KeyData.class)
     @ACLMapping("admin.sharedsecret")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public KeyData replaceSharedSecret(@PathParam("id") String id);
+    public Response replaceSharedSecret(@PathParam("id") String id);
 
     @DELETE
     @Path("{id}/shared-secret")
+    @ClientResponseType(entityType=Void.class)
     @ACLMapping("admin.sharedsecret")
-    public void deleteSharedSecret(@PathParam("id") String id);
+    public Response deleteSharedSecret(@PathParam("id") String id);
 
     @DELETE
+    @ClientResponseType(entityType=Void.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void deleteConnector(@QueryParam("host") String host,
+    public Response deleteConnector(@QueryParam("host") String host,
             @QueryParam("port") String port);
 
     @GET
     @Path("{id}/shared-secret")
+    @ClientResponseType(entityType=KeyData.class)
     @ACLMapping("admin.sharedsecret")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public KeyData getSharedSecret(@PathParam("id") String id);
+    public Response getSharedSecret(@PathParam("id") String id);
 }
diff --git a/base/server/cms/src/com/netscape/cms/servlet/tks/TPSConnectorService.java b/base/server/cms/src/com/netscape/cms/servlet/tks/TPSConnectorService.java
index 197fbb8f9f3c949a10fbcb7da9b8f6af6c0008fc..c11f1239f552ef6cee999186d10bfb79ecb7d494 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/tks/TPSConnectorService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/tks/TPSConnectorService.java
@@ -38,10 +38,11 @@ import com.netscape.certsrv.tps.cert.TPSCertResource;
 import com.netscape.certsrv.usrgrp.IUGSubsystem;
 import com.netscape.certsrv.usrgrp.IUser;
 import com.netscape.cms.realm.PKIPrincipal;
+import com.netscape.cms.servlet.base.PKIService;
 import com.netscape.cmsutil.crypto.CryptoUtil;
 import com.netscape.cmsutil.util.Utils;
 
-public class TPSConnectorService implements TPSConnectorResource {
+public class TPSConnectorService extends PKIService implements TPSConnectorResource {
 
     private static final String TPS_LIST = "tps.list";
 
@@ -58,7 +59,7 @@ public class TPSConnectorService implements TPSConnectorResource {
     public IUGSubsystem userGroupManager = (IUGSubsystem) CMS.getSubsystem(CMS.SUBSYSTEM_UG);
 
     @Override
-    public TPSConnectorCollection findConnectors(Integer start, Integer size) {
+    public Response findConnectors(Integer start, Integer size) {
         try {
             String tpsList = cs.getString(TPS_LIST, "");
             Iterator<String> entries = Arrays.asList(StringUtils.split(tpsList,",")).iterator();
@@ -88,7 +89,7 @@ public class TPSConnectorService implements TPSConnectorResource {
                 response.addLink(new Link("next", uri));
             }
 
-            return response;
+            return createOKResponse(response);
 
         } catch (EBaseException e) {
             e.printStackTrace();
@@ -109,13 +110,20 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public TPSConnectorData getConnector(String id) {
+    public Response getConnector(String id) {
+        return createOKResponse(getConnectorData(id));
+    }
+
+    public TPSConnectorData getConnectorData(String id) {
 
         if (id == null) throw new BadRequestException("TPS connector ID is null.");
 
         try {
-            if (connectorExists(id)) return createTPSConnectorData(id);
-            throw new ResourceNotFoundException("Connector " + id + " not found.");
+            if (!connectorExists(id))
+                throw new ResourceNotFoundException("Connector " + id + " not found.");
+
+            return createTPSConnectorData(id);
+
         } catch (EBaseException e) {
             e.printStackTrace();
             throw new PKIException("Unable to get TPS connection data" + e);
@@ -123,16 +131,19 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public TPSConnectorData getConnector(String host, String port) {
+    public Response getConnector(String host, String port) {
 
         if (host == null) throw new BadRequestException("TPS connector host is null.");
         if (port == null) throw new BadRequestException("TPS connector port is null.");
 
         try {
             String id = getConnectorID(host, port);
-            if (id != null) return createTPSConnectorData(id);
-            throw new ResourceNotFoundException(
-                    "Connector not found for " + host + ":" + port);
+            if (id == null)
+                throw new ResourceNotFoundException(
+                        "Connector not found for " + host + ":" + port);
+
+            return createOKResponse(createTPSConnectorData(id));
+
         } catch (EBaseException e) {
             e.printStackTrace();
             throw new PKIException("Unable to get TPS connection data" + e);
@@ -166,10 +177,7 @@ public class TPSConnectorService implements TPSConnectorResource {
             addToConnectorList(newID);
             cs.commit(true);
 
-            return Response
-                    .created(newData.getLink().getHref())
-                    .entity(newData)
-                    .build();
+            return createCreatedResponse(newData, newData.getLink().getHref());
 
         } catch (EBaseException e) {
             CMS.debug("Unable to create new TPS Connector: " + e);
@@ -199,17 +207,15 @@ public class TPSConnectorService implements TPSConnectorResource {
             if ((data.getUserID() != null) || (data.getNickname() != null)) {
                 throw new UnauthorizedException("Cannot change userid or nickname using this interface");
             }
-            TPSConnectorData curData = getConnector(id);
+            TPSConnectorData curData = getConnectorData(id);
             curData.setHost(data.getHost());
             curData.setPort(data.getPort());
 
             saveClientData(curData);
             cs.commit(true);
 
-            return Response
-                    .ok(curData.getLink().getHref())
-                    .entity(curData)
-                    .build();
+            return createOKResponse(curData);
+
         } catch (EBaseException e) {
             CMS.debug("Unable to modify TPS Connector: " + e);
             e.printStackTrace();
@@ -236,17 +242,20 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public void deleteConnector(String id) {
+    public Response deleteConnector(String id) {
         try {
             if (StringUtils.isEmpty(id))
                 throw new BadRequestException("Attempt to delete TPS connection with null or empty id");
 
-            if (!connectorExists(id)) return;
+            if (!connectorExists(id)) return createNoContentResponse();
 
             deleteSharedSecret(id);
             cs.removeSubStore("tps." + id);
             removeFromConnectorList(id);
             cs.commit(true);
+
+            return createNoContentResponse();
+
         } catch (EBaseException e) {
             e.printStackTrace();
             throw new PKIException("Failed to delete TPS connection" + e);
@@ -254,7 +263,7 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public void deleteConnector(String host, String port) {
+    public Response deleteConnector(String host, String port) {
 
         if (host == null) throw new BadRequestException("TPS connector host is null.");
         if (port == null) throw new BadRequestException("TPS connector port is null.");
@@ -267,10 +276,12 @@ public class TPSConnectorService implements TPSConnectorResource {
             e.printStackTrace();
             throw new PKIException("Failed to delete TPS connector: " + e);
         }
+
+        return createNoContentResponse();
     }
 
     @Override
-    public KeyData createSharedSecret(String id) {
+    public Response createSharedSecret(String id) {
 
         if (id == null) throw new BadRequestException("TPS connector ID is null.");
 
@@ -299,7 +310,8 @@ public class TPSConnectorService implements TPSConnectorResource {
             byte[] wrappedKey = CryptoUtil.exportSharedSecret(nickname, certs[0]);
             KeyData keyData = new KeyData();
             keyData.setWrappedPrivateData(Utils.base64encode(wrappedKey));
-            return keyData;
+
+            return createOKResponse(keyData);
 
         } catch (InvalidKeyException | IllegalStateException | NoSuchAlgorithmException
                 | InvalidAlgorithmParameterException | EBaseException
@@ -329,7 +341,7 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public KeyData replaceSharedSecret(String id) {
+    public Response replaceSharedSecret(String id) {
 
         if (id == null) throw new BadRequestException("TPS connector ID is null.");
 
@@ -355,7 +367,9 @@ public class TPSConnectorService implements TPSConnectorResource {
             byte[] wrappedKey = CryptoUtil.exportSharedSecret(nickname, certs[0]);
             KeyData keyData = new KeyData();
             keyData.setWrappedPrivateData(Utils.base64encode(wrappedKey));
-            return keyData;
+
+            return createOKResponse(keyData);
+
         } catch (InvalidKeyException | IllegalStateException | NoSuchAlgorithmException
                 | InvalidAlgorithmParameterException | EBaseException
                 | NotInitializedException | TokenException | IOException | InvalidKeyFormatException e) {
@@ -366,7 +380,7 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public void deleteSharedSecret(String id) {
+    public Response deleteSharedSecret(String id) {
 
         if (id == null) throw new BadRequestException("TPS connector ID is null.");
 
@@ -383,12 +397,15 @@ public class TPSConnectorService implements TPSConnectorResource {
 
             String nickname = userid + " sharedSecret";
             if (!CryptoUtil.sharedSecretExists(nickname)) {
-                return;
+                return createNoContentResponse();
             }
             CryptoUtil.deleteSharedSecret(nickname);
 
             cs.putString("tps." + id + ".nickname", "");
             cs.commit(true);
+
+            return createNoContentResponse();
+
         } catch (InvalidKeyException | IllegalStateException | EBaseException
                 | NotInitializedException | TokenException e) {
             e.printStackTrace();
@@ -398,7 +415,7 @@ public class TPSConnectorService implements TPSConnectorResource {
     }
 
     @Override
-    public KeyData getSharedSecret(String id) {
+    public Response getSharedSecret(String id) {
 
         if (id == null) throw new BadRequestException("TPS connector ID is null.");
 
@@ -412,7 +429,7 @@ public class TPSConnectorService implements TPSConnectorResource {
 
             String nickname = userid + " sharedSecret";
             if (!CryptoUtil.sharedSecretExists(nickname)) {
-                return null;
+                return createNoContentResponse();
             }
 
             // get user cert
@@ -422,7 +439,9 @@ public class TPSConnectorService implements TPSConnectorResource {
             byte[] wrappedKey = CryptoUtil.exportSharedSecret(nickname, certs[0]);
             KeyData keyData = new KeyData();
             keyData.setWrappedPrivateData(Utils.base64encode(wrappedKey));
-            return keyData;
+
+            return createOKResponse(keyData);
+
         } catch (InvalidKeyException | IllegalStateException | NoSuchAlgorithmException
                 | InvalidAlgorithmParameterException | EBaseException
                 | NotInitializedException | TokenException | IOException | InvalidKeyFormatException e) {
-- 
1.8.4.2

