use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class MongoRecords method insert.
@Override
public void insert(String collectionName, String guid, JSONObject value) throws FailedDBOperationException, RecordExistsException {
db.requestStart();
try {
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
DBObject dbObject;
try {
dbObject = (DBObject) JSON.parse(value.toString());
} catch (Exception e) {
throw new FailedDBOperationException(collectionName, guid, "Unable to parse json" + e.getMessage());
}
try {
collection.insert(dbObject);
} catch (DuplicateKeyException e) {
throw new RecordExistsException(collectionName, guid);
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} insert failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, dbObject.toString(), "Original mongo exception:" + e.getMessage());
}
} finally {
db.requestDone();
}
}
use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class MongoRecords method selectRecordsQuery.
private MongoRecordCursor selectRecordsQuery(String collectionName, ColumnField valuesMapField, String query, List<String> projection, boolean explain) throws FailedDBOperationException {
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
DBCursor cursor = null;
try {
if (projection == null || // in the projection
(!projection.isEmpty() && projection.get(0).equals(GNSProtocol.ENTIRE_RECORD.toString()))) {
cursor = collection.find(parseMongoQuery(query, valuesMapField));
} else {
cursor = collection.find(parseMongoQuery(query, valuesMapField), generateProjection(projection));
}
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecordsQuery failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, query, "Original mongo exception:" + e.getMessage());
}
if (explain) {
System.out.println(cursor.explain().toString());
}
return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class GetCode 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 reader = json.getString(GNSProtocol.READER.toString());
String action = json.getString(GNSProtocol.AC_ACTION.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 {
return new CommandResponse(ResponseCode.NO_ERROR, ActiveCode.getCode(header, commandPacket, guid, action, reader, signature, message, timestamp, handler));
} catch (FailedDBOperationException | 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.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class NSFieldAccess method lookupListFieldAnywhere.
/**
* Looks up the value of an old-style list field in the guid.
* If allowQueryToOtherNSs is true and guid doesn't exists on this Name Server,
* sends a read query from this Name Server to a Local Name Server.
* Returns the value of a field in a GNSProtocol.GUID.toString() as a ResultValue.
*
* @param guid
* @param field
* @param allowRemoteLookup
* @param handler
* @return ResultValue containing the value of the field or an empty ResultValue if field cannot be found
* @throws edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException
*/
public static ResultValue lookupListFieldAnywhere(InternalRequestHeader header, String guid, String field, boolean allowRemoteLookup, ClientRequestHandlerInterface handler) throws FailedDBOperationException {
ResultValue result = lookupListFieldLocallySafe(guid, field, handler.getApp().getDB());
// and we're allowed then send a query to another server
if (result.isEmpty() && !handler.getApp().getDB().containsName(guid) && allowRemoteLookup) {
try {
String stringResult = handler.getInternalClient().execute(GNSCommandInternal.fieldRead(guid, field, header)).getResultString();
result = new ResultValue(stringResult);
} catch (Exception e) {
ClientSupportConfig.getLogger().log(Level.SEVERE, "Problem getting record from remote server: {0}", e);
}
if (!result.isEmpty()) {
ClientSupportConfig.getLogger().log(Level.FINE, "@@@@@@ Field {0} in {1}" + " not found on this server but was found thru remote query. " + "Returning {2}", new Object[] { field, guid, result.toString() });
}
}
return result;
}
Aggregations