use of edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException in project GNS by MobilityFirst.
the class ActiveCodeHandler method handleActiveCode.
/**
* This interface is used for the class out of activecode package to trigger active code.
* It requires the parameters for running active code such as guid, field, and value.
* It runs the requests and returns the processed result to the caller.
*
*
* @param header header is needed for depth query
* @param guid
* @param field
* @param action the actions in {@code ActiveCode}
* @param value
* @param db db is needed for fetching active code to run
* @return the processed result as an JSONObject, the original value is returned if there is an error with code execution
* @throws InternalRequestException
*/
public static JSONObject handleActiveCode(InternalRequestHeader header, String guid, String field, String action, JSONObject value, BasicRecordMap db) throws InternalRequestException {
if (Config.getGlobalBoolean(GNSConfig.GNSC.DISABLE_ACTIVE_CODE)) {
return value;
}
if (header != null) {
// this is a depth query, and we do not call the code again, as it will form a infinite loop if not.
if (guid.equals(header.getOriginatingGUID()) && header.getTTL() < InternalRequestHeader.DEFAULT_TTL) {
return value;
}
} else {
// without a header, the code can misbehave without any regulation, therefore we return the value immediately if no header presents
return value;
}
long t = System.nanoTime();
ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO handles:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
/**
* Only execute active code for user field
* FIXME:
* <p>
* Read can be a single-field read or multi-field read.
* If it's a single-field read, then the field can not be a internal field.
* If it's a multi-field read, then there may be some field is internal.
* <p>
* Write has no field value, but if there should not be an internal
* field in the JSONObject value.
*/
if ((action.equals(ActiveCode.READ_ACTION) && field != null && InternalField.isInternalField(field)) || (action.equals(ActiveCode.WRITE_ACTION) && ((value != null && containInternalField(value)) || (field != null && InternalField.isInternalField(field))))) {
ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO no need to handle:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
return value;
}
JSONObject newResult = value;
if (field == null || !InternalField.isInternalField(field)) {
//FIXME: Seems like this field lookup all could be replaced by something
// like NSFieldAccess.lookupJSONFieldLocalNoAuth
NameRecord activeCodeNameRecord = null;
try {
activeCodeNameRecord = NameRecord.getNameRecordMultiUserFields(db, guid, ColumnFieldType.USER_JSON, ActiveCode.getCodeField(action));
} catch (RecordNotFoundException | FailedDBOperationException | IllegalArgumentException e) {
e.printStackTrace();
return value;
}
ValuesMap codeMap = null;
try {
codeMap = activeCodeNameRecord.getValuesMap();
} catch (FieldNotFoundException e) {
e.printStackTrace();
return value;
}
if (codeMap != null && value != null) {
String code;
try {
code = codeMap.getString(ActiveCode.getCodeField(action));
} catch (JSONException | IllegalArgumentException e) {
return value;
}
// Prepare values for query
String accessorGuid = header == null ? guid : header.getOriginatingGUID();
if (header.getSourceAddress() != null) {
try {
value.put(SOURCE_IP_FIELD, header.getSourceAddress());
} catch (JSONException e) {
e.printStackTrace();
}
}
// Run code
newResult = runCode(header, code, guid, accessorGuid, action, value, header.getTTL());
// Strip the appended fields
if (newResult.has(SOURCE_IP_FIELD)) {
newResult.remove(SOURCE_IP_FIELD);
}
} else if (codeMap == null) {
ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO no code to run:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
}
}
ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO The result after executing active code is {0}", new Object[] { newResult });
DelayProfiler.updateDelayNano("activeTotal", t);
return newResult;
}
use of edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException in project GNS by MobilityFirst.
the class NSFieldAccess method lookupFieldLocalNoAuth.
private static ValuesMap lookupFieldLocalNoAuth(String guid, String field, ColumnFieldType returnFormat, BasicRecordMap database) throws FailedDBOperationException {
NameRecord nameRecord = null;
ClientSupportConfig.getLogger().log(Level.FINE, "XXXXXXXXXXXXXXXXXXXXX LOOKUP_FIELD_LOCALLY: {0} : {1}", new Object[] { guid, field });
// Try to look up the value in the database
try {
// Check for the case where we're returning all the fields the entire record.
if (GNSProtocol.ENTIRE_RECORD.toString().equals(field)) {
ClientSupportConfig.getLogger().log(Level.FINE, "ALL FIELDS: Format={0}", new Object[] { returnFormat });
// need everything so just grab all the fields
nameRecord = NameRecord.getNameRecord(database, guid);
// Otherwise if field is specified we're just looking up that single field.
} else if (field != null) {
ClientSupportConfig.getLogger().log(Level.FINE, "Field={0} Format={1}", new Object[] { field, returnFormat });
long t = System.nanoTime();
// otherwise grab the field the user wanted
nameRecord = NameRecord.getNameRecordMultiUserFields(database, guid, returnFormat, field);
if (Util.oneIn(100)) {
DelayProfiler.updateDelayNano("getNameRecordMultiUserFields", t);
}
}
if (nameRecord != null) {
ClientSupportConfig.getLogger().log(Level.FINE, "VALUES MAP={0}", new Object[] { nameRecord.getValuesMap().toString() });
return nameRecord.getValuesMap();
}
} catch (RecordNotFoundException e) {
ClientSupportConfig.getLogger().log(Level.FINE, "Record not found for name: {0} Key = {1}", new Object[] { guid, field });
} catch (FieldNotFoundException e) {
ClientSupportConfig.getLogger().log(Level.FINE, "Field not found {0} : {1}", new Object[] { guid, e });
}
return null;
}
use of edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException in project GNS by MobilityFirst.
the class NSFieldAccess method lookupFieldsLocalNoAuth.
/**
*
* @param header
* @param guid
* @param fields
* @param returnFormat
* @param handler
* @return a values map
* @throws FailedDBOperationException
*/
public static ValuesMap lookupFieldsLocalNoAuth(InternalRequestHeader header, String guid, List<String> fields, ColumnFieldType returnFormat, ClientRequestHandlerInterface handler) throws FailedDBOperationException {
// Try to look up the value in the database
try {
ClientSupportConfig.getLogger().log(Level.FINE, "Fields={0} Format={1}", new Object[] { fields, returnFormat });
String[] fieldArray = new String[fields.size()];
fieldArray = fields.toArray(fieldArray);
// Grab the fields the user wanted
NameRecord nameRecord = NameRecord.getNameRecordMultiUserFields(handler.getApp().getDB(), guid, returnFormat, fieldArray);
if (nameRecord != null) {
// active code handling
ValuesMap valuesMap = nameRecord.getValuesMap();
//if (!Config.getGlobalBoolean(GNSConfig.GNSC.DISABLE_ACTIVE_CODE)) {
try {
JSONObject result = ActiveCodeHandler.handleActiveCode(header, guid, null, ActiveCode.READ_ACTION, valuesMap, handler.getApp().getDB());
valuesMap = result != null ? new ValuesMap(result) : valuesMap;
} catch (InternalRequestException e) {
e.printStackTrace();
}
//}
return valuesMap;
}
} catch (RecordNotFoundException e) {
ClientSupportConfig.getLogger().log(Level.FINE, "Record not found for name: {0}", guid);
} catch (FieldNotFoundException e) {
ClientSupportConfig.getLogger().log(Level.FINE, "Field not found for {0} : {1}", new Object[] { guid, e });
}
return null;
}
use of edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException in project GNS by MobilityFirst.
the class Admintercessor method collectTaggedGuids.
/**
* Sends a command to collect all guids that contain the given tag.
*
* @param tagName
* @param handler
* @return a set of strings
*/
public Set<String> collectTaggedGuids(String tagName, ClientRequestHandlerInterface handler) {
int id;
if ((id = sendDumpOutputHelper(tagName, handler)) == -1) {
return null;
}
waitForDumpResponse(id);
Map<String, TreeSet<NameRecord>> result = dumpResult.get(id);
dumpResult.remove(id);
if (result != null) {
Set<String> allGuids = new HashSet<>();
for (TreeSet<NameRecord> records : result.values()) {
try {
for (NameRecord record : records) {
allGuids.add(record.getName());
}
} catch (FieldNotFoundException e) {
}
}
return allGuids;
} else {
return null;
}
}
use of edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException in project GNS by MobilityFirst.
the class NoSQLTest method test_21_UpdateEntireRecord.
/**
*
*/
@Test
public void test_21_UpdateEntireRecord() {
JSONObject json = new JSONObject();
try {
JSONObject innerJson = new JSONObject();
innerJson.put("key", "value");
json.put("map", innerJson);
} catch (JSONException e) {
fail("Problem creating json " + e);
}
ValuesMap valuesMap = new ValuesMap(json);
NameRecord nameRecord = new NameRecord(recordMap, guid2, valuesMap);
try {
instance.updateEntireRecord(collection, guid2, nameRecord.getValuesMap());
} catch (FailedDBOperationException | FieldNotFoundException e) {
fail("Problem during insert " + e);
}
}
Aggregations