From 766404c4a5deaa889e67b742414f8f014d49d735 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 14 Feb 2014 21:42:12 -0500
Subject: [PATCH] Updated REST interface for TPS tokens.

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

Ticket #554
---
 .../netscape/certsrv/tps/token/TokenClient.java    |  9 +++++---
 .../netscape/certsrv/tps/token/TokenResource.java  |  9 +++++---
 .../dogtagpki/server/tps/token/TokenService.java   | 25 +++++++++-------------
 3 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java b/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java
index 35f6e16759729fd0f8edcbaaec9290d49247fe61..98690cb99a8c5fcf4fc7f07f2e16dcd146ddd9a1 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java
@@ -41,11 +41,13 @@ public class TokenClient extends Client {
     }
 
     public TokenCollection findTokens(Integer start, Integer size) {
-        return resource.findTokens(start, size);
+        Response response = resource.findTokens(start, size);
+        return client.getEntity(response, TokenCollection.class);
     }
 
     public TokenData getToken(String tokenID) {
-        return resource.getToken(tokenID);
+        Response response = resource.getToken(tokenID);
+        return client.getEntity(response, TokenData.class);
     }
 
     public TokenData addToken(TokenData tokenData) {
@@ -59,6 +61,7 @@ public class TokenClient extends Client {
     }
 
     public void removeToken(String tokenID) {
-        resource.removeToken(tokenID);
+        Response response = resource.removeToken(tokenID);
+        client.getEntity(response, Void.class);
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java b/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java
index 529efe8c0c03ae01645110e5f364b73e5e6e1332..294ace128c36a1ae28e49a2ef64d36180254d7a6 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java
@@ -45,15 +45,17 @@ import com.netscape.certsrv.base.PATCH;
 public interface TokenResource {
 
     @GET
+    @ClientResponseType(entityType=TokenCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public TokenCollection findTokens(
+    public Response findTokens(
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
 
     @GET
     @Path("{tokenID}")
+    @ClientResponseType(entityType=TokenData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public TokenData getToken(@PathParam("tokenID") String tokenID);
+    public Response getToken(@PathParam("tokenID") String tokenID);
 
     @POST
     @ClientResponseType(entityType=TokenData.class)
@@ -84,7 +86,8 @@ public interface TokenResource {
 
     @DELETE
     @Path("{tokenID}")
+    @ClientResponseType(entityType=Void.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("tokens.remove")
-    public void removeToken(@PathParam("tokenID") String tokenID);
+    public Response removeToken(@PathParam("tokenID") String tokenID);
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
index 361fc29ecdc1c2444be752747c5f65c9442236e9..548c797b7ba15938fd3281a26a236a71ff8f2548 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
@@ -107,7 +107,7 @@ public class TokenService extends PKIService implements TokenResource {
     }
 
     @Override
-    public TokenCollection findTokens(Integer start, Integer size) {
+    public Response findTokens(Integer start, Integer size) {
 
         CMS.debug("TokenService.findTokens()");
 
@@ -145,7 +145,7 @@ public class TokenService extends PKIService implements TokenResource {
                 response.addLink(new Link("next", uri));
             }
 
-            return response;
+            return createOKResponse(response);
 
         } catch (Exception e) {
             e.printStackTrace();
@@ -154,7 +154,7 @@ public class TokenService extends PKIService implements TokenResource {
     }
 
     @Override
-    public TokenData getToken(String tokenID) {
+    public Response getToken(String tokenID) {
 
         if (tokenID == null) throw new BadRequestException("Token ID is null.");
 
@@ -164,7 +164,7 @@ public class TokenService extends PKIService implements TokenResource {
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            return createTokenData(database.getRecord(tokenID));
+            return createOKResponse(createTokenData(database.getRecord(tokenID)));
 
         } catch (Exception e) {
             e.printStackTrace();
@@ -187,10 +187,7 @@ public class TokenService extends PKIService implements TokenResource {
             database.addRecord(tokenID, createTokenRecord(tokenData));
             tokenData = createTokenData(database.getRecord(tokenID));
 
-            return Response
-                    .created(tokenData.getLink().getHref())
-                    .entity(tokenData)
-                    .build();
+            return createCreatedResponse(tokenData, tokenData.getLink().getHref());
 
         } catch (Exception e) {
             e.printStackTrace();
@@ -216,9 +213,7 @@ public class TokenService extends PKIService implements TokenResource {
 
             tokenData = createTokenData(database.getRecord(tokenID));
 
-            return Response
-                    .ok(tokenData)
-                    .build();
+            return createOKResponse(tokenData);
 
         } catch (Exception e) {
             e.printStackTrace();
@@ -249,9 +244,7 @@ public class TokenService extends PKIService implements TokenResource {
 
             tokenData = createTokenData(database.getRecord(tokenID));
 
-            return Response
-                    .ok(tokenData)
-                    .build();
+            return createOKResponse(tokenData);
 
         } catch (Exception e) {
             e.printStackTrace();
@@ -260,7 +253,7 @@ public class TokenService extends PKIService implements TokenResource {
     }
 
     @Override
-    public void removeToken(String tokenID) {
+    public Response removeToken(String tokenID) {
 
         if (tokenID == null) throw new BadRequestException("Token ID is null.");
 
@@ -271,6 +264,8 @@ public class TokenService extends PKIService implements TokenResource {
             TokenDatabase database = subsystem.getTokenDatabase();
             database.removeRecord(tokenID);
 
+            return createNoContentResponse();
+
         } catch (Exception e) {
             e.printStackTrace();
             throw new PKIException(e.getMessage());
-- 
1.8.4.2

