serval-dna/java/org/servalproject/test/Rhizome.java
Andrew Bettison 34b6ff48bf Rhizome Java API: get raw payload
Added more "Serval-Rhizome-Bundle-xxx" headers to /restful/rhizome/
responses, so that a more complete manifest can be constructed from
them
2014-07-10 06:29:09 +09:30

142 lines
4.6 KiB
Java

/**
* Copyright (C) 2014 Serval Project Inc.
*
* This file is part of Serval Software (http://www.servalproject.org)
*
* Serval Software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.servalproject.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import org.servalproject.servaldna.ServalDClient;
import org.servalproject.servaldna.ServalDInterfaceException;
import org.servalproject.servaldna.ServerControl;
import org.servalproject.servaldna.BundleId;
import org.servalproject.servaldna.rhizome.RhizomeManifest;
import org.servalproject.servaldna.rhizome.RhizomeListBundle;
import org.servalproject.servaldna.rhizome.RhizomeBundleList;
import org.servalproject.servaldna.rhizome.RhizomeManifestBundle;
import org.servalproject.servaldna.rhizome.RhizomePayloadRawBundle;
public class Rhizome {
static String manifestFields(RhizomeManifest manifest, String sep)
{
return "id=" + manifest.id + sep +
"version=" + manifest.version + sep +
"filesize=" + manifest.filesize + sep +
"filehash=" + manifest.filehash + sep +
"sender=" + manifest.sender + sep +
"recipient=" + manifest.recipient + sep +
"date=" + manifest.date + sep +
"service=" + manifest.service + sep +
"name=" + manifest.name + sep +
"BK=" + manifest.BK;
}
static void rhizome_list() throws ServalDInterfaceException, IOException, InterruptedException
{
ServalDClient client = new ServerControl().getRestfulClient();
RhizomeBundleList list = null;
try {
list = client.rhizomeListBundles();
RhizomeListBundle bundle;
while ((bundle = list.nextBundle()) != null) {
System.out.println(
"_rowId=" + bundle.rowId +
", _token=" + bundle.token +
", _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();
RhizomeManifestBundle bundle = client.rhizomeManifest(bid);
System.out.println(
"_insertTime=" + bundle.insertTime + "\n" +
"_author=" + bundle.author + "\n" +
"_secret=" + bundle.secret + "\n" +
manifestFields(bundle.manifest, "\n") + "\n"
);
FileOutputStream out = new FileOutputStream(dstpath);
out.write(bundle.manifestText());
out.close();
System.exit(0);
}
static void rhizome_payload_raw(BundleId bid, String dstpath) throws ServalDInterfaceException, IOException, InterruptedException
{
ServalDClient client = new ServerControl().getRestfulClient();
FileOutputStream out = new FileOutputStream(dstpath);
try {
RhizomePayloadRawBundle bundle = client.rhizomePayloadRaw(bid);
InputStream in = bundle.rawPayloadInputStream;
byte[] buf = new byte[4096];
int n;
while ((n = in.read(buf)) > 0)
out.write(buf, 0, n);
in.close();
out.close();
out = null;
System.out.println(
"_insertTime=" + bundle.insertTime + "\n" +
"_author=" + bundle.author + "\n" +
"_secret=" + bundle.secret + "\n" +
manifestFields(bundle.manifest, "\n") + "\n"
);
}
finally {
if (out != null)
out.close();
}
System.exit(0);
}
public static void main(String... args)
{
if (args.length < 1)
return;
String methodName = args[0];
try {
if (methodName.equals("rhizome-list"))
rhizome_list();
else if (methodName.equals("rhizome-manifest"))
rhizome_manifest(new BundleId(args[1]), args[2]);
else if (methodName.equals("rhizome-payload-raw"))
rhizome_payload_raw(new BundleId(args[1]), args[2]);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.err.println("No such command: " + methodName);
System.exit(1);
}
}