use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class AccountAccess method addGuid.
/**
* Adds a new GUID associated with an existing account.
* <p>
* These records will be created:<br>
* GUID: "_GNS_PRIMARY_GUID" -- GUID (primary) for secondary guid<br>
* GUID: "_GNS_GUID_INFO" -- {guid info}<br>
* HRN: "_GNS_GUID" -- GUID<br>
*
* @param header
* @param commandPacket
*
* @param accountInfo
* - the accountInfo of the account to add the GUID to
* @param accountGuidInfo
* @param name
* = the human readable name to associate with the GUID
* @param guid
* - the new GUID
* @param publicKey
* - the public key to use with the new account
* @param handler
* @return status result
*/
public static CommandResponse addGuid(InternalRequestHeader header, CommandPacket commandPacket, AccountInfo accountInfo, GuidInfo accountGuidInfo, String name, String guid, String publicKey, ClientRequestHandlerInterface handler) {
/* arun: The commented out code below checking for duplicates is
* incorrect. What we need to do is to check for conflicts in HRN-GUID
* bindings. If an HRN being created already exists, but the
* corresponding GUID does not exist, we should create it. Otherwise,
* the caller will interpret the duplicate name exception incorrectly as
* a successful creation.
*
* if ((AccountAccess.lookupGuidAnywhere(name, handler)) != null) {
* return new CommandResponse( ResponseCode.DUPLICATE_NAME_EXCEPTION,
* GNSProtocol.BAD_RESPONSE.toString() + " " +
* GNSProtocol.DUPLICATE_NAME.toString() + " " + name); }
*
* if ((AccountAccess.lookupGuidInfoAnywhere(guid, handler)) != null) {
* return new CommandResponse( ResponseCode.DUPLICATE_GUID_EXCEPTION,
* GNSProtocol.BAD_RESPONSE.toString() + " " +
* GNSProtocol.DUPLICATE_GUID.toString() + " " + name); } */
boolean createdName = false, createdGUID = false;
try {
JSONObject jsonHRN = new JSONObject();
jsonHRN.put(HRN_GUID, guid);
ResponseCode code;
code = handler.getInternalClient().createOrExists(new CreateServiceName(name, jsonHRN.toString()));
/* arun: Return the error if we could not createField the HRN
* (alias) record and the error indicates that it is not a duplicate
* ID exception because of a limbo create operation from a previous
* unsuccessful attempt. */
String boundGUID = null;
if (code.equals(ResponseCode.DUPLICATE_ID_EXCEPTION) && !(guid.equals(boundGUID = HRNMatchingGUIDExists(header, handler, code, name, guid)))) {
return new CommandResponse(ResponseCode.CONFLICTING_GUID_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + ResponseCode.CONFLICTING_GUID_EXCEPTION.getProtocolCode() + " " + name + "(" + guid + ")" + " " + (code.getMessage() != null ? code.getMessage() + " " : "") + "; HRN " + name + " is already bound to GUID " + boundGUID + " != " + guid);
}
if (code.isExceptionOrError() && !code.equals(ResponseCode.DUPLICATE_ID_EXCEPTION)) {
return new CommandResponse(code, GNSProtocol.BAD_RESPONSE.toString() + " " + code.getProtocolCode() + " " + name + "(" + guid + ")" + " " + code.getMessage());
}
assert (!code.isExceptionOrError() || guid.equals(boundGUID));
createdName = true;
// else name created
GuidInfo guidInfo = new GuidInfo(name, guid, publicKey);
JSONObject jsonGuid = new JSONObject();
jsonGuid.put(GUID_INFO, guidInfo.toJSONObject());
jsonGuid.put(PRIMARY_GUID, accountInfo.getGuid());
// set up ACL to look like this
// "_GNS_ACL": {
// "READ_WHITELIST": {"+ALL+": {"MD": [<publickey>, "+ALL+"]}},
// "WRITE_WHITELIST": {"+ALL+": {"MD": [<publickey>]}}
JSONObject acl = createACL(GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(GNSProtocol.EVERYONE.toString(), accountGuidInfo.getPublicKey()), GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(accountGuidInfo.getPublicKey()));
// prefix is the same for all acls so just pick one to use here
jsonGuid.put(MetaDataTypeName.READ_WHITELIST.getPrefix(), acl);
// The addGuid needs to be rolled back if the second step fails.
ResponseCode guidCode;
guidCode = handler.getInternalClient().createOrExists(new CreateServiceName(guid, jsonGuid.toString()));
assert (guidCode != null);
String boundHRN = null;
if (guidCode.equals(ResponseCode.DUPLICATE_ID_EXCEPTION) && !name.equals(boundHRN = GUIDMatchingHRNExists(header, handler, guidCode, name, // rollback name creation
guid))) {
return rollback(handler, ResponseCode.CONFLICTING_GUID_EXCEPTION.setMessage(": Existing GUID " + guid + " is associated with " + boundHRN + " and can not be associated with the HRN " + name), name, guid);
}
// redundant to check with GNSClientInternal
if (guidCode.isExceptionOrError() && !guidCode.equals(ResponseCode.DUPLICATE_ID_EXCEPTION)) {
return new CommandResponse(guidCode, GNSProtocol.BAD_RESPONSE.toString() + " " + guidCode.getProtocolCode() + " " + guidCode.getMessage());
}
// else all good, continue
assert (!guidCode.isExceptionOrError() || name.equals(boundHRN)) : "code=" + guidCode + "; boundHRN=" + boundHRN + "; name=" + name + "; for GUID=" + guid;
createdGUID = true;
// else both name and guid created successfully
updateAccountInfoNoAuthentication(header, commandPacket, accountInfo.addGuid(guid).noteUpdate(), handler, true);
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString() + " " + " [created " + name + " and " + guid + " and updated account info successfully]");
} catch (ClientException ce) {
return new CommandResponse(ce.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " " + ce.getCode() + " " + ce.getMessage() + (createdName ? "; created " + name + (createdGUID ? "; created " + guid + "; failed to update account info" : "") : "; created neither " + name + " nor " + guid));
} catch (JSONException | ServerRuntimeException e) {
return CommandResponse.toCommandResponse(e);
}
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class ActiveCode method setCode.
/**
* Sets active code for the guid and action.
*
* @param header
* @param guid
* @param commandPacket
* @param action
* @param code
* @param writer
* @param signature
* @param message
* @param timestamp
* @param handler
* @return a {@link ResponseCode}
* @throws org.json.JSONException
*/
public static ResponseCode setCode(InternalRequestHeader header, CommandPacket commandPacket, String guid, String action, String code, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) throws JSONException, IllegalArgumentException {
JSONObject json;
json = new JSONObject();
// getCodeField can throw IllegalArgumentException
json.put(getCodeField(action), code);
ResponseCode response = FieldAccess.updateUserJSON(header, commandPacket, guid, json, writer, signature, message, timestamp, handler);
return response;
}
use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.
the class ActiveCode method clearCode.
/**
* Clears the active code for the guid and action.
*
* @param header
* @param commandPacket
* @param guid
* @param action
* @param writer
* @param signature
* @param message
* @param timestamp
* @param handler
* @return a {@link ResponseCode}
*/
public static ResponseCode clearCode(InternalRequestHeader header, CommandPacket commandPacket, String guid, String action, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) throws IllegalArgumentException {
// can throw IllegalArgumentException
String field = getCodeField(action);
ResponseCode response = FieldAccess.update(header, commandPacket, guid, field, "", null, -1, UpdateOperation.SINGLE_FIELD_REMOVE_FIELD, writer, signature, message, timestamp, handler);
return response;
}
use of edu.umass.cs.gnscommon.ResponseCode 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);
}
use of edu.umass.cs.gnscommon.ResponseCode 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());
}
}
}
Aggregations