Java gateway interface test class
authorBill Erickson <berick@esilibrary.com>
Thu, 15 Mar 2012 21:26:16 +0000 (17:26 -0400)
committerDan Scott <dscott@laurentian.ca>
Sun, 29 Apr 2012 18:22:35 +0000 (14:22 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Dan Scott <dscott@laurentian.ca>
src/java/org/opensrf/test/TestGateway.java [new file with mode: 0644]

diff --git a/src/java/org/opensrf/test/TestGateway.java b/src/java/org/opensrf/test/TestGateway.java
new file mode 100644 (file)
index 0000000..967e929
--- /dev/null
@@ -0,0 +1,58 @@
+package org.opensrf.test;
+
+import org.opensrf.*;
+import org.opensrf.util.*;
+import org.opensrf.net.http.*;
+
+import java.net.URL;
+import java.util.List;
+import java.util.Arrays;
+
+public class TestGateway {
+    
+    public static void main(String[] args) throws java.net.MalformedURLException {
+
+        if (args.length == 0) {
+            System.err.println("Please provide a gateway URL: e.g. http://example.org/osrf-gateway-v1");
+            return;
+        }
+
+        // configure the connection
+        HttpConnection conn = new HttpConnection(args[0]);
+
+        Method method = new Method("opensrf.system.echo");
+        method.addParam("Hello, Gateway");
+        method.addParam(new Integer(12345));
+        method.addParam(new Boolean(true));
+        method.addParam(Arrays.asList(8,6,7,5,3,0,9));
+
+        // sync test
+        HttpRequest req = new GatewayRequest(conn, "opensrf.math", method).send();
+        Object resp;
+        while ( (resp = req.recv()) != null) {
+            System.out.println("Sync Response: " + resp);
+        }
+
+        // exceptions are captured instead of thrown, 
+        // primarily to better support async requests
+        if (req.failed()) {
+            req.getFailure().printStackTrace();
+            return;
+        }
+
+        // async test
+        for (int i = 0; i < 10; i++) {
+            final int ii = i; // required for nested class
+            HttpRequest req2 = new GatewayRequest(conn, "opensrf.math", method);
+            req2.sendAsync(
+                new HttpRequestHandler() {
+                    public void onResponse(HttpRequest req, Object resp) {
+                        System.out.println("Async Response: " + ii + " : " + resp);
+                    }
+                }
+            );
+        }
+    }
+}
+
+