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);
  }
Injects {@link Account} and {@link Container} for the PUT and GET 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.
ambry-frontend/src/main/java/com/github/ambry/frontend/AccountAndContainerInjector.java