Search in sources :

Example 1 with InternalRequestException

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

the class NSFieldAccess method lookupFieldsLocalNoAuth.

/**
   *
   * @param header
   * @param guid
   * @param fields
   * @param returnFormat
   * @param handler
   * @return a values map
   * @throws FailedDBOperationException
   */
public static ValuesMap lookupFieldsLocalNoAuth(InternalRequestHeader header, String guid, List<String> fields, ColumnFieldType returnFormat, ClientRequestHandlerInterface handler) throws FailedDBOperationException {
    // Try to look up the value in the database
    try {
        ClientSupportConfig.getLogger().log(Level.FINE, "Fields={0} Format={1}", new Object[] { fields, returnFormat });
        String[] fieldArray = new String[fields.size()];
        fieldArray = fields.toArray(fieldArray);
        // Grab the fields the user wanted
        NameRecord nameRecord = NameRecord.getNameRecordMultiUserFields(handler.getApp().getDB(), guid, returnFormat, fieldArray);
        if (nameRecord != null) {
            // active code handling
            ValuesMap valuesMap = nameRecord.getValuesMap();
            //if (!Config.getGlobalBoolean(GNSConfig.GNSC.DISABLE_ACTIVE_CODE)) {
            try {
                JSONObject result = ActiveCodeHandler.handleActiveCode(header, guid, null, ActiveCode.READ_ACTION, valuesMap, handler.getApp().getDB());
                valuesMap = result != null ? new ValuesMap(result) : valuesMap;
            } catch (InternalRequestException e) {
                e.printStackTrace();
            }
            //}
            return valuesMap;
        }
    } catch (RecordNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Record not found for name: {0}", guid);
    } catch (FieldNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Field not found for {0} : {1}", new Object[] { guid, e });
    }
    return null;
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) JSONObject(org.json.JSONObject)

Example 2 with InternalRequestException

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

the class AddToGroup method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, ParseException {
    JSONObject json = commandPacket.getCommand();
    String guid = json.getString(GNSProtocol.GUID.toString());
    String member = json.getString(GNSProtocol.MEMBER.toString());
    // writer might be same as guid
    String writer = json.optString(GNSProtocol.WRITER.toString(), guid);
    // signature and message can be empty for unsigned cases
    String signature = json.optString(GNSProtocol.SIGNATURE.toString(), null);
    String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);
    Date timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : // can be null on older client
    null;
    ResponseCode responseCode;
    try {
        if (!(responseCode = GroupAccess.addToGroup(header, guid, member, writer, signature, message, timestamp, handler)).isExceptionOrError()) {
            return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
        } else {
            return new CommandResponse(responseCode, GNSProtocol.BAD_RESPONSE.toString() + " " + responseCode.getProtocolCode());
        }
    } catch (ClientException | IOException | InternalRequestException e) {
        return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) Date(java.util.Date)

Example 3 with InternalRequestException

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

the class AccountAccess method lookupAccountInfoFromGuid.

/**
   * Obtains the account info record for the given guid if that guid was used
   * to createField an account.
   * <p>
   * GUID: "ACCOUNT_INFO" -- {account} for primary guid<br>
   * GUID: "GUID" -- guid (primary) for secondary guid<br>
   * GUID: "GUID_INFO" -- {guid info}<br>
   * HRN: "GUID" -- GUID<br>
   * <p>
   * GUID = Globally Unique Identifier<br>
   * HRN = Human Readable Name<br>
   *
   * @param guid
   * @param handler
   * @param allowRemoteLookup
   * @return the account info record or null if it could not be found
   */
private static AccountInfo lookupAccountInfoFromGuid(InternalRequestHeader header, String guid, ClientRequestHandlerInterface handler, boolean allowRemoteLookup) {
    try {
        ValuesMap result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, guid, ACCOUNT_INFO, handler.getApp(), false);
        GNSConfig.getLogger().log(Level.FINE, "ValuesMap for {0} / {1}: {2}", new Object[] { guid, ACCOUNT_INFO, result != null ? result.getSummary() : result });
        if (result != null) {
            return new AccountInfo(new JSONObject(result.getString(ACCOUNT_INFO)));
        }
    } catch (FailedDBOperationException | JSONException | ParseException e) {
    // Do nothing as this is a normal result when the record doesn't
    // exist.
    }
    GNSConfig.getLogger().log(Level.FINE, "ACCOUNT_INFO NOT FOUND for {0}", guid);
    if (allowRemoteLookup) {
        GNSConfig.getLogger().log(Level.FINE, "LOOKING REMOTELY for ACCOUNT_INFO for {0}", guid);
        String value = null;
        try {
            value = handler.getInternalClient().execute(GNSCommandInternal.fieldRead(guid, ACCOUNT_INFO, header)).getResultString();
        } catch (IOException | JSONException | ClientException e) {
        } catch (InternalRequestException e) {
            //FIXME: This should do something other than print a stack trace
            e.printStackTrace();
        }
        // exist.
        if (value != null) {
            try {
                return new AccountInfo(new JSONObject(value));
            } catch (JSONException | ParseException e) {
            // Do nothing as this is a normal result when the record
            // doesn't exist.
            }
        }
    }
    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) ParseException(java.text.ParseException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Example 4 with InternalRequestException

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

the class AccountAccess method lookupPrimaryGuid.

/**
   * If this is a subguid associated with an account, returns the guid of that
   * account, otherwise returns null.
   * <p>
   * guid = Globally Unique Identifier
   *
   * @param header
   * @param guid
   * @param handler
   * @param allowRemoteLookup
   * @return a guid
   */
public static String lookupPrimaryGuid(InternalRequestHeader header, String guid, ClientRequestHandlerInterface handler, boolean allowRemoteLookup) {
    try {
        ValuesMap result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, guid, PRIMARY_GUID, handler.getApp(), false);
        GNSConfig.getLogger().log(Level.FINE, "ValuesMap for {0} / {1}: {2}", new Object[] { guid, PRIMARY_GUID, result });
        if (result != null) {
            return result.getString(PRIMARY_GUID);
        }
    } catch (FailedDBOperationException | JSONException e) {
        GNSConfig.getLogger().log(Level.SEVERE, "Problem extracting PRIMARY_GUID from {0} :{1}", new Object[] { guid, e });
    }
    String value = null;
    GNSConfig.getLogger().log(Level.FINE, "PRIMARY_GUID NOT FOUND LOCALLY for {0}", guid);
    if (allowRemoteLookup) {
        GNSConfig.getLogger().log(Level.FINE, "LOOKING REMOTELY for PRIMARY_GUID for {0}", guid);
        try {
            value = handler.getInternalClient().execute(GNSCommandInternal.fieldRead(guid, PRIMARY_GUID, header)).getResultString();
            if (!FieldAccess.SINGLE_FIELD_VALUE_ONLY && value != null) {
                value = new JSONObject(value).getString(PRIMARY_GUID);
            }
        } catch (IOException | JSONException | ClientException | InternalRequestException e) {
            GNSConfig.getLogger().log(Level.SEVERE, "Problem getting HRN_GUID for {0} from remote server: {1}", new Object[] { guid, 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 5 with InternalRequestException

use of edu.umass.cs.gnscommon.exceptions.server.InternalRequestException 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)

Aggregations

InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)17 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)15 IOException (java.io.IOException)13 JSONObject (org.json.JSONObject)13 JSONException (org.json.JSONException)10 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)7 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)7 RandomString (edu.umass.cs.gnscommon.utils.RandomString)6 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)5 CommandResponse (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse)4 Date (java.util.Date)4 ResultValue (edu.umass.cs.gnsserver.utils.ResultValue)2 ParseException (java.text.ParseException)2 ClientRequest (edu.umass.cs.gigapaxos.interfaces.ClientRequest)1 Request (edu.umass.cs.gigapaxos.interfaces.Request)1 RequestIdentifier (edu.umass.cs.gigapaxos.interfaces.RequestIdentifier)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)1 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)1 AdminCommandPacket (edu.umass.cs.gnscommon.packets.AdminCommandPacket)1 CommandPacket (edu.umass.cs.gnscommon.packets.CommandPacket)1