Add Java api to add identity with name and number

This commit is contained in:
Jeremy Lakeman 2016-06-07 11:40:43 +09:30
parent 7e497d01d6
commit 9f4f56b663
5 changed files with 66 additions and 1 deletions

View File

@ -91,6 +91,11 @@ public class ServalDClient implements ServalDHttpConnectionFactory
return KeyringCommon.setDidName(this, sid, did, name, pin);
}
public KeyringIdentity keyringAdd(String did, String name, String pin) throws ServalDInterfaceException, IOException
{
return KeyringCommon.addIdentity(this, did, name, pin);
}
public RhizomeBundleList rhizomeListBundles() throws ServalDInterfaceException, IOException
{
RhizomeBundleList list = new RhizomeBundleList(this);

View File

@ -228,4 +228,31 @@ public class KeyringCommon
}
}
public static KeyringIdentity addIdentity(ServalDHttpConnectionFactory connector, String did, String name, String pin)
throws IOException, ServalDInterfaceException
{
Vector<ServalDHttpConnectionFactory.QueryParam> query_params = new Vector<ServalDHttpConnectionFactory.QueryParam>();
if (did != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("did", did));
if (name != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("name", name));
if (pin != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("pin", pin));
HttpURLConnection conn = connector.newServalDHttpConnection("/restful/keyring/add", query_params);
conn.connect();
Status status = receiveRestfulResponse(conn, HttpURLConnection.HTTP_OK);
try {
decodeRestfulStatus(status);
dumpStatus(status, System.err);
if (status.identity == null)
throw new ServalDInterfaceException("invalid JSON response; missing identity");
return status.identity;
}
finally {
if (status.input_stream != null)
status.input_stream.close();
}
}
}

View File

@ -70,6 +70,17 @@ public class Keyring {
System.exit(0);
}
static void add(String did, String name, String pin) throws ServalDInterfaceException, IOException, InterruptedException
{
ServalDClient client = new ServerControl().getRestfulClient();
KeyringIdentity id = client.keyringAdd(did, name, pin);
System.out.println("sid=" + id.sid +
", did=" + id.did +
", name=" + id.name
);
System.exit(0);
}
public static void main(String... args)
{
if (args.length < 1)
@ -80,6 +91,8 @@ public class Keyring {
keyring_list(args.length >= 2 ? args[1] : null);
else if (methodName.equals("set"))
set(new SubscriberId(args[1]), args[2], args[3], args.length >= 5 ? args[4] : null);
else if (methodName.equals("add"))
add(args[1], args[2], args.length >= 4 ? args[3] : null);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);

View File

@ -211,9 +211,15 @@ static int restful_keyring_add(httpd_request *r, const char *remainder)
if (*remainder)
return 404;
const char *pin = http_request_get_query_param(&r->http, "pin");
const keyring_identity *id = keyring_create_identity(keyring, pin ? pin : "");
const char *did = http_request_get_query_param(&r->http, "did");
const char *name = http_request_get_query_param(&r->http, "name");
keyring_identity *id = keyring_create_identity(keyring, pin ? pin : "");
if (id == NULL)
return http_request_keyring_response(r, 500, "Could not create identity");
if (did || name){
if (keyring_set_did(id, did ? did : "", name ? name : "") == -1)
return http_request_keyring_response(r, 500, "Could not set identity DID/Name");
}
if (keyring_commit(keyring) == -1)
return http_request_keyring_response(r, 500, "Could not store new identity");
return http_request_keyring_response_identity(r, 200, CONTENT_TYPE_JSON, id);

View File

@ -111,6 +111,20 @@ test_keyringListPin() {
assertStdoutGrep --matches=1 "sid=$SIDA3"
}
doc_keyringAddDidName="Java API add new identity"
setup_keyringAddDidName() {
IDENTITY_COUNT=1
setup
}
test_keyringAddDidName() {
executeJavaOk org.servalproject.test.Keyring add 987654321 'Joe Bloggs'
tfw_cat --stdout --stderr
assertStdoutGrep --matches=1 "sid=${rexp_sid}, did=987654321, name=Joe Bloggs$"
executeOk_servald keyring list
assert_keyring_list 2
assertStdoutGrep --stderr --matches=1 "^${rexp_sid}:987654321:Joe Bloggs\$"
}
doc_keyringSetDidName="Java API set DID and name"
setup_keyringSetDidName() {
IDENTITY_COUNT=2