summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/certsrv/base
diff options
context:
space:
mode:
authorAde Lee <alee@redhat.com>2014-01-30 10:30:49 -0500
committerAde Lee <alee@redhat.com>2014-02-04 13:37:31 -0500
commitd33998c72a34dc9f04e29ce0248fc2b7b88e0fc5 (patch)
tree2e162b63d487cf1f43102325cc466a9a2b7bbc32 /base/common/src/com/netscape/certsrv/base
parent3e48a7560406e0f4430bc620e35762bdd00099c0 (diff)
downloadpki-d33998c72a34dc9f04e29ce0248fc2b7b88e0fc5.tar.gz
pki-d33998c72a34dc9f04e29ce0248fc2b7b88e0fc5.tar.xz
pki-d33998c72a34dc9f04e29ce0248fc2b7b88e0fc5.zip
Rename KeyRequest to ResourceMessage
Refactor ResourceMessage to include classname instead of Request Type. Also changed PKIException.Data to extend ResourceMessage. Modifications to the server code to get the tests working.
Diffstat (limited to 'base/common/src/com/netscape/certsrv/base')
-rw-r--r--base/common/src/com/netscape/certsrv/base/PKIException.java66
-rw-r--r--base/common/src/com/netscape/certsrv/base/ResourceMessage.java180
-rw-r--r--base/common/src/com/netscape/certsrv/base/UserNotFoundException.java4
3 files changed, 185 insertions, 65 deletions
diff --git a/base/common/src/com/netscape/certsrv/base/PKIException.java b/base/common/src/com/netscape/certsrv/base/PKIException.java
index 6afb8f72b..65ff6db8e 100644
--- a/base/common/src/com/netscape/certsrv/base/PKIException.java
+++ b/base/common/src/com/netscape/certsrv/base/PKIException.java
@@ -17,23 +17,14 @@
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.base;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
-import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlValue;
-import javax.xml.bind.annotation.adapters.XmlAdapter;
-import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class PKIException extends RuntimeException {
@@ -93,64 +84,13 @@ public class PKIException extends RuntimeException {
}
@XmlRootElement(name="PKIException")
- public static class Data {
-
- @XmlElement(name="ClassName")
- public String className;
+ public static class Data extends ResourceMessage{
@XmlElement(name="Code")
public int code;
@XmlElement(name="Message")
public String message;
-
- @XmlElement(name="Attributes")
- @XmlJavaTypeAdapter(MapAdapter.class)
- public Map<String, String> attributes = new LinkedHashMap<String, String>();
-
- public String getAttribute(String name) {
- return attributes.get(name);
- }
-
- public void setAttribute(String name, String value) {
- attributes.put(name, value);
- }
- }
-
- public static class MapAdapter extends XmlAdapter<AttributeList, Map<String, String>> {
-
- public AttributeList marshal(Map<String, String> map) {
- AttributeList list = new AttributeList();
- for (Map.Entry<String, String> entry : map.entrySet()) {
- Attribute attribute = new Attribute();
- attribute.name = entry.getKey();
- attribute.value = entry.getValue();
- list.attributes.add(attribute);
- }
- return list;
- }
-
- public Map<String, String> unmarshal(AttributeList list) {
- Map<String, String> map = new LinkedHashMap<String, String>();
- for (Attribute attribute : list.attributes) {
- map.put(attribute.name, attribute.value);
- }
- return map;
- }
- }
-
- public static class AttributeList {
- @XmlElement(name="Attribute")
- public List<Attribute> attributes = new ArrayList<Attribute>();
- }
-
- public static class Attribute {
-
- @XmlAttribute
- public String name;
-
- @XmlValue
- public String value;
}
@Provider
@@ -171,8 +111,8 @@ public class PKIException extends RuntimeException {
data.className = PKIException.class.getName();
data.code = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
data.message = "An error has occured";
- data.setAttribute("attr1", "value1");
- data.setAttribute("attr2", "value2");
+ data.setProperty("attr1", "value1");
+ data.setProperty("attr2", "value2");
JAXBContext context = JAXBContext.newInstance(Data.class);
Marshaller marshaller = context.createMarshaller();
diff --git a/base/common/src/com/netscape/certsrv/base/ResourceMessage.java b/base/common/src/com/netscape/certsrv/base/ResourceMessage.java
new file mode 100644
index 000000000..dd2f48078
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/base/ResourceMessage.java
@@ -0,0 +1,180 @@
+package com.netscape.certsrv.base;
+
+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.ws.rs.core.MultivaluedMap;
+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.XmlRootElement;
+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
+ */
+@XmlRootElement(name="ResourceMessage")
+public class ResourceMessage {
+
+ protected Map<String, String> properties = new LinkedHashMap<String, String>();
+ Link link;
+ String className;
+
+ public ResourceMessage() {
+ // required for jax-b
+ }
+
+ public ResourceMessage(MultivaluedMap<String, String> form) {
+ for (Map.Entry<String, List<String>> entry: form.entrySet()) {
+ properties.put(entry.getKey(), entry.getValue().get(0));
+ }
+ }
+
+ @XmlElement(name = "ClassName")
+ public String getClassName() {
+ return className;
+ }
+
+ public void setClassName(String className) {
+ this.className = className;
+ }
+
+ @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 + ((className == null) ? 0 : className.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;
+ ResourceMessage other = (ResourceMessage) 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 (className == null) {
+ if (other.className != null)
+ return false;
+ } else if (!className.equals(other.className))
+ 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;
+ }
+ }
+
+}
diff --git a/base/common/src/com/netscape/certsrv/base/UserNotFoundException.java b/base/common/src/com/netscape/certsrv/base/UserNotFoundException.java
index 63df7067a..ec14b59aa 100644
--- a/base/common/src/com/netscape/certsrv/base/UserNotFoundException.java
+++ b/base/common/src/com/netscape/certsrv/base/UserNotFoundException.java
@@ -21,12 +21,12 @@ public class UserNotFoundException extends ResourceNotFoundException {
public UserNotFoundException(Data data) {
super(data);
- userId = data.getAttribute("userId");
+ userId = data.getProperty("userId");
}
public Data getData() {
Data data = super.getData();
- data.setAttribute("userId", userId);
+ data.setProperty("userId", userId);
return data;
}