2
|
1 package begin;
|
|
2
|
6
|
3 import java.net.URI;
|
|
4
|
2
|
5 import javax.ws.rs.core.MediaType;
|
|
6
|
12
|
7 import org.json.JSONException;
|
|
8 import org.json.JSONObject;
|
|
9
|
8
|
10
|
2
|
11 import com.sun.jersey.api.client.Client;
|
12
|
12 import com.sun.jersey.api.client.ClientHandlerException;
|
2
|
13 import com.sun.jersey.api.client.ClientResponse;
|
12
|
14 import com.sun.jersey.api.client.UniformInterfaceException;
|
2
|
15 import com.sun.jersey.api.client.WebResource;
|
|
16
|
|
17 public class MakeNode {
|
6
|
18 final static String SERVER_ROOT_URI = "http://localhost:7474";
|
|
19 final static String nodeEntryPointUri = "http://localhost:7474/db/data/node";
|
12
|
20 final static String nodeIndexEntryPointUri = "http://localhost:7474/db/data/index/node";
|
6
|
21
|
|
22 public static URI createNode() {
|
|
23 WebResource resource = Client.create().resource(nodeEntryPointUri);
|
|
24 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class);
|
|
25 System.out.println(String.format(
|
|
26 "POST to [%s], status code [%d], location header [%s]",
|
|
27 nodeEntryPointUri, response.getStatus(), response.getLocation()
|
|
28 .toString()));
|
|
29 return response.getLocation();
|
|
30 }
|
2
|
31
|
12
|
32 public static ClientResponse getNode(String nodeUri) {
|
6
|
33 WebResource resource = Client.create().resource(nodeUri);
|
|
34 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
|
|
35 System.out.println(String.format("GET on [%s], status code [%d]",nodeUri, response.getStatus() ));
|
|
36 return response;
|
|
37 }
|
|
38
|
|
39 public static void setProperty(String uri,String propertyName,String propertyValue) {
|
|
40 String propertyUri = uri + "/properties/" + propertyName;
|
|
41 WebResource resource = Client.create().resource(propertyUri); // http://localhost:7474/db/data/node/(node_id)/properties/(property_name)
|
|
42 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
|
|
43 .type(MediaType.APPLICATION_JSON)
|
|
44 .entity(toJsonStringLiteral(propertyValue))
|
|
45 .put(ClientResponse.class);
|
|
46 System.out.println(String.format("PUT to [%s], status code [%d]",propertyUri, response.getStatus()));
|
|
47 }
|
|
48
|
|
49 public static void addProperty(URI nodeUri, String propertyName,
|
|
50 String propertyValue) {
|
|
51 setProperty(nodeUri.toString(), propertyName, propertyValue);
|
|
52 }
|
|
53
|
|
54 private static String toJsonStringLiteral(String str) {
|
|
55 return "\"" + str + "\"";
|
|
56 }
|
|
57
|
12
|
58 public static void createRelationship(String uri1, String uri2, String typeValue) throws JSONException {
|
6
|
59 String relationshipUri = uri1 + "/relationships";
|
|
60 WebResource resource = Client.create().resource(relationshipUri);
|
8
|
61 JSONObject obj = new JSONObject();
|
|
62 obj.put("to",uri2);
|
|
63 obj.put("type", typeValue);
|
|
64
|
6
|
65 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
|
8
|
66 .entity(obj.toString())
|
6
|
67 .post(ClientResponse.class);
|
|
68
|
|
69 System.out.println(String.format("POST to [%s], status code [%d]",relationshipUri , response.getStatus()));
|
|
70 }
|
|
71
|
12
|
72 public static void deleteNode(String uri) {
|
|
73 WebResource resource = Client.create().resource(uri);
|
|
74 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class);
|
|
75 System.out.println(String.format(
|
|
76 "DELETE to [%s], status code [%d]", uri, response.getStatus()));
|
|
77 }
|
|
78
|
|
79 public static URI createIndexNode(String indexName) throws JSONException {
|
|
80 WebResource resource = Client.create().resource(nodeIndexEntryPointUri);
|
|
81 JSONObject obj = new JSONObject();
|
|
82 obj.put("name",indexName);
|
|
83 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
|
|
84 .type(MediaType.APPLICATION_JSON)
|
|
85 .entity(obj.toString())
|
|
86 .post(ClientResponse.class);
|
|
87 System.out.println(String.format(
|
|
88 "POST to [%s], status code [%d], location header [%s]",
|
|
89 nodeIndexEntryPointUri, response.getStatus(), response.getLocation()
|
|
90 .toString()));
|
|
91 return response.getLocation();
|
|
92 }
|
|
93
|
|
94 public static URI addNodeToIndex(String indexUri, String value, String uri, String key) throws JSONException {
|
|
95 WebResource resource = Client.create().resource(indexUri);
|
|
96 JSONObject obj = new JSONObject();
|
|
97 obj.put("value", value);
|
|
98 obj.put("uri",uri);
|
|
99 obj.put("key",key);
|
|
100
|
|
101 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
|
|
102 .type(MediaType.APPLICATION_JSON)
|
|
103 .entity(obj.toString())
|
|
104 .post(ClientResponse.class);
|
|
105 System.out.println(String.format(
|
|
106 "POST to [%s], status code [%d], location header [%s]",
|
|
107 nodeIndexEntryPointUri, response.getStatus(), response.getLocation()
|
|
108 .toString()));
|
|
109 return response.getLocation();
|
|
110 }
|
|
111
|
|
112 public static void main(String[] args) throws ClientHandlerException, UniformInterfaceException, JSONException {
|
2
|
113
|
6
|
114 final String uri1 = "http://localhost:7474/db/data/node/1";
|
|
115 final String uri2 = "http://localhost:7474/db/data/node/2";
|
12
|
116
|
|
117 final String indexUri = "http://localhost:7474/db/data/index/node/hogehoge";
|
|
118 addNodeToIndex(indexUri, "test_value", uri1, "test_key");
|
6
|
119
|
12
|
120 ClientResponse response = getNode(uri1);
|
|
121 // System.out.println(response.getEntity(String.class));
|
|
122 JSONObject obj = new JSONObject(response.getEntity(String.class));
|
|
123 System.out.println(obj.toString());
|
|
124
|
|
125
|
|
126 /*
|
|
127
|
8
|
128 getNode(uri2);
|
|
129 createRelationship(uri1, uri2, "RELATED_TO");
|
12
|
130 */
|
6
|
131
|
2
|
132
|
6
|
133 /*
|
|
134 URI firstNode = createNode();
|
|
135 addProperty(firstNode, "name", "Joe Strummer");
|
|
136 */
|
|
137 /*
|
|
138 setProperty(uri1, "name", "Jason Kidd");
|
|
139 setProperty(uri2, "name", "Magic");
|
|
140 */
|
|
141
|
|
142 }
|
|
143
|
|
144 public static void testCreateNodeWithProperty() {
|
|
145 WebResource resource = Client.create().resource(nodeEntryPointUri);
|
|
146 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
|
|
147 .type(MediaType.APPLICATION_JSON).entity("{ \"foo\" : \"hogehoge\" }"/* Empty Node */)
|
|
148 .post(ClientResponse.class);
|
|
149 System.out.println(String.format(
|
|
150 "POST to [%s], status code [%d], location header [%s]",
|
|
151 nodeEntryPointUri, response.getStatus(), response.getLocation()
|
|
152 .toString()));
|
2
|
153 }
|
|
154
|
6
|
155
|
|
156
|
|
157
|
2
|
158 }
|