Search in sources :

Example 21 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class NoSQLTest method test_20_InsertRecord.

/**
   *
   */
@Test
public void test_20_InsertRecord() {
    JSONObject json = new JSONObject();
    try {
        json.put(field, "some value");
    } catch (JSONException e) {
        fail("Problem creating json " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    NameRecord nameRecord = new NameRecord(recordMap, guid2, valuesMap);
    try {
        instance.insert(collection, guid2, nameRecord.toJSONObject());
    } catch (FailedDBOperationException | JSONException | RecordExistsException e) {
        fail("Problem during insert " + e);
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) Test(org.junit.Test)

Example 22 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class NoSQLTest method test_24_LookupEntireRecordGuid2Again.

/**
   *
   */
@Test
public void test_24_LookupEntireRecordGuid2Again() {
    JSONObject json = new JSONObject();
    try {
        JSONObject innerJson = new JSONObject();
        innerJson.put("key", "value");
        json.put("map", innerJson);
        json.put("new", "newValue");
    } catch (JSONException e) {
        fail("Problem creating json " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    JSONObject expected = new JSONObject();
    try {
        expected.put(NameRecord.NAME.getName(), guid2);
        expected.put(NameRecord.VALUES_MAP.getName(), valuesMap);
    } catch (JSONException e) {
        fail("Problem creating json expected value: " + e);
    }
    try {
        JSONAssert.assertEquals(expected, instance.lookupEntireRecord(collection, guid2), JSONCompareMode.STRICT);
    } catch (RecordNotFoundException | FailedDBOperationException | JSONException e) {
        fail("Problem during LookupEntireRecord: " + e);
    }
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) Test(org.junit.Test)

Example 23 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException 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;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 24 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class Select method aclCheckFilterForRecordsArray.

/**
   * This filters entire records if the query uses fields that cannot be accessed in the
   * returned record by the reader. Otherwise the user would be able to determine that
   * some GUIDS contain specific values for fields they can't access.
   *
   * @param packet
   * @param records
   * @param reader
   * @param app
   * @return
   */
private static JSONArray aclCheckFilterForRecordsArray(SelectRequestPacket packet, JSONArray records, String reader, GNSApplicationInterface<String> app) {
    JSONArray result = new JSONArray();
    for (int i = 0; i < records.length(); i++) {
        try {
            JSONObject record = records.getJSONObject(i);
            String guid = record.getString(NameRecord.NAME.getName());
            List<String> queryFields = getFieldsForQueryType(packet);
            ResponseCode responseCode = NSAuthentication.signatureAndACLCheck(null, guid, null, queryFields, reader, null, null, MetaDataTypeName.READ_WHITELIST, app, true);
            LOGGER.log(Level.FINE, "{0} ACL check for select: guid={0} queryFields={1} responsecode={2}", new Object[] { app.getNodeID(), guid, queryFields, responseCode });
            if (responseCode.isOKResult()) {
                result.put(record);
            }
        } 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 result;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 25 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class Select method handleSelectRequestFromNS.

/**
   * Handle a select request from the collecting NS. This is what other NSs do when they
   * get a SelectRequestPacket from the NS that originally received the packet (the one that is collecting
   * all the records).
   * This NS looks up the records and returns them.
   *
   * @param incomingJSON
   * @param app
   * @throws JSONException
   */
@SuppressWarnings("unchecked")
private static void handleSelectRequestFromNS(SelectRequestPacket request, GNSApplicationInterface<String> app) throws JSONException {
    LOGGER.log(Level.FINE, "NS {0} {1} received query {2}", new Object[] { Select.class.getSimpleName(), app.getNodeID(), request.getSummary() });
    try {
        // grab the records
        JSONArray jsonRecords = getJSONRecordsForSelect(request, app);
        jsonRecords = aclCheckFilterReturnedRecord(request, jsonRecords, request.getReader(), app);
        @SuppressWarnings("unchecked") SelectResponsePacket response = SelectResponsePacket.makeSuccessPacketForFullRecords(request.getId(), request.getClientAddress(), request.getCcpQueryId(), request.getNsQueryId(), app.getNodeAddress(), jsonRecords);
        LOGGER.log(Level.FINE, "NS {0} sending back {1} record(s) in response to {2}", new Object[] { app.getNodeID(), jsonRecords.length(), request.getSummary() });
        // and send them back to the originating NS
        app.sendToAddress(request.getNSReturnAddress(), response.toJSONObject());
    } catch (FailedDBOperationException | JSONException | IOException e) {
        LOGGER.log(Level.SEVERE, "Exception while handling select request: {0}", e);
        SelectResponsePacket failResponse = SelectResponsePacket.makeFailPacket(request.getId(), request.getClientAddress(), request.getNsQueryId(), app.getNodeAddress(), e.getMessage());
        try {
            app.sendToAddress(request.getNSReturnAddress(), failResponse.toJSONObject());
        } catch (IOException f) {
            LOGGER.log(Level.SEVERE, "Unable to send Failure SelectResponsePacket: {0}", f);
        }
    }
}
Also used : SelectResponsePacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) 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