From e76c441de8a6ae9f7a610bf10c7edabda5213d0f Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 14 Feb 2014 19:01:55 -0500
Subject: [PATCH] Updated REST interface for TPS connections.

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

Ticket #554
---
 .../certsrv/tps/connection/ConnectionClient.java   |  9 +++++---
 .../certsrv/tps/connection/ConnectionResource.java |  9 +++++---
 .../server/tps/connection/ConnectionService.java   | 25 +++++++++-------------
 3 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionClient.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionClient.java
index 8014e56b1d763c1d8f349b5da1958d6594710bbf..4ef59366c6114f03a4124971d7f38cc661a2c613 100644
--- a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionClient.java
@@ -41,11 +41,13 @@ public class ConnectionClient extends Client {
     }
 
     public ConnectionCollection findConnections(Integer start, Integer size) {
-        return resource.findConnections(start, size);
+        Response response = resource.findConnections(start, size);
+        return client.getEntity(response, ConnectionCollection.class);
     }
 
     public ConnectionData getConnection(String connectionID) {
-        return resource.getConnection(connectionID);
+        Response response = resource.getConnection(connectionID);
+        return client.getEntity(response, ConnectionData.class);
     }
 
     public ConnectionData addConnection(ConnectionData connectionData) {
@@ -64,6 +66,7 @@ public class ConnectionClient extends Client {
     }
 
     public void removeConnection(String connectionID) {
-        resource.removeConnection(connectionID);
+        Response response = resource.removeConnection(connectionID);
+        client.getEntity(response, Void.class);
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionResource.java b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionResource.java
index 21f4c08e5d2aa1a4e421e47ea7e4812212a98e67..72334a04b81548ff09f1abdbd4d155771b349129 100644
--- a/base/common/src/com/netscape/certsrv/tps/connection/ConnectionResource.java
+++ b/base/common/src/com/netscape/certsrv/tps/connection/ConnectionResource.java
@@ -44,15 +44,17 @@ import com.netscape.certsrv.authentication.AuthMethodMapping;
 public interface ConnectionResource {
 
     @GET
+    @ClientResponseType(entityType=ConnectionCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public ConnectionCollection findConnections(
+    public Response findConnections(
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
 
     @GET
     @Path("{connectionID}")
+    @ClientResponseType(entityType=ConnectionData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public ConnectionData getConnection(@PathParam("connectionID") String connectionID);
+    public Response getConnection(@PathParam("connectionID") String connectionID);
 
     @POST
     @ACLMapping("connections.add")
@@ -83,7 +85,8 @@ public interface ConnectionResource {
 
     @DELETE
     @Path("{connectionID}")
+    @ClientResponseType(entityType=Void.class)
     @ACLMapping("connections.remove")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void removeConnection(@PathParam("connectionID") String connectionID);
+    public Response removeConnection(@PathParam("connectionID") String connectionID);
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionService.java
index 93187a775db3fa25c4ed8c7055a517d2ad8cfe27..a78420eda2fea22b095ca44c1ff52082d4d76d73 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/connection/ConnectionService.java
@@ -93,7 +93,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
     }
 
     @Override
-    public ConnectionCollection findConnections(Integer start, Integer size) {
+    public Response findConnections(Integer start, Integer size) {
 
         CMS.debug("ConnectionService.findConnections()");
 
@@ -131,7 +131,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
                 response.addLink(new Link("next", uri));
             }
 
-            return response;
+            return createOKResponse(response);
 
         } catch (PKIException e) {
             throw e;
@@ -143,7 +143,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
     }
 
     @Override
-    public ConnectionData getConnection(String connectionID) {
+    public Response getConnection(String connectionID) {
 
         if (connectionID == null) throw new BadRequestException("Connection ID is null.");
 
@@ -153,7 +153,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             ConnectionDatabase database = subsystem.getConnectionDatabase();
 
-            return createConnectionData(database.getRecord(connectionID));
+            return createOKResponse(createConnectionData(database.getRecord(connectionID)));
 
         } catch (PKIException e) {
             throw e;
@@ -178,10 +178,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
             database.addRecord(connectionData.getID(), createConnectionRecord(connectionData));
             connectionData = createConnectionData(database.getRecord(connectionData.getID()));
 
-            return Response
-                    .created(connectionData.getLink().getHref())
-                    .entity(connectionData)
-                    .build();
+            return createCreatedResponse(connectionData, connectionData.getLink().getHref());
 
         } catch (PKIException e) {
             throw e;
@@ -227,9 +224,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
 
             connectionData = createConnectionData(database.getRecord(connectionID));
 
-            return Response
-                    .ok(connectionData)
-                    .build();
+            return createOKResponse(connectionData);
 
         } catch (PKIException e) {
             throw e;
@@ -287,9 +282,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
 
             ConnectionData connectionData = createConnectionData(database.getRecord(connectionID));
 
-            return Response
-                    .ok(connectionData)
-                    .build();
+            return createOKResponse(connectionData);
 
         } catch (PKIException e) {
             throw e;
@@ -301,7 +294,7 @@ public class ConnectionService extends PKIService implements ConnectionResource
     }
 
     @Override
-    public void removeConnection(String connectionID) {
+    public Response removeConnection(String connectionID) {
 
         if (connectionID == null) throw new BadRequestException("Connection ID is null.");
 
@@ -320,6 +313,8 @@ public class ConnectionService extends PKIService implements ConnectionResource
 
             database.removeRecord(connectionID);
 
+            return createNoContentResponse();
+
         } catch (PKIException e) {
             throw e;
 
-- 
1.8.4.2

