From 1fb59ef1405bdf8fbadcdf5f156aec3d34375ef1 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Wed, 19 Feb 2014 11:22:31 -0500
Subject: [PATCH] Updated REST interface for key requests.

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

Ticket #554
---
 .../src/com/netscape/certsrv/key/KeyClient.java      | 15 ++++++++++-----
 .../com/netscape/certsrv/key/KeyRequestResource.java | 16 ++++++++++------
 .../cms/servlet/request/KeyRequestService.java       | 20 +++++++++++++-------
 3 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/key/KeyClient.java b/base/common/src/com/netscape/certsrv/key/KeyClient.java
index bdb84fddbf020dd9633575d5e967335e1596c3cf..374b793ffdc94046ce79f0ea2d3906c53fe62099 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyClient.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyClient.java
@@ -102,7 +102,7 @@ public class KeyClient extends Client {
             Integer pageSize,
             Integer maxResults,
             Integer maxTime) {
-        return keyRequestClient.listRequests(
+        Response response = keyRequestClient.listRequests(
                 requestState,
                 requestType,
                 clientID,
@@ -110,10 +110,12 @@ public class KeyClient extends Client {
                 pageSize,
                 maxResults,
                 maxTime);
+        return client.getEntity(response, KeyRequestInfoCollection.class);
     }
 
     public KeyRequestInfo getRequestInfo(RequestId id) {
-        return keyRequestClient.getRequestInfo(id);
+        Response response = keyRequestClient.getRequestInfo(id);
+        return client.getEntity(response, KeyRequestInfo.class);
     }
 
     public KeyRequestResponse archiveSecurityData(byte[] encoded, String clientId, String dataType, String algorithm, int strength) {
@@ -172,14 +174,17 @@ public class KeyClient extends Client {
     }
 
     public void approveRequest(RequestId id) {
-        keyRequestClient.approveRequest(id);
+        Response response = keyRequestClient.approveRequest(id);
+        client.getEntity(response, Void.class);
     }
 
     public void rejectRequest(RequestId id) {
-        keyRequestClient.rejectRequest(id);
+        Response response = keyRequestClient.rejectRequest(id);
+        client.getEntity(response, Void.class);
     }
 
     public void cancelRequest(RequestId id) {
-        keyRequestClient.cancelRequest(id);
+        Response response = keyRequestClient.cancelRequest(id);
+        client.getEntity(response, Void.class);
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
index e6e6c2e70d7001b32adda41eca01724565e40504..867136bc1220ea1beb8f9fb38c7bdd43f18062e8 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
@@ -40,8 +40,9 @@ public interface KeyRequestResource {
      * Used to generate list of key requests based on the search parameters
      */
     @GET
+    @ClientResponseType(entityType=KeyRequestInfoCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public KeyRequestInfoCollection listRequests(@QueryParam("requestState") String requestState,
+    public Response listRequests(@QueryParam("requestState") String requestState,
                                             @QueryParam("requestType") String requestType,
                                             @QueryParam("clientID") String clientID,
                                             @QueryParam("start") RequestId start,
@@ -66,19 +67,22 @@ public interface KeyRequestResource {
      */
     @GET
     @Path("{id}")
+    @ClientResponseType(entityType=KeyRequestInfo.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public KeyRequestInfo getRequestInfo(@PathParam("id") RequestId id);
+    public Response getRequestInfo(@PathParam("id") RequestId id);
 
     @POST
     @Path("{id}/approve")
-    public void approveRequest(@PathParam("id") RequestId id);
+    @ClientResponseType(entityType=Void.class)
+    public Response approveRequest(@PathParam("id") RequestId id);
 
     @POST
     @Path("{id}/reject")
-    public void rejectRequest(@PathParam("id") RequestId id);
+    @ClientResponseType(entityType=Void.class)
+    public Response rejectRequest(@PathParam("id") RequestId id);
 
     @POST
     @Path("{id}/cancel")
-    public void cancelRequest(@PathParam("id") RequestId id);
-
+    @ClientResponseType(entityType=Void.class)
+    public Response cancelRequest(@PathParam("id") RequestId id);
 }
diff --git a/base/server/cms/src/com/netscape/cms/servlet/request/KeyRequestService.java b/base/server/cms/src/com/netscape/cms/servlet/request/KeyRequestService.java
index a81ca0acbd50580888bdb52c3269ba8bfee176bc..b85978a34017aa28bf259e23fe6dc70272ed375a 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/request/KeyRequestService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/request/KeyRequestService.java
@@ -136,7 +136,7 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
      * Used to retrieve key request info for a specific request
      */
     @Override
-    public KeyRequestInfo getRequestInfo(RequestId id) {
+    public Response getRequestInfo(RequestId id) {
         if (id == null) {
             CMS.debug("getRequestInfo: is is null");
             throw new BadRequestException("Unable to get Request: invalid ID");
@@ -155,7 +155,7 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
             // request does not exist
             throw new RequestNotFoundException(id);
         }
-        return info;
+        return createOKResponse(info);
     }
 
     public Response archiveKey(KeyArchivalRequest data) {
@@ -251,7 +251,7 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
     }
 
     @Override
-    public void approveRequest(RequestId id) {
+    public Response approveRequest(RequestId id) {
         if (id == null) {
             throw new BadRequestException("Invalid request id.");
         }
@@ -272,10 +272,12 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
             auditRecoveryRequestChange(id, ILogger.FAILURE, "approve");
             throw new PKIException(e.toString());
         }
+
+        return createNoContentResponse();
     }
 
     @Override
-    public void rejectRequest(RequestId id) {
+    public Response rejectRequest(RequestId id) {
         if (id == null) {
             throw new BadRequestException("Invalid request id.");
         }
@@ -289,10 +291,12 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
             auditRecoveryRequestChange(id, ILogger.FAILURE, "reject");
             throw new PKIException(e.toString());
         }
+
+        return createNoContentResponse();
     }
 
     @Override
-    public void cancelRequest(RequestId id) {
+    public Response cancelRequest(RequestId id) {
         if (id == null) {
             throw new BadRequestException("Invalid request id.");
         }
@@ -306,13 +310,15 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
             auditRecoveryRequestChange(id, ILogger.FAILURE, "cancel");
             throw new PKIException(e.toString());
         }
+
+        return createNoContentResponse();
     }
 
     /**
      * Used to generate list of key requests based on the search parameters
      */
     @Override
-    public KeyRequestInfoCollection listRequests(String requestState, String requestType, String clientID,
+    public Response listRequests(String requestState, String requestType, String clientID,
             RequestId start, Integer pageSize, Integer maxResults, Integer maxTime) {
         // auth and authz
 
@@ -334,7 +340,7 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
             e.printStackTrace();
             throw new PKIException(e.toString());
         }
-        return requests;
+        return createOKResponse(requests);
     }
 
     private String createSearchFilter(String requestState, String requestType, String clientID) {
-- 
1.8.4.2

