use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class AccountAccess method updateAccountInfo.
private static ResponseCode updateAccountInfo(InternalRequestHeader header, CommandPacket commandPacket, String guid, AccountInfo accountInfo, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler, boolean sendToReplica) {
try {
ResponseCode response;
if (sendToReplica) {
// We potentially need to send the update to different replica.
try {
handler.getInternalClient().execute(GNSCommandInternal.fieldUpdate(guid, ACCOUNT_INFO, accountInfo.toJSONObject(), header));
response = ResponseCode.NO_ERROR;
} catch (JSONException e) {
GNSConfig.getLogger().log(Level.SEVERE, "JSON parse error with remote query:{0}", e);
response = ResponseCode.JSON_PARSE_ERROR;
} catch (ClientException | IOException | InternalRequestException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem with remote query:{0}", e);
response = ResponseCode.UNSPECIFIED_ERROR;
}
} else {
GNSConfig.getLogger().log(Level.FINE, "Updating locally for GUID {0}:{1}<-{1}", new Object[] { guid, ACCOUNT_INFO, accountInfo });
// Do the update locally.
JSONObject json = new JSONObject();
json.put(ACCOUNT_INFO, accountInfo.toJSONObject());
response = FieldAccess.updateUserJSON(header, commandPacket, guid, json, writer, signature, message, timestamp, handler);
}
return response;
} catch (JSONException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing account info:{0}", e);
return ResponseCode.JSON_PARSE_ERROR;
}
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class FieldAccess method lookupMultipleFields.
/**
* Reads the value of fields in a guid.
* Field(s) is a string the naming the field(s). Field(s) can us dot
* notation to indicate subfields.
*
* @param header
* @param commandPacket
*
* @param guid
* @param fields - mutually exclusive with field
* @param reader
* @param signature
* @param message
* @param timestamp
* @param handler
* @return the value of a single field
*/
public static CommandResponse lookupMultipleFields(InternalRequestHeader header, CommandPacket commandPacket, String guid, ArrayList<String> fields, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
ResponseCode errorCode = signatureAndACLCheckForRead(header, commandPacket, guid, //field
null, fields, reader, signature, message, timestamp, handler.getApp());
if (errorCode.isExceptionOrError()) {
return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
}
ValuesMap valuesMap;
try {
valuesMap = NSFieldAccess.lookupFieldsLocalNoAuth(header, guid, fields, ColumnFieldType.USER_JSON, handler);
// note: reader can also be null here
if (!header.verifyInternal()) {
// don't strip internal fields when doing a read for other servers
valuesMap = valuesMap.removeInternalFields();
}
// multiple field return
return new CommandResponse(ResponseCode.NO_ERROR, valuesMap.toString());
} catch (FailedDBOperationException e) {
return new CommandResponse(ResponseCode.DATABASE_OPERATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.DATABASE_OPERATION_ERROR.toString() + " " + e);
}
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class FieldAccess method lookupSingleField.
/**
* Reads the value of field in a guid.
* Field(s) is a string the naming the field(s). Field(s) can us dot
* notation to indicate subfields.
*
* @param header
* @param commandPacket
*
* @param guid
* @param field - mutually exclusive with fields
* @param reader
* @param signature
* @param message
* @param timestamp
* @param handler
* @return the value of a single field
*/
public static CommandResponse lookupSingleField(InternalRequestHeader header, CommandPacket commandPacket, String guid, String field, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
ResponseCode errorCode = signatureAndACLCheckForRead(header, commandPacket, guid, field, // fields
null, reader, signature, message, timestamp, handler.getApp());
if (errorCode.isExceptionOrError()) {
return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
}
ValuesMap valuesMap;
try {
valuesMap = NSFieldAccess.lookupJSONFieldLocally(header, guid, field, handler.getApp());
// note: reader can also be null here
if (!header.verifyInternal()) {
// don't strip internal fields when doing a read for other servers
valuesMap = valuesMap.removeInternalFields();
}
if (valuesMap != null) {
/* arun: changed to not rely on JSONException. The previous code was relying
on valuesMap.getString() throwing a JSONException and conveying a GNSProtocol.JSON_PARSE_ERROR.toString(),
which is incorrect in this case because it should give a FIELD_NOT_FOUND_EXCEPTION
to the client.
*/
if (valuesMap.isNull(field)) {
return new CommandResponse(ResponseCode.FIELD_NOT_FOUND_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.FIELD_NOT_FOUND.toString() + " " + guid + ":" + field + " ");
} else {
// arun: added support for SINGLE_FIELD_VALUE_ONLY flag
return new CommandResponse(ResponseCode.NO_ERROR, SINGLE_FIELD_VALUE_ONLY ? valuesMap.getString(field) : valuesMap.toString());
}
} else {
return new CommandResponse(ResponseCode.NO_ERROR, EMPTY_STRING);
}
} catch (FailedDBOperationException e) {
return new CommandResponse(ResponseCode.DATABASE_OPERATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.DATABASE_OPERATION_ERROR.toString() + " " + e);
} catch (JSONException e) {
return new CommandResponse(ResponseCode.JSON_PARSE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e);
}
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class FieldAccess method signatureAndACLCheckForRead.
/**
*
* @param header
* @param commandPacket
* @param guid
* @param field
* @param fields
* @param reader
* @param signature
* @param message
* @param timestamp
* @param app
* @param skipSigCheck
* @return the ResponseCode
*/
public static ResponseCode signatureAndACLCheckForRead(InternalRequestHeader header, CommandPacket commandPacket, String guid, String field, List<String> fields, String reader, String signature, String message, Date timestamp, GNSApplicationInterface<String> app, boolean skipSigCheck) {
ResponseCode errorCode = ResponseCode.NO_ERROR;
LOGGER.log(Level.FINE, "signatureAndACLCheckForRead guid: {0} field: {1} reader: {2}", new Object[] { guid, field, reader });
try {
assert (header != null);
// note: reader can also be null here
if (!header.verifyInternal() && !commandPacket.getCommandType().isMutualAuth() && (field != null || fields != null)) {
errorCode = NSAuthentication.signatureAndACLCheck(header, guid, field, fields, reader, signature, message, MetaDataTypeName.READ_WHITELIST, app, skipSigCheck);
} else {
LOGGER.log(Level.FINEST, "reader={0}; internal={1} field={2}; fields={3};", new Object[] { reader, header.verifyInternal(), field, fields });
// internal and mutual auth commands don't need ACL checks
if ((header.verifyInternal() && (GNSProtocol.INTERNAL_QUERIER.toString().equals(reader))) || commandPacket.getCommandType().isMutualAuth()) {
return ResponseCode.NO_ERROR;
}
//Fixme: I'm guessing this case is for active code only.
if (field != null) {
errorCode = NSAuthentication.aclCheck(header, guid, field, header.getQueryingGUID(), MetaDataTypeName.READ_WHITELIST, app).getResponseCode();
} else if (fields != null) {
for (String aField : fields) {
AclCheckResult aclResult = NSAuthentication.aclCheck(header, guid, aField, header.getQueryingGUID(), MetaDataTypeName.READ_WHITELIST, app);
if (aclResult.getResponseCode().isExceptionOrError()) {
errorCode = aclResult.getResponseCode();
}
}
}
}
// Check for stale commands.
if (timestamp != null) {
if (timestamp.before(DateUtils.addMinutes(new Date(), -Config.getGlobalInt(GNSConfig.GNSC.STALE_COMMAND_INTERVAL_IN_MINUTES)))) {
errorCode = ResponseCode.STALE_COMMAND_VALUE;
}
}
} catch (InvalidKeyException | InvalidKeySpecException | SignatureException | NoSuchAlgorithmException | FailedDBOperationException | UnsupportedEncodingException e) {
errorCode = ResponseCode.SIGNATURE_ERROR;
}
return errorCode;
}
use of edu.umass.cs.gnscommon.ResponseCode 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);
}
}
Aggregations