>From 1a0645a9ba55a0c48c9624eedd1e146a5a41cb8a Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Tue, 5 Nov 2013 15:42:35 -0500
Subject: [PATCH] Fix KeyRequest and Key Resources to return correct exit codes

Also added some missing checks, and some missing options in the Key Request CLI
---
 .../netscape/certsrv/key/KeyRequestResource.java   | 15 +++++--
 .../src/com/netscape/certsrv/kra/KRAClient.java    | 12 ++++--
 .../netscape/cmstools/key/KeyRequestFindCLI.java   |  8 ++++
 .../com/netscape/cms/servlet/key/KeyService.java   |  9 ++++-
 .../cms/servlet/request/KeyRequestService.java     | 46 +++++++++++++++++-----
 5 files changed, 72 insertions(+), 18 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
index 6fc12d5eefffba4c515ef444c386b6e0e6359247..b7bea70571ecbe1551cea0f423b92b03d7230a16 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
@@ -9,6 +9,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;
@@ -48,28 +51,32 @@ public interface KeyRequestResource {
     // Archiving - used to test integration with a browser
     @POST
     @Path("archive")
+    @ClientResponseType(entityType=KeyRequestInfo.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_FORM_URLENCODED})
-    public KeyRequestInfo archiveKey(MultivaluedMap<String, String> form);
+    public Response archiveKey(MultivaluedMap<String, String> form);
 
     @POST
     @Path("archive")
+    @ClientResponseType(entityType=KeyRequestInfo.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public KeyRequestInfo archiveKey(KeyArchivalRequest data);
+    public Response archiveKey(KeyArchivalRequest data);
 
     //Recovery - used to test integration with a browser
     @POST
     @Path("recover")
+    @ClientResponseType(entityType=KeyRequestInfo.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_FORM_URLENCODED})
-    public KeyRequestInfo recoverKey(MultivaluedMap<String, String> form);
+    public Response recoverKey(MultivaluedMap<String, String> form);
 
     @POST
     @Path("recover")
+    @ClientResponseType(entityType=KeyRequestInfo.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public KeyRequestInfo recoverKey(KeyRecoveryRequest data);
+    public Response recoverKey(KeyRecoveryRequest data);
 
     @POST
     @Path("{id}/approve")
diff --git a/base/common/src/com/netscape/certsrv/kra/KRAClient.java b/base/common/src/com/netscape/certsrv/kra/KRAClient.java
index 658aba50bf03d93aab2c6bbe7783a4d04b79350e..73ad56454fd2ba16b04cab576037f3c8cd56dd63 100644
--- a/base/common/src/com/netscape/certsrv/kra/KRAClient.java
+++ b/base/common/src/com/netscape/certsrv/kra/KRAClient.java
@@ -75,8 +75,10 @@ public class KRAClient extends SubsystemClient {
         data.setClientId(clientId);
         data.setDataType(dataType);
 
-        KeyRequestInfo info = keyRequestClient.archiveKey(data);
-        return info;
+        @SuppressWarnings("unchecked")
+        ClientResponse<KeyRequestInfo> response = (ClientResponse<KeyRequestInfo>)
+                keyRequestClient.archiveKey(data);
+        return client.getEntity(response);
     }
 
     public KeyDataInfo getKeyData(String clientId, String status) {
@@ -109,8 +111,10 @@ public class KRAClient extends SubsystemClient {
             data.setNonceData(Utils.base64encode(nonceData));
         }
 
-        KeyRequestInfo info = keyRequestClient.recoverKey(data);
-        return info;
+        @SuppressWarnings("unchecked")
+        ClientResponse<KeyRequestInfo> response = (ClientResponse<KeyRequestInfo>)
+                keyRequestClient.recoverKey(data);
+        return client.getEntity(response);
     }
 
     public void approveRecovery(RequestId recoveryId) {
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
index 14770efdb59db75c329e327fb5a5ce39fd14aac9..d74a2c2f1c4bfe5c1473ee54b8d2dffa7958af66 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
@@ -68,6 +68,14 @@ public class KeyRequestFindCLI extends CLI {
         option.setArgName("max time");
         options.addOption(option);
 
+        option = new Option(null, "start", true, "Page to start");
+        option.setArgName("starting page");
+        options.addOption(option);
+
+        option = new Option(null, "pageSize", true, "Page size");
+        option.setArgName("page size");
+        options.addOption(option);
+
         CommandLine cmd = null;
 
         try {
diff --git a/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java b/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java
index f642417e22e3635d0e7981e229084fdd47011511..90bc61d2d1af16666a605f84c718698cc222dd58 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java
@@ -95,7 +95,12 @@ public class KeyService extends PKIService implements KeyResource {
      * @param data
      * @return
      */
+    @Override
     public KeyData retrieveKey(KeyRecoveryRequest data) {
+        if (data == null) {
+            CMS.debug("retrieveKey: data is null");
+            throw new BadRequestException("Cannot retrieve key. Invalid request");
+        }
         // auth and authz
         KeyId keyId = validateRequest(data);
         RequestId requestID = data.getRequestId();
@@ -117,6 +122,7 @@ public class KeyService extends PKIService implements KeyResource {
     }
 
     // retrieval - used to test integration with a browser
+    @Override
     public KeyData retrieveKey(MultivaluedMap<String, String> form) {
         KeyRecoveryRequest data = new KeyRecoveryRequest(form);
         return retrieveKey(data);
@@ -281,6 +287,7 @@ public class KeyService extends PKIService implements KeyResource {
     /**
      * Used to generate list of key infos based on the search parameters
      */
+    @Override
     public KeyDataInfos listKeys(String clientID, String status, Integer maxResults, Integer maxTime) {
         // auth and authz
 
@@ -297,7 +304,7 @@ public class KeyService extends PKIService implements KeyResource {
 
             e = repo.searchKeys(filter, maxResults, maxTime);
             if (e == null) {
-                throw new EBaseException("search results are null");
+                return infos;
             }
 
             while (e.hasMoreElements()) {
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 8db16b51f73cd159757afb842403bbf139880acf..06b03176d85bd0f187a13e08a0c5e8eb8b7325ae 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
@@ -18,11 +18,16 @@
 
 package com.netscape.cms.servlet.request;
 
+import java.net.URI;
+import java.net.URISyntaxException;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 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 com.netscape.certsrv.apps.CMS;
@@ -77,7 +82,12 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
     /**
      * Used to retrieve key request info for a specific request
      */
+    @Override
     public KeyRequestInfo getRequestInfo(RequestId id) {
+        if (id == null) {
+            CMS.debug("getRequestInfo: is is null");
+            throw new BadRequestException("Unable to get Request: invalid ID");
+        }
         // auth and authz
         KeyRequestDAO dao = new KeyRequestDAO();
         KeyRequestInfo info;
@@ -96,12 +106,14 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
     }
 
     // Archiving - used to test integration with a browser
-    public KeyRequestInfo archiveKey(MultivaluedMap<String, String> form) {
+    @Override
+    public Response archiveKey(MultivaluedMap<String, String> form) {
         KeyArchivalRequest data = new KeyArchivalRequest(form);
         return archiveKey(data);
     }
 
-    public KeyRequestInfo archiveKey(KeyArchivalRequest data) {
+    @Override
+    public Response archiveKey(KeyArchivalRequest data) {
         // auth and authz
         // Catch this before internal server processing has to deal with it
 
@@ -116,21 +128,28 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
         try {
             info = dao.submitRequest(data, uriInfo);
             auditArchivalRequestMade(info.getRequestId(), ILogger.SUCCESS, data.getClientId());
-        } catch (EBaseException e) {
+
+            return Response
+                    .created(new URI(info.getRequestURL()))
+                    .entity(info)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+        } catch (EBaseException | URISyntaxException e) {
             e.printStackTrace();
             auditArchivalRequestMade(null, ILogger.FAILURE, data.getClientId());
             throw new PKIException(e.toString());
         }
-        return info;
     }
 
     //Recovery - used to test integration with a browser
-    public KeyRequestInfo recoverKey(MultivaluedMap<String, String> form) {
+    @Override
+    public Response recoverKey(MultivaluedMap<String, String> form) {
         KeyRecoveryRequest data = new KeyRecoveryRequest(form);
         return recoverKey(data);
     }
 
-    public KeyRequestInfo recoverKey(KeyRecoveryRequest data) {
+    @Override
+    public Response recoverKey(KeyRecoveryRequest data) {
         // auth and authz
 
         //Check for entirely illegal data combination here
@@ -150,14 +169,20 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
         try {
             info = dao.submitRequest(data, uriInfo);
             auditRecoveryRequestMade(info.getRequestId(), ILogger.SUCCESS, data.getKeyId());
-        } catch (EBaseException e) {
+
+            return Response
+                    .created(new URI(info.getRequestURL()))
+                    .entity(info)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+        } catch (EBaseException | URISyntaxException e) {
             e.printStackTrace();
             auditRecoveryRequestMade(null, ILogger.FAILURE, data.getKeyId());
             throw new PKIException(e.toString());
         }
-        return info;
     }
 
+    @Override
     public void approveRequest(RequestId id) {
         if (id == null) {
             throw new BadRequestException("Invalid request id.");
@@ -174,6 +199,7 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
         }
     }
 
+    @Override
     public void rejectRequest(RequestId id) {
         if (id == null) {
             throw new BadRequestException("Invalid request id.");
@@ -190,9 +216,10 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
         }
     }
 
+    @Override
     public void cancelRequest(RequestId id) {
         if (id == null) {
-            throw new BadRequestException("Request id is null.");
+            throw new BadRequestException("Invalid request id.");
         }
         // auth and authz
         KeyRequestDAO dao = new KeyRequestDAO();
@@ -209,6 +236,7 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
     /**
      * Used to generate list of key requests based on the search parameters
      */
+    @Override
     public KeyRequestInfos listRequests(String requestState, String requestType, String clientID,
             RequestId start, Integer pageSize, Integer maxResults, Integer maxTime) {
         // auth and authz
-- 
1.8.3.1

