15
|
1 import javax.ws.rs.core.MediaType;
|
|
2
|
16
|
3 import models.NodeModel;
|
|
4
|
15
|
5 import org.json.JSONException;
|
|
6 import org.json.JSONObject;
|
|
7
|
|
8 import com.sun.jersey.api.client.Client;
|
|
9 import com.sun.jersey.api.client.ClientResponse;
|
|
10 import com.sun.jersey.api.client.WebResource;
|
|
11
|
|
12
|
|
13 public class RequestTest {
|
|
14 final static String SERVER_ROOT_URI = "http://localhost:9000";
|
|
15
|
|
16 public static void main(String[] args) throws JSONException {
|
|
17
|
|
18 createUser("taro");
|
16
|
19 /*
|
15
|
20 getUser("taro");
|
|
21 getUserInfo("taro","requests/");
|
|
22 getUserInfo("taro","claims/");
|
|
23 getUserInfo("taro","consensus/");
|
16
|
24 */
|
|
25 createClaim("taro");
|
15
|
26
|
|
27
|
|
28
|
|
29 }
|
|
30
|
16
|
31 public static void createClaim(String author) throws JSONException {
|
15
|
32 JSONObject toulmin = new JSONObject();
|
16
|
33 toulmin.put(NodeModel.TITLE, "アプリでGraphDBを利用する。");
|
|
34 toulmin.put(NodeModel.CONTENTS, "最近話題のデータベースとしてGraphDBがある。我々のアプリでは、新しい技術のためにもGraphDBを利用したい。");
|
|
35 toulmin.put(NodeModel.QUALIFIER, "絶対");
|
|
36 toulmin.put(NodeModel.WARRANT,"GraphDBの実用例 etc...");
|
|
37 toulmin.put(NodeModel.BACKING, "GraphDBの最新動向 etc...");
|
|
38 toulmin.put(NodeModel.DATA,"GraphDBの実用例 etc...");
|
|
39 toulmin.put(NodeModel.REBUTTLE,"");
|
|
40
|
15
|
41 JSONObject jobj = new JSONObject();
|
16
|
42 jobj.put(NodeModel.TOULMIN, toulmin);
|
|
43 jobj.put(NodeModel.AUTHOR, author);
|
|
44 String[] users = {"akifumi","yosiaki"};
|
|
45 jobj.put(NodeModel.USERS,users);
|
|
46 jobj.put(NodeModel.TYPE, "unanimously");
|
|
47
|
|
48 final String uri = SERVER_ROOT_URI + "/claims/create";
|
|
49 WebResource resource = Client.create().resource(uri);
|
|
50 ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON)
|
|
51 .entity(jobj.toString())
|
|
52 .post(ClientResponse.class);
|
|
53 System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus()));
|
|
54 System.out.println(response.getEntity(String.class));
|
|
55
|
15
|
56 }
|
|
57
|
|
58 public static void getUserInfo(String name, String preUri) {
|
|
59 final String uri = SERVER_ROOT_URI + "/users/"+preUri+name;
|
|
60 WebResource resource = Client.create().resource(uri);
|
|
61 ClientResponse response = resource.get(ClientResponse.class);
|
|
62 System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus()));
|
|
63 System.out.println(response.getEntity(String.class));
|
|
64 }
|
|
65
|
|
66 public static void createUser(String name) {
|
|
67 final String uri = SERVER_ROOT_URI + "/users/create/"+name;
|
|
68 WebResource resource = Client.create().resource(uri);
|
|
69 ClientResponse response = resource.put(ClientResponse.class);
|
|
70 System.out.println(String.format("PUT on [%s], status code [%d]", uri, response.getStatus()));
|
|
71 System.out.println(response.getEntity(String.class));
|
|
72
|
|
73 }
|
|
74
|
|
75 public static void getUser(String name) {
|
|
76 final String uri = SERVER_ROOT_URI + "/users/browse/"+name;
|
|
77 WebResource resource = Client.create().resource(uri);
|
|
78 ClientResponse response = resource.get(ClientResponse.class);
|
|
79 System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus()));
|
|
80 System.out.println(response.getEntity(String.class));
|
|
81 }
|
|
82
|
|
83
|
|
84 public static void postName() throws JSONException {
|
|
85 final String uri = SERVER_ROOT_URI + "/hello";
|
|
86 WebResource resource = Client.create().resource(uri);
|
|
87 JSONObject jobj = new JSONObject();
|
|
88 jobj.put("name","taro");
|
|
89 // ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
|
|
90 ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON)
|
|
91 .entity(jobj.toString())
|
|
92 .post(ClientResponse.class);
|
|
93 System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus()));
|
|
94 System.out.println(response.getEntity(String.class));
|
|
95
|
|
96 }
|
|
97
|
|
98
|
|
99
|
|
100 }
|