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