>From 4d445804e2d849b7e9bdc4d92dc5a185161d7be2 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Sat, 25 Jan 2014 00:49:41 -0500
Subject: [PATCH 182/187] Add new POST endpoint for creating requests

---
 .../netscape/certsrv/key/KeyArchivalRequest.java   |  6 +-
 .../netscape/certsrv/key/KeyRecoveryRequest.java   |  6 +-
 .../certsrv/key/{Request.java => KeyRequest.java}  | 15 +++-
 .../netscape/certsrv/key/KeyRequestResource.java   | 12 +++
 .../certsrv/key/SymKeyGenerationRequest.java       | 86 ++++++++++++++++++++++
 .../src/com/netscape/certsrv/kra/KRAClient.java    |  9 ++-
 .../cms/servlet/request/KeyRequestService.java     | 28 +++++++
 7 files changed, 151 insertions(+), 11 deletions(-)
 rename base/common/src/com/netscape/certsrv/key/{Request.java => KeyRequest.java} (92%)
 create mode 100644 base/common/src/com/netscape/certsrv/key/SymKeyGenerationRequest.java

diff --git a/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java b/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java
index 9343d0afea48e6c2ea9b661c3f8afb4153788b6e..9a8d29131dbdd9c4e1e12e7d04f23ba04cbb88ee 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java
@@ -32,7 +32,7 @@ import javax.xml.bind.annotation.XmlRootElement;
  */
 @XmlRootElement(name="KeyArchivalRequest")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class KeyArchivalRequest extends Request {
+public class KeyArchivalRequest extends KeyRequest {
 
     private static final String CLIENT_ID = "clientID";
     private static final String DATA_TYPE = "dataType";
@@ -92,7 +92,7 @@ public class KeyArchivalRequest extends Request {
 
     public String toString() {
         try {
-            return Request.marshal(this, KeyArchivalRequest.class);
+            return KeyRequest.marshal(this, KeyArchivalRequest.class);
         } catch (Exception e) {
             return super.toString();
         }
@@ -100,7 +100,7 @@ public class KeyArchivalRequest extends Request {
 
     public static KeyArchivalRequest valueOf(String string) throws Exception {
         try {
-            return Request.unmarshal(string, KeyArchivalRequest.class);
+            return KeyRequest.unmarshal(string, KeyArchivalRequest.class);
         } catch (Exception e) {
             return null;
         }
diff --git a/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java b/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java
index f2d259bf87795bf63f418d8f275944e4be01af79..74f4bcd44f1c7c754ce3b057a17dbfe8772c787f 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java
@@ -35,7 +35,7 @@ import com.netscape.certsrv.request.RequestId;
  */
 @XmlRootElement(name="KeyRecoveryRequest")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class KeyRecoveryRequest extends Request {
+public class KeyRecoveryRequest extends KeyRequest {
 
     private static final String KEY_ID = "keyId";
     private static final String REQUEST_ID = "requestId";
@@ -167,7 +167,7 @@ public class KeyRecoveryRequest extends Request {
 
     public static KeyRecoveryRequest valueOf(String string) throws Exception {
         try {
-            return Request.unmarshal(string, KeyRecoveryRequest.class);
+            return KeyRequest.unmarshal(string, KeyRecoveryRequest.class);
         } catch (Exception e) {
             return null;
         }
@@ -175,7 +175,7 @@ public class KeyRecoveryRequest extends Request {
 
     public String toString() {
         try {
-            return Request.marshal(this, KeyRecoveryRequest.class);
+            return KeyRequest.marshal(this, KeyRecoveryRequest.class);
         } catch (Exception e) {
             return super.toString();
         }
diff --git a/base/common/src/com/netscape/certsrv/key/Request.java b/base/common/src/com/netscape/certsrv/key/KeyRequest.java
similarity index 92%
rename from base/common/src/com/netscape/certsrv/key/Request.java
rename to base/common/src/com/netscape/certsrv/key/KeyRequest.java
index f65809b025c7da1f0591c04707378c2014add306..364136877e7d4a0188e863c625d50f76ffc3d63c 100644
--- a/base/common/src/com/netscape/certsrv/key/Request.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyRequest.java
@@ -8,6 +8,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.core.MultivaluedMap;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
@@ -23,12 +24,22 @@ import org.jboss.resteasy.plugins.providers.atom.Link;
 /**
  * @author Ade Lee
  */
-public class Request {
+public class KeyRequest {
 
     Map<String, String> properties = new LinkedHashMap<String, String>();
     Link link;
     String requestType;
 
+    public KeyRequest() {
+        // required for jax-b
+    }
+
+    public KeyRequest(MultivaluedMap<String, String> form) {
+        for (Map.Entry<String, List<String>> entry: form.entrySet()) {
+            properties.put(entry.getKey(), entry.getValue().get(0));
+        }
+    }
+
     @XmlElement(name = "RequestType")
     public String getRequestType() {
         return requestType;
@@ -128,7 +139,7 @@ public class Request {
             return false;
         if (getClass() != obj.getClass())
             return false;
-        Request other = (Request) obj;
+        KeyRequest other = (KeyRequest) obj;
         if (link == null) {
             if (other.link != null)
                 return false;
diff --git a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
index 5d576c05fbc81f19121e0f77808fd6be97052503..412908fc9da85f87e362acf483cfa56854f225e6 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
@@ -45,6 +45,18 @@ public interface KeyRequestResource {
                                             @QueryParam("maxResults") Integer maxResults,
                                             @QueryParam("maxTime") Integer maxTime);
 
+    // Archiving - used to test integration with a browser
+    @POST
+    @ClientResponseType(entityType=KeyRequestInfo.class)
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED})
+    public Response createRequest(MultivaluedMap<String, String> form);
+
+    @POST
+    @ClientResponseType(entityType=KeyRequestInfo.class)
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public Response createRequest(KeyRequest data);
 
     /**
      * Used to retrieve key request info for a specific request
diff --git a/base/common/src/com/netscape/certsrv/key/SymKeyGenerationRequest.java b/base/common/src/com/netscape/certsrv/key/SymKeyGenerationRequest.java
new file mode 100644
index 0000000000000000000000000000000000000000..1abaaab0004bff3eb6db7aa620827a25e806c683
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/key/SymKeyGenerationRequest.java
@@ -0,0 +1,86 @@
+package com.netscape.certsrv.key;
+
+import javax.ws.rs.core.MultivaluedMap;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * @author alee
+ *
+ */
+@XmlRootElement(name="SymKeyGenerationRequest")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class SymKeyGenerationRequest extends KeyRequest {
+
+    private static final String CLIENT_ID = "clientID";
+    private static final String DATA_TYPE = "dataType";
+
+    public SymKeyGenerationRequest() {
+        // required for JAXB (defaults)
+    }
+
+    public SymKeyGenerationRequest(MultivaluedMap<String, String> form) {
+        this.properties.put(CLIENT_ID, form.getFirst(CLIENT_ID));
+        this.properties.put(DATA_TYPE, form.getFirst(DATA_TYPE));
+    }
+
+    /**
+     * @return the clientId
+     */
+    public String getClientId() {
+        return properties.get(CLIENT_ID);
+    }
+
+    /**
+     * @param clientId the clientId to set
+     */
+    public void setClientId(String clientId) {
+        this.properties.put(CLIENT_ID, clientId);
+    }
+
+    /**
+     * @return the dataType
+     */
+    public String getDataType() {
+        return this.properties.get(DATA_TYPE);
+    }
+
+    /**
+     * @param dataType the dataType to set
+     */
+    public void setDataType(String dataType) {
+        this.properties.put(DATA_TYPE, dataType);
+    }
+
+    public String toString() {
+        try {
+            return KeyRequest.marshal(this, SymKeyGenerationRequest.class);
+        } catch (Exception e) {
+            return super.toString();
+        }
+    }
+
+    public static SymKeyGenerationRequest valueOf(String string) throws Exception {
+        try {
+            return KeyRequest.unmarshal(string, SymKeyGenerationRequest.class);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        SymKeyGenerationRequest before = new SymKeyGenerationRequest();
+        before.setClientId("vek 12345");
+        before.setDataType(KeyRequestResource.SYMMETRIC_KEY_TYPE);
+        before.setRequestType(KeyRequestResource.KEY_GENERATION_REQUEST);
+
+        String string = before.toString();
+        System.out.println(string);
+
+        SymKeyGenerationRequest after = SymKeyGenerationRequest.valueOf(string);
+        System.out.println(before.equals(after));
+    }
+
+}
diff --git a/base/common/src/com/netscape/certsrv/kra/KRAClient.java b/base/common/src/com/netscape/certsrv/kra/KRAClient.java
index 81d600c3395987772e865a79bb71d18183b21a5e..48e918a581c62eeb57bef48050407f3baf5049ac 100644
--- a/base/common/src/com/netscape/certsrv/kra/KRAClient.java
+++ b/base/common/src/com/netscape/certsrv/kra/KRAClient.java
@@ -74,10 +74,11 @@ public class KRAClient extends SubsystemClient {
         data.setWrappedPrivateData(req1);
         data.setClientId(clientId);
         data.setDataType(dataType);
+        data.setRequestType(KeyRequestResource.ARCHIVAL_REQUEST);
 
         @SuppressWarnings("unchecked")
         ClientResponse<KeyRequestInfo> response = (ClientResponse<KeyRequestInfo>)
-                keyRequestClient.archiveKey(data);
+                keyRequestClient.createRequest(data);
         return client.getEntity(response);
     }
 
@@ -110,10 +111,11 @@ public class KRAClient extends SubsystemClient {
         if (nonceData != null) {
             data.setNonceData(Utils.base64encode(nonceData));
         }
+        data.setRequestType(KeyRequestResource.RECOVERY_REQUEST);
 
         @SuppressWarnings("unchecked")
         ClientResponse<KeyRequestInfo> response = (ClientResponse<KeyRequestInfo>)
-                keyRequestClient.recoverKey(data);
+                keyRequestClient.createRequest(data);
         return client.getEntity(response);
     }
 
@@ -150,10 +152,11 @@ public class KRAClient extends SubsystemClient {
         KeyRecoveryRequest data = new KeyRecoveryRequest();
         data.setKeyId(new KeyId(keyId));
         data.setCertificate(b64Certificate);
+        data.setRequestType(KeyRequestResource.RECOVERY_REQUEST);
 
         @SuppressWarnings("unchecked")
         ClientResponse<KeyRequestInfo> response = (ClientResponse<KeyRequestInfo>)
-                keyRequestClient.recoverKey(data);
+                keyRequestClient.createRequest(data);
         return client.getEntity(response).getRequestId();
     }
 
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 ada11be7cabf5e3d5efd65b9a2c08dbcf40c813d..3ea92406af4f7c9fcaaf794fd909f31766572210 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
@@ -41,9 +41,11 @@ import com.netscape.certsrv.base.PKIException;
 import com.netscape.certsrv.dbs.keydb.KeyId;
 import com.netscape.certsrv.key.KeyArchivalRequest;
 import com.netscape.certsrv.key.KeyRecoveryRequest;
+import com.netscape.certsrv.key.KeyRequest;
 import com.netscape.certsrv.key.KeyRequestInfo;
 import com.netscape.certsrv.key.KeyRequestInfos;
 import com.netscape.certsrv.key.KeyRequestResource;
+import com.netscape.certsrv.key.SymKeyGenerationRequest;
 import com.netscape.certsrv.kra.IKeyRecoveryAuthority;
 import com.netscape.certsrv.kra.IKeyService;
 import com.netscape.certsrv.logging.ILogger;
@@ -377,4 +379,30 @@ public class KeyRequestService extends PKIService implements KeyRequestResource
                 clientId);
         auditor.log(msg);
     }
+
+    @Override
+    public Response createRequest(MultivaluedMap<String, String> form) {
+        KeyRequest data = new KeyRequest(form);
+        return createRequest(data);
+    }
+
+    @Override
+    public Response createRequest(KeyRequest data) {
+        String requestType = data.getRequestType();
+        switch(requestType) {
+        case KeyRequestResource.ARCHIVAL_REQUEST:
+            return archiveKey((KeyArchivalRequest) data);
+        case KeyRequestResource.RECOVERY_REQUEST:
+            return recoverKey((KeyRecoveryRequest) data);
+        case KeyRequestResource.KEY_GENERATION_REQUEST:
+            return generateSymKey((SymKeyGenerationRequest) data);
+        default:
+            throw new BadRequestException("Invalid request");
+        }
+    }
+
+    public Response generateSymKey(SymKeyGenerationRequest data) {
+        // TODO Auto-generated method stub
+        return null;
+    }
 }
-- 
1.8.4.2

