use of edu.umass.cs.gnscommon.exceptions.client.ClientException in project GNS by MobilityFirst.
the class AclExists method parse.
@Override
public void parse(String commandText) throws Exception {
try {
GNSClientCommands gnsClient = module.getGnsClient();
StringTokenizer st = new StringTokenizer(commandText.trim());
if (st.countTokens() != 2) {
wrongArguments();
return;
}
String field = st.nextToken();
boolean isWrite = "write".equalsIgnoreCase(st.nextToken());
// Set ACL
boolean exists = gnsClient.fieldAclExists(isWrite ? AclAccessType.WRITE_WHITELIST : AclAccessType.READ_WHITELIST, module.getCurrentGuid(), field);
if (exists) {
// Then read ACLs
JSONArray acl = gnsClient.aclGet(isWrite ? AclAccessType.WRITE_WHITELIST : AclAccessType.READ_WHITELIST, module.getCurrentGuid(), field, module.getCurrentGuid().getGuid());
console.printString((isWrite ? "Write" : "Read") + " ACL is: " + acl.toString());
console.printNewline();
} else {
console.printString((isWrite ? "Write" : "Read") + " ACL for field " + field + " does not exist");
console.printNewline();
}
} catch (IOException | ClientException e) {
console.printString("Failed to access GNS ( " + e + ")\n");
}
}
use of edu.umass.cs.gnscommon.exceptions.client.ClientException in project GNS by MobilityFirst.
the class FieldCreateList method parse.
@Override
public void parse(String commandText) throws Exception {
try {
StringTokenizer st = new StringTokenizer(commandText.trim());
if ((st.countTokens() != 1) && (st.countTokens() != 2)) {
wrongArguments();
return;
}
String field = st.nextToken();
String value = "";
if (st.hasMoreTokens()) {
value = st.nextToken();
}
GNSClientCommands gnsClient = module.getGnsClient();
gnsClient.fieldCreateList(module.getCurrentGuid().getGuid(), field, new JSONArray().put(value), module.getCurrentGuid());
console.printString("New field " + field + " created with value '" + value + "'");
console.printNewline();
} catch (IOException | ClientException e) {
console.printString("Failed to access GNS ( " + e + ")\n");
}
}
use of edu.umass.cs.gnscommon.exceptions.client.ClientException in project GNS by MobilityFirst.
the class RegisterAccountSecured method execute.
@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException, InternalRequestException {
JSONObject json = commandPacket.getCommand();
String name = json.getString(GNSProtocol.NAME.toString());
String publicKey = json.getString(GNSProtocol.PUBLIC_KEY.toString());
String password = json.getString(GNSProtocol.PASSWORD.toString());
String signature = json.optString(GNSProtocol.SIGNATURE.toString(), null);
String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);
String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
if (!NSAccessSupport.verifySignature(publicKey, signature, message)) {
return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
}
try {
// Add the account but don't enable email verification
CommandResponse result = AccountAccess.addAccount(header, commandPacket, handler.getHttpServerHostPortString(), name, guid, publicKey, password, false, handler);
if (result.getExceptionOrErrorCode().isOKResult()) {
// Everything is hunkey dorey so return the new guid
return new CommandResponse(ResponseCode.NO_ERROR, guid);
} else {
assert (result.getExceptionOrErrorCode() != null);
// Otherwise return the error response.
return result;
}
} catch (ClientException | IOException e) {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
}
}
use of edu.umass.cs.gnscommon.exceptions.client.ClientException in project GNS by MobilityFirst.
the class AddToGroup method execute.
@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, ParseException {
JSONObject json = commandPacket.getCommand();
String guid = json.getString(GNSProtocol.GUID.toString());
String member = json.getString(GNSProtocol.MEMBER.toString());
// writer might be same as guid
String writer = json.optString(GNSProtocol.WRITER.toString(), guid);
// signature and message can be empty for unsigned cases
String signature = json.optString(GNSProtocol.SIGNATURE.toString(), null);
String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);
Date timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : // can be null on older client
null;
ResponseCode responseCode;
try {
if (!(responseCode = GroupAccess.addToGroup(header, guid, member, writer, signature, message, timestamp, handler)).isExceptionOrError()) {
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
} else {
return new CommandResponse(responseCode, GNSProtocol.BAD_RESPONSE.toString() + " " + responseCode.getProtocolCode());
}
} catch (ClientException | IOException | InternalRequestException e) {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
}
}
use of edu.umass.cs.gnscommon.exceptions.client.ClientException in project GNS by MobilityFirst.
the class ServerIntegrationTest method test_190_Substitute.
/**
* Tests different DB substitute methods.
*/
@Test
public void test_190_Substitute() {
//CHECKED FOR VALIDITY
String testSubstituteGuid = "testSubstituteGUID" + RandomString.randomString(12);
String field = "people";
GuidEntry testEntry = null;
try {
// Utils.clearTestGuids(client);
// System.out.println("cleared old GUIDs");
testEntry = clientCommands.guidCreate(masterGuid, testSubstituteGuid);
} catch (Exception e) {
failWithStackTrace("Exception during init: ", e);
}
try {
clientCommands.fieldAppendOrCreateList(testEntry.getGuid(), field, new JSONArray(Arrays.asList("Frank", "Joe", "Sally", "Rita")), testEntry);
} catch (IOException | ClientException e) {
failWithStackTrace("Exception during create: ", e);
}
try {
HashSet<String> expected = new HashSet<>(Arrays.asList("Frank", "Joe", "Sally", "Rita"));
HashSet<String> actual = JSONUtils.JSONArrayToHashSet(clientCommands.fieldReadArray(testEntry.getGuid(), field, testEntry));
Assert.assertEquals(expected, actual);
} catch (Exception e) {
failWithStackTrace("Exception when we were not expecting it: ", e);
}
try {
clientCommands.fieldSubstitute(testEntry.getGuid(), field, "Christy", "Sally", testEntry);
} catch (Exception e) {
failWithStackTrace("Exception during substitute: ", e);
}
try {
HashSet<String> expected = new HashSet<>(Arrays.asList("Frank", "Joe", "Christy", "Rita"));
HashSet<String> actual = JSONUtils.JSONArrayToHashSet(clientCommands.fieldReadArray(testEntry.getGuid(), field, testEntry));
Assert.assertEquals(expected, actual);
} catch (Exception e) {
failWithStackTrace("Exception when we were not expecting it: ", e);
}
}
Aggregations