aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/frontend/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/org/ury/frontend/client')
-rw-r--r--src/uk/org/ury/frontend/client/Client.java60
-rw-r--r--src/uk/org/ury/frontend/client/test/ClientTest.java57
2 files changed, 117 insertions, 0 deletions
diff --git a/src/uk/org/ury/frontend/client/Client.java b/src/uk/org/ury/frontend/client/Client.java
new file mode 100644
index 0000000..9606d89
--- /dev/null
+++ b/src/uk/org/ury/frontend/client/Client.java
@@ -0,0 +1,60 @@
+package uk.org.ury.frontend.client;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import java.util.Map;
+
+import uk.org.ury.common.protocol.ProtocolUtils;
+import uk.org.ury.common.protocol.exceptions.DecodeFailureException;
+
+public class Client {
+ /**
+ * Get a raw response from the server.
+ *
+ * @param file
+ * The "file", including path and query string, to fetch from the
+ * server.
+ *
+ * @return The response from the server, as a key-value map.
+ *
+ * @throws DecodeFailureException
+ * if the decode failed.
+ */
+ public Map<?, ?> get(String file) throws DecodeFailureException {
+ URL url = null;
+ URLConnection uc = null;
+ String result = "";
+
+ try {
+ url = new URL("http://localhost:8000" + file);
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ try {
+ uc = url.openConnection();
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(
+ uc.getInputStream()));
+
+ String inputLine;
+
+ for (inputLine = in.readLine(); inputLine != null; inputLine = in
+ .readLine()) {
+ result += inputLine;
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return ProtocolUtils.decode(result);
+ }
+}
diff --git a/src/uk/org/ury/frontend/client/test/ClientTest.java b/src/uk/org/ury/frontend/client/test/ClientTest.java
new file mode 100644
index 0000000..d75a0ac
--- /dev/null
+++ b/src/uk/org/ury/frontend/client/test/ClientTest.java
@@ -0,0 +1,57 @@
+/**
+ *
+ */
+package uk.org.ury.frontend.client.test;
+
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import uk.org.ury.common.protocol.Directive;
+import uk.org.ury.common.protocol.exceptions.DecodeFailureException;
+import uk.org.ury.frontend.client.Client;
+
+/**
+ * JUnit test for the low-level client logic.
+ *
+ * @author Matt Windsor
+ */
+public class ClientTest {
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * Test method for {@link uk.org.ury.frontend.client.Client#get(java.lang.String)}.
+ */
+ @Test
+ public void testGet() {
+ Client client = new Client();
+
+ Map<?, ?> response = null;
+
+ try {
+ response = client.get("/server/ServerRequestHandler?function=test");
+ } catch (DecodeFailureException e) {
+ e.printStackTrace();
+ }
+
+ Assert.assertEquals("Test succeeded.",
+ response.get(Directive.INFO.toString()));
+ }
+}