Search in sources :

Example 31 with FailedDBOperationException

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

Example 32 with FailedDBOperationException

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

the class FieldAccess method selectGroupSetupQuery.

/**
   * Sends a select request to the server to setup a context aware group guid and retrieve all the guids matching the query.
   *
   * @param header
   * @param commandPacket
   * @param reader
   * @param accountGuid
   * @param query
   * @param publicKey
   * @param interval - the refresh interval (queries made more quickly than this will get a cached value)
   * @param signature
   * @param message
   * @param handler
   * @return a command response
   * @throws InternalRequestException
   */
public static CommandResponse selectGroupSetupQuery(InternalRequestHeader header, CommandPacket commandPacket, String reader, String accountGuid, String query, String publicKey, int interval, String signature, String message, ClientRequestHandlerInterface handler) throws InternalRequestException {
    String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
    // Check to see if the guid doesn't exists and if so create it...
    if (AccountAccess.lookupGuidInfoAnywhere(header, guid, handler) == null) {
        // This code is similar to the code in AddGuid command except that we're not checking signatures... yet.
        // FIXME: This should probably include authentication
        GuidInfo accountGuidInfo;
        if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, accountGuid, handler)) == null) {
            return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + accountGuid);
        }
        AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, accountGuid, handler);
        if (accountInfo == null) {
            return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + accountGuid);
        }
        if (!accountInfo.isVerified()) {
            return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
        } else if (accountInfo.getGuids().size() > Config.getGlobalInt(GNSConfig.GNSC.ACCOUNT_GUID_MAX_SUBGUIDS)) {
            return new CommandResponse(ResponseCode.TOO_MANY_GUIDS_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.TOO_MANY_GUIDS.toString());
        } else {
            // The alias (HRN) of the new guid is a hash of the query.
            String name = Base64.encodeToString(ShaOneHashFunction.getInstance().hash(query), false);
            CommandResponse groupGuidCreateresult = AccountAccess.addGuid(header, commandPacket, accountInfo, accountGuidInfo, name, guid, publicKey, handler);
            // If there was a problem adding return that error response.
            if (!groupGuidCreateresult.getExceptionOrErrorCode().isOKResult()) {
                return groupGuidCreateresult;
            }
        }
    }
    JSONArray result;
    try {
        SelectRequestPacket packet = SelectRequestPacket.MakeGroupSetupRequest(-1, reader, query, null, guid, interval);
        result = executeSelectHelper(header, commandPacket, packet, reader, signature, message, handler.getApp());
        if (result != null) {
            return new CommandResponse(ResponseCode.NO_ERROR, result.toString());
        }
    } catch (IOException | JSONException | FailedDBOperationException e) {
        LOGGER.log(Level.FINE, "Silently failing select for query: {0} field: {1} reader: {2} due to {3}", new Object[] { guid, query, reader, e.getMessage() });
    }
    return new CommandResponse(ResponseCode.NO_ERROR, EMPTY_JSON_ARRAY_STRING);
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) SelectRequestPacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket) IOException(java.io.IOException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 33 with FailedDBOperationException

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

the class Select method getMySelectedRecords.

@SuppressWarnings("unchecked")
private static SelectResponsePacket getMySelectedRecords(SelectRequestPacket request, GNSApplicationInterface<String> app) {
    SelectResponsePacket response;
    try {
        // grab the records
        JSONArray jsonRecords = getJSONRecordsForSelect(request, app);
        jsonRecords = aclCheckFilterReturnedRecord(request, jsonRecords, request.getReader(), app);
        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 self-select request {2}", new Object[] { app.getNodeID(), jsonRecords.length(), request.getSummary() });
    } catch (FailedDBOperationException e) {
        LOGGER.log(Level.SEVERE, "Exception while handling self-select request: {0}", e.getMessage());
        //e.printStackTrace();
        response = SelectResponsePacket.makeFailPacket(request.getId(), request.getClientAddress(), request.getNsQueryId(), app.getNodeAddress(), e.getMessage());
    }
    return response;
}
Also used : SelectResponsePacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket) JSONArray(org.json.JSONArray) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 34 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException 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);
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 35 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException 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);
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) 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