Rhizome Java API: list bundles new since token

This commit is contained in:
Andrew Bettison 2014-07-12 22:49:00 +09:30
parent 606f087dd5
commit 4c6612a61e
6 changed files with 115 additions and 7 deletions

View File

@ -81,6 +81,13 @@ public class ServalDClient implements ServalDHttpConnectionFactory
return list;
}
public RhizomeBundleList rhizomeListBundlesSince(String token) throws ServalDInterfaceException, IOException
{
RhizomeBundleList list = new RhizomeBundleList(this, token);
list.connect();
return list;
}
public RhizomeManifestBundle rhizomeManifest(BundleId bid) throws ServalDInterfaceException, IOException
{
return RhizomeCommon.rhizomeManifest(this, bid);

View File

@ -41,9 +41,15 @@ public class RhizomeBundleList {
private HttpURLConnection httpConnection;
private JSONTokeniser json;
private JSONTableScanner table;
private String sinceToken;
int rowCount;
public RhizomeBundleList(ServalDHttpConnectionFactory connector)
{
this(connector, null);
}
public RhizomeBundleList(ServalDHttpConnectionFactory connector, String since_token)
{
this.httpConnector = connector;
this.table = new JSONTableScanner()
@ -61,6 +67,7 @@ public class RhizomeBundleList {
.addColumn("sender", SubscriberId.class, JSONTokeniser.Narrow.ALLOW_NULL)
.addColumn("recipient", SubscriberId.class, JSONTokeniser.Narrow.ALLOW_NULL)
.addColumn("name", String.class);
this.sinceToken = since_token;
}
public boolean isConnected()
@ -72,7 +79,10 @@ public class RhizomeBundleList {
{
try {
rowCount = 0;
if (this.sinceToken == null)
httpConnection = httpConnector.newServalDHttpConnection("/restful/rhizome/bundlelist.json");
else
httpConnection = httpConnector.newServalDHttpConnection("/restful/rhizome/newsince/" + this.sinceToken + "/bundlelist.json");
httpConnection.connect();
json = RhizomeCommon.receiveRestfulResponse(httpConnection, HttpURLConnection.HTTP_OK);
json.consume(JSONTokeniser.Token.START_OBJECT);

View File

@ -85,6 +85,32 @@ public class Rhizome {
System.exit(0);
}
static void rhizome_list_newsince(String token) throws ServalDInterfaceException, IOException, InterruptedException
{
System.err.println("token=" + token);
ServalDClient client = new ServerControl().getRestfulClient();
RhizomeBundleList list = null;
try {
list = client.rhizomeListBundlesSince(token);
RhizomeListBundle bundle;
while ((bundle = list.nextBundle()) != null) {
System.out.println(
"_token=" + bundle.token +
", _rowId=" + bundle.rowId +
", _insertTime=" + bundle.insertTime +
", _author=" + bundle.author +
", _fromHere=" + bundle.fromHere +
", " + manifestFields(bundle.manifest, ", ")
);
}
}
finally {
if (list != null)
list.close();
}
System.exit(0);
}
static void rhizome_manifest(BundleId bid, String dstpath) throws ServalDInterfaceException, IOException, InterruptedException
{
ServalDClient client = new ServerControl().getRestfulClient();
@ -244,6 +270,8 @@ public class Rhizome {
try {
if (methodName.equals("rhizome-list"))
rhizome_list();
else if (methodName.equals("rhizome-list-newsince"))
rhizome_list_newsince(args[1]);
else if (methodName.equals("rhizome-manifest"))
rhizome_manifest(new BundleId(args[1]), args[2]);
else if (methodName.equals("rhizome-payload-raw"))

View File

@ -45,14 +45,23 @@ _executeJava() {
*) break;;
esac
done
"$func" "${opts[@]}" --core-backtrace java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" "$@"
"$func" "${opts[@]}" java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" "$@"
}
_run() {
tfw_log "$@"
"$@"
}
runJava() {
_executeJava _run "$@"
}
executeJava() {
_executeJava execute "$@"
_executeJava execute --core-backtrace "$@"
}
executeJavaOk() {
_executeJava executeOk "$@"
_executeJava executeOk --core-backtrace "$@"
}

View File

@ -146,6 +146,60 @@ test_RhizomeList() {
done
}
doc_RhizomeListNewSince="Java API list Rhizome bundles since token"
setup_RhizomeListNewSince() {
set_extra_config() {
executeOk_servald config set api.restful.newsince_timeout 60s \
set api.restful.newsince_poll_ms 500
}
setup
rhizome_add_bundles $SIDA1 0 5
executeJavaOk org.servalproject.test.Rhizome rhizome-list
assertStdoutLineCount == 6
unset_vars_with_prefix XX_
unpack_vars XX_ "$(sed -n -e 1p "$TFWSTDOUT")"
token="$XX__token"
assert [ -n "$token" ]
}
test_RhizomeListNewSince() {
rhizome_list_newsince() {
runJava org.servalproject.test.Rhizome rhizome-list-newsince "$token" >newsince$i
}
for i in 1 2 3; do
fork %list$i rhizome_list_newsince
done
wait_until [ -e newsince1 -a -e newsince2 -a -e newsince3 ]
rhizome_add_bundles $SIDA1 6 10
for i in 1 2 3; do
wait_until --timeout=10 grep "${BID[10]}" newsince$i
done
fork_terminate_all
fork_wait_all
for i in 1 2 3; do
tfw_preserve newsince$i
for ((n = 0; n <= 5; ++n)); do
assertGrep --matches=0 newsince$i "\<id=${BID[$n]}\>"
done
for ((n = 6; n <= 10; ++n)); do
assertGrep --matches=1 newsince$i "\<id=${BID[$n]}\>"
unset_vars_with_prefix XX_
unpack_vars XX_ "$(sed -n -e "/\bid=${BID[$n]}\b/p" newsince$i)"
assert [ "$XX_id" = "${BID[$n]}" ]
assert [ "$XX_version" = "${VERSION[$n]}" ]
assert [ "$XX_filesize" = "${SIZE[$n]}" ]
assert [ "$XX_filehash" = "${HASH[$n]}" ]
assert [ "$XX_date" = "${DATE[$n]}" ]
assert [ "$XX_service" = "file" ]
assert [ "$XX_name" = "file$n" ]
assert [ "$XX__rowId" = "${ROWID[$n]}" ]
assert [ "$XX__fromHere" = "1" ]
assert [ "$XX__author" = "$SIDA1" ]
assert [ "$XX__insertTime" = "${INSERTTIME[$n]}" ]
assert [ -n "$XX__token" ]
done
done
}
assert_metadata() {
local n=$1
assertStdoutGrep --matches=1 "^id=${BID[$n]}$CR\$"

View File

@ -159,8 +159,8 @@ test_RhizomeList() {
done
}
doc_RhizomeNewSince="HTTP RESTful list Rhizome bundles since token as JSON"
setup_RhizomeNewSince() {
doc_RhizomeListNewSince="HTTP RESTful list Rhizome bundles since token as JSON"
setup_RhizomeListNewSince() {
set_extra_config() {
executeOk_servald config set api.restful.newsince_timeout 60s \
set api.restful.newsince_poll_ms 500
@ -178,7 +178,7 @@ setup_RhizomeNewSince() {
token=$(jq --raw-output '.[0][".token"]' array_of_objects.json)
assert [ -n "$token" ]
}
test_RhizomeNewSince() {
test_RhizomeListNewSince() {
for i in 1 2 3; do
fork %curl$i curl \
--silent --fail --show-error \