Rhizome Java API: insert anonymous bundle

This commit is contained in:
Andrew Bettison 2014-07-10 21:19:15 +09:30
parent 04b2a20e54
commit a87133d4d3
4 changed files with 62 additions and 14 deletions

View File

@ -34,6 +34,7 @@ import java.net.HttpURLConnection;
import org.servalproject.codec.Base64; import org.servalproject.codec.Base64;
import org.servalproject.servaldna.SubscriberId; import org.servalproject.servaldna.SubscriberId;
import org.servalproject.servaldna.BundleId; import org.servalproject.servaldna.BundleId;
import org.servalproject.servaldna.BundleSecret;
import org.servalproject.servaldna.ServalDCommand; import org.servalproject.servaldna.ServalDCommand;
import org.servalproject.servaldna.ServalDInterfaceException; import org.servalproject.servaldna.ServalDInterfaceException;
import org.servalproject.servaldna.rhizome.RhizomeCommon; import org.servalproject.servaldna.rhizome.RhizomeCommon;
@ -95,7 +96,7 @@ public class ServalDClient implements ServalDHttpConnectionFactory
return RhizomeCommon.rhizomePayload(this, bid); return RhizomeCommon.rhizomePayload(this, bid);
} }
public RhizomeInsertBundle rhizomeInsert(SubscriberId author, RhizomeIncompleteManifest manifest) public RhizomeInsertBundle rhizomeInsert(SubscriberId author, RhizomeIncompleteManifest manifest, BundleSecret secret)
throws ServalDInterfaceException, throws ServalDInterfaceException,
IOException, IOException,
RhizomeInvalidManifestException, RhizomeInvalidManifestException,
@ -104,10 +105,10 @@ public class ServalDClient implements ServalDHttpConnectionFactory
RhizomeReadOnlyException, RhizomeReadOnlyException,
RhizomeEncryptionException RhizomeEncryptionException
{ {
return RhizomeCommon.rhizomeInsert(this, author, manifest); return RhizomeCommon.rhizomeInsert(this, author, manifest, secret);
} }
public RhizomeInsertBundle rhizomeInsert(SubscriberId author, RhizomeIncompleteManifest manifest, InputStream payloadStream, String fileName) public RhizomeInsertBundle rhizomeInsert(SubscriberId author, RhizomeIncompleteManifest manifest, BundleSecret secret, InputStream payloadStream, String fileName)
throws ServalDInterfaceException, throws ServalDInterfaceException,
IOException, IOException,
RhizomeInvalidManifestException, RhizomeInvalidManifestException,
@ -116,7 +117,7 @@ public class ServalDClient implements ServalDHttpConnectionFactory
RhizomeReadOnlyException, RhizomeReadOnlyException,
RhizomeEncryptionException RhizomeEncryptionException
{ {
return RhizomeCommon.rhizomeInsert(this, author, manifest, payloadStream, fileName); return RhizomeCommon.rhizomeInsert(this, author, manifest, secret, payloadStream, fileName);
} }
public MeshMSConversationList meshmsListConversations(SubscriberId sid) throws ServalDInterfaceException, IOException, MeshMSException public MeshMSConversationList meshmsListConversations(SubscriberId sid) throws ServalDInterfaceException, IOException, MeshMSException

View File

@ -337,7 +337,8 @@ public class RhizomeCommon
public static RhizomeInsertBundle rhizomeInsert(ServalDHttpConnectionFactory connector, public static RhizomeInsertBundle rhizomeInsert(ServalDHttpConnectionFactory connector,
SubscriberId author, SubscriberId author,
RhizomeIncompleteManifest manifest) RhizomeIncompleteManifest manifest,
BundleSecret secret)
throws ServalDInterfaceException, throws ServalDInterfaceException,
IOException, IOException,
RhizomeInvalidManifestException, RhizomeInvalidManifestException,
@ -346,12 +347,13 @@ public class RhizomeCommon
RhizomeReadOnlyException, RhizomeReadOnlyException,
RhizomeEncryptionException RhizomeEncryptionException
{ {
return rhizomeInsert(connector, author, manifest, null, null); return rhizomeInsert(connector, author, manifest, secret, null, null);
} }
public static RhizomeInsertBundle rhizomeInsert(ServalDHttpConnectionFactory connector, public static RhizomeInsertBundle rhizomeInsert(ServalDHttpConnectionFactory connector,
SubscriberId author, SubscriberId author,
RhizomeIncompleteManifest manifest, RhizomeIncompleteManifest manifest,
BundleSecret secret,
InputStream payloadStream, InputStream payloadStream,
String fileName) String fileName)
throws ServalDInterfaceException, throws ServalDInterfaceException,
@ -378,6 +380,14 @@ public class RhizomeCommon
wr.print("\r\n"); wr.print("\r\n");
wr.print(author.toHex()); wr.print(author.toHex());
} }
if (secret != null) {
wr.print("\r\n--" + boundary + "\r\n");
wr.print("Content-Disposition: form-data; name=\"bundle-secret\"\r\n");
wr.print("Content-Type: rhizome/bundle-secret\r\n");
wr.print("Content-Transfer-Encoding: hex\r\n");
wr.print("\r\n");
wr.print(secret.toHex());
}
wr.print("\r\n--" + boundary + "\r\n"); wr.print("\r\n--" + boundary + "\r\n");
wr.print("Content-Disposition: form-data; name=\"manifest\"\r\n"); wr.print("Content-Disposition: form-data; name=\"manifest\"\r\n");
wr.print("Content-Type: rhizome/manifest; format=\"text+binarysig\"\r\n"); wr.print("Content-Type: rhizome/manifest; format=\"text+binarysig\"\r\n");

View File

@ -30,6 +30,7 @@ import org.servalproject.servaldna.ServalDClient;
import org.servalproject.servaldna.ServalDInterfaceException; import org.servalproject.servaldna.ServalDInterfaceException;
import org.servalproject.servaldna.ServerControl; import org.servalproject.servaldna.ServerControl;
import org.servalproject.servaldna.BundleId; import org.servalproject.servaldna.BundleId;
import org.servalproject.servaldna.BundleSecret;
import org.servalproject.servaldna.SubscriberId; import org.servalproject.servaldna.SubscriberId;
import org.servalproject.servaldna.rhizome.RhizomeManifest; import org.servalproject.servaldna.rhizome.RhizomeManifest;
import org.servalproject.servaldna.rhizome.RhizomeIncompleteManifest; import org.servalproject.servaldna.rhizome.RhizomeIncompleteManifest;
@ -183,20 +184,31 @@ public class Rhizome {
System.exit(0); System.exit(0);
} }
static void rhizome_insert(String author, String manifestpath, String payloadPath, String manifestoutpath, String payloadName) static void rhizome_insert( String author,
throws ServalDInterfaceException, IOException, InterruptedException, SubscriberId.InvalidHexException String manifestPath,
String payloadPath,
String manifestoutpath,
String payloadName,
String secretHex)
throws ServalDInterfaceException,
IOException,
InterruptedException,
SubscriberId.InvalidHexException
{ {
ServalDClient client = new ServerControl().getRestfulClient(); ServalDClient client = new ServerControl().getRestfulClient();
try { try {
RhizomeIncompleteManifest manifest = RhizomeIncompleteManifest.fromTextFormat(new FileInputStream(manifestpath)); RhizomeIncompleteManifest manifest = new RhizomeIncompleteManifest();
if (manifestPath != null && manifestPath.length() != 0)
manifest.parseTextFormat(new FileInputStream(manifestPath));
RhizomeInsertBundle bundle; RhizomeInsertBundle bundle;
SubscriberId authorSid = author == null || author.length() == 0 ? null : new SubscriberId(author); SubscriberId authorSid = author == null || author.length() == 0 ? null : new SubscriberId(author);
BundleSecret secret = secretHex == null || secretHex.length() == 0 ? null : new BundleSecret(secretHex);
if (payloadName == null || payloadName.length() == 0) if (payloadName == null || payloadName.length() == 0)
payloadName = new File(payloadPath).getName(); payloadName = new File(payloadPath).getName();
if (payloadPath == null || payloadPath.length() == 0) if (payloadPath == null || payloadPath.length() == 0)
bundle = client.rhizomeInsert(authorSid, manifest); bundle = client.rhizomeInsert(authorSid, manifest, secret);
else else
bundle = client.rhizomeInsert(authorSid, manifest, new FileInputStream(payloadPath), payloadName); bundle = client.rhizomeInsert(authorSid, manifest, secret, new FileInputStream(payloadPath), payloadName);
System.out.println( System.out.println(
"_status=" + bundle.status + "\n" + "_status=" + bundle.status + "\n" +
(bundle.rowId == null ? "" : "_rowId=" + bundle.rowId + "\n") + (bundle.rowId == null ? "" : "_rowId=" + bundle.rowId + "\n") +
@ -239,7 +251,8 @@ public class Rhizome {
args[2], // manifest path args[2], // manifest path
args.length > 3 ? args[3] : null, // payload path args.length > 3 ? args[3] : null, // payload path
args.length > 4 ? args[4] : null, // manifest out path args.length > 4 ? args[4] : null, // manifest out path
args.length > 5 ? args[5] : null // payload name args.length > 5 ? args[5] : null, // payload name
args.length > 6 ? args[6] : null // bundle secret
); );
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -351,8 +351,7 @@ test_RhizomeInsert() {
--author=${author[4]} file4 --author=${author[4]} file4
for n in 1 2 3 4; do for n in 1 2 3 4; do
$SED -e '/^version=/d;/^date=/d;/^filehash=/d;/^filesize=/d;/^[^a-zA-Z]/,$d' xfile$n.manifest >nmanifest$n $SED -e '/^version=/d;/^date=/d;/^filehash=/d;/^filesize=/d;/^[^a-zA-Z]/,$d' xfile$n.manifest >nmanifest$n
assertGrep nmanifest$n '^id=' assert_manifest_fields nmanifest$n id !version !date !filehash !filesize
tfw_cat -v nmanifest$n
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' nmanifest$n nfile$n nfile$n.manifest "nfile$n" executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' nmanifest$n nfile$n nfile$n.manifest "nfile$n"
tfw_cat --stdout --stderr -v nfile$n.manifest tfw_cat --stdout --stderr -v nfile$n.manifest
if [ -n "${author[$n]}" ]; then if [ -n "${author[$n]}" ]; then
@ -369,4 +368,29 @@ test_RhizomeInsert() {
done done
} }
doc_RhizomeInsertAnon="Java API update anonymous Rhizome bundle"
setup_RhizomeInsertAnon() {
setup
create_file file1 1001
executeOk_servald rhizome add file '' file1 file1.manifest
extract_stdout_manifestid BID
extract_stdout_secret SECRET
assert_manifest_fields file1.manifest id !BK
$SED -e '/^version=/d;/^date=/d;/^filehash=/d;/^filesize=/d;/^[^a-zA-Z]/,$d' file1.manifest >file2.manifest
assert_manifest_fields file2.manifest id !version !date !filehash !filesize
create_file file2 1002
}
test_RhizomeInsertAnon() {
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' file2.manifest file2 ifile2.manifest "file2" "$SECRET"
tfw_cat --stdout --stderr -v ifile2.manifest
assertStdoutGrep '^_status=NEW$'
assertStdoutGrep "^id=$BID\$"
assertStderrGrep --matches=1 "^bundle_status_code=NEW$CR\$"
assertStderrGrep --matches=1 --ignore-case "^bundle_status_message=.*bundle new to store.*$CR\$"
assertStderrGrep --matches=1 "^payload_status_code=NEW$CR\$"
assertStderrGrep --matches=1 --ignore-case "^payload_status_message=.*payload new to store.*$CR\$"
executeOk_servald rhizome list
assert_rhizome_list --fromhere=0 --manifest=ifile2.manifest file2
}
runTests "$@" runTests "$@"