>From 0f5c3ae7c7d94c1baf0eb1bea01b70fc177d0439 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Fri, 24 Jan 2014 16:15:05 -0500
Subject: [PATCH 181/187] Use a generic request object

Refactored KeyRequest API to use requests containing a
generic request object.
---
 .../netscape/certsrv/key/KeyArchivalRequest.java   |  79 +++++-----
 .../netscape/certsrv/key/KeyRecoveryRequest.java   | 108 +++++++------
 .../netscape/certsrv/key/KeyRequestResource.java   |   6 +
 .../src/com/netscape/certsrv/key/Request.java      | 167 +++++++++++++++++++++
 4 files changed, 274 insertions(+), 86 deletions(-)
 create mode 100644 base/common/src/com/netscape/certsrv/key/Request.java

diff --git a/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java b/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java
index 2d2d84c945695da5084b58dd21a3ea8fbbe5930c..9343d0afea48e6c2ea9b661c3f8afb4153788b6e 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java
@@ -24,7 +24,6 @@ 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.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
 /**
@@ -33,91 +32,93 @@ import javax.xml.bind.annotation.XmlRootElement;
  */
 @XmlRootElement(name="KeyArchivalRequest")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class KeyArchivalRequest {
+public class KeyArchivalRequest extends Request {
 
     private static final String CLIENT_ID = "clientID";
-    private static final String TRANS_WRAPPED_SESSION_KEY = "transWrappedSessionKey";
     private static final String DATA_TYPE = "dataType";
     private static final String WRAPPED_PRIVATE_DATA = "wrappedPrivateData";
 
-    @XmlElement
-    protected String clientId;
-
-    @XmlElement
-    protected String transWrappedSessionKey;
-
-    @XmlElement
-    protected String dataType;
-
-    @XmlElement
-    protected String wrappedPrivateData;
-
     public KeyArchivalRequest() {
         // required for JAXB (defaults)
     }
 
     public KeyArchivalRequest(MultivaluedMap<String, String> form) {
-        clientId = form.getFirst(CLIENT_ID);
-        transWrappedSessionKey = form.getFirst(TRANS_WRAPPED_SESSION_KEY);
-        dataType = form.getFirst(DATA_TYPE);
-        wrappedPrivateData = form.getFirst(WRAPPED_PRIVATE_DATA);
+        this.properties.put(CLIENT_ID, form.getFirst(CLIENT_ID));
+        this.properties.put(DATA_TYPE, form.getFirst(DATA_TYPE));
+        this.properties.put(WRAPPED_PRIVATE_DATA, form.getFirst(WRAPPED_PRIVATE_DATA));
     }
 
     /**
      * @return the clientId
      */
     public String getClientId() {
-        return clientId;
+        return properties.get(CLIENT_ID);
     }
 
     /**
      * @param clientId the clientId to set
      */
     public void setClientId(String clientId) {
-        this.clientId = clientId;
-    }
-
-    /**
-     * @return the transWrappedSessionKey
-     */
-    public String getTransWrappedSessionKey() {
-        return transWrappedSessionKey;
-    }
-
-    /**
-     * @param transWrappedSessionKey the transWrappedSessionKey to set
-     */
-    public void setTransWrappedSessionKey(String transWrappedSessionKey) {
-        this.transWrappedSessionKey = transWrappedSessionKey;
+        this.properties.put(CLIENT_ID, clientId);
     }
 
     /**
      * @return the dataType
      */
     public String getDataType() {
-        return dataType;
+        return this.properties.get(DATA_TYPE);
     }
 
     /**
      * @param dataType the dataType to set
      */
     public void setDataType(String dataType) {
-        this.dataType = dataType;
+        this.properties.put(DATA_TYPE, dataType);
     }
 
     /**
      * @return the wrappedPrivateData
      */
     public String getWrappedPrivateData() {
-        return wrappedPrivateData;
+        return this.properties.get(WRAPPED_PRIVATE_DATA);
     }
 
     /**
      * @param wrappedPrivateData the wrappedPrivateData to set
      */
     public void setWrappedPrivateData(String wrappedPrivateData) {
-        this.wrappedPrivateData = wrappedPrivateData;
+        this.properties.put(WRAPPED_PRIVATE_DATA, wrappedPrivateData);
     }
 
+    public String toString() {
+        try {
+            return Request.marshal(this, KeyArchivalRequest.class);
+        } catch (Exception e) {
+            return super.toString();
+        }
+    }
+
+    public static KeyArchivalRequest valueOf(String string) throws Exception {
+        try {
+            return Request.unmarshal(string, KeyArchivalRequest.class);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        KeyArchivalRequest before = new KeyArchivalRequest();
+        before.setClientId("vek 12345");
+        before.setDataType(KeyRequestResource.SYMMETRIC_KEY_TYPE);
+        before.setRequestType(KeyRequestResource.ARCHIVAL_REQUEST);
+        before.setWrappedPrivateData("XXXXABCDEFXXX");
+
+        String string = before.toString();
+        System.out.println(string);
+
+        KeyArchivalRequest after = KeyArchivalRequest.valueOf(string);
+        System.out.println(before.equals(after));
+    }
 
 }
diff --git a/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java b/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java
index aa69d83362262c15d036b4e4bb5d7a4cb321384b..f2d259bf87795bf63f418d8f275944e4be01af79 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyRecoveryRequest.java
@@ -24,14 +24,10 @@ 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.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
-import com.netscape.certsrv.dbs.keydb.KeyIdAdapter;
 import com.netscape.certsrv.request.RequestId;
-import com.netscape.certsrv.request.RequestIdAdapter;
 
 /**
  * @author alee
@@ -39,36 +35,15 @@ import com.netscape.certsrv.request.RequestIdAdapter;
  */
 @XmlRootElement(name="KeyRecoveryRequest")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class KeyRecoveryRequest {
+public class KeyRecoveryRequest extends Request {
 
     private static final String KEY_ID = "keyId";
     private static final String REQUEST_ID = "requestId";
     private static final String TRANS_WRAPPED_SESSION_KEY = "transWrappedSessionKey";
     private static final String SESSION_WRAPPED_PASSPHRASE = "sessionWrappedPassphrase";
     private static final String NONCE_DATA = "nonceData";
-
-    @XmlElement
-    @XmlJavaTypeAdapter(KeyIdAdapter.class)
-    protected KeyId keyId;
-
-    @XmlElement
-    @XmlJavaTypeAdapter(RequestIdAdapter.class)
-    protected RequestId requestId;
-
-    @XmlElement
-    protected String transWrappedSessionKey;
-
-    @XmlElement
-    protected String sessionWrappedPassphrase;
-
-    @XmlElement
-    protected String nonceData;
-
-    @XmlElement
-    protected String certificate;
-
-    @XmlElement
-    protected String passphrase;
+    private static final String CERTIFICATE = "certificate";
+    private static final String PASSPHRASE = "passphrase";
 
     public KeyRecoveryRequest() {
         // required for JAXB (defaults)
@@ -76,70 +51,73 @@ public class KeyRecoveryRequest {
 
     public KeyRecoveryRequest(MultivaluedMap<String, String> form) {
         if (form.containsKey(KEY_ID)) {
-            keyId = new KeyId(form.getFirst(KEY_ID));
+            this.properties.put(KEY_ID, form.getFirst(KEY_ID));
         }
         if (form.containsKey(REQUEST_ID)) {
-            requestId = new RequestId(form.getFirst(REQUEST_ID));
+            this.properties.put(REQUEST_ID, form.getFirst(REQUEST_ID));
         }
-        transWrappedSessionKey = form.getFirst(TRANS_WRAPPED_SESSION_KEY);
-        sessionWrappedPassphrase = form.getFirst(SESSION_WRAPPED_PASSPHRASE);
-        nonceData = form.getFirst(NONCE_DATA);
+        this.properties.put(TRANS_WRAPPED_SESSION_KEY, form.getFirst(TRANS_WRAPPED_SESSION_KEY));
+        this.properties.put(SESSION_WRAPPED_PASSPHRASE, form.getFirst(SESSION_WRAPPED_PASSPHRASE));
+        this.properties.put(NONCE_DATA, form.getFirst(NONCE_DATA));
+        this.properties.put(CERTIFICATE, form.getFirst(CERTIFICATE));
+        this.properties.put(PASSPHRASE, form.getFirst(PASSPHRASE));
+
     }
 
     /**
      * @return the keyId
      */
     public KeyId getKeyId() {
-        return keyId;
+        return new KeyId(this.properties.get(KEY_ID));
     }
 
     /**
      * @param keyId the keyId to set
      */
     public void setKeyId(KeyId keyId) {
-        this.keyId = keyId;
+        this.properties.put(KEY_ID, keyId.toString());
     }
 
     /**
      * @return the requestId
      */
     public RequestId getRequestId() {
-        return requestId;
+        return new RequestId(this.properties.get(REQUEST_ID));
     }
 
     /**
      * @param requestId the requestId to set
      */
     public void setRequestId(RequestId requestId) {
-        this.requestId = requestId;
+        this.properties.put(REQUEST_ID, requestId.toString());
     }
 
     /**
      * @return the transWrappedSessionKey
      */
     public String getTransWrappedSessionKey() {
-        return transWrappedSessionKey;
+        return this.properties.get(TRANS_WRAPPED_SESSION_KEY);
     }
 
     /**
      * @param transWrappedSessionKey the transWrappedSessionKey to set
      */
     public void setTransWrappedSessionKey(String transWrappedSessionKey) {
-        this.transWrappedSessionKey = transWrappedSessionKey;
+        this.properties.put(TRANS_WRAPPED_SESSION_KEY, transWrappedSessionKey);
     }
 
     /**
      * @return the sessionWrappedPassphrase
      */
     public String getSessionWrappedPassphrase() {
-        return sessionWrappedPassphrase;
+        return this.properties.get(SESSION_WRAPPED_PASSPHRASE);
     }
 
     /**
      * @param sessionWrappedPassphrase the sessionWrappedPassphrase to set
      */
     public void setSessionWrappedPassphrase(String sessionWrappedPassphrase) {
-        this.sessionWrappedPassphrase = sessionWrappedPassphrase;
+        this.properties.put(SESSION_WRAPPED_PASSPHRASE, sessionWrappedPassphrase);
     }
 
     /**
@@ -147,7 +125,7 @@ public class KeyRecoveryRequest {
      */
 
     public String getNonceData() {
-        return nonceData;
+        return this.properties.get(NONCE_DATA);
     }
 
     /**
@@ -155,34 +133,70 @@ public class KeyRecoveryRequest {
      */
 
     public void setNonceData(String nonceData) {
-        this.nonceData = nonceData;
+        this.properties.put(NONCE_DATA, nonceData);
     }
 
     /**
      * @return the certificate
      */
     public String getCertificate() {
-        return certificate;
+        return this.properties.get(CERTIFICATE);
     }
 
     /**
      * @param certificate the certificate to set
      */
     public void setCertificate(String certificate) {
-        this.certificate = certificate;
+        this.properties.put(CERTIFICATE, certificate);
     }
 
     /**
      * @return the passphrase
      */
     public String getPassphrase() {
-        return passphrase;
+        return this.properties.get(PASSPHRASE);
     }
 
     /**
      * @param passphrase the passphrase to set
      */
     public void setPassphrase(String passphrase) {
-        this.passphrase = passphrase;
+        this.properties.put(PASSPHRASE, passphrase);
+    }
+
+
+    public static KeyRecoveryRequest valueOf(String string) throws Exception {
+        try {
+            return Request.unmarshal(string, KeyRecoveryRequest.class);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public String toString() {
+        try {
+            return Request.marshal(this, KeyRecoveryRequest.class);
+        } catch (Exception e) {
+            return super.toString();
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        KeyRecoveryRequest before = new KeyRecoveryRequest();
+        before.setKeyId(new KeyId("0x123456"));
+        before.setNonceData("nonce-XXX12345");
+        before.setPassphrase("password");
+        before.setRequestId(new RequestId("0x123F"));
+        before.setCertificate("123ABCAAAA");
+        before.setSessionWrappedPassphrase("XXXXXXXX1234");
+        before.setTransWrappedSessionKey("124355AAA");
+        before.setRequestType(KeyRequestResource.RECOVERY_REQUEST);
+
+        String string = before.toString();
+        System.out.println(string);
+
+        KeyRecoveryRequest after = KeyRecoveryRequest.valueOf(string);
+        System.out.println(before.equals(after));
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
index 9256ac46133382a035fd60d212750e851850b770..5d576c05fbc81f19121e0f77808fd6be97052503 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyRequestResource.java
@@ -22,10 +22,16 @@ import com.netscape.certsrv.request.RequestId;
 @AuthMethodMapping("keyrequests")
 public interface KeyRequestResource {
 
+    /* Data types */
     public final String SYMMETRIC_KEY_TYPE = "symmetricKey";
     public final String PASS_PHRASE_TYPE = "passPhrase";
     public final String ASYMMETRIC_KEY_TYPE = "asymmetricKey";
 
+    /* Request types */
+    public final String ARCHIVAL_REQUEST = "archival";
+    public final String KEY_GENERATION_REQUEST = "keygen";
+    public final String RECOVERY_REQUEST = "recovery";
+
     /**
      * Used to generate list of key requests based on the search parameters
      */
diff --git a/base/common/src/com/netscape/certsrv/key/Request.java b/base/common/src/com/netscape/certsrv/key/Request.java
new file mode 100644
index 0000000000000000000000000000000000000000..f65809b025c7da1f0591c04707378c2014add306
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/key/Request.java
@@ -0,0 +1,167 @@
+package com.netscape.certsrv.key;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlValue;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+
+import org.jboss.resteasy.plugins.providers.atom.Link;
+
+/**
+ * @author Ade Lee
+ */
+public class Request {
+
+    Map<String, String> properties = new LinkedHashMap<String, String>();
+    Link link;
+    String requestType;
+
+    @XmlElement(name = "RequestType")
+    public String getRequestType() {
+        return requestType;
+    }
+
+    public void setRequestType(String requestType) {
+        this.requestType = requestType;
+    }
+
+    @XmlElement(name = "Properties")
+    @XmlJavaTypeAdapter(MapAdapter.class)
+    public Map<String, String> getProperties() {
+        return properties;
+    }
+
+    public void setProperties(Map<String, String> properties) {
+        this.properties.clear();
+        this.properties.putAll(properties);
+    }
+
+    public Collection<String> getPropertyNames() {
+        return properties.keySet();
+    }
+
+    public String getProperty(String name) {
+        return properties.get(name);
+    }
+
+    public void setProperty(String name, String value) {
+        properties.put(name, value);
+    }
+
+    public String removeProperty(String name) {
+        return properties.remove(name);
+    }
+
+    public static class MapAdapter extends XmlAdapter<PropertyList, Map<String, String>> {
+
+        public PropertyList marshal(Map<String, String> map) {
+            PropertyList list = new PropertyList();
+            for (Map.Entry<String, String> entry : map.entrySet()) {
+                Property property = new Property();
+                property.name = entry.getKey();
+                property.value = entry.getValue();
+                list.properties.add(property);
+            }
+            return list;
+        }
+
+        public Map<String, String> unmarshal(PropertyList list) {
+            Map<String, String> map = new LinkedHashMap<String, String>();
+            for (Property property : list.properties) {
+                map.put(property.name, property.value);
+            }
+            return map;
+        }
+    }
+
+    public static class PropertyList {
+        @XmlElement(name = "Property")
+        public List<Property> properties = new ArrayList<Property>();
+    }
+
+    public static class Property {
+
+        @XmlAttribute
+        public String name;
+
+        @XmlValue
+        public String value;
+    }
+
+    @XmlElement(name = "Link")
+    public Link getLink() {
+        return link;
+    }
+
+    public void setLink(Link link) {
+        this.link = link;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((link == null) ? 0 : link.hashCode());
+        result = prime * result + ((properties == null) ? 0 : properties.hashCode());
+        result = prime * result + ((requestType == null) ? 0 : requestType.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        Request other = (Request) obj;
+        if (link == null) {
+            if (other.link != null)
+                return false;
+        } else if (!link.equals(other.link))
+            return false;
+        if (properties == null) {
+            if (other.properties != null)
+                return false;
+        } else if (!properties.equals(other.properties))
+            return false;
+        if (requestType == null) {
+            if (other.requestType != null)
+                return false;
+        } else if (!requestType.equals(other.requestType))
+            return false;
+        return true;
+    }
+
+    public static <T> String marshal(T object, Class<T> clazz) throws JAXBException {
+        Marshaller marshaller = JAXBContext.newInstance(clazz).createMarshaller();
+        StringWriter sw = new StringWriter();
+        marshaller.marshal(object, sw);
+        return sw.toString();
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <T> T unmarshal(String string, Class<T> clazz) throws Exception {
+        try {
+            Unmarshaller unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
+            return (T) unmarshaller.unmarshal(new StringReader(string));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+}
-- 
1.8.4.2

