Search in sources :

Example 26 with ValuesMap

use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.

the class NSFieldAccess method lookupJSONFieldLocalNoAuth.

/**
   * Looks up the value of a field in the guid on this NameServer.
   * Active code is handled if handleActiveCode is true.
   *
   * Returns the value of a field in a GNSProtocol.GUID.toString() as a ValuesMap.
   *
   * @param header
   * @param guid
   * @param field
   * @param gnsApp
   * @param handleActiveCode
   * @return ResultValue
   * @throws edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException
   */
public static ValuesMap lookupJSONFieldLocalNoAuth(InternalRequestHeader header, String guid, String field, GNSApplicationInterface<String> gnsApp, boolean handleActiveCode) throws FailedDBOperationException {
    ValuesMap valuesMap = lookupFieldLocalNoAuth(guid, field, ColumnFieldType.USER_JSON, gnsApp.getDB());
    if (handleActiveCode) {
        try {
            JSONObject result = ActiveCodeHandler.handleActiveCode(header, guid, field, ActiveCode.READ_ACTION, valuesMap, gnsApp.getDB());
            valuesMap = result != null ? new ValuesMap(result) : valuesMap;
        } catch (InternalRequestException e) {
        // Active code field lookup failed, do nothing and return the original value
        }
    }
    return valuesMap;
}
Also used : JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap)

Example 27 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 28 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 29 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 30 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)

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