use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class AccountAccess method lookupGuidInfo.
/**
* Obtains the guid info record from the database for guid given.
* <p>
* guid = Globally Unique Identifier<br>
*
* @param guid
* @param handler
* @param allowRemoteLookup
* @return an {@link GuidInfo} instance
*/
private static GuidInfo lookupGuidInfo(InternalRequestHeader header, String guid, ClientRequestHandlerInterface handler, boolean allowRemoteLookup) {
GuidInfo result;
if ((result = GUID_INFO_CACHE.getIfPresent(guid)) != null) {
GNSConfig.getLogger().log(Level.FINE, "GuidInfo found in cache {0}", guid);
return result;
}
GNSConfig.getLogger().log(Level.FINE, "allowRemoteLookup is {0}", allowRemoteLookup);
try {
ValuesMap valuesMapResult = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, guid, GUID_INFO, handler.getApp(), false);
GNSConfig.getLogger().log(Level.FINE, "ValuesMap for {0} / {1} {2}", new Object[] { guid, GUID_INFO, valuesMapResult != null ? valuesMapResult.getSummary() : valuesMapResult });
if (valuesMapResult != null) {
result = new GuidInfo(new JSONObject(valuesMapResult.getString(GUID_INFO)));
GUID_INFO_CACHE.put(guid, result);
return result;
}
} catch (FailedDBOperationException | JSONException | ParseException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem extracting GUID_INFO from {0} :{1}", new Object[] { guid, e });
}
GNSConfig.getLogger().log(Level.FINE, "GUID_INFO NOT FOUND for {0}", guid);
if (allowRemoteLookup) {
GNSConfig.getLogger().log(Level.FINE, "LOOKING REMOTELY for GUID_INFO for {0}", guid);
String value = null;
Object obj;
try {
value = (obj = handler.getInternalClient().execute(GNSCommandInternal.fieldRead(guid, GUID_INFO, header)).getResultMap().get(GUID_INFO)) != null ? obj.toString() : value;
} catch (IOException | JSONException | ClientException | InternalRequestException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem getting GUID_INFO for {0} from remote server: {1}", new Object[] { guid, e });
}
if (value != null) {
try {
result = new GuidInfo(new JSONObject(value));
GUID_INFO_CACHE.put(guid, result);
return result;
} catch (JSONException | ParseException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing GUID_INFO value from remote server for {0}: {1}", new Object[] { guid, e });
}
}
}
return null;
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class AccountAccess method lookupGuid.
/**
* Returns the guid associated with name which is a HRN or null if one of
* that name does not exist.
* <p>
* guid = Globally Unique Identifier<br>
* HRN = Human Readable Name<br>
*
* @param name
* @param handler
* @param allowRemoteLookup
* @return a guid or null if the corresponding guid does not exist
*/
private static String lookupGuid(InternalRequestHeader header, String name, ClientRequestHandlerInterface handler, boolean allowRemoteLookup) {
try {
ValuesMap result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, name, HRN_GUID, handler.getApp(), false);
GNSConfig.getLogger().log(Level.FINE, "ValuesMap for {0} / {1}: {2}", new Object[] { name, HRN_GUID, result });
if (result != null) {
return result.getString(HRN_GUID);
}
} catch (FailedDBOperationException | JSONException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem extracting HRN_GUID from {0} :{1}", new Object[] { name, e });
}
//
String value = null;
GNSConfig.getLogger().log(Level.FINE, "HRN_GUID NOT FOUND for {0}", name);
if (allowRemoteLookup) {
GNSConfig.getLogger().log(Level.FINE, "LOOKING REMOTELY for HRN_GUID for {0}", name);
try {
value = handler.getInternalClient().execute(GNSCommandInternal.fieldRead(name, HRN_GUID, header)).getResultString();
if (!FieldAccess.SINGLE_FIELD_VALUE_ONLY && value != null) {
GNSConfig.getLogger().log(Level.FINE, "Found HRN_GUID for {0}:{1}", new Object[] { name, value });
value = new JSONObject(value).getString(HRN_GUID);
}
} catch (IOException | JSONException | ClientException | InternalRequestException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem getting HRN_GUID for {0} from remote server: {1}", new Object[] { name, e });
}
}
return value;
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class ActiveCode method getCode.
/**
* Gets the currently set active code for the guid and action.
*
* @param header
* @param commandPacket
* @param guid
* @param action
* @param reader
* @param signature
* @param message
* @param timestamp
* @param handler
* @return a string
* @throws edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException
* @throws org.json.JSONException
*/
public static String getCode(InternalRequestHeader header, CommandPacket commandPacket, String guid, String action, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) throws IllegalArgumentException, FailedDBOperationException, JSONException {
// can throw IllegalArgumentException
String field = getCodeField(action);
ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, field, // fields
null, reader, signature, message, timestamp, handler.getApp());
if (errorCode.isExceptionOrError()) {
return GNSProtocol.NULL_RESPONSE.toString();
}
ValuesMap result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, guid, field, handler.getApp(), // the false disables active code handling which we obviously don't want here
false);
return result.getString(field);
}
use of edu.umass.cs.gnsserver.utils.ValuesMap 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.gnsserver.utils.ValuesMap 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);
}
}
Aggregations