Search in sources :

Example 1 with AccountServiceException

use of com.github.ambry.account.AccountServiceException in project ambry by linkedin.

the class AccountAndContainerInjector method injectTargetAccountAndContainerFromBlobId.

/**
 * Obtains the target {@link Account} and {@link Container} id from the blobId string, queries the {@link AccountService}
 * to get the corresponding {@link Account} and {@link Container}, and injects the target {@link Account} and
 * {@link Container} into the {@link RestRequest}.
 * @param blobId The blobId to get the target {@link Account} and {@link Container} id.
 * @param restRequest The rest request to insert the target {@link Account} and {@link Container}.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException if 1) either {@link Account} or {@link Container} could not be found; or 2)
 *                              either {@link Account} or {@link Container} IDs were explicitly specified as
 *                              {@link Account#UNKNOWN_ACCOUNT_ID} or {@link Container#UNKNOWN_CONTAINER_ID}.
 */
public void injectTargetAccountAndContainerFromBlobId(BlobId blobId, RestRequest restRequest, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    Account targetAccount = accountService.getAccountById(blobId.getAccountId());
    if (targetAccount == null) {
        frontendMetrics.getHeadDeleteUnrecognizedAccountCount.inc();
        // @todo The check can be removed once HelixAccountService is running with UNKNOWN_ACCOUNT created.
        if (blobId.getAccountId() != Account.UNKNOWN_ACCOUNT_ID) {
            throw new RestServiceException("Account from blobId=" + blobId.getID() + "with accountId=" + blobId.getAccountId() + " cannot be recognized", RestServiceErrorCode.InvalidAccount);
        } else {
            logger.debug("Account cannot be found for blobId={} with accountId={}. Setting targetAccount to UNKNOWN_ACCOUNT", blobId.getID(), blobId.getAccountId());
            targetAccount = accountService.getAccountById(Account.UNKNOWN_ACCOUNT_ID);
        }
    }
    Container targetContainer;
    try {
        targetContainer = accountService.getContainerById(blobId.getAccountId(), blobId.getContainerId());
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container with Id= " + blobId.getContainerId() + " from account " + targetAccount.getName() + "for blobId=" + blobId.getID(), RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    if (targetContainer == null) {
        frontendMetrics.getHeadDeleteUnrecognizedContainerCount.inc();
        throw new RestServiceException("Container from blobId=" + blobId.getID() + "with accountId=" + blobId.getAccountId() + " containerId=" + blobId.getContainerId() + " cannot be recognized", RestServiceErrorCode.InvalidContainer);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Example 2 with AccountServiceException

use of com.github.ambry.account.AccountServiceException in project ambry by linkedin.

the class AccountAndContainerInjector method injectAccountAndContainerForNamedBlob.

/**
 * Injects target {@link Account} and {@link Container} for named blob requests. This will treat the request path as
 * a named blob path that includes the account and container names.
 * @param restRequest The Put {@link RestRequest}.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException
 */
public void injectAccountAndContainerForNamedBlob(RestRequest restRequest, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    accountAndContainerSanityCheck(restRequest);
    NamedBlobPath namedBlobPath = NamedBlobPath.parse(getRequestPath(restRequest), restRequest.getArgs());
    String accountName = namedBlobPath.getAccountName();
    Account targetAccount = accountService.getAccountByName(accountName);
    if (targetAccount == null) {
        frontendMetrics.unrecognizedAccountNameCount.inc();
        throw new RestServiceException("Account cannot be found for accountName=" + accountName + " in put request with account and container headers.", RestServiceErrorCode.InvalidAccount);
    }
    ensureAccountNameMatch(targetAccount, restRequest);
    String containerName = namedBlobPath.getContainerName();
    Container targetContainer;
    try {
        targetContainer = accountService.getContainerByName(accountName, containerName);
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container " + containerName + " from account " + accountName + " in put request with account and container headers.", RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    if (targetContainer == null) {
        frontendMetrics.unrecognizedContainerNameCount.inc();
        throw new RestServiceException("Container cannot be found for accountName=" + accountName + " and containerName=" + containerName + " in put request with account and container headers.", RestServiceErrorCode.InvalidContainer);
    }
    if (targetContainer.getNamedBlobMode() == Container.NamedBlobMode.DISABLED) {
        throw new RestServiceException("Named blob APIs disabled for this container. account=" + accountName + ", container=" + containerName, RestServiceErrorCode.BadRequest);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Example 3 with AccountServiceException

use of com.github.ambry.account.AccountServiceException in project ambry by linkedin.

the class AccountAndContainerInjector method injectAccountAndContainerUsingServiceId.

/**
 * Inject {@link Account} and {@link Container} into a {@link RestRequest} based on a blob's service ID and
 * privacy setting.
 * @param restRequest The {@link RestRequest} to inject {@link Account} and {@link Container} object.
 * @param serviceId The service ID associated with the blob.
 * @param isPrivate The blob's privacy setting.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException if either of {@link Account} or {@link Container} object could not be found.
 */
private void injectAccountAndContainerUsingServiceId(RestRequest restRequest, String serviceId, boolean isPrivate, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    // First, try to see if a migrated account exists for the service ID.
    Account targetAccount = accountService.getAccountByName(serviceId);
    if (targetAccount == null) {
        frontendMetrics.unrecognizedServiceIdCount.inc();
        logger.debug("Account cannot be found for put request with serviceId={}. Setting targetAccount to UNKNOWN_ACCOUNT", serviceId);
        // If a migrated account does not exist fall back to the UNKNOWN_ACCOUNT.
        targetAccount = accountService.getAccountById(Account.UNKNOWN_ACCOUNT_ID);
    }
    // Either the UNKNOWN_ACCOUNT, or the migrated account should contain default public/private containers
    Container targetContainer;
    short containerId = isPrivate ? Container.DEFAULT_PRIVATE_CONTAINER_ID : Container.DEFAULT_PUBLIC_CONTAINER_ID;
    try {
        targetContainer = accountService.getContainerById(targetAccount.getId(), containerId);
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container with Id= " + containerId + " from account " + targetAccount.getName() + "for put request; ServiceId=" + serviceId + ", isPrivate=" + isPrivate, RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    if (targetContainer == null) {
        throw new RestServiceException("Invalid account or container to inject; serviceId=" + serviceId + ", isPrivate=" + isPrivate, RestServiceErrorCode.InternalServerError);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Example 4 with AccountServiceException

use of com.github.ambry.account.AccountServiceException in project ambry by linkedin.

the class AccountAndContainerInjector method injectAccountAndContainerUsingAccountAndContainerHeaders.

/**
 * Injects {@link Account} and {@link Container} for the PUT requests that carry the target account and container headers.
 * @param restRequest The {@link RestRequest} to inject {@link Account} and {@link Container} object.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException if either of {@link Account} or {@link Container} object could not be found.
 */
private void injectAccountAndContainerUsingAccountAndContainerHeaders(RestRequest restRequest, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    String accountName = getHeader(restRequest.getArgs(), Headers.TARGET_ACCOUNT_NAME, false);
    Account targetAccount = accountService.getAccountByName(accountName);
    if (targetAccount == null) {
        frontendMetrics.unrecognizedAccountNameCount.inc();
        throw new RestServiceException("Account cannot be found for accountName=" + accountName + " in put request with account and container headers.", RestServiceErrorCode.InvalidAccount);
    }
    ensureAccountNameMatch(targetAccount, restRequest);
    String containerName = getHeader(restRequest.getArgs(), Headers.TARGET_CONTAINER_NAME, false);
    Container targetContainer;
    try {
        targetContainer = accountService.getContainerByName(accountName, containerName);
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container " + containerName + " from account " + accountName + " for put request with account and container headers.", RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    if (targetContainer == null) {
        frontendMetrics.unrecognizedContainerNameCount.inc();
        throw new RestServiceException("Container cannot be found for accountName=" + accountName + " and containerName=" + containerName + " in put request with account and container headers.", RestServiceErrorCode.InvalidContainer);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Aggregations

Account (com.github.ambry.account.Account)4 AccountServiceException (com.github.ambry.account.AccountServiceException)4 Container (com.github.ambry.account.Container)4 RestServiceException (com.github.ambry.rest.RestServiceException)4