]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/test/TestClient.java
added a bunch of comments to the test client to help explain the api
[OpenSRF.git] / src / java / org / opensrf / test / TestClient.java
1 package org.opensrf.test;
2 import org.opensrf.*;
3 import org.opensrf.util.*;
4 import java.util.Map;
5 import java.util.Date;
6 import java.util.List;
7 import java.util.ArrayList;
8 import java.io.PrintStream;
9
10
11 public class TestClient {
12
13     public static void main(String args[]) throws Exception {
14
15         /** which opensrf service are we sending our request to */
16         String service; 
17         /** which opensrf method we're calling */
18         String method;
19         /** method params, captures from command-line args */
20         List<Object> params;
21         /** knows how to read JSON */
22         JSONReader reader;
23         /** opensrf request */
24         Request request;
25         /** request result */
26         Result result;
27         /** start time for the request */
28         long start;
29         /** for brevity */
30         PrintStream out = System.out;
31
32         if(args.length < 3) {
33             out.println( "usage: org.opensrf.test.TestClient "+
34                 "<osrfConfigFile> <service> <method> [<JSONparam1>, <JSONparam2>]");
35             return;
36         }
37
38         /** connect to the opensrf network,  default config context 
39          * for opensrf_core.xml is /config/opensrf */
40         Sys.bootstrapClient(args[0], "/config/opensrf");
41
42         /* grab the server, method, and any params from the command line */
43         service = args[1];
44         method = args[2];
45         params = new ArrayList<Object>();
46         for(int i = 3; i < args.length; i++) 
47             params.add(new JSONReader(args[i]).read());
48
49
50         /** build the client session */
51         ClientSession session = new ClientSession(service);
52
53         /** kick off the timer */
54         start = new Date().getTime();
55
56         /** Create the request object from the session, method and params */
57         request = session.request(method, params);
58
59         while( (result = request.recv(60000)) != null ) { 
60             /** loop over the results and print the JSON version of the content */
61
62             if(result.getStatusCode() != 200) { 
63                 /** make sure the request succeeded */
64                 out.println("status = " + result.getStatus());
65                 out.println("status code = " + result.getStatusCode());
66                 continue;
67             }
68
69             /** JSON-ify the resulting object and print it */
70             out.println("\nresult JSON: " + new JSONWriter(result.getContent()).write());
71         }
72         
73         /** How long did the request take? */
74         out.println("Request round trip took: " + (new Date().getTime() - start) + " ms.");
75     }
76 }
77
78
79