vayacondios-server 0.1.12 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -0
- data/config/vayacondios.example.yaml +3 -0
- data/config/vayacondios.yaml +3 -0
- data/lib/tasks/spec.rake +2 -0
- data/lib/vayacondios/legacy_switch.rb +38 -0
- data/lib/vayacondios/server/model/itemset_document.rb +18 -10
- data/lib/vayacondios/version.rb +1 -1
- data/pom.xml +22 -2
- data/spec/server/itemset_legacy_spec.rb +320 -0
- data/spec/server/itemset_spec.rb +11 -6
- data/src/main/java/com/infochimps/util/DebugUtil.java +14 -14
- data/src/main/java/com/infochimps/util/HttpHelper.java +17 -17
- data/src/main/java/com/infochimps/vayacondios/ItemSets.java +29 -119
- data/src/main/java/com/infochimps/vayacondios/LinkToVCD.java +18 -0
- data/src/main/java/com/infochimps/vayacondios/MemoryVCDShim.java +84 -0
- data/src/main/java/com/infochimps/vayacondios/Organization.java +15 -2
- data/src/main/java/com/infochimps/vayacondios/StandardVCDLink.java +152 -0
- data/src/main/java/com/infochimps/vayacondios/VCDIntegrationTest.java +1 -1
- data/src/main/java/com/infochimps/vayacondios/VayacondiosClient.java +3 -13
- data/src/test/java/com/infochimps/vayacondios/TestVayacondiosInMemory.java +78 -0
- metadata +11 -4
@@ -33,28 +33,28 @@ public class HttpHelper {
|
|
33
33
|
}
|
34
34
|
|
35
35
|
public static BufferedReader open(Logger log,
|
36
|
-
|
37
|
-
|
36
|
+
String urlString,
|
37
|
+
Charset inputCharset) throws IOException {
|
38
38
|
return getReader(openStream(log, urlString), inputCharset);
|
39
39
|
}
|
40
40
|
|
41
41
|
public static BufferedReader open(Logger log,
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
String urlString,
|
43
|
+
HashMap<String,String> extraHeaders,
|
44
|
+
Charset inputCharset) throws IOException {
|
45
45
|
return getReader(openStream(log, urlString, extraHeaders), inputCharset);
|
46
46
|
|
47
47
|
}
|
48
48
|
|
49
49
|
public static InputStream openStream(Logger log,
|
50
|
-
|
50
|
+
String urlString) throws IOException {
|
51
51
|
HttpURLConnection con = getConnection(urlString, log);
|
52
52
|
return getStream(con, log);
|
53
53
|
}
|
54
54
|
|
55
55
|
public static InputStream openStream(Logger log,
|
56
|
-
|
57
|
-
|
56
|
+
String urlString,
|
57
|
+
HashMap<String,String> extraHeaders) throws IOException {
|
58
58
|
HttpURLConnection con = getConnection(urlString, log);
|
59
59
|
for (Entry<String,String> header : extraHeaders.entrySet())
|
60
60
|
con.setRequestProperty(header.getKey(), header.getValue());
|
@@ -70,8 +70,8 @@ public class HttpHelper {
|
|
70
70
|
}
|
71
71
|
|
72
72
|
HttpURLConnection con = (HttpURLConnection)(USE_CHARLES ?
|
73
|
-
|
74
|
-
|
73
|
+
url.openConnection(DebugUtil.useCharles()) :
|
74
|
+
url.openConnection());
|
75
75
|
|
76
76
|
String userInfo = url.getUserInfo();
|
77
77
|
if (userInfo != null) {
|
@@ -83,7 +83,7 @@ public class HttpHelper {
|
|
83
83
|
}
|
84
84
|
|
85
85
|
private static InputStream getStream(HttpURLConnection con,
|
86
|
-
|
86
|
+
Logger log) throws IOException {
|
87
87
|
InputStream in = null;
|
88
88
|
|
89
89
|
try { in = con.getInputStream(); }
|
@@ -95,12 +95,12 @@ public class HttpHelper {
|
|
95
95
|
|
96
96
|
InputStream errorStream = con.getErrorStream();
|
97
97
|
if (errorStream != null) {
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
98
|
+
BufferedReader r = new BufferedReader(new InputStreamReader(errorStream));
|
99
|
+
try { for (String line; (line = r.readLine()) != null; log.warn(line)); }
|
100
|
+
catch (IOException nested_exc) {
|
101
|
+
log.error("Got an exception in the exception handler: {}", nested_exc);
|
102
|
+
throw e;
|
103
|
+
}
|
104
104
|
}
|
105
105
|
throw e;
|
106
106
|
}
|
@@ -23,6 +23,7 @@ import java.io.OutputStream;
|
|
23
23
|
import java.io.OutputStreamWriter;
|
24
24
|
import java.io.IOException;
|
25
25
|
import java.lang.reflect.Type;
|
26
|
+
import java.lang.reflect.InvocationTargetException;
|
26
27
|
import java.net.HttpURLConnection;
|
27
28
|
import java.util.ArrayList;
|
28
29
|
import java.util.List;
|
@@ -35,12 +36,17 @@ import org.slf4j.Logger;
|
|
35
36
|
* This is the first level of the Vayacondios path-building hierarchy
|
36
37
|
* that is capable of directly manipulating itemsets.
|
37
38
|
*/
|
38
|
-
public class ItemSets extends Organization {
|
39
|
+
public class ItemSets<LinkType extends LinkToVCD> extends Organization {
|
39
40
|
public ItemSets(PathBuilder delegate) { super(delegate); }
|
40
41
|
|
41
|
-
|
42
|
+
/**
|
43
|
+
* @param linkClass for testing purposes. can be used to shim up a
|
44
|
+
* dummy vayacondios session.
|
45
|
+
*/
|
46
|
+
public ItemSets(Organization org, LinkType linkToVCD) {
|
42
47
|
super(org);
|
43
48
|
_org = org;
|
49
|
+
(_vcdLink = linkToVCD).setParent(this);
|
44
50
|
}
|
45
51
|
|
46
52
|
//----------------------------------------------------------------------------
|
@@ -69,44 +75,7 @@ public class ItemSets extends Organization {
|
|
69
75
|
* @return a collection of items
|
70
76
|
*/
|
71
77
|
public List<Item> fetch(String topic, String id) throws IOException {
|
72
|
-
|
73
|
-
try {
|
74
|
-
reader = openUrl(urlString(PATH_COMPONENT, topic, id));
|
75
|
-
} catch (FileNotFoundException ex) {
|
76
|
-
// In the case of a 404, return an empty set.
|
77
|
-
return new ArrayList();
|
78
|
-
}
|
79
|
-
String line = reader.readLine();
|
80
|
-
JsonElement response;
|
81
|
-
JsonElement itemSet;
|
82
|
-
|
83
|
-
ArrayList<Item> result = new ArrayList<Item>();
|
84
|
-
|
85
|
-
// assume Vayacondios response comes in a single line
|
86
|
-
if (line != null &&
|
87
|
-
(response = PARSER.parse(line)).isJsonObject() &&
|
88
|
-
(itemSet = (response.getAsJsonObject().get("contents"))).isJsonArray()) {
|
89
|
-
for (JsonElement elem : itemSet.getAsJsonArray()) {
|
90
|
-
if (!elem.isJsonPrimitive()) {
|
91
|
-
LOG.warn("ignoring non-primitive in itemset: " + elem);
|
92
|
-
continue;
|
93
|
-
}
|
94
|
-
|
95
|
-
JsonPrimitive item = elem.getAsJsonPrimitive();
|
96
|
-
if (item.isBoolean()) result.add(new Item(item.getAsBoolean()));
|
97
|
-
else if (item.isNumber()) result.add(new Item(item.getAsNumber()));
|
98
|
-
else if (item.isString()) result.add(new Item(item.getAsString()));
|
99
|
-
|
100
|
-
else LOG.warn("ignoring unrecognized type in itemset: " + item);
|
101
|
-
}
|
102
|
-
}
|
103
|
-
|
104
|
-
if ((line = reader.readLine()) != null)
|
105
|
-
LOG.warn("expected eof but saw " + line);
|
106
|
-
|
107
|
-
reader.close();
|
108
|
-
|
109
|
-
return result;
|
78
|
+
return _vcdLink.fetch(topic, id);
|
110
79
|
}
|
111
80
|
|
112
81
|
/**
|
@@ -121,8 +90,8 @@ public class ItemSets extends Organization {
|
|
121
90
|
* @param items items whose existence should be ensured in the set.
|
122
91
|
*/
|
123
92
|
public void create(String topic,
|
124
|
-
|
125
|
-
|
93
|
+
String id,
|
94
|
+
List<Item> items) throws IOException {
|
126
95
|
mutate("PUT", topic, id, items);
|
127
96
|
}
|
128
97
|
|
@@ -137,8 +106,8 @@ public class ItemSets extends Organization {
|
|
137
106
|
* @param items items whose absence should be ensured in the set.
|
138
107
|
*/
|
139
108
|
public void remove(String topic,
|
140
|
-
|
141
|
-
|
109
|
+
String id,
|
110
|
+
List<Item> items) throws IOException {
|
142
111
|
mutate("DELETE", topic, id, items);
|
143
112
|
}
|
144
113
|
|
@@ -154,8 +123,8 @@ public class ItemSets extends Organization {
|
|
154
123
|
* @param items items whose existence should be ensured in the set.
|
155
124
|
*/
|
156
125
|
public void update(String topic,
|
157
|
-
|
158
|
-
|
126
|
+
String id,
|
127
|
+
List<Item> items) throws IOException {
|
159
128
|
mutate("PATCH", topic, id, items);
|
160
129
|
}
|
161
130
|
|
@@ -170,75 +139,16 @@ public class ItemSets extends Organization {
|
|
170
139
|
//----------------------------------------------------------------------------
|
171
140
|
|
172
141
|
protected void mutate(String method,
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
// serialize the items
|
178
|
-
HashMap content = new HashMap();
|
179
|
-
content.put("contents", items);
|
180
|
-
String body = GSON.toJson(content);
|
181
|
-
// connect to our standard path
|
182
|
-
URL url = new URL(urlString(PATH_COMPONENT, topic, id));
|
183
|
-
HttpURLConnection connection = (HttpURLConnection)
|
184
|
-
((Boolean.valueOf(System.getProperty("ics.http.use_charles"))) ?
|
185
|
-
url.openConnection(DebugUtil.useCharles()) : url.openConnection());
|
186
|
-
|
187
|
-
// configure connection
|
188
|
-
connection.setDoOutput(true);
|
189
|
-
|
190
|
-
// NOTE: Uncommenting this (and not calling
|
191
|
-
// connection.getInputStream()) causes Java to hang without
|
192
|
-
// sending its payload.
|
193
|
-
|
194
|
-
// connection.setDoInput(false);
|
195
|
-
|
196
|
-
if (method.equals("DELETE")) {
|
197
|
-
connection.setRequestMethod("PUT");
|
198
|
-
connection.setRequestProperty("X-Method", "DELETE");
|
199
|
-
} else if (method.equals("PATCH")) {
|
200
|
-
connection.setRequestMethod("PUT");
|
201
|
-
connection.setRequestProperty("X-Method", "PATCH");
|
202
|
-
} else connection.setRequestMethod(method);
|
203
|
-
connection.setRequestProperty("Content-Type", "application/json");
|
204
|
-
connection.setRequestProperty("Accept", "*/*");
|
205
|
-
connection.setRequestProperty("Content-Length",
|
206
|
-
Integer.toString(body.getBytes().length));
|
207
|
-
connection.setUseCaches(false);
|
208
|
-
|
209
|
-
LOG.debug("sending: " + body);
|
210
|
-
LOG.debug("via " +
|
211
|
-
connection.getRequestMethod() +
|
212
|
-
" to " +
|
213
|
-
urlString(PATH_COMPONENT, topic, id));
|
214
|
-
|
215
|
-
// connect and write
|
216
|
-
OutputStream os = connection.getOutputStream();
|
217
|
-
os.write(body.getBytes("UTF-8"));
|
218
|
-
os.flush();
|
219
|
-
os.close();
|
220
|
-
|
221
|
-
// ignore reponse
|
222
|
-
InputStream is = connection.getInputStream();
|
223
|
-
|
224
|
-
LOG.trace("ignoring response from Vayacondios.");
|
225
|
-
byte buf[] = new byte[256];
|
226
|
-
while (is.read(buf) != -1);
|
227
|
-
LOG.trace("response ignored.");
|
228
|
-
is.close();
|
229
|
-
|
230
|
-
// fin.
|
231
|
-
connection.disconnect();
|
142
|
+
String topic,
|
143
|
+
String id,
|
144
|
+
List<Item> items) throws IOException {
|
145
|
+
_vcdLink.mutate(method, topic, id, items);
|
232
146
|
}
|
233
147
|
|
148
|
+
private LinkType _vcdLink;
|
234
149
|
private Organization _org;
|
235
150
|
|
236
|
-
private static final
|
237
|
-
private static final Logger LOG = getLogger();
|
238
|
-
private static final String PATH_COMPONENT = "itemset";
|
239
|
-
private static final Gson GSON = new GsonBuilder().
|
240
|
-
registerTypeAdapter(Item.class, new Item.Serializer()).
|
241
|
-
create();
|
151
|
+
private static final Logger LOG = getLogger();
|
242
152
|
|
243
153
|
//============================================================================
|
244
154
|
// Topic
|
@@ -393,10 +303,10 @@ public class ItemSets extends Organization {
|
|
393
303
|
public static class Item {
|
394
304
|
static class Serializer implements JsonSerializer {
|
395
305
|
public JsonElement serialize(Object item,
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
306
|
+
Type typeOfId,
|
307
|
+
JsonSerializationContext context) {
|
308
|
+
return GSON.toJsonTree(Item.class.isAssignableFrom(item.getClass()) ?
|
309
|
+
((Item)item).getObject() : item);
|
400
310
|
}
|
401
311
|
private static final Gson GSON = new Gson();
|
402
312
|
private static final Logger LOG = getLogger();
|
@@ -419,19 +329,19 @@ public class ItemSets extends Organization {
|
|
419
329
|
|
420
330
|
public Boolean getAsBoolean() {
|
421
331
|
if (_type != TYPE.BOOLEAN)
|
422
|
-
|
332
|
+
throw new ClassCastException("item is not a boolean");
|
423
333
|
return (Boolean)_item;
|
424
334
|
}
|
425
335
|
|
426
336
|
public Number getAsNumber() {
|
427
337
|
if (_type != TYPE.NUMBER)
|
428
|
-
|
338
|
+
throw new ClassCastException("item is not a number");
|
429
339
|
return (Number)_item;
|
430
340
|
}
|
431
341
|
|
432
342
|
public String getAsString() {
|
433
343
|
if (_type != TYPE.STRING)
|
434
|
-
|
344
|
+
throw new ClassCastException("item is not a string");
|
435
345
|
return (String)_item;
|
436
346
|
}
|
437
347
|
|
@@ -449,7 +359,7 @@ public class ItemSets extends Organization {
|
|
449
359
|
|
450
360
|
public boolean equals(Object other) {
|
451
361
|
return (Item.class.isAssignableFrom(other.getClass())) ?
|
452
|
-
|
362
|
+
_item.equals(((Item)other).getObject()) : _item.equals(other);
|
453
363
|
}
|
454
364
|
|
455
365
|
//--------------------------------------------------------------------------
|
@@ -0,0 +1,18 @@
|
|
1
|
+
package com.infochimps.vayacondios;
|
2
|
+
|
3
|
+
import java.util.List;
|
4
|
+
import java.io.IOException;
|
5
|
+
import static com.infochimps.vayacondios.ItemSets.Item;
|
6
|
+
|
7
|
+
public abstract class LinkToVCD {
|
8
|
+
public abstract List<Item> fetch(String topic, String id) throws IOException;
|
9
|
+
public abstract void mutate(String method,
|
10
|
+
String topic,
|
11
|
+
String id,
|
12
|
+
List<Item> items) throws IOException;
|
13
|
+
|
14
|
+
protected ItemSets getParent() { return _parent; }
|
15
|
+
public void setParent(ItemSets parent) { _parent = parent; }
|
16
|
+
|
17
|
+
private ItemSets _parent;
|
18
|
+
}
|
@@ -0,0 +1,84 @@
|
|
1
|
+
package com.infochimps.vayacondios;
|
2
|
+
|
3
|
+
import com.google.gson.Gson;
|
4
|
+
import com.google.gson.GsonBuilder;
|
5
|
+
import com.google.gson.JsonElement;
|
6
|
+
import com.google.gson.JsonIOException;
|
7
|
+
import com.google.gson.JsonObject;
|
8
|
+
import com.google.gson.JsonParser;
|
9
|
+
import com.google.gson.JsonPrimitive;
|
10
|
+
import com.google.gson.JsonParseException;
|
11
|
+
import com.google.gson.JsonSerializer;
|
12
|
+
import com.google.gson.JsonSerializationContext;
|
13
|
+
import com.google.gson.JsonSyntaxException;
|
14
|
+
|
15
|
+
import java.io.BufferedReader;
|
16
|
+
import java.io.FileNotFoundException;
|
17
|
+
import java.io.InputStream;
|
18
|
+
import java.io.OutputStream;
|
19
|
+
import java.io.IOException;
|
20
|
+
import java.net.HttpURLConnection;
|
21
|
+
import java.nio.charset.Charset;
|
22
|
+
import java.util.ArrayList;
|
23
|
+
import java.util.HashMap;
|
24
|
+
import java.util.List;
|
25
|
+
import java.util.Map;
|
26
|
+
|
27
|
+
import java.net.URL;
|
28
|
+
|
29
|
+
import org.slf4j.Logger;
|
30
|
+
|
31
|
+
import static com.infochimps.util.CurrentClass.getLogger;
|
32
|
+
import static com.infochimps.vayacondios.ItemSets.Item;
|
33
|
+
|
34
|
+
public class MemoryVCDShim extends LinkToVCD {
|
35
|
+
public MemoryVCDShim() {
|
36
|
+
_topics = new HashMap<String, Map<String, List<Item>>>();
|
37
|
+
}
|
38
|
+
|
39
|
+
public List<Item> fetch(String topicName, String id) {
|
40
|
+
Map<String, List<Item>> topic = _topics.get(topicName);
|
41
|
+
List<Item> result;
|
42
|
+
|
43
|
+
LOG.trace("topics before fetch: {}", _topics);
|
44
|
+
|
45
|
+
if (topic == null || (result = topic.get(id)) == null) {
|
46
|
+
LOG.debug("couldn't find {}.{}. returning empty list", topicName, id);
|
47
|
+
return new ArrayList<Item>();
|
48
|
+
} else {
|
49
|
+
LOG.debug("returning {}.{} => {}", topicName, id, result);
|
50
|
+
return result;
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
public void mutate(String method,
|
55
|
+
String topicName,
|
56
|
+
String id,
|
57
|
+
List<Item> items) {
|
58
|
+
LOG.trace("topics before mutate: {}", _topics);
|
59
|
+
|
60
|
+
Map<String, List<Item>> topic = _topics.get(topicName);
|
61
|
+
if (topic == null) {
|
62
|
+
LOG.debug("creating topic {}.", topicName);
|
63
|
+
_topics.put(topicName, topic = new HashMap<String, List<Item>>());
|
64
|
+
}
|
65
|
+
|
66
|
+
if (method.equals("DELETE")) {
|
67
|
+
LOG.trace("removing {} from {}.{}", items, topicName, id);
|
68
|
+
topic.get(id).removeAll(items);
|
69
|
+
}
|
70
|
+
else if (method.equals("PATCH")) {
|
71
|
+
LOG.trace("adding {} to {}.{}", items, topicName, id);
|
72
|
+
topic.get(id).addAll(items);
|
73
|
+
}
|
74
|
+
else if (method.equals("PUT")) {
|
75
|
+
LOG.trace("creating {}.{} with {}", topicName, id, items);
|
76
|
+
topic.put(id, items);
|
77
|
+
}
|
78
|
+
|
79
|
+
LOG.trace("topics after mutate: {}", _topics);
|
80
|
+
}
|
81
|
+
|
82
|
+
private Map<String, Map<String, List<Item>>> _topics;
|
83
|
+
private static final Logger LOG = getLogger();
|
84
|
+
}
|
@@ -6,7 +6,8 @@ import java.util.Collection;
|
|
6
6
|
* An organization is the last commmon class in the Vayacondios
|
7
7
|
* hierarchy between the itemset and stash interface.
|
8
8
|
*/
|
9
|
-
public class Organization extends
|
9
|
+
public class Organization<LinkType extends LinkToVCD>
|
10
|
+
extends VayacondiosClient {
|
10
11
|
public Organization(PathBuilder delegate) { super(delegate); }
|
11
12
|
|
12
13
|
public Organization(VayacondiosClient server, String organization) {
|
@@ -22,7 +23,19 @@ public class Organization extends VayacondiosClient {
|
|
22
23
|
/**
|
23
24
|
* @return new ItemSets path builder for this organization
|
24
25
|
*/
|
25
|
-
public ItemSets itemsets() {
|
26
|
+
public ItemSets<LinkType> itemsets() {
|
27
|
+
return itemsets(new StandardVCDLink());
|
28
|
+
}
|
29
|
+
|
30
|
+
|
31
|
+
/**
|
32
|
+
* @param linkType link type to use to
|
33
|
+
*
|
34
|
+
* @return new ItemSets path builder for this organization
|
35
|
+
*/
|
36
|
+
public ItemSets<LinkType> itemsets(LinkToVCD linkToVCD) {
|
37
|
+
return new ItemSets(this, linkToVCD);
|
38
|
+
}
|
26
39
|
|
27
40
|
//----------------------------------------------------------------------------
|
28
41
|
// API HTTP path components
|
@@ -0,0 +1,152 @@
|
|
1
|
+
package com.infochimps.vayacondios;
|
2
|
+
|
3
|
+
import com.google.gson.Gson;
|
4
|
+
import com.google.gson.GsonBuilder;
|
5
|
+
import com.google.gson.JsonElement;
|
6
|
+
import com.google.gson.JsonIOException;
|
7
|
+
import com.google.gson.JsonObject;
|
8
|
+
import com.google.gson.JsonParser;
|
9
|
+
import com.google.gson.JsonPrimitive;
|
10
|
+
import com.google.gson.JsonParseException;
|
11
|
+
import com.google.gson.JsonSerializer;
|
12
|
+
import com.google.gson.JsonSerializationContext;
|
13
|
+
import com.google.gson.JsonSyntaxException;
|
14
|
+
|
15
|
+
|
16
|
+
import java.io.BufferedReader;
|
17
|
+
import java.io.FileNotFoundException;
|
18
|
+
import java.io.InputStream;
|
19
|
+
import java.io.OutputStream;
|
20
|
+
import java.io.IOException;
|
21
|
+
import java.net.HttpURLConnection;
|
22
|
+
import java.nio.charset.Charset;
|
23
|
+
import java.util.ArrayList;
|
24
|
+
import java.util.HashMap;
|
25
|
+
import java.util.List;
|
26
|
+
|
27
|
+
import java.net.URL;
|
28
|
+
|
29
|
+
import org.slf4j.Logger;
|
30
|
+
|
31
|
+
import com.infochimps.util.DebugUtil;
|
32
|
+
import com.infochimps.util.HttpHelper;
|
33
|
+
import static com.infochimps.util.CurrentClass.getLogger;
|
34
|
+
import static com.infochimps.vayacondios.ItemSets.Item;
|
35
|
+
|
36
|
+
public class StandardVCDLink extends LinkToVCD {
|
37
|
+
public List<Item> fetch(String topic, String id) throws IOException {
|
38
|
+
BufferedReader reader = null;
|
39
|
+
try {
|
40
|
+
reader = openUrl(getParent().urlString(PATH_COMPONENT, topic, id));
|
41
|
+
} catch (FileNotFoundException ex) {
|
42
|
+
// In the case of a 404, return an empty set.
|
43
|
+
return new ArrayList();
|
44
|
+
}
|
45
|
+
String line = reader.readLine();
|
46
|
+
JsonElement response;
|
47
|
+
JsonElement itemSet;
|
48
|
+
|
49
|
+
ArrayList<Item> result = new ArrayList<Item>();
|
50
|
+
|
51
|
+
// assume Vayacondios response comes in a single line
|
52
|
+
if (line != null &&
|
53
|
+
(response = PARSER.parse(line)).isJsonObject() &&
|
54
|
+
(itemSet = (response.getAsJsonObject().get("contents"))).isJsonArray()) {
|
55
|
+
for (JsonElement elem : itemSet.getAsJsonArray()) {
|
56
|
+
if (!elem.isJsonPrimitive()) {
|
57
|
+
LOG.warn("ignoring non-primitive in itemset: " + elem);
|
58
|
+
continue;
|
59
|
+
}
|
60
|
+
|
61
|
+
JsonPrimitive item = elem.getAsJsonPrimitive();
|
62
|
+
if (item.isBoolean()) result.add(new Item(item.getAsBoolean()));
|
63
|
+
else if (item.isNumber()) result.add(new Item(item.getAsNumber()));
|
64
|
+
else if (item.isString()) result.add(new Item(item.getAsString()));
|
65
|
+
|
66
|
+
else LOG.warn("ignoring unrecognized type in itemset: " + item);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
if ((line = reader.readLine()) != null)
|
71
|
+
LOG.warn("expected eof but saw " + line);
|
72
|
+
|
73
|
+
reader.close();
|
74
|
+
|
75
|
+
return result;
|
76
|
+
}
|
77
|
+
|
78
|
+
public void mutate(String method,
|
79
|
+
String topic,
|
80
|
+
String id,
|
81
|
+
List<Item> items) throws IOException {
|
82
|
+
|
83
|
+
// serialize the items
|
84
|
+
HashMap content = new HashMap();
|
85
|
+
content.put("contents", items);
|
86
|
+
String body = GSON.toJson(content);
|
87
|
+
// connect to our standard path
|
88
|
+
URL url = new URL(getParent().urlString(PATH_COMPONENT, topic, id));
|
89
|
+
HttpURLConnection connection = (HttpURLConnection)
|
90
|
+
((Boolean.valueOf(System.getProperty("ics.http.use_charles"))) ?
|
91
|
+
url.openConnection(DebugUtil.useCharles()) : url.openConnection());
|
92
|
+
|
93
|
+
// configure connection
|
94
|
+
connection.setDoOutput(true);
|
95
|
+
|
96
|
+
// NOTE: Uncommenting this (and not calling
|
97
|
+
// connection.getInputStream()) causes Java to hang without
|
98
|
+
// sending its payload.
|
99
|
+
|
100
|
+
// connection.setDoInput(false);
|
101
|
+
|
102
|
+
if (method.equals("DELETE")) {
|
103
|
+
connection.setRequestMethod("PUT");
|
104
|
+
connection.setRequestProperty("X-Method", "DELETE");
|
105
|
+
} else if (method.equals("PATCH")) {
|
106
|
+
connection.setRequestMethod("PUT");
|
107
|
+
connection.setRequestProperty("X-Method", "PATCH");
|
108
|
+
} else connection.setRequestMethod(method);
|
109
|
+
connection.setRequestProperty("Content-Type", "application/json");
|
110
|
+
connection.setRequestProperty("Accept", "*/*");
|
111
|
+
connection.setRequestProperty("Content-Length",
|
112
|
+
Integer.toString(body.getBytes().length));
|
113
|
+
connection.setUseCaches(false);
|
114
|
+
|
115
|
+
LOG.debug("sending: " + body);
|
116
|
+
LOG.debug("via " +
|
117
|
+
connection.getRequestMethod() +
|
118
|
+
" to " +
|
119
|
+
getParent().urlString(PATH_COMPONENT, topic, id));
|
120
|
+
|
121
|
+
// connect and write
|
122
|
+
OutputStream os = connection.getOutputStream();
|
123
|
+
os.write(body.getBytes("UTF-8"));
|
124
|
+
os.flush();
|
125
|
+
os.close();
|
126
|
+
|
127
|
+
// ignore reponse
|
128
|
+
InputStream is = connection.getInputStream();
|
129
|
+
|
130
|
+
LOG.trace("ignoring response from Vayacondios.");
|
131
|
+
byte buf[] = new byte[256];
|
132
|
+
while (is.read(buf) != -1);
|
133
|
+
LOG.trace("response ignored.");
|
134
|
+
is.close();
|
135
|
+
|
136
|
+
// fin.
|
137
|
+
connection.disconnect();
|
138
|
+
}
|
139
|
+
|
140
|
+
private BufferedReader openUrl(String urlString) throws IOException {
|
141
|
+
HashMap headers = new HashMap();
|
142
|
+
headers.put("Accept", "*/*");
|
143
|
+
return HttpHelper.open(LOG, urlString, headers, Charset.forName("UTF-8"));
|
144
|
+
}
|
145
|
+
|
146
|
+
private static final Gson GSON = new GsonBuilder().
|
147
|
+
registerTypeAdapter(Item.class, new Item.Serializer()).
|
148
|
+
create();
|
149
|
+
private static final JsonParser PARSER = new JsonParser();
|
150
|
+
private static final Logger LOG = getLogger();
|
151
|
+
private static final String PATH_COMPONENT = "itemset";
|
152
|
+
}
|
@@ -76,7 +76,7 @@ public class VCDIntegrationTest {
|
|
76
76
|
|
77
77
|
public static void main(String argv[]) {
|
78
78
|
System.out.println("*** If Vayacondios is not running on port " + VCD_PORT + ", " +
|
79
|
-
|
79
|
+
"this will fail. ***");
|
80
80
|
System.out.println("Running Vayacondios integration test...");
|
81
81
|
|
82
82
|
try {
|
@@ -45,9 +45,9 @@ public class VayacondiosClient extends PathBuilder {
|
|
45
45
|
//----------------------------------------------------------------------------
|
46
46
|
|
47
47
|
protected String urlString(String organization,
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
String type,
|
49
|
+
String topic,
|
50
|
+
String id) {
|
51
51
|
return new StringBuilder().
|
52
52
|
append("http://").
|
53
53
|
append(getServerName()).
|
@@ -67,16 +67,6 @@ public class VayacondiosClient extends PathBuilder {
|
|
67
67
|
protected int getPort() { return _port; }
|
68
68
|
protected String getServerName() { return _serverName; }
|
69
69
|
|
70
|
-
//----------------------------------------------------------------------------
|
71
|
-
// private methods
|
72
|
-
//----------------------------------------------------------------------------
|
73
|
-
|
74
|
-
protected BufferedReader openUrl(String urlString) throws IOException {
|
75
|
-
HashMap headers = new HashMap();
|
76
|
-
headers.put("Accept", "*/*");
|
77
|
-
return HttpHelper.open(LOG, urlString, headers, Charset.forName("UTF-8"));
|
78
|
-
}
|
79
|
-
|
80
70
|
//----------------------------------------------------------------------------
|
81
71
|
// fields
|
82
72
|
//----------------------------------------------------------------------------
|