diff options
author | Ade Lee <alee@redhat.com> | 2014-01-24 16:15:05 -0500 |
---|---|---|
committer | Ade Lee <alee@redhat.com> | 2014-02-04 13:35:50 -0500 |
commit | 12eef29a23c4cccc09d1e51a60766f87a782cf46 (patch) | |
tree | 895091feab617acde7b7dac2707f52c1213c4ba8 /base/common | |
parent | 78031e70bc2eff088c8cab28d7d550fa76b1528c (diff) | |
download | pki-12eef29a23c4cccc09d1e51a60766f87a782cf46.tar.gz pki-12eef29a23c4cccc09d1e51a60766f87a782cf46.tar.xz pki-12eef29a23c4cccc09d1e51a60766f87a782cf46.zip |
Use a generic request object
Refactored KeyRequest API to use requests containing a
generic request object.
Diffstat (limited to 'base/common')
4 files changed, 274 insertions, 86 deletions
diff --git a/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java b/base/common/src/com/netscape/certsrv/key/KeyArchivalRequest.java index 2d2d84c94..9343d0afe 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 aa69d8336..f2d259bf8 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 9256ac461..5d576c05f 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 000000000..f65809b02 --- /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; + } + } + +} |