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.
10 import org.arvados.sdk.java.Arvados;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import java.io.BufferedReader;
19 import java.io.InputStreamReader;
21 public class ArvadosSDKJavaExampleWithPrompt {
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.
28 * Please refer to http://doc.arvados.org/api/index.html for a complete list
29 * of the available API methods.
31 public static void main(String[] args) throws Exception {
32 String apiName = "arvados";
33 String apiVersion = "v1";
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\"]]");
43 System.out.println("\nEnter ^C when you want to quit");
45 // use configured env variables for API TOKEN, HOST and HOST_INSECURE
46 Arvados arv = new Arvados(apiName, apiVersion);
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>>> ");
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"));
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"));
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));
75 System.out.print("\n>>> ");
76 Map paramsMap = new HashMap();
80 param = in.readLine();
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());
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");
97 // Make a "call" for the given resource name and method name
99 System.out.println ("Making a call for " + resourceName + " " + methodName);
100 Map response = arv.call(resourceName, methodName, paramsMap);
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);
110 System.out.println(entry.getKey() + " = " + entry.getValue());
113 } catch (Exception e){
114 System.out.println (e.getMessage());
115 System.out.println ("\nSet up a new call");
117 } catch (Exception e) {
118 System.out.println (e.getMessage());
119 System.out.println ("\nSet up a new call");