Search in sources :

Example 6 with InternalRequestException

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

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

the class AccountAccess method removeGuidInternal.

/**
   * Remove a guid. If ignoreAccountGuid is true we're deleting
   * the account guid as well so we don't have to check or
   * update that info.
   * The accountInfo parameter can be null in which case we
   * look it up.
   *
   * @param header
   * @param commandPacket
   * @param guidInfo
   * @param accountInfo - can be null in which case we look it up
   * @param ignoreAccountGuid
   * @param handler
   * @return the command response
   */
// This can be called from the context of an account guid deleting one if it's
// subguids or a guid deleting itself. The difference being who signs the command,
// but that's outside of this function
private static CommandResponse removeGuidInternal(InternalRequestHeader header, CommandPacket commandPacket, GuidInfo guidInfo, AccountInfo accountInfo, boolean ignoreAccountGuid, ClientRequestHandlerInterface handler) {
    GNSConfig.getLogger().log(Level.FINE, "REMOVE: GUID INFO: {0} ACCOUNT INFO: {1}", new Object[] { guidInfo, accountInfo });
    // (unless we're sure it's not because we're deleting an account guid)
    if (!ignoreAccountGuid) {
        if (lookupAccountInfoFromGuidAnywhere(header, guidInfo.getGuid(), handler) != null) {
            return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guidInfo.getGuid() + " is an account guid");
        }
    }
    // Fill in a missing account info
    if (accountInfo == null) {
        String accountGuid = AccountAccess.lookupPrimaryGuid(header, guidInfo.getGuid(), handler, true);
        // should not happen unless records got messed up in GNS
        if (accountGuid == null) {
            return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + guidInfo.getGuid() + " does not have a primary account guid");
        }
        if ((accountInfo = lookupAccountInfoFromGuidAnywhere(header, accountGuid, handler)) == null) {
            return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + guidInfo.getGuid() + " cannot find primary account guid for " + accountGuid);
        }
    }
    // Step 1 - remove any group links
    ResponseCode removedGroupLinksResponseCode;
    try {
        removedGroupLinksResponseCode = GroupAccess.removeGuidFromGroups(header, commandPacket, guidInfo.getGuid(), handler);
    } catch (IOException | InternalRequestException | JSONException e) {
        removedGroupLinksResponseCode = ResponseCode.UPDATE_ERROR;
    } catch (ClientException e) {
        removedGroupLinksResponseCode = e.getCode();
    }
    // Step 2 - update the account info record unless this is part of an account guid delete
    ResponseCode accountInfoResponseCode;
    if (!ignoreAccountGuid) {
        accountInfo.removeGuid(guidInfo.getGuid());
        accountInfo.noteUpdate();
        accountInfoResponseCode = updateAccountInfoNoAuthentication(header, commandPacket, accountInfo, handler, true);
    } else {
        accountInfoResponseCode = ResponseCode.NO_ERROR;
    }
    // Step 3 - delete the HRN record
    ResponseCode deleteNameResponseCode;
    try {
        deleteNameResponseCode = handler.getInternalClient().deleteOrNotExists(guidInfo.getName(), true);
    } catch (ClientException e) {
        deleteNameResponseCode = e.getCode();
    }
    if ((removedGroupLinksResponseCode.isExceptionOrError() || accountInfoResponseCode.isExceptionOrError()) || deleteNameResponseCode.isExceptionOrError()) {
        // Don't really care who caused the error, other than for debugging.
        return new CommandResponse(ResponseCode.UPDATE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + (removedGroupLinksResponseCode.isOKResult() ? "" : "; failed to remove group links") + (accountInfoResponseCode.isOKResult() ? "" : "; failed to update account info " + accountInfo.getGuid()) + (deleteNameResponseCode.isOKResult() ? "" : "; failed to delete " + guidInfo.getName()));
    } else {
        // Step 3.5 - delete the cache entry
        GUID_INFO_CACHE.invalidate(guidInfo.getGuid());
        // Step 4 - If all the above stuff worked we delete the guid record
        ResponseCode deleteGuidResponseCode;
        try {
            deleteGuidResponseCode = handler.getInternalClient().deleteOrNotExists(guidInfo.getGuid(), true);
        } catch (ClientException e) {
            return new CommandResponse(e.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " Failed to delete " + guidInfo.getGuid());
        }
        if (deleteGuidResponseCode.isOKResult()) {
            return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
        } else {
            return new CommandResponse(deleteGuidResponseCode, GNSProtocol.BAD_RESPONSE.toString() + " Failed to delete " + guidInfo.getGuid());
        }
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) JSONException(org.json.JSONException) RandomString(edu.umass.cs.gnscommon.utils.RandomString) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Example 8 with InternalRequestException

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

the class AccountAccess method removeAccount.

/**
   * Removes a GNS user account.
   *
   * @param header
   * @param commandPacket
   * @param accountInfo
   * @param handler
   * @return status result
   */
public static CommandResponse removeAccount(InternalRequestHeader header, CommandPacket commandPacket, AccountInfo accountInfo, ClientRequestHandlerInterface handler) {
    // Step 1 - remove any group links
    ResponseCode removedGroupLinksResponseCode;
    try {
        removedGroupLinksResponseCode = GroupAccess.removeGuidFromGroups(header, commandPacket, accountInfo.getGuid(), handler);
    } catch (ClientException e) {
        removedGroupLinksResponseCode = e.getCode();
    } catch (IOException | InternalRequestException | JSONException e) {
        removedGroupLinksResponseCode = ResponseCode.UPDATE_ERROR;
    }
    // Step 2 - delete all the aliases records for this account
    ResponseCode deleteAliasesResponseCode = ResponseCode.NO_ERROR;
    for (String alias : accountInfo.getAliases()) {
        ResponseCode responseCode;
        try {
            responseCode = handler.getInternalClient().deleteOrNotExists(alias, true);
        } catch (ClientException e) {
            responseCode = e.getCode();
        }
        if (responseCode.isExceptionOrError()) {
            deleteAliasesResponseCode = ResponseCode.UPDATE_ERROR;
        }
    }
    // Step 3 - delete all the subGuids
    ResponseCode deleteSubGuidsResponseCode = ResponseCode.NO_ERROR;
    for (String subguid : accountInfo.getGuids()) {
        GuidInfo subGuidInfo = lookupGuidInfoAnywhere(header, subguid, handler);
        if (subGuidInfo != null && removeGuidInternal(header, commandPacket, subGuidInfo, accountInfo, true, handler).getExceptionOrErrorCode().isExceptionOrError()) {
            deleteSubGuidsResponseCode = ResponseCode.UPDATE_ERROR;
        }
    }
    // Step 4 - delete the HRN record
    ResponseCode deleteNameResponseCode;
    try {
        deleteNameResponseCode = handler.getInternalClient().deleteOrNotExists(accountInfo.getName(), true);
    } catch (ClientException e) {
        deleteNameResponseCode = e.getCode();
    }
    if ((removedGroupLinksResponseCode.isExceptionOrError() || deleteAliasesResponseCode.isExceptionOrError()) || deleteSubGuidsResponseCode.isExceptionOrError() || deleteNameResponseCode.isExceptionOrError()) {
        // Don't really care who caused the error, other than for debugging.
        return new CommandResponse(ResponseCode.UPDATE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + (removedGroupLinksResponseCode.isOKResult() ? "" : "; failed to remove links") + (deleteAliasesResponseCode.isOKResult() ? "" : "; failed to remove aliases") + (deleteSubGuidsResponseCode.isOKResult() ? "" : "; failed to remove subguids") + (deleteNameResponseCode.isOKResult() ? "" : "failed to delete " + accountInfo.getName()));
    } else {
        // Step 4.5 - delete the cache guid info cache entry
        GUID_INFO_CACHE.invalidate(accountInfo.getGuid());
        // Step 5 - If all the above stuff worked we delete the account guid record
        ResponseCode deleteGuidResponseCode;
        try {
            deleteGuidResponseCode = handler.getInternalClient().deleteOrNotExists(accountInfo.getGuid(), true);
        } catch (ClientException e) {
            return new CommandResponse(e.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " Failed to delete " + accountInfo.getGuid());
        }
        if (deleteGuidResponseCode.isOKResult()) {
            return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
        } else {
            return new CommandResponse(deleteGuidResponseCode, GNSProtocol.BAD_RESPONSE.toString() + " Failed to delete " + accountInfo.getGuid());
        }
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) RandomString(edu.umass.cs.gnscommon.utils.RandomString)

Example 9 with InternalRequestException

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

the class AccountAccess method updateAccountInfo.

private static ResponseCode updateAccountInfo(InternalRequestHeader header, CommandPacket commandPacket, String guid, AccountInfo accountInfo, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler, boolean sendToReplica) {
    try {
        ResponseCode response;
        if (sendToReplica) {
            // We potentially need to send the update to different replica.
            try {
                handler.getInternalClient().execute(GNSCommandInternal.fieldUpdate(guid, ACCOUNT_INFO, accountInfo.toJSONObject(), header));
                response = ResponseCode.NO_ERROR;
            } catch (JSONException e) {
                GNSConfig.getLogger().log(Level.SEVERE, "JSON parse error with remote query:{0}", e);
                response = ResponseCode.JSON_PARSE_ERROR;
            } catch (ClientException | IOException | InternalRequestException e) {
                GNSConfig.getLogger().log(Level.SEVERE, "Problem with remote query:{0}", e);
                response = ResponseCode.UNSPECIFIED_ERROR;
            }
        } else {
            GNSConfig.getLogger().log(Level.FINE, "Updating locally for GUID {0}:{1}<-{1}", new Object[] { guid, ACCOUNT_INFO, accountInfo });
            // Do the update locally.
            JSONObject json = new JSONObject();
            json.put(ACCOUNT_INFO, accountInfo.toJSONObject());
            response = FieldAccess.updateUserJSON(header, commandPacket, guid, json, writer, signature, message, timestamp, handler);
        }
        return response;
    } catch (JSONException e) {
        GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing account info:{0}", e);
        return ResponseCode.JSON_PARSE_ERROR;
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException)

Example 10 with InternalRequestException

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

the class ActiveQueryHandler method handleWriteQuery.

/**
	 * This method handles write query from the worker. 
	 * @param am 
	 * @param header 
	 * @return the response ActiveMessage
	 */
public ActiveMessage handleWriteQuery(ActiveMessage am, InternalRequestHeader header) {
    ActiveMessage resp = null;
    if (header.hasBeenCoordinatedOnce()) {
        return new ActiveMessage(am.getId(), null, "Write failed");
    }
    try {
        // FIXME: the field parameter is deprecated. It is null for this query.
        app.write(header, am.getTargetGuid(), am.getAccessor(), new JSONObject(am.getValue()));
        resp = new ActiveMessage(am.getId(), new JSONObject().toString(), null);
    } catch (ClientException | InternalRequestException | JSONException e) {
        resp = new ActiveMessage(am.getId(), null, "Write failed");
    }
    return resp;
}
Also used : JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) JSONException(org.json.JSONException) 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