Search in sources :

Example 31 with ValuesMap

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

Example 32 with ValuesMap

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;
}
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 33 with ValuesMap

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

Example 34 with ValuesMap

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);
    }
}
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 ValuesMap

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);
    }
}
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

ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)35 JSONException (org.json.JSONException)25 JSONObject (org.json.JSONObject)24 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)20 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)9 IOException (java.io.IOException)8 Test (org.junit.Test)8 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)7 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)6 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)5 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)5 RandomString (edu.umass.cs.gnscommon.utils.RandomString)4 ArrayList (java.util.ArrayList)4 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)3 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)3 HashMap (java.util.HashMap)3 JSONArray (org.json.JSONArray)3 ParseException (java.text.ParseException)2 Future (java.util.concurrent.Future)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2