8784: Fix test for latest firefox.
[arvados.git] / sdk / java / ArvadosSDKJavaExampleWithPrompt.java
1 /**
2  * This Sample test program is useful in getting started with using Arvados Java SDK.
3  * This program creates an Arvados instance using the configured environment variables.
4  * It then provides a prompt to input method name and input parameters. 
5  * The program them invokes the API server to execute the specified method.  
6  * 
7  * @author radhika
8  */
9
10 import org.arvados.sdk.Arvados;
11
12 import java.io.File;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.io.BufferedReader;
19 import java.io.InputStreamReader;
20
21 public class ArvadosSDKJavaExampleWithPrompt {
22   /**
23    * Make sure the following environment variables are set before using Arvados:
24    * ARVADOS_API_TOKEN, ARVADOS_API_HOST and ARVADOS_API_HOST_INSECURE Set
25    * ARVADOS_API_HOST_INSECURE to true if you are using self-singed certificates
26    * in development and want to bypass certificate validations.
27    * 
28    * Please refer to http://doc.arvados.org/api/index.html for a complete list
29    * of the available API methods.
30    */
31   public static void main(String[] args) throws Exception {
32     String apiName = "arvados";
33     String apiVersion = "v1";
34
35     System.out.print("Welcome to Arvados Java SDK.");
36     System.out.println("\nYou can use this example to call API methods interactively.");
37     System.out.println("\nPlease refer to http://doc.arvados.org/api/index.html for api documentation");
38     System.out.println("\nTo make the calls, enter input data at the prompt.");
39     System.out.println("When entering parameters, you may enter a simple string or a well-formed json.");
40     System.out.println("For example to get a user you may enter:  user, zzzzz-12345-67890");
41     System.out.println("Or to filter links, you may enter:  filters, [[ \"name\", \"=\", \"can_manage\"]]");
42
43     System.out.println("\nEnter ^C when you want to quit");
44
45     // use configured env variables for API TOKEN, HOST and HOST_INSECURE
46     Arvados arv = new Arvados(apiName, apiVersion);
47
48     while (true) {
49       try {
50         // prompt for resource
51         System.out.println("\n\nEnter Resource name (for example users)");
52         System.out.println("\nAvailable resources are: " + arv.getAvailableResourses());
53         System.out.print("\n>>> ");
54
55         // read resource name
56         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
57         String resourceName = in.readLine().trim();
58         if ("".equals(resourceName)) {
59           throw (new Exception("No resource name entered"));
60         }
61         // read method name
62         System.out.println("\nEnter method name (for example get)");
63         System.out.println("\nAvailable methods are: " + arv.getAvailableMethodsForResourse(resourceName));
64         System.out.print("\n>>> ");
65         String methodName = in.readLine().trim();
66         if ("".equals(methodName)) {
67           throw (new Exception("No method name entered"));
68         }
69
70         // read method parameters
71         System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
72         System.out.println("\nAvailable parameters are: " + 
73               arv.getAvailableParametersForMethod(resourceName, methodName));
74         
75         System.out.print("\n>>> ");
76         Map paramsMap = new HashMap();
77         String param = "";
78         try {
79           do {
80             param = in.readLine();
81             if (param.isEmpty())
82               break;
83             int index = param.indexOf(","); // first comma
84             String paramName = param.substring(0, index);
85             String paramValue = param.substring(index+1);
86             paramsMap.put(paramName.trim(), paramValue.trim());
87
88             System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
89             System.out.print("\n>>> ");
90           } while (!param.isEmpty());
91         } catch (Exception e) {
92           System.out.println (e.getMessage());
93           System.out.println ("\nSet up a new call");
94           continue;
95         }
96
97         // Make a "call" for the given resource name and method name
98         try {
99           System.out.println ("Making a call for " + resourceName + " " + methodName);
100           Map response = arv.call(resourceName, methodName, paramsMap);
101
102           Set<Entry<String,Object>> entrySet = (Set<Entry<String,Object>>)response.entrySet();
103           for (Map.Entry<String, Object> entry : entrySet) {
104             if ("items".equals(entry.getKey())) {
105               List items = (List)entry.getValue();
106               for (Object item : items) {
107                 System.out.println("    " + item);
108               }            
109             } else {
110               System.out.println(entry.getKey() + " = " + entry.getValue());
111             }
112           }
113         } catch (Exception e){
114           System.out.println (e.getMessage());
115           System.out.println ("\nSet up a new call");
116         }
117       } catch (Exception e) {
118         System.out.println (e.getMessage());
119         System.out.println ("\nSet up a new call");
120       }
121     }
122   }
123 }