use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class FieldAccess method lookupJSONArray.
/**
* Supports reading of the old style data formatted as a JSONArray of strings.
* Much of the internal system data is still stored in this format.
* The new format also supports JSONArrays as part of the
* whole "guid data is a JSONObject" format so we might be
* transitioning away from this altogether at some point.
*
* @param header
* @param commandPacket
* @param guid
* @param field
* @param reader
* @param signature
* @param timestamp
* @param message
* @param handler
* @return a command response
*/
public static CommandResponse lookupJSONArray(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());
}
String resultString;
ResultValue value = NSFieldAccess.lookupListFieldLocallySafe(guid, field, handler.getApp().getDB());
if (!value.isEmpty()) {
try {
resultString = new JSONObject().put(field, value).toString();
} catch (JSONException e) {
return new CommandResponse(ResponseCode.JSON_PARSE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + ResponseCode.JSON_PARSE_ERROR);
}
} else {
resultString = new JSONObject().toString();
}
return new CommandResponse(ResponseCode.NO_ERROR, resultString);
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class FieldAccess method lookupOne.
/**
* Returns the first value of the field in a guid in a an old-style JSONArray field value.
*
* @param header
* @param commandPacket
* @param guid
* @param field
* @param reader
* @param signature
* @param message
* @param timestamp
* @param handler
* @return a command response
*/
public static CommandResponse lookupOne(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());
}
String resultString;
ResultValue value = NSFieldAccess.lookupListFieldLocallySafe(guid, field, handler.getApp().getDB());
if (!value.isEmpty()) {
Object singleValue = value.get(0);
if (singleValue instanceof Number) {
resultString = singleValue.toString();
} else {
resultString = (String) value.get(0);
}
} else {
return new CommandResponse(ResponseCode.FIELD_NOT_FOUND_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.FIELD_NOT_FOUND.toString());
}
return new CommandResponse(ResponseCode.NO_ERROR, resultString);
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class FieldAccess method lookupOneMultipleValues.
/**
* Returns the first value of all the fields in a guid in an old-style JSONArray field value.
*
* @param header
* @param commandPacket
* @param guid
* @param reader
* @param signature
* @param message
* @param timestamp
* @param handler
* @return a command response
*/
public static CommandResponse lookupOneMultipleValues(InternalRequestHeader header, CommandPacket commandPacket, String guid, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, GNSProtocol.ENTIRE_RECORD.toString(), //fields
null, reader, signature, message, timestamp, handler.getApp());
if (errorCode.isExceptionOrError()) {
return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
}
String resultString;
ResponseCode responseCode;
try {
ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldLocally(null, guid, GNSProtocol.ENTIRE_RECORD.toString(), handler.getApp());
if (valuesMap != null) {
resultString = valuesMap.removeInternalFields().toJSONObjectFirstOnes().toString();
responseCode = ResponseCode.NO_ERROR;
} else {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.BAD_GUID_ERROR;
}
} catch (FailedDBOperationException e) {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.DATABASE_OPERATION_ERROR;
} catch (JSONException e) {
resultString = GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e.getMessage();
responseCode = ResponseCode.JSON_PARSE_ERROR;
}
return new CommandResponse(responseCode, resultString);
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class AccountAccess method removeAlias.
/**
* Remove an alias from an account.
*
* @param header
* @param commandPacket
*
* @param accountInfo
* @param alias
* @param writer
* @param signature
* @param message
* @param timestamp
* @param handler
* @return status result
*/
public static CommandResponse removeAlias(InternalRequestHeader header, CommandPacket commandPacket, AccountInfo accountInfo, String alias, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
GNSConfig.getLogger().log(Level.FINE, "ALIAS: {0} ALIASES:{1}", new Object[] { alias, accountInfo.getAliases() });
if (!accountInfo.containsAlias(alias)) {
return new CommandResponse(ResponseCode.BAD_ALIAS_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ALIAS.toString());
}
// remove the GNSProtocol.NAME.toString() -- GUID record
ResponseCode responseCode;
try {
if ((responseCode = handler.getInternalClient().deleteOrNotExists(alias, true)).isExceptionOrError()) {
return new CommandResponse(responseCode, GNSProtocol.BAD_RESPONSE.toString() + " " + responseCode.getProtocolCode() + " " + responseCode.getMessage());
}
} catch (ClientException ce) {
return new CommandResponse(ce.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " " + ce.getCode() + " " + ce.getMessage());
}
// Now updated the account record
accountInfo.removeAlias(alias);
accountInfo.noteUpdate();
if ((responseCode = updateAccountInfo(header, commandPacket, accountInfo.getGuid(), accountInfo, writer, signature, message, timestamp, handler, true)).isExceptionOrError()) {
return new CommandResponse(responseCode, GNSProtocol.BAD_RESPONSE.toString() + " " + responseCode.getProtocolCode());
}
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class Select method aclCheckFilterFields.
/**
* This filters individual fields if the cannot be accessed by the reader.
*
* @param packet
* @param records
* @param reader
* @param app
* @return
*/
private static JSONArray aclCheckFilterFields(SelectRequestPacket packet, JSONArray records, String reader, GNSApplicationInterface<String> app) {
for (int i = 0; i < records.length(); i++) {
try {
JSONObject record = records.getJSONObject(i);
String guid = record.getString(NameRecord.NAME.getName());
// Look at the keys in the values map
JSONObject valuesMap = record.getJSONObject(NameRecord.VALUES_MAP.getName());
Iterator<?> keys = valuesMap.keys();
while (keys.hasNext()) {
String field = (String) keys.next();
if (!InternalField.isInternalField(field)) {
LOGGER.log(Level.FINE, "{0} Checking: {1}", new Object[] { app.getNodeID(), field });
ResponseCode responseCode = NSAuthentication.signatureAndACLCheck(null, guid, field, null, reader, null, null, MetaDataTypeName.READ_WHITELIST, app, true);
if (!responseCode.isOKResult()) {
LOGGER.log(Level.FINE, "{0} Removing: {1}", new Object[] { app.getNodeID(), field });
// removing the offending field
keys.remove();
}
}
}
} catch (JSONException | InvalidKeyException | InvalidKeySpecException | SignatureException | NoSuchAlgorithmException | FailedDBOperationException | UnsupportedEncodingException e) {
// ignore json errros
LOGGER.log(Level.FINE, "{0} Problem getting guid from json: {1}", new Object[] { app.getNodeID(), e.getMessage() });
}
}
return records;
}
Aggregations