aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/uk/org/ury/client/Client.java4
-rw-r--r--src/uk/org/ury/client/test/ClientTest.java4
-rw-r--r--src/uk/org/ury/library/LibraryRequestHandler.java2
-rw-r--r--src/uk/org/ury/library/viewer/LibraryViewer.java79
-rw-r--r--src/uk/org/ury/library/viewer/LibraryViewerPanel.java6
-rw-r--r--src/uk/org/ury/protocol/Directive.java18
-rw-r--r--src/uk/org/ury/protocol/ProtocolUtils.java98
-rw-r--r--src/uk/org/ury/protocol/Status.java17
-rw-r--r--src/uk/org/ury/protocol/exceptions/DecodeFailureException.java32
-rw-r--r--src/uk/org/ury/protocol/exceptions/InvalidMessageException.java33
-rw-r--r--src/uk/org/ury/server/Server.java4
11 files changed, 233 insertions, 64 deletions
diff --git a/src/uk/org/ury/client/Client.java b/src/uk/org/ury/client/Client.java
index 3d810c5..1a3b53a 100644
--- a/src/uk/org/ury/client/Client.java
+++ b/src/uk/org/ury/client/Client.java
@@ -10,8 +10,8 @@ import java.net.URLConnection;
import java.util.Map;
-import uk.org.ury.server.protocol.DecodeFailureException;
-import uk.org.ury.server.protocol.ProtocolUtils;
+import uk.org.ury.protocol.ProtocolUtils;
+import uk.org.ury.protocol.exceptions.DecodeFailureException;
public class Client
{
diff --git a/src/uk/org/ury/client/test/ClientTest.java b/src/uk/org/ury/client/test/ClientTest.java
index 48669aa..707a80f 100644
--- a/src/uk/org/ury/client/test/ClientTest.java
+++ b/src/uk/org/ury/client/test/ClientTest.java
@@ -12,8 +12,8 @@ import org.junit.Before;
import org.junit.Test;
import uk.org.ury.client.Client;
-import uk.org.ury.server.protocol.DecodeFailureException;
-import uk.org.ury.server.protocol.Directive;
+import uk.org.ury.protocol.Directive;
+import uk.org.ury.protocol.exceptions.DecodeFailureException;
/**
diff --git a/src/uk/org/ury/library/LibraryRequestHandler.java b/src/uk/org/ury/library/LibraryRequestHandler.java
index bd7595b..ad174b8 100644
--- a/src/uk/org/ury/library/LibraryRequestHandler.java
+++ b/src/uk/org/ury/library/LibraryRequestHandler.java
@@ -15,10 +15,10 @@ import uk.org.ury.database.exceptions.MissingCredentialsException;
import uk.org.ury.database.exceptions.QueryFailureException;
import uk.org.ury.library.exceptions.EmptySearchException;
import uk.org.ury.library.item.LibraryItem;
+import uk.org.ury.protocol.Directive;
import uk.org.ury.server.Server;
import uk.org.ury.server.RequestHandler;
import uk.org.ury.server.exceptions.HandleFailureException;
-import uk.org.ury.server.protocol.Directive;
/**
diff --git a/src/uk/org/ury/library/viewer/LibraryViewer.java b/src/uk/org/ury/library/viewer/LibraryViewer.java
index 3e09594..9e854b7 100644
--- a/src/uk/org/ury/library/viewer/LibraryViewer.java
+++ b/src/uk/org/ury/library/viewer/LibraryViewer.java
@@ -8,14 +8,6 @@ import java.util.Set;
import uk.org.ury.client.Client;
-import uk.org.ury.config.ConfigReader;
-
-import uk.org.ury.database.DatabaseDriver;
-import uk.org.ury.database.UserClass;
-
-import uk.org.ury.database.exceptions.MissingCredentialsException;
-import uk.org.ury.database.exceptions.QueryFailureException;
-
import uk.org.ury.frontend.AbstractFrontendModule;
import uk.org.ury.frontend.FrontendMaster;
import uk.org.ury.frontend.FrontendModulePanel;
@@ -26,9 +18,10 @@ import uk.org.ury.library.exceptions.EmptySearchException;
import uk.org.ury.library.item.LibraryItem;
import uk.org.ury.library.item.LibraryItemProperty;
-import uk.org.ury.server.protocol.DecodeFailureException;
-import uk.org.ury.server.protocol.Directive;
-import uk.org.ury.server.protocol.Status;
+import uk.org.ury.protocol.Directive;
+import uk.org.ury.protocol.ProtocolUtils;
+import uk.org.ury.protocol.exceptions.DecodeFailureException;
+import uk.org.ury.protocol.exceptions.InvalidMessageException;
/**
@@ -47,7 +40,6 @@ public class LibraryViewer extends AbstractFrontendModule
private static final long serialVersionUID = -2782366476480563739L;
private List<LibraryItem> libraryList;
private LibraryViewerPanel panel;
- private ConfigReader config;
/**
@@ -56,16 +48,7 @@ public class LibraryViewer extends AbstractFrontendModule
public
LibraryViewer ()
- {
- try
- {
- config = new ConfigReader ("res/conf.xml");
- }
- catch (MissingCredentialsException e)
- {
- System.out.println(e);
- }
-
+ {
libraryList = new ArrayList<LibraryItem> ();
panel = null;
}
@@ -77,23 +60,7 @@ public class LibraryViewer extends AbstractFrontendModule
public FrontendModulePanel
runFrontend (FrontendMaster master)
- {
- dd = null;
-
- try
- {
- dd = new DatabaseDriver (config, UserClass.READ_ONLY);
- }
- catch (MissingCredentialsException e)
- {
- // TODO: Privilege de-escalation
- master.fatalError (e.getMessage ());
- }
- catch (Exception f)
- {
- master.fatalError (f.getMessage ());
- }
-
+ {
try
{
panel = new LibraryViewerPanel (this, master);
@@ -119,16 +86,20 @@ public class LibraryViewer extends AbstractFrontendModule
* @throws EmptySearchException if the search string is
* empty or null (from LibraryUtils.search).
*
- * @throws QueryFailureException if the search query
- * fails (from LibraryUtils.search).
+ * @throws InvalidMessageException if the response from
+ * the server is invalid.
*/
public void
doSearch (String search)
- throws EmptySearchException, QueryFailureException
+ throws EmptySearchException, InvalidMessageException
{
// TODO: fan out?
+ if (search == null || search == "")
+ throw new EmptySearchException ();
+
+
Client cl = new Client ();
Map<?, ?> response = null;
libraryList.clear ();
@@ -139,25 +110,23 @@ public class LibraryViewer extends AbstractFrontendModule
}
catch (DecodeFailureException e)
{
- throw new QueryFailureException (e.getMessage ());
+ throw new InvalidMessageException (e.getMessage ());
}
// Check to see if this is Map<String, ?> by looking for the status,
// which should always be in a valid response.
- if (response.containsKey (Directive.STATUS.toString ()) == false
- || (response.get (Directive.STATUS.toString ()) instanceof String) == false)
- throw new QueryFailureException ("Status not provided.");
+
- if (((String) response.get (Directive.STATUS.toString ()))
- .equals (Status.OK.toString ()) == false)
- throw new QueryFailureException ((String) response.get (Directive.REASON.toString ()));
+ if (ProtocolUtils.responseIsOK (response) == false)
+ throw new InvalidMessageException ((String)
+ response.get (Directive.REASON.toString ()));
// Should contain a list of items, even if there are no items.
if (response.containsKey (Directive.ITEMS.toString ()) == false)
- throw new QueryFailureException ("No item set returned.");
+ throw new InvalidMessageException ("No item set returned.");
if ((response.get (Directive.ITEMS.toString ()) instanceof List<?>) == false)
- throw new QueryFailureException ("Malformed item list.");
+ throw new InvalidMessageException ("Malformed item list.");
for (Object obj : (List<?>) response.get (Directive.ITEMS.toString ()))
@@ -166,7 +135,7 @@ public class LibraryViewer extends AbstractFrontendModule
= new HashMap<LibraryItemProperty, String> ();
if (obj instanceof Map<?, ?> == false)
- throw new QueryFailureException ("Malformed item.");
+ throw new InvalidMessageException ("Malformed item.");
Set<?> keySet = ((Map<?, ?>) obj).keySet ();
@@ -176,10 +145,10 @@ public class LibraryViewer extends AbstractFrontendModule
if ((key instanceof String
&& ((Map<?, ?>) obj).get (key) instanceof String)
== false)
- throw new QueryFailureException ("Not a valid property.");
+ throw new InvalidMessageException ("Not a valid property.");
else if (LibraryItemProperty.valueOf ((String) key) == null)
- throw new QueryFailureException ("Property type "
- + key + " not recognised.");
+ throw new InvalidMessageException ("Property type "
+ + key + " not recognised.");
else
properties.put (LibraryItemProperty.valueOf ((String) key),
(String) ((Map<?, ?>) obj).get (key));
diff --git a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
index e05164e..b3ffdd3 100644
--- a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
+++ b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java
@@ -15,13 +15,15 @@ import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
-import uk.org.ury.database.exceptions.QueryFailureException;
import uk.org.ury.frontend.FrontendMaster;
import uk.org.ury.frontend.FrontendModulePanel;
import uk.org.ury.frontend.HintField;
import uk.org.ury.frontend.exceptions.UICreationFailureException;
+
import uk.org.ury.library.exceptions.EmptySearchException;
+import uk.org.ury.protocol.exceptions.InvalidMessageException;
+
/**
* Frontend panel providing access to an underlying library viewer.
@@ -173,7 +175,7 @@ public class LibraryViewerPanel extends FrontendModulePanel
{
master.doSearch (searchTerm);
}
- catch (QueryFailureException e)
+ catch (InvalidMessageException e)
{
searchFailureMessage = String.format (rb.getString ("ERR_SEARCH_FAILED"),
searchTerm, e.getMessage ());
diff --git a/src/uk/org/ury/protocol/Directive.java b/src/uk/org/ury/protocol/Directive.java
new file mode 100644
index 0000000..bd0be0e
--- /dev/null
+++ b/src/uk/org/ury/protocol/Directive.java
@@ -0,0 +1,18 @@
+/**
+ *
+ */
+package uk.org.ury.protocol;
+
+/**
+ * Directives supported by the protocol.
+ *
+ * @author Matt Windsor
+ */
+
+public enum Directive
+ {
+ 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/protocol/ProtocolUtils.java b/src/uk/org/ury/protocol/ProtocolUtils.java
new file mode 100644
index 0000000..d38eeed
--- /dev/null
+++ b/src/uk/org/ury/protocol/ProtocolUtils.java
@@ -0,0 +1,98 @@
+/**
+ *
+ */
+package uk.org.ury.protocol;
+
+import java.util.Map;
+
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+
+import uk.org.ury.protocol.exceptions.DecodeFailureException;
+import uk.org.ury.protocol.exceptions.InvalidMessageException;
+
+
+/**
+ * Utilities for converting between strings encoded in the response
+ * protocol and collections of items, as well as validating and
+ * unpicking protocol messages.
+ *
+ * @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.");
+ }
+
+
+ /**
+ * Check if a response is flagged as having OK status.
+ *
+ * @param response The response message, as a key-value map
+ * (eg in decoded format).
+ *
+ * @return true if the response is flagged with OK status,
+ * false if not (eg ERROR status).
+ *
+ * @throws InvalidMessageException if the response is
+ * invalid (eg the status is missing).
+ */
+
+ public static boolean
+ responseIsOK (Map<?, ?> response)
+ throws InvalidMessageException
+ {
+ if (response.containsKey (Directive.STATUS.toString ()) == false)
+ throw new InvalidMessageException ("No status line in response.");
+
+ if ((response.get (Directive.STATUS.toString ()) instanceof String) == false)
+ throw new InvalidMessageException ("Status is not a string.");
+
+ return (((String) response.get (Directive.STATUS.toString ()))
+ .equals (Status.OK.toString ()));
+ }
+}
diff --git a/src/uk/org/ury/protocol/Status.java b/src/uk/org/ury/protocol/Status.java
new file mode 100644
index 0000000..f822c34
--- /dev/null
+++ b/src/uk/org/ury/protocol/Status.java
@@ -0,0 +1,17 @@
+/**
+ *
+ */
+package uk.org.ury.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
+ }
diff --git a/src/uk/org/ury/protocol/exceptions/DecodeFailureException.java b/src/uk/org/ury/protocol/exceptions/DecodeFailureException.java
new file mode 100644
index 0000000..708732b
--- /dev/null
+++ b/src/uk/org/ury/protocol/exceptions/DecodeFailureException.java
@@ -0,0 +1,32 @@
+/**
+ *
+ */
+package uk.org.ury.protocol.exceptions;
+
+
+/**
+ * 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/protocol/exceptions/InvalidMessageException.java b/src/uk/org/ury/protocol/exceptions/InvalidMessageException.java
new file mode 100644
index 0000000..3cf1105
--- /dev/null
+++ b/src/uk/org/ury/protocol/exceptions/InvalidMessageException.java
@@ -0,0 +1,33 @@
+/**
+ *
+ */
+package uk.org.ury.protocol.exceptions;
+
+
+/**
+ * Generic exception thrown when a protocol function cannot process a
+ * message due to an issue with the message.
+ *
+ * @author Matt Windsor
+ */
+
+public class InvalidMessageException extends Exception
+{
+ /**
+ *
+ */
+ private static final long serialVersionUID = -3972492943653273528L;
+
+
+ /**
+ * Construct a new InvalidMessageException with a reason.
+ *
+ * @param reason The reason for throwing the exception.
+ */
+
+ public
+ InvalidMessageException (String reason)
+ {
+ super (reason);
+ }
+}
diff --git a/src/uk/org/ury/server/Server.java b/src/uk/org/ury/server/Server.java
index 697476e..e75cb97 100644
--- a/src/uk/org/ury/server/Server.java
+++ b/src/uk/org/ury/server/Server.java
@@ -36,14 +36,14 @@ import uk.org.ury.database.DatabaseDriver;
import uk.org.ury.database.UserClass;
import uk.org.ury.database.exceptions.ConnectionFailureException;
import uk.org.ury.database.exceptions.MissingCredentialsException;
+import uk.org.ury.protocol.Directive;
+import uk.org.ury.protocol.Status;
import uk.org.ury.server.exceptions.BadRequestException;
import uk.org.ury.server.exceptions.HandleFailureException;
import uk.org.ury.server.exceptions.HandlerNotFoundException;
import uk.org.ury.server.exceptions.HandlerSetupFailureException;
import uk.org.ury.server.exceptions.HandlingException;
import uk.org.ury.server.exceptions.NotAHandlerException;
-import uk.org.ury.server.protocol.Directive;
-import uk.org.ury.server.protocol.Status;
/**
* The unified URY server, accepting requests over HTTP.