annotate test/RequestTest.java @ 15:0adbec4c7091

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