From 185d93fca053fbc7407c1cc1294712a5d3c04359 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Wed, 12 Feb 2014 15:10:30 -0500
Subject: [PATCH] Updated REST interface for certificate requests.

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

Ticket #554
---
 .../src/com/netscape/certsrv/cert/CertClient.java  | 40 +++++++++++------
 .../netscape/certsrv/cert/CertRequestResource.java | 46 +++++++++++++-------
 .../netscape/cmstools/cert/CertRequestFindCLI.java |  2 +-
 .../cms/servlet/request/CertRequestService.java    | 50 +++++++++++++---------
 4 files changed, 87 insertions(+), 51 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/cert/CertClient.java b/base/common/src/com/netscape/certsrv/cert/CertClient.java
index c52372c54bbe24809d41ef5102fb259bfae8842a..3af93269f284ab826901e6cfc4424f023850f31b 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertClient.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertClient.java
@@ -81,56 +81,68 @@ public class CertClient extends Client {
     }
 
     public CertRequestInfos enrollRequest(CertEnrollmentRequest data) {
-        return certRequestClient.enrollCert(data);
+        Response response = certRequestClient.enrollCert(data);
+        return client.getEntity(response, CertRequestInfos.class);
     }
 
     public CertRequestInfo getRequest(RequestId id) {
-        return certRequestClient.getRequestInfo(id);
+        Response response = certRequestClient.getRequestInfo(id);
+        return client.getEntity(response, CertRequestInfo.class);
     }
 
     public CertReviewResponse reviewRequest(RequestId id) {
-        return certRequestClient.reviewRequest(id);
+        Response response = certRequestClient.reviewRequest(id);
+        return client.getEntity(response, CertReviewResponse.class);
     }
 
     public void approveRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.approveRequest(id, data);
+        Response response = certRequestClient.approveRequest(id, data);
+        client.getEntity(response, Void.class);
     }
 
     public void rejectRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.rejectRequest(id, data);
+        Response response = certRequestClient.rejectRequest(id, data);
+        client.getEntity(response, Void.class);
     }
 
     public void cancelRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.cancelRequest(id, data);
+        Response response = certRequestClient.cancelRequest(id, data);
+        client.getEntity(response, Void.class);
     }
 
     public void updateRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.updateRequest(id, data);
+        Response response = certRequestClient.updateRequest(id, data);
+        client.getEntity(response, Void.class);
     }
 
     public void validateRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.validateRequest(id, data);
+        Response response = certRequestClient.validateRequest(id, data);
+        client.getEntity(response, Void.class);
     }
 
     public void assignRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.assignRequest(id, data);
+        Response response = certRequestClient.assignRequest(id, data);
+        client.getEntity(response, Void.class);
     }
 
     public void unassignRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.unassignRequest(id, data);
+        Response response = certRequestClient.unassignRequest(id, data);
+        client.getEntity(response, Void.class);
     }
 
     public CertRequestInfos listRequests(String requestState, String requestType, RequestId start, Integer pageSize,
             Integer maxResults, Integer maxTime) {
-        return certRequestClient.listRequests(requestState, requestType, start, pageSize, maxResults, maxTime);
+        Response response = certRequestClient.listRequests(requestState, requestType, start, pageSize, maxResults, maxTime);
+        return client.getEntity(response, CertRequestInfos.class);
     }
 
     public CertEnrollmentRequest getEnrollmentTemplate(String id) {
-        return certRequestClient.getEnrollmentTemplate(id);
+        Response response = certRequestClient.getEnrollmentTemplate(id);
+        return client.getEntity(response, CertEnrollmentRequest.class);
     }
 
     public ProfileDataInfos listEnrollmentTemplates(Integer start, Integer size) {
-        return certRequestClient.listEnrollmentTemplates(start, size);
+        Response response = certRequestClient.listEnrollmentTemplates(start, size);
+        return client.getEntity(response, ProfileDataInfos.class);
     }
-
 }
diff --git a/base/common/src/com/netscape/certsrv/cert/CertRequestResource.java b/base/common/src/com/netscape/certsrv/cert/CertRequestResource.java
index 8dc30bcdfd8674c497cbe9e0ee84d61946b3d80c..e1514293bd22ca295d56157239881d74d0928570 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertRequestResource.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertRequestResource.java
@@ -26,6 +26,9 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.annotations.ClientResponseType;
 
 import com.netscape.certsrv.acls.ACLMapping;
 import com.netscape.certsrv.authentication.AuthMethodMapping;
@@ -38,33 +41,37 @@ public interface CertRequestResource {
     // Enrollment - used to test integration with a browser
     @POST
     @Path("certrequests")
+    @ClientResponseType(entityType=CertRequestInfos.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
-    public CertRequestInfos enrollCert(MultivaluedMap<String, String> form);
+    public Response enrollCert(MultivaluedMap<String, String> form);
 
     @POST
     @Path("certrequests")
+    @ClientResponseType(entityType=CertRequestInfos.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public CertRequestInfos enrollCert(CertEnrollmentRequest data);
+    public Response enrollCert(CertEnrollmentRequest data);
 
     /**
      * Used to retrieve cert request info for a specific request
      */
     @GET
     @Path("certrequests/{id}")
+    @ClientResponseType(entityType=CertRequestInfo.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public CertRequestInfo getRequestInfo(@PathParam("id") RequestId id);
+    public Response getRequestInfo(@PathParam("id") RequestId id);
 
     /**
      * Used to generate list of cert requests based on the search parameters
      */
     @GET
     @Path("agent/certrequests")
+    @ClientResponseType(entityType=CertRequestInfos.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public CertRequestInfos listRequests(@QueryParam("requestState") String requestState,
+    public Response listRequests(@QueryParam("requestState") String requestState,
             @QueryParam("requestType") String requestType,
             @QueryParam("start") RequestId start,
             @QueryParam("pageSize") Integer pageSize,
@@ -73,70 +80,79 @@ public interface CertRequestResource {
 
     @GET
     @Path("agent/certrequests/{id}")
+    @ClientResponseType(entityType=CertReviewResponse.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public CertReviewResponse reviewRequest(@PathParam("id") RequestId id);
+    public Response reviewRequest(@PathParam("id") RequestId id);
 
     @GET
     @Path("certrequests/profiles")
+    @ClientResponseType(entityType=ProfileDataInfos.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public ProfileDataInfos listEnrollmentTemplates(
+    public Response listEnrollmentTemplates(
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
 
     @GET
     @Path("certrequests/profiles/{id}")
+    @ClientResponseType(entityType=CertEnrollmentRequest.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public CertEnrollmentRequest getEnrollmentTemplate(@PathParam("id") String id);
+    public Response getEnrollmentTemplate(@PathParam("id") String id);
 
     @POST
     @Path("agent/certrequests/{id}/approve")
+    @ClientResponseType(entityType=Void.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public void approveRequest(@PathParam("id") RequestId id, CertReviewResponse data);
+    public Response approveRequest(@PathParam("id") RequestId id, CertReviewResponse data);
 
     @POST
     @Path("agent/certrequests/{id}/reject")
+    @ClientResponseType(entityType=Void.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public void rejectRequest(@PathParam("id") RequestId id, CertReviewResponse data);
+    public Response rejectRequest(@PathParam("id") RequestId id, CertReviewResponse data);
 
     @POST
     @Path("agent/certrequests/{id}/cancel")
+    @ClientResponseType(entityType=Void.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public void cancelRequest(@PathParam("id") RequestId id, CertReviewResponse data);
+    public Response cancelRequest(@PathParam("id") RequestId id, CertReviewResponse data);
 
     @POST
     @Path("agent/certrequests/{id}/update")
+    @ClientResponseType(entityType=Void.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public void updateRequest(@PathParam("id") RequestId id, CertReviewResponse data);
+    public Response updateRequest(@PathParam("id") RequestId id, CertReviewResponse data);
 
     @POST
     @Path("agent/certrequests/{id}/validate")
+    @ClientResponseType(entityType=Void.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public void validateRequest(@PathParam("id") RequestId id, CertReviewResponse data);
+    public Response validateRequest(@PathParam("id") RequestId id, CertReviewResponse data);
 
     @POST
     @Path("agent/certrequests/{id}/unassign")
+    @ClientResponseType(entityType=Void.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public void unassignRequest(@PathParam("id") RequestId id, CertReviewResponse data);
+    public Response unassignRequest(@PathParam("id") RequestId id, CertReviewResponse data);
 
     @POST
     @Path("agent/certrequests/{id}/assign")
+    @ClientResponseType(entityType=Void.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certrequests")
     @AuthMethodMapping("certrequests")
-    public void assignRequest(@PathParam("id") RequestId id, CertReviewResponse data);
-
+    public Response assignRequest(@PathParam("id") RequestId id, CertReviewResponse data);
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
index d09c7698f43f8ad732f4c800441dbec8b27e0b74..d547b8d779358fdc510b30341ac54f2960680252 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
@@ -82,7 +82,7 @@ public class CertRequestFindCLI extends CLI {
         String requestType = cmd.getOptionValue("type");
         if (requestType != null && requestType.equals("all")) requestType = null;
 
-        CertRequestInfos response = certCLI.certClient.certRequestClient.listRequests(requestState, requestType, start, size, maxResults, maxTime);
+        CertRequestInfos response = certCLI.certClient.listRequests(requestState, requestType, start, size, maxResults, maxTime);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/server/cms/src/com/netscape/cms/servlet/request/CertRequestService.java b/base/server/cms/src/com/netscape/cms/servlet/request/CertRequestService.java
index cd3ffee5a60abb4627571833581384e5eb78bdf1..df50a007e0e2c716952e61e1759ac4895acfe23a 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/request/CertRequestService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/request/CertRequestService.java
@@ -29,6 +29,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Request;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
 import org.jboss.resteasy.plugins.providers.atom.Link;
@@ -90,7 +91,7 @@ public class CertRequestService extends PKIService implements CertRequestResourc
      * Used to retrieve key request info for a specific request
      */
     @Override
-    public CertRequestInfo getRequestInfo(RequestId id) {
+    public Response getRequestInfo(RequestId id) {
         if (id == null) {
             CMS.debug("getRequestInfo: id is null");
             throw new BadRequestException("Unable to get request: invalid id");
@@ -110,18 +111,18 @@ public class CertRequestService extends PKIService implements CertRequestResourc
             throw new RequestNotFoundException(id);
         }
 
-        return info;
+        return createOKResponse(info);
     }
 
     // Enrollment - used to test integration with a browser
     @Override
-    public CertRequestInfos enrollCert(MultivaluedMap<String, String> form) {
+    public Response enrollCert(MultivaluedMap<String, String> form) {
         CertEnrollmentRequest data = new CertEnrollmentRequest(form);
         return enrollCert(data);
     }
 
     @Override
-    public CertRequestInfos enrollCert(CertEnrollmentRequest data) {
+    public Response enrollCert(CertEnrollmentRequest data) {
 
         if (data == null) {
             CMS.debug("enrollCert: data is null");
@@ -150,42 +151,49 @@ public class CertRequestService extends PKIService implements CertRequestResourc
         // because it is possible to create more than one request
         // as a result of this enrollment
 
-        return infos;
+        return createOKResponse(infos);
     }
 
     @Override
-    public void approveRequest(RequestId id, CertReviewResponse data) {
+    public Response approveRequest(RequestId id, CertReviewResponse data) {
         changeRequestState(id, data, "approve");
+        return createNoContentResponse();
     }
 
     @Override
-    public void rejectRequest(RequestId id, CertReviewResponse data) {
+    public Response rejectRequest(RequestId id, CertReviewResponse data) {
         changeRequestState(id, data, "reject");
+        return createNoContentResponse();
     }
 
     @Override
-    public void cancelRequest(RequestId id, CertReviewResponse data) {
+    public Response cancelRequest(RequestId id, CertReviewResponse data) {
         changeRequestState(id, data, "cancel");
+        return createNoContentResponse();
     }
 
     @Override
-    public void updateRequest(RequestId id, CertReviewResponse data) {
+    public Response updateRequest(RequestId id, CertReviewResponse data) {
         changeRequestState(id, data, "update");
+        return createNoContentResponse();
     }
 
     @Override
-    public void validateRequest(RequestId id, CertReviewResponse data) {
+    public Response validateRequest(RequestId id, CertReviewResponse data) {
         changeRequestState(id, data, "validate");
+        return createNoContentResponse();
     }
 
     @Override
-    public void unassignRequest(RequestId id, CertReviewResponse data) {
+    public Response unassignRequest(RequestId id, CertReviewResponse data) {
         changeRequestState(id, data, "unassign");
+        return createNoContentResponse();
     }
 
     @Override
-    public void assignRequest(RequestId id, CertReviewResponse data) {
+    public Response assignRequest(RequestId id, CertReviewResponse data) {
         changeRequestState(id, data, "assign");
+        return createNoContentResponse();
     }
 
     public void changeRequestState(RequestId id, CertReviewResponse data, String op) {
@@ -222,7 +230,7 @@ public class CertRequestService extends PKIService implements CertRequestResourc
     }
 
     @Override
-    public CertReviewResponse reviewRequest(@PathParam("id") RequestId id) {
+    public Response reviewRequest(@PathParam("id") RequestId id) {
         if (id == null) {
             CMS.debug("reviewRequest: id is null");
             throw new BadRequestException("Unable to review request: invalid id");
@@ -243,14 +251,14 @@ public class CertRequestService extends PKIService implements CertRequestResourc
             throw new RequestNotFoundException(id);
         }
 
-        return info;
+        return createOKResponse(info);
     }
 
     /**
      * Used to generate list of cert requests based on the search parameters
      */
     @Override
-    public CertRequestInfos listRequests(String requestState, String requestType,
+    public Response listRequests(String requestState, String requestType,
             RequestId start, Integer pageSize, Integer maxResults, Integer maxTime) {
         // get ldap filter
         String filter = createSearchFilter(requestState, requestType);
@@ -270,7 +278,7 @@ public class CertRequestService extends PKIService implements CertRequestResourc
             e.printStackTrace();
             throw new PKIException("Error listing cert requests!");
         }
-        return requests;
+        return createOKResponse(requests);
     }
 
     private String createSearchFilter(String requestState, String requestType) {
@@ -300,7 +308,7 @@ public class CertRequestService extends PKIService implements CertRequestResourc
     }
 
     @Override
-    public CertEnrollmentRequest getEnrollmentTemplate(String profileId) {
+    public Response getEnrollmentTemplate(String profileId) {
         if (profileId == null) {
             CMS.debug("getEnrollmenTemplate: invalid request. profileId is null");
             throw new BadRequestException("Invalid ProfileId");
@@ -355,11 +363,11 @@ public class CertRequestService extends PKIService implements CertRequestResourc
             }
         }
 
-        return request;
+        return createOKResponse(request);
     }
 
     @Override
-    public ProfileDataInfos listEnrollmentTemplates(Integer start, Integer size) {
+    public Response listEnrollmentTemplates(Integer start, Integer size) {
 
         start = start == null ? DEFAULT_START : start;
         size = size == null ? DEFAULT_PAGESIZE : size;
@@ -374,7 +382,7 @@ public class CertRequestService extends PKIService implements CertRequestResourc
         boolean visibleOnly = true;
 
         Enumeration<String> e = ps.getProfileIds();
-        if (e == null) return infos;
+        if (e == null) return createOKResponse(infos);
 
         // store non-null results in a list
         List<ProfileDataInfo> results = new ArrayList<ProfileDataInfo>();
@@ -407,6 +415,6 @@ public class CertRequestService extends PKIService implements CertRequestResourc
             infos.addLink(new Link("next", uri));
         }
 
-        return infos;
+        return createOKResponse(infos);
     }
 }
-- 
1.8.4.2

