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("\nTo make the calls, enter input data at the prompt.");
38 System.out.println("When entering parameters, you may enter a simple string or a well-formed json.");
39 System.out.println("For example to get a user you may enter: user, zzzzz-12345-67890");
40 System.out.println("Or to filter links, you may enter: filters, [[ \"name\", \"=\", \"can_manage\"]]");
42 System.out.println("\nEnter ^C when you want to quit");
44 // use configured env variables for API TOKEN, HOST and HOST_INSECURE
45 Arvados arv = new Arvados(apiName, apiVersion);
49 // prompt for resource
50 System.out.println("\n\nEnter Resource name (for example users)");
51 System.out.println("\nAvailable resources are: " + arv.getAvailableResourses());
52 System.out.print("\n>>> ");
55 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
56 String resourceName = in.readLine().trim();
57 if ("".equals(resourceName)) {
58 throw (new Exception("No resource name entered"));
61 System.out.println("\nEnter method name (for example get)");
62 System.out.println("\nAvailable methods are: " + arv.getAvailableMethodsForResourse(resourceName));
63 System.out.print("\n>>> ");
64 String methodName = in.readLine().trim();
65 if ("".equals(methodName)) {
66 throw (new Exception("No method name entered"));
69 // read method parameters
70 System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
71 System.out.print("\n>>> ");
72 Map paramsMap = new HashMap();
76 param = in.readLine();
79 int index = param.indexOf(","); // first comma
80 String paramName = param.substring(0, index);
81 String paramValue = param.substring(index+1);
82 paramsMap.put(paramName.trim(), paramValue.trim());
84 System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
85 System.out.print("\n>>> ");
86 } while (!param.isEmpty());
87 } catch (Exception e) {
88 System.out.println (e.getMessage());
89 System.out.println ("\nSet up a new call");
93 // Make a "call" for the given resource name and method name
95 System.out.println ("Making a call for " + resourceName + " " + methodName);
96 Map response = arv.call(resourceName, methodName, paramsMap);
98 Set<Entry<String,Object>> entrySet = (Set<Entry<String,Object>>)response.entrySet();
99 for (Map.Entry<String, Object> entry : entrySet) {
100 if ("items".equals(entry.getKey())) {
101 List items = (List)entry.getValue();
102 for (Object item : items) {
103 System.out.println(" " + item);
106 System.out.println(entry.getKey() + " = " + entry.getValue());
109 } catch (Exception e){
110 System.out.println (e.getMessage());
111 System.out.println ("\nSet up a new call");
113 } catch (Exception e) {
114 System.out.println (e.getMessage());
115 System.out.println ("\nSet up a new call");