aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/server/protocol
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/org/ury/server/protocol')
-rw-r--r--src/uk/org/ury/server/protocol/DecodeFailureException.java31
-rw-r--r--src/uk/org/ury/server/protocol/Directive.java59
-rw-r--r--src/uk/org/ury/server/protocol/ProtocolUtils.java67
-rw-r--r--src/uk/org/ury/server/protocol/Status.java17
4 files changed, 119 insertions, 55 deletions
diff --git a/src/uk/org/ury/server/protocol/DecodeFailureException.java b/src/uk/org/ury/server/protocol/DecodeFailureException.java
new file mode 100644
index 0000000..faa2b1f
--- /dev/null
+++ b/src/uk/org/ury/server/protocol/DecodeFailureException.java
@@ -0,0 +1,31 @@
+/**
+ *
+ */
+package uk.org.ury.server.protocol;
+
+/**
+ * Exception thrown when the protocol decoder fails.
+ *
+ * @author Matt Windsor
+ */
+
+public class DecodeFailureException extends Exception
+{
+ /**
+ *
+ */
+ private static final long serialVersionUID = -3972492943653273528L;
+
+
+ /**
+ * Construct a new DecodeFailureException with a reason.
+ *
+ * @param reason The reason for throwing the exception.
+ */
+
+ public
+ DecodeFailureException (String reason)
+ {
+ super (reason);
+ }
+}
diff --git a/src/uk/org/ury/server/protocol/Directive.java b/src/uk/org/ury/server/protocol/Directive.java
index 0d7a162..ce1b8b9 100644
--- a/src/uk/org/ury/server/protocol/Directive.java
+++ b/src/uk/org/ury/server/protocol/Directive.java
@@ -11,59 +11,8 @@ package uk.org.ury.server.protocol;
public enum Directive
{
- // ID String representation Singleton?
-
- /** Directive marking the start of an item block. */
- ITEM_START ("ITEM-START" , false),
-
- /** Directive marking a property inside an item block. */
- ITEM_PROPERTY ("PROP" , false),
-
- /** Directive marking the end of an item block. */
- ITEM_END ("ITEM-END" , false);
-
-
-
- private String strRep; // String representation
- private boolean isSingleton; // Is a singleton?
-
-
- /**
- * Construct a new Directive.
- *
- * @param strRep The string representation of the Directive.
- *
- * @param isSingleton If true, then the Directive accepts no
- * properties. If false, then the Directive
- * must be provided with at least one.
- */
-
- private
- Directive (String strRep, boolean isSingleton)
- {
- this.strRep = strRep;
- this.isSingleton = isSingleton;
- }
-
-
- /**
- * @return the string representation.
- */
-
- public String
- toString ()
- {
- return strRep;
- }
-
-
- /**
- * @return true if the directive has no properties.
- */
-
- public Boolean
- isSingleton ()
- {
- return isSingleton;
- }
+ INFO, // Information string (can usually be ignored)
+ ITEMS, // Item
+ STATUS, // Status code (from the enum Status)
+ REASON; // Error reason
}
diff --git a/src/uk/org/ury/server/protocol/ProtocolUtils.java b/src/uk/org/ury/server/protocol/ProtocolUtils.java
new file mode 100644
index 0000000..e45bb22
--- /dev/null
+++ b/src/uk/org/ury/server/protocol/ProtocolUtils.java
@@ -0,0 +1,67 @@
+/**
+ *
+ */
+package uk.org.ury.server.protocol;
+
+import java.util.Map;
+
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+
+
+/**
+ * Utilities for converting between strings encoded in the response
+ * protocol and collections of items.
+ *
+ * @author Matt Windsor
+ *
+ */
+
+public class ProtocolUtils
+{
+ /**
+ * Encode a key-value map into a protocol string.
+ *
+ * The map can contain strings, lists and other maps. Other
+ * types may be accepted by the underlying encoding engine,
+ * but the above types are the only ones explicitly accepted.
+ *
+ * @param items The key-value map of items, which should contain
+ * strings, lists and maps. The keys of any map
+ * should be protocol directives.
+ *
+ * @return A string containing the encoded representation of
+ * the map.
+ */
+
+ public static String
+ encode (Map<String, Object> items)
+ {
+ return JSONValue.toJSONString (items);
+ }
+
+
+ /**
+ * Decode a protocol string into a key-value map.
+ *
+ * @param string The string to decode.
+ *
+ * @return A key-value map mapping directives to strings,
+ * lists and maps.
+ *
+ * @throws DecodeFailureException if the decoding engine
+ * returns something other than a map.
+ */
+
+ public static Map<?, ?>
+ decode (String string) throws DecodeFailureException
+ {
+ Object result = JSONValue.parse (string);
+
+ if (result instanceof JSONObject)
+ return (JSONObject) result;
+ else
+ throw new DecodeFailureException ("Result not a map.");
+ }
+
+}
diff --git a/src/uk/org/ury/server/protocol/Status.java b/src/uk/org/ury/server/protocol/Status.java
new file mode 100644
index 0000000..c9d848d
--- /dev/null
+++ b/src/uk/org/ury/server/protocol/Status.java
@@ -0,0 +1,17 @@
+/**
+ *
+ */
+package uk.org.ury.server.protocol;
+
+
+/**
+ * Statuses that can follow the STATUS directory.
+ *
+ * @author Matt Windsor
+ */
+
+public enum Status
+ {
+ OK, // The request was processed OK; response should be valid
+ ERROR // An error occurred; message provided as REASON directive
+ }