use of edu.umass.cs.gnscommon.exceptions.client.AclException in project GNS by MobilityFirst.
the class CommandUtils method checkResponse.
/**
*
* @param command
*
* @param responsePacket
* @return Response as a string.
* @throws ClientException
*/
public static ResponsePacket checkResponse(ResponsePacket responsePacket, CommandPacket command) throws ClientException {
ResponseCode code = responsePacket.getErrorCode();
String returnValue = responsePacket.getReturnValue();
// wants to return a null value.
if (code.isOKResult()) {
return (returnValue.startsWith(GNSProtocol.NULL_RESPONSE.toString())) ? null : //returnValue;
responsePacket;
}
// else error
String errorSummary = code + ": " + returnValue + //+ ": " + responsePacket.getSummary()
(command != null ? " for command " + command.getSummary() : "");
switch(code) {
case SIGNATURE_ERROR:
throw new EncryptionException(code, errorSummary);
case BAD_GUID_ERROR:
case BAD_ACCESSOR_ERROR:
case BAD_ACCOUNT_ERROR:
throw new InvalidGuidException(code, errorSummary);
case FIELD_NOT_FOUND_ERROR:
throw new FieldNotFoundException(code, errorSummary);
case ACCESS_ERROR:
throw new AclException(code, errorSummary);
case VERIFICATION_ERROR:
throw new VerificationException(code, errorSummary);
case ALREADY_VERIFIED_EXCEPTION:
throw new VerificationException(code, errorSummary);
case DUPLICATE_ID_EXCEPTION:
//case DUPLICATE_NAME_EXCEPTION:
throw new DuplicateNameException(code, errorSummary);
case DUPLICATE_FIELD_EXCEPTION:
throw new InvalidFieldException(code, errorSummary);
case ACTIVE_REPLICA_EXCEPTION:
throw new InvalidGuidException(code, errorSummary);
case NONEXISTENT_NAME_EXCEPTION:
throw new InvalidGuidException(code, errorSummary);
case TIMEOUT:
case RECONFIGURATION_EXCEPTION:
throw new ClientException(code, errorSummary);
default:
throw new ClientException(code, "Error received with an unknown response code: " + errorSummary);
}
}
use of edu.umass.cs.gnscommon.exceptions.client.AclException in project GNS by MobilityFirst.
the class ClientACLExample 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 {
client = new GNSClientCommands();
System.out.println("[Client connected to GNS]\n");
try {
System.out.println("// account GUID creation\n" + "client.execute(" + ACCOUNT_NAME + ")");
client.execute(GNSCommand.createAccount(ACCOUNT_NAME));
GUID = GuidUtils.getGUIDKeys(ACCOUNT_NAME);
} 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("{\"name\":\"me\",\"location\":\"work\"}");
// Write out the JSON Object
client.execute(GNSCommand.update(GUID, json));
System.out.println("\n// Update guid record\n" + "client.update(guid, record) // record=" + json);
// Remove default read access from guid
client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID, GNSProtocol.ENTIRE_RECORD.toString(), GNSProtocol.ALL_GUIDS.toString()));
System.out.println("\n// Remove default read access from guid\n" + "client.aclRemove(READ_WHITELIST, guid, ALL_FIELDS, ALL_GUIDS)");
// Create phoneGuid
// First we create an alias for the phoneGuid
String phoneAlias = "phone" + RandomString.randomString(12);
// Create a sub guid under our guid account
client.execute(GNSCommand.guidCreate(GUID, phoneAlias));
// Get the GuidEntry from the local database
phoneGuid = GuidUtils.getGUIDKeys(phoneAlias);
System.out.println("\n// Create phoneGuid\n" + "client.guidCreate(guid, phoneAlias) // phoneAlias=" + phoneAlias);
// Give phoneGuid read access to fields in guid
// If we had not removed the default read access from guid this step
// would be unnecessary
client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID, GNSProtocol.ENTIRE_RECORD.toString(), phoneGuid.getGuid()));
JSONObject result = client.execute(GNSCommand.read(GUID.getGuid(), phoneGuid)).getResultJSONObject();
System.out.println("\n// Give phoneGuid read access to fields in guid and read guid entry as phoneGuid\n" + "client.aclAdd(READ_WHITELIST, guid, ALL_FIELDS, phoneGuid)\n" + "client.read(guid, phoneGuid) -> " + result);
// Allow phoneGuid to write to the location field of guid
client.execute(GNSCommand.aclAdd(AclAccessType.WRITE_WHITELIST, GUID, "location", phoneGuid.getGuid()));
System.out.println("\n// Give phoneGuid write access to \"location\" field of guid\n" + "client.aclAdd(WRITE_WHITELIST, guid, \"location\", phoneGuid)");
// As phoneGuid, update the location field on guid
client.execute(GNSCommand.fieldUpdate(GUID.getGuid(), "location", "home", phoneGuid));
String field_result = client.execute(GNSCommand.fieldRead(GUID.getGuid(), "location", phoneGuid)).getResultString();
System.out.println("\n// Use phoneGuid to update \"location\" field of guid\n" + "client.fieldUpdate(guid, \"location\", \"home\", phoneGuid)\n" + "client.fieldRead(guid.getGuid(), \"location\", phoneGuid) -> " + field_result);
// Remove phoneGuid from ACL
client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID, GNSProtocol.ENTIRE_RECORD.toString(), phoneGuid.getGuid()));
client.execute(GNSCommand.aclRemove(AclAccessType.WRITE_WHITELIST, GUID, "location", phoneGuid.getGuid()));
System.out.println("\n// Remove phoneGuid from guid's read and write whitelists \n" + "client.aclRemove(READ_WHITELIST, guid, ALL_FIELDS, phoneGuid))\n" + "client.aclRemove(WRITE_WHITELIST, guid, \"location\", phoneGuid);");
// Verify phoneGuid can't read guid (exception expected)
try {
System.out.println("\n// Attempting to read from guid using phoneGuid (failure expected)\n" + "client.read(guid, phoneGuid)");
result = client.execute(GNSCommand.read(GUID.getGuid(), phoneGuid)).getResultJSONObject();
System.out.println("SOMETHING WENT WRONG. An exception should have been thrown. Terminating.");
client.close();
System.exit(1);
} catch (AclException e) {
System.out.println("// client.read failed as expected with the following AclException:\n" + e.getMessage());
}
// expected)
try {
System.out.println("\n// Attempting to update \"location\" field of guid using phoneGuid (failure expected)\n" + "client.fieldUpdate(guid.getGuid(), \"location\", \"vacation\", phoneGuid)");
client.execute(GNSCommand.fieldUpdate(GUID.getGuid(), "location", "vacation", phoneGuid));
System.out.println("\nSOMETHING WENT WRONG. An exception should have been thrown. Terminating.");
client.close();
System.exit(1);
} catch (AclException e) {
System.out.println("// client.fieldUpdate failed as expected with the following AclException:\n" + e.getMessage());
}
System.out.println("\n// Example complete, gracefully closing the client\n" + "client.close()");
client.close();
}
Aggregations