From 292722d95d12300ca711e86a0ec1ea1b1a69825e Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 11 Feb 2014 16:23:59 -0500
Subject: [PATCH] Updated REST interface for certificates.

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

Ticket #554
---
 .../src/com/netscape/certsrv/cert/CertClient.java  | 23 +++++++++----
 .../com/netscape/certsrv/cert/CertResource.java    | 24 +++++++++----
 .../com/netscape/cms/servlet/cert/CertService.java | 40 ++++++++++++----------
 3 files changed, 55 insertions(+), 32 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/cert/CertClient.java b/base/common/src/com/netscape/certsrv/cert/CertClient.java
index ab7ad9a55a37ef93c7bdede2827babdbd8f27aa1..c52372c54bbe24809d41ef5102fb259bfae8842a 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertClient.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertClient.java
@@ -19,6 +19,8 @@ package com.netscape.certsrv.cert;
 
 import java.net.URISyntaxException;
 
+import javax.ws.rs.core.Response;
+
 import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.dbs.certdb.CertId;
@@ -44,31 +46,38 @@ public class CertClient extends Client {
     }
 
     public CertData getCert(CertId id) {
-        return certClient.getCert(id);
+        Response response = certClient.getCert(id);
+        return client.getEntity(response, CertData.class);
     }
 
     public CertData reviewCert(CertId id) {
-        return certClient.reviewCert(id);
+        Response response = certClient.reviewCert(id);
+        return client.getEntity(response, CertData.class);
     }
 
     public CertDataInfos listCerts(String status, Integer maxResults, Integer maxTime, Integer start, Integer size) {
-        return certClient.listCerts(status, maxResults, maxTime, start, size);
+        Response response = certClient.listCerts(status, maxResults, maxTime, start, size);
+        return client.getEntity(response, CertDataInfos.class);
     }
 
     public CertDataInfos findCerts(CertSearchRequest data, Integer start, Integer size) {
-        return certClient.searchCerts(data, start, size);
+        Response response = certClient.searchCerts(data, start, size);
+        return client.getEntity(response, CertDataInfos.class);
     }
 
     public CertRequestInfo revokeCert(CertId id, CertRevokeRequest request) {
-        return certClient.revokeCert(id, request);
+        Response response = certClient.revokeCert(id, request);
+        return client.getEntity(response, CertRequestInfo.class);
     }
 
     public CertRequestInfo revokeCACert(CertId id, CertRevokeRequest request) {
-        return certClient.revokeCACert(id, request);
+        Response response = certClient.revokeCACert(id, request);
+        return client.getEntity(response, CertRequestInfo.class);
     }
 
     public CertRequestInfo unrevokeCert(CertId id, CertUnrevokeRequest request) {
-        return certClient.unrevokeCert(id, request);
+        Response response = certClient.unrevokeCert(id, request);
+        return client.getEntity(response, CertRequestInfo.class);
     }
 
     public CertRequestInfos enrollRequest(CertEnrollmentRequest data) {
diff --git a/base/common/src/com/netscape/certsrv/cert/CertResource.java b/base/common/src/com/netscape/certsrv/cert/CertResource.java
index 9f527e130846d01f05b023c053cf0842e171583a..23e8987557093f269bad997edf7559e92fabcaf4 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertResource.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertResource.java
@@ -8,6 +8,9 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.annotations.ClientResponseType;
 
 import com.netscape.certsrv.acls.ACLMapping;
 import com.netscape.certsrv.authentication.AuthMethodMapping;
@@ -18,8 +21,9 @@ public interface CertResource {
 
     @GET
     @Path("certs")
+    @ClientResponseType(entityType=CertDataInfos.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public CertDataInfos listCerts(
+    public Response listCerts(
             @QueryParam("status") String status,
             @QueryParam("maxResults") Integer maxResults,
             @QueryParam("maxTime") Integer maxTime,
@@ -28,46 +32,52 @@ public interface CertResource {
 
     @POST
     @Path("certs/search")
+    @ClientResponseType(entityType=CertDataInfos.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public CertDataInfos searchCerts(
+    public Response searchCerts(
             CertSearchRequest data,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
 
     @GET
     @Path("certs/{id}")
+    @ClientResponseType(entityType=CertData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public CertData getCert(@PathParam("id") CertId id);
+    public Response getCert(@PathParam("id") CertId id);
 
     @GET
     @Path("agent/certs/{id}")
+    @ClientResponseType(entityType=CertData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certs")
     @AuthMethodMapping("certs")
-    public CertData reviewCert(@PathParam("id") CertId id);
+    public Response reviewCert(@PathParam("id") CertId id);
 
     @POST
     @Path("agent/certs/{id}/revoke-ca")
+    @ClientResponseType(entityType=CertRequestInfo.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certs")
     @AuthMethodMapping("certs")
-    public CertRequestInfo revokeCACert(@PathParam("id") CertId id, CertRevokeRequest request);
+    public Response revokeCACert(@PathParam("id") CertId id, CertRevokeRequest request);
 
     @POST
     @Path("agent/certs/{id}/revoke")
+    @ClientResponseType(entityType=CertRequestInfo.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certs")
     @AuthMethodMapping("certs")
-    public CertRequestInfo revokeCert(@PathParam("id") CertId id, CertRevokeRequest request);
+    public Response revokeCert(@PathParam("id") CertId id, CertRevokeRequest request);
 
     @POST
     @Path("agent/certs/{id}/unrevoke")
+    @ClientResponseType(entityType=CertRequestInfo.class)
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @ACLMapping("certs")
     @AuthMethodMapping("certs")
-    public CertRequestInfo unrevokeCert(@PathParam("id") CertId id, CertUnrevokeRequest request);
+    public Response unrevokeCert(@PathParam("id") CertId id, CertUnrevokeRequest request);
 }
diff --git a/base/server/cms/src/com/netscape/cms/servlet/cert/CertService.java b/base/server/cms/src/com/netscape/cms/servlet/cert/CertService.java
index 1b4954822f1d910902405f7187708889cc02d8a9..e8e735e467a520a0f63eaff2d8712980db29bcf6 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/cert/CertService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/cert/CertService.java
@@ -38,6 +38,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Request;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
 import netscape.security.pkcs.ContentInfo;
@@ -62,7 +63,6 @@ import com.netscape.certsrv.cert.CertData;
 import com.netscape.certsrv.cert.CertDataInfo;
 import com.netscape.certsrv.cert.CertDataInfos;
 import com.netscape.certsrv.cert.CertNotFoundException;
-import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertResource;
 import com.netscape.certsrv.cert.CertRetrievalRequest;
 import com.netscape.certsrv.cert.CertRevokeRequest;
@@ -116,16 +116,20 @@ public class CertService extends PKIService implements CertResource {
     }
 
     @Override
-    public CertData getCert(CertId id) {
-        return getCert(id, false);
+    public Response getCert(CertId id) {
+        return createOKResponse(getCertData(id));
     }
 
     @Override
-    public CertData reviewCert(CertId id) {
-        return getCert(id, true);
+    public Response reviewCert(CertId id) {
+        return createOKResponse(getCertData(id, true));
     }
 
-    public CertData getCert(CertId id, boolean generateNonce) {
+    public CertData getCertData(CertId id) {
+        return getCertData(id, false);
+    }
+
+    public CertData getCertData(CertId id, boolean generateNonce) {
         if (id == null) {
             throw new BadRequestException("Unable to get certificate: Invalid id.");
         }
@@ -149,16 +153,16 @@ public class CertService extends PKIService implements CertResource {
     }
 
     @Override
-    public CertRequestInfo revokeCACert(CertId id, CertRevokeRequest request) {
+    public Response revokeCACert(CertId id, CertRevokeRequest request) {
         return revokeCert(id, request, true);
     }
 
     @Override
-    public CertRequestInfo revokeCert(CertId id, CertRevokeRequest request) {
+    public Response revokeCert(CertId id, CertRevokeRequest request) {
         return revokeCert(id, request, false);
     }
 
-    public CertRequestInfo revokeCert(CertId id, CertRevokeRequest request, boolean caCert) {
+    public Response revokeCert(CertId id, CertRevokeRequest request, boolean caCert) {
         if (id == null) {
             CMS.debug("revokeCert: id is null");
             throw new BadRequestException("Unable to revoke cert: invalid id");
@@ -171,7 +175,7 @@ public class CertService extends PKIService implements CertResource {
         // check cert actually exists.  This will throw a CertNotFoundException
         // if the cert does not exist
         @SuppressWarnings("unused")
-        CertData data = getCert(id);
+        CertData data = getCertData(id);
 
         RevocationReason revReason = request.getReason();
         if (revReason == RevocationReason.REMOVE_FROM_CRL) {
@@ -287,7 +291,7 @@ public class CertService extends PKIService implements CertResource {
         try {
             IRequest certRequest = processor.getRequest();
             CertRequestDAO dao = new CertRequestDAO();
-            return dao.getRequest(certRequest.getRequestId(), uriInfo);
+            return createOKResponse(dao.getRequest(certRequest.getRequestId(), uriInfo));
 
         } catch (EBaseException e) {
             throw new PKIException(e.getMessage());
@@ -295,7 +299,7 @@ public class CertService extends PKIService implements CertResource {
     }
 
     @Override
-    public CertRequestInfo unrevokeCert(CertId id, CertUnrevokeRequest request) {
+    public Response unrevokeCert(CertId id, CertUnrevokeRequest request) {
         if (id == null) {
             CMS.debug("unrevokeCert: id is null");
             throw new BadRequestException("Unable to unrevoke cert: invalid id");
@@ -308,7 +312,7 @@ public class CertService extends PKIService implements CertResource {
         // check cert actually exists.  This will throw a CertNotFoundException
         // if the cert does not exist
         @SuppressWarnings("unused")
-        CertData data = getCert(id);
+        CertData data = getCertData(id);
 
         RevocationProcessor processor;
         try {
@@ -358,7 +362,7 @@ public class CertService extends PKIService implements CertResource {
         try {
             IRequest certRequest = processor.getRequest();
             CertRequestDAO dao = new CertRequestDAO();
-            return dao.getRequest(certRequest.getRequestId(), uriInfo);
+            return createOKResponse(dao.getRequest(certRequest.getRequestId(), uriInfo));
 
         } catch (EBaseException e) {
             throw new PKIException(e.getMessage());
@@ -389,7 +393,7 @@ public class CertService extends PKIService implements CertResource {
     }
 
     @Override
-    public CertDataInfos listCerts(String status, Integer maxResults, Integer maxTime, Integer start, Integer size) {
+    public Response listCerts(String status, Integer maxResults, Integer maxTime, Integer start, Integer size) {
 
         maxResults = maxResults == null ? DEFAULT_MAXRESULTS : maxResults;
         maxTime    = maxTime == null ? DEFAULT_MAXTIME : maxTime;
@@ -437,11 +441,11 @@ public class CertService extends PKIService implements CertResource {
             throw new PKIException("Error listing certs in CertService.listCerts!", e);
         }
 
-        return infos;
+        return createOKResponse(infos);
     }
 
     @Override
-    public CertDataInfos searchCerts(CertSearchRequest data, Integer start, Integer size) {
+    public Response searchCerts(CertSearchRequest data, Integer start, Integer size) {
 
         if (data == null) {
             throw new BadRequestException("Search request is null.");
@@ -490,7 +494,7 @@ public class CertService extends PKIService implements CertResource {
             throw new PKIException("Error searching certs in CertService.searchCerts!", e1);
         }
 
-        return infos;
+        return createOKResponse(infos);
     }
 
     public CertData getCert(CertRetrievalRequest data, boolean generateNonce) throws EBaseException, CertificateEncodingException {
-- 
1.8.4.2

