]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/test/TestGateway.java
967e9290efb8b2669e6f4c07cba7a2302f70c603
[OpenSRF.git] / src / java / org / opensrf / test / TestGateway.java
1 package org.opensrf.test;
2
3 import org.opensrf.*;
4 import org.opensrf.util.*;
5 import org.opensrf.net.http.*;
6
7 import java.net.URL;
8 import java.util.List;
9 import java.util.Arrays;
10
11 public class TestGateway {
12     
13     public static void main(String[] args) throws java.net.MalformedURLException {
14
15         if (args.length == 0) {
16             System.err.println("Please provide a gateway URL: e.g. http://example.org/osrf-gateway-v1");
17             return;
18         }
19
20         // configure the connection
21         HttpConnection conn = new HttpConnection(args[0]);
22
23         Method method = new Method("opensrf.system.echo");
24         method.addParam("Hello, Gateway");
25         method.addParam(new Integer(12345));
26         method.addParam(new Boolean(true));
27         method.addParam(Arrays.asList(8,6,7,5,3,0,9));
28
29         // sync test
30         HttpRequest req = new GatewayRequest(conn, "opensrf.math", method).send();
31         Object resp;
32         while ( (resp = req.recv()) != null) {
33             System.out.println("Sync Response: " + resp);
34         }
35
36         // exceptions are captured instead of thrown, 
37         // primarily to better support async requests
38         if (req.failed()) {
39             req.getFailure().printStackTrace();
40             return;
41         }
42
43         // async test
44         for (int i = 0; i < 10; i++) {
45             final int ii = i; // required for nested class
46             HttpRequest req2 = new GatewayRequest(conn, "opensrf.math", method);
47             req2.sendAsync(
48                 new HttpRequestHandler() {
49                     public void onResponse(HttpRequest req, Object resp) {
50                         System.out.println("Async Response: " + ii + " : " + resp);
51                     }
52                 }
53             );
54         }
55     }
56 }
57
58