use of edu.umass.cs.gnsclient.client.http.HttpClient in project GNS by MobilityFirst.
the class HTTPClientCASAAppExample method main.
/**
* @param args
* @throws IOException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws ClientException
* @throws InvalidKeyException
* @throws SignatureException
* @throws Exception
*/
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
// Create the client
client = new HttpClient("127.0.0.1", 8080);
// First thing we do is create an account to simulate the master account guid
// that the ACS is using - don't worry about this for now.
createSimulatedMasterAccount();
//
// Now on to the app commands.
//
// Check to see if our guid already exists
String lookupResult = null;
try {
lookupResult = client.lookupGuid(ourAppAlias);
} catch (ClientException e) {
// The normal result if the alias doesn't exist
} catch (IOException e) {
System.out.println("\nProblem during lookupGuid: " + e);
System.exit(1);
}
// If it exists we exit
if (lookupResult != null) {
System.out.println("\nGuid for " + ourAppAlias + " already exists. This can happen if you " + "run this class twice. Exiting.");
System.exit(1);
}
System.out.println("\n// guid lookup\n" + "lookupGuid didn't find " + ourAppAlias + ". This is good.");
// Note that accountGuidCreate looks up or creates a private public key pair for us.
try {
guidEntry = client.accountGuidCreate(ourAppAlias, "samplePassword");
} catch (DuplicateNameException e) {
// ignore as it is most likely because of a seemingly failed creation operation that actually succeeded.
System.out.println("Account GUID " + guidEntry + " aready exists on the server; continuing.");
}
System.out.println("\n// guid create\n" + "Created guid for " + ourAppAlias + ": " + guidEntry.getGuid());
//
// Update the ACLs.
// Next we remove the default read access that the GNS gives for all fields.
client.aclRemove(AclAccessType.READ_WHITELIST, guidEntry, GNSProtocol.ENTIRE_RECORD.toString(), GNSProtocol.EVERYONE.toString());
System.out.println("\n// acl update\n" + "Removed default read whitelist for " + ourAppAlias);
// Look up the guid of the simulate master account we created above.
// In the running architecture this is created by the ACS.
String masterGuid;
// Give the master ACS guid (simulated) access to all fields of our guid for reading and writing.
masterGuid = client.lookupGuid(ourAppAlias);
client.aclAdd(AclAccessType.READ_WHITELIST, guidEntry, GNSProtocol.ENTIRE_RECORD.toString(), masterGuid);
client.aclAdd(AclAccessType.WRITE_WHITELIST, guidEntry, GNSProtocol.ENTIRE_RECORD.toString(), masterGuid);
System.out.println("\n// acl update part two\n" + "Added read and write access for " + masterGuid + " to " + ourAppAlias);
// Now do some writing and reading writing.
// Create a JSON Object to initialize our guid record
JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
// Write out the JSON Object.
client.update(guidEntry, json);
System.out.println("\n// record update\n" + "client.update(guidEntry, record) // record=" + json);
// And read the entire object back in.
JSONObject result = client.read(guidEntry);
System.out.println("client.read(guidEntry) -> " + result.toString());
client.close();
System.out.println("\nclient.close() // test successful");
}
use of edu.umass.cs.gnsclient.client.http.HttpClient in project GNS by MobilityFirst.
the class HTTPClientExample method main.
/**
* @param args
* @throws IOException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws ClientException
* @throws InvalidKeyException
* @throws SignatureException
* @throws Exception
*/
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
// Create the client will connect to GNS HTTP server running locally.
client = new HttpClient("127.0.0.1", 8080);
try {
/**
* Create an account GUID if one doesn't already exists. The true
* flag makes it verbosely print out what it is doing. The password
* is for future use.
* lookupOrCreateAccountGuid "cheats" by bypassing email-based or
* other verification mechanisms using a shared secret between the
* server and the client.
*
*/
System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)");
guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
} catch (Exception | Error e) {
System.out.println("Exception during accountGuid creation: " + e);
e.printStackTrace();
System.exit(1);
}
// Create a JSON Object to initialize our guid record
JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
// Write out the JSON Object
client.update(guid, json);
System.out.println("\n// record update\n" + "client.update(GUID, record) // record=" + json);
// and read the entire object back in
JSONObject result = client.read(guid);
System.out.println("client.read(GUID) -> " + result.toString());
// Change a field
client.update(guid, new JSONObject("{\"occupation\":\"rocket scientist\"}"));
System.out.println("\n// field update\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue={\"occupation\":\"rocket scientist\"}");
// and read the entire object back in
result = client.read(guid);
System.out.println("client.read(GUID) -> " + result.toString());
// Add a field
client.update(guid, new JSONObject("{\"ip address\":\"127.0.0.1\"}"));
System.out.println("\n// field add\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue= {\"ip address\":\"127.0.0.1\"}");
// and read the entire object back in
result = client.read(guid);
System.out.println("client.read(GUID) -> " + result.toString());
// Remove a field
client.fieldRemove(guid.getGuid(), "gibberish", guid);
System.out.println("\n// field remove\n" + "client.fieldRemove(GUID, \"gibberish\")");
// and read the entire object back in
result = client.read(guid);
System.out.println("client.read(GUID) -> " + result.toString());
// Add some more stuff to read back
JSONObject newJson = new JSONObject();
JSONObject subJson = new JSONObject();
subJson.put("sally", "red");
subJson.put("sammy", "green");
JSONObject subsubJson = new JSONObject();
subsubJson.put("right", "seven");
subsubJson.put("left", "eight");
subJson.put("sally", subsubJson);
newJson.put("flapjack", subJson);
client.update(guid, newJson);
System.out.println("\n// field add with JSON value\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue=" + newJson);
// Read a single field at the top level
String resultString = client.fieldRead(guid, "flapjack");
System.out.println("client.fieldRead(\"flapjack\") -> " + resultString);
// Read a single field using dot notation
resultString = client.fieldRead(guid, "flapjack.sally.right");
System.out.println("\n// dotted field read\n" + "client.fieldRead(GUID, \"flapjack.sally.right\") -> " + resultString);
// Update a field using dot notation
JSONArray newValue = new JSONArray(Arrays.asList("One", "Ready", "Frap"));
client.fieldUpdate(guid, "flapjack.sammy", newValue);
System.out.println("\n// dotted field update\n" + "client.fieldUpdate(GUID, \"flapjack.sammy\", " + newValue);
// Read the same field using dot notation
resultString = client.fieldRead(guid, "flapjack.sammy");
System.out.println("client.fieldRead(GUID, \"flapjack.sammy\") -> " + resultString);
// Read two fields at a time
resultString = client.fieldRead(guid, new ArrayList<String>(Arrays.asList("name", "occupation")));
System.out.println("\n// multi-field read\n" + "client.fieldRead(GUID, [\"name\",\"occupation\"] -> " + resultString);
// Read the entire object back in
result = client.read(guid);
System.out.println("\nclient.read(GUID) -> " + result.toString());
// Delete created GUID
client.accountGuidRemove(guid);
System.out.println("\n// GUID delete\n" + "client.accountGuidRemove(GUID) // GUID=" + guid);
// Try read the entire record
try {
result = client.read(guid);
} catch (Exception e) {
System.out.println("\n// non-existent GUID error (expected)\n" + "client.read(GUID) // GUID= " + guid + "\n " + e.getMessage());
}
client.close();
System.out.println("\nclient.close() // test successful");
}
Aggregations