Search in sources :

Example 46 with FailedDBOperationException

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();
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) DBCollection(com.mongodb.DBCollection) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Example 47 with FailedDBOperationException

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());
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 48 with FailedDBOperationException

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());
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) Date(java.util.Date) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 49 with FailedDBOperationException

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;
}
Also used : ResultValue(edu.umass.cs.gnsserver.utils.ResultValue) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) IOException(java.io.IOException) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Aggregations

FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)49 JSONException (org.json.JSONException)36 JSONObject (org.json.JSONObject)36 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)22 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)18 BasicDBObject (com.mongodb.BasicDBObject)13 MongoException (com.mongodb.MongoException)13 DBCollection (com.mongodb.DBCollection)12 DBObject (com.mongodb.DBObject)12 IOException (java.io.IOException)11 JSONArray (org.json.JSONArray)9 Test (org.junit.Test)9 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)7 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)7 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)7 DBCursor (com.mongodb.DBCursor)6 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)6 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)6 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)5 RandomString (edu.umass.cs.gnscommon.utils.RandomString)4