use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class AbstractUpdateList 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 field = json.getString(GNSProtocol.FIELD.toString());
String value = json.getString(GNSProtocol.VALUE.toString());
String oldValue = json.optString(GNSProtocol.OLD_VALUE.toString(), null);
int argument = json.optInt(GNSProtocol.ARGUMENT.toString(), -1);
String writer = json.optString(GNSProtocol.WRITER.toString(), guid);
String signature = json.optString(GNSProtocol.SIGNATURE.toString(), null);
String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);
Date timestamp;
if (json.has(GNSProtocol.TIMESTAMP.toString())) {
// can be null on older client
timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : null;
} else {
timestamp = null;
}
ResponseCode responseCode;
if (!(responseCode = FieldAccess.update(header, commandPacket, guid, field, JSONUtils.JSONArrayToResultValue(new JSONArray(value)), oldValue != null ? JSONUtils.JSONArrayToResultValue(new JSONArray(oldValue)) : null, argument, getUpdateOperation(), 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());
}
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class CreateList 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 field = json.getString(GNSProtocol.FIELD.toString());
String value = json.getString(GNSProtocol.VALUE.toString());
// the opt hair below is for the subclasses... cute, huh?
// writer might be same as guid
String writer = json.optString(GNSProtocol.WRITER.toString(), guid);
String signature = json.getString(GNSProtocol.SIGNATURE.toString());
String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
Date timestamp;
if (json.has(GNSProtocol.TIMESTAMP.toString())) {
// can be null on older client
timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : null;
} else {
timestamp = null;
}
ResponseCode responseCode;
if (!(responseCode = FieldAccess.createField(header, commandPacket, guid, field, new ResultValue(value), 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());
}
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class SetCode 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 writer = json.getString(GNSProtocol.WRITER.toString());
String action = json.getString(GNSProtocol.AC_ACTION.toString());
String code = json.getString(GNSProtocol.AC_CODE.toString());
String signature = json.getString(GNSProtocol.SIGNATURE.toString());
String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
Date timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : // can be null on older client
null;
try {
ResponseCode response = ActiveCode.setCode(header, commandPacket, guid, action, code, writer, signature, message, timestamp, handler);
if (!response.isExceptionOrError()) {
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
} else {
return new CommandResponse(response, GNSProtocol.BAD_RESPONSE.toString() + " " + response.getProtocolCode());
}
} catch (IllegalArgumentException | JSONException e) {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + ResponseCode.UNSPECIFIED_ERROR.getProtocolCode() + " " + e.getMessage());
}
}
use of edu.umass.cs.gnscommon.ResponseCode 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.ResponseCode in project GNS by MobilityFirst.
the class AccountAccess method addMultipleGuids.
/**
* Add multiple guids to an account.
*
* @param header
* @param commandPacket
*
* @param names
* - the list of names
* @param publicKeys
* - the list of public keys associated with the names
* @param accountInfo
* @param accountGuidInfo
* @param handler
* @return a command response
*/
public static CommandResponse addMultipleGuids(InternalRequestHeader header, CommandPacket commandPacket, List<String> names, List<String> publicKeys, AccountInfo accountInfo, GuidInfo accountGuidInfo, ClientRequestHandlerInterface handler) {
try {
long startTime = System.currentTimeMillis();
//Set<String> guids = new HashSet<>();
Map<String, JSONObject> hrnMap = new HashMap<>();
Map<String, JSONObject> guidInfoMap = new HashMap<>();
for (int i = 0; i < names.size(); i++) {
String name = names.get(i);
String publicKey = publicKeys.get(i);
String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
accountInfo.addGuid(guid);
//guids.add(guid);
// HRN records
JSONObject jsonHRN = new JSONObject();
jsonHRN.put(HRN_GUID, guid);
hrnMap.put(name, jsonHRN);
// guid info record
GuidInfo guidInfo = new GuidInfo(name, guid, publicKey);
JSONObject jsonGuid = new JSONObject();
jsonGuid.put(GUID_INFO, guidInfo.toJSONObject());
jsonGuid.put(PRIMARY_GUID, accountInfo.getGuid());
// set up ACL to look like this
// "_GNS_ACL": {
// "READ_WHITELIST": {"+ALL+": {"MD": [<publickey>, "+ALL+"]}},
// "WRITE_WHITELIST": {"+ALL+": {"MD": [<publickey>]}}
JSONObject acl = createACL(GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(GNSProtocol.EVERYONE.toString(), accountGuidInfo.getPublicKey()), GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(accountGuidInfo.getPublicKey()));
// prefix is the same for all acls so just pick one to use here
jsonGuid.put(MetaDataTypeName.READ_WHITELIST.getPrefix(), acl);
guidInfoMap.put(guid, jsonGuid);
}
DelayProfiler.updateDelay("addMultipleGuidsSetup", startTime);
accountInfo.noteUpdate();
// First we create the HRN records as a batch
ResponseCode returnCode;
// First try to create the HRNS to insure that that name does not already exist
Map<String, String> nameStates = new HashMap<>();
for (String key : hrnMap.keySet()) {
nameStates.put(key, hrnMap.get(key).toString());
}
if (!(returnCode = handler.getInternalClient().createOrExists(new CreateServiceName(nameStates))).isExceptionOrError()) {
// now we update the account info
if (updateAccountInfoNoAuthentication(header, commandPacket, accountInfo, handler, true).isOKResult()) {
HashMap<String, String> guidInfoNameStates = new HashMap<>();
for (String key : guidInfoMap.keySet()) {
guidInfoNameStates.put(key, guidInfoMap.get(key).toString());
}
handler.getInternalClient().createOrExists(new CreateServiceName(guidInfoNameStates));
GNSConfig.getLogger().info(DelayProfiler.getStats());
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
}
}
return new CommandResponse(returnCode, GNSProtocol.BAD_RESPONSE.toString() + " " + returnCode.getProtocolCode() + " " + names);
} catch (JSONException e) {
return new CommandResponse(ResponseCode.JSON_PARSE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e.getMessage());
} catch (ServerRuntimeException | ClientException e) {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
}
}
Aggregations