]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/test/TestGateway.java
LP1827055 Remove Python libs, install bits, and docs
[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) {
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         try {
21
22             // configure the connection
23             HttpConnection conn = new HttpConnection(args[0]);
24
25             Method method = new Method("opensrf.system.echo");
26             method.addParam("Hello, Gateway");
27             method.addParam(new Integer(12345));
28             method.addParam(new Boolean(true));
29             method.addParam(Arrays.asList(8,6,7,5,3,0,9));
30
31             // sync test
32             HttpRequest req = new GatewayRequest(conn, "opensrf.math", method).send();
33             Object resp;
34             while ( (resp = req.recv()) != null) {
35                 System.out.println("Sync Response: " + resp);
36             }
37
38             // async test
39             for (int i = 0; i < 10; i++) {
40                 final int ii = i; // required for nested class
41                 HttpRequest req2 = new GatewayRequest(conn, "opensrf.math", method);
42
43                 req2.sendAsync(
44                     new HttpRequestHandler() {
45
46                         // called once per response
47                         public void onResponse(HttpRequest req, Object resp) {
48                             System.out.println("Async Response: " + ii + " : " + resp);
49                         }
50
51                         // called after all responses have been received
52                         // used primarily when you don't know how many responses will be returned
53                         public void onComplete(HttpRequest req) {
54                             System.out.println("Async Request complete : " + ii);
55                         }
56
57                         // ruh-roh
58                         public void onError(HttpRequest req, Exception ex) {
59                             if (ex instanceof java.io.IOException) 
60                                 System.err.println("Trouble communicating with gateway server!");
61                             ex.printStackTrace();
62                         }
63                     }
64                 );
65             }
66
67         } catch (java.net.MalformedURLException ex) {
68             System.err.println("Malformed Gateway URL! " + args[0]);
69             ex.printStackTrace();
70
71         } catch (java.io.IOException ex) {
72             System.err.println("Trouble communicating with gateway server!");
73             ex.printStackTrace();
74         }
75     }
76 }
77
78