Search in sources :

Example 1 with InternalRequestHeader

use of edu.umass.cs.gnsserver.interfaces.InternalRequestHeader in project GNS by MobilityFirst.

the class NameResolution method lookupGuidField.

/**
   * Lookup the field or fields in the guid.
   * Returns a JSONObject containing the fields and values
   * or null if the domainName doesn't exist.
   * @param addr 
   * @param id 
   * @param domain - the HRN of the guid
   * @param field - the field to lookup (mutually exclusive with fieldNames)
   * @param fields - the fields to lookup (mutually exclusive with fieldNames)
   * @param handler
   * @return a JSONObject containing the fields and values or null
   */
public static JSONObject lookupGuidField(String addr, int id, String domain, String field, ArrayList<String> fields, ClientRequestHandlerInterface handler) {
    /**
     * Querying multiple types together is allowed in DNS protocol, but practically not supported.
     * Therefore, no need for us to implement support for multi-type query.
     */
    /**
     * 1. Lookup guid for the domain name
     */
    String guid = null;
    final ValuesMap result;
    try {
        result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, domain, HRN_GUID, handler.getApp(), false);
        if (result != null) {
            guid = result.getString(HRN_GUID);
        }
    } catch (FailedDBOperationException | JSONException e) {
        NameResolution.getLogger().log(Level.FINE, "No guid for {0}: {1}", new Object[] { domain, e });
        return null;
    }
    /**
     * 2. Lookup the record
     */
    JSONObject value = null;
    if (guid != null) {
        // Generate a DNS header for local read 
        InternalRequestHeader header = new InternalRequestHeader() {

            @Override
            public long getOriginatingRequestID() {
                return id;
            }

            @Override
            public String getOriginatingGUID() {
                try {
                    return result.getString(HRN_GUID);
                } catch (JSONException e) {
                    return null;
                }
            }

            @Override
            public int getTTL() {
                return InternalRequestHeader.DEFAULT_TTL;
            }

            @Override
            public boolean hasBeenCoordinatedOnce() {
                // DNS request does not need coordination
                return false;
            }

            @Override
            public String getSourceAddress() {
                return addr;
            }
        };
        try {
            value = NSFieldAccess.lookupFieldsLocalNoAuth(header, guid, fields, ColumnFieldType.USER_JSON, handler);
        } catch (FailedDBOperationException e) {
            NameResolution.getLogger().log(Level.FINE, "Fetching record failed for {0}: {1}", new Object[] { domain, e });
        }
    } else {
        NameResolution.getLogger().log(Level.FINE, "No guid for {0} is found", new Object[] { domain });
    }
    return value;
}
Also used : JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) InternalRequestHeader(edu.umass.cs.gnsserver.interfaces.InternalRequestHeader) JSONObject(org.json.JSONObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Aggregations

FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)1 InternalRequestHeader (edu.umass.cs.gnsserver.interfaces.InternalRequestHeader)1 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1