@Override
  public RestLiResponseEnvelope buildRestLiResponseData(
      RestRequest request,
      RoutingResult routingResult,
      Object result,
      Map<String, String> headers,
      List<HttpCookie> cookies) {
    if (result instanceof BatchCreateKVResult) {
      BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
      if (list.getResults() == null) {
        throw new RestLiServiceException(
            HttpStatus.S_500_INTERNAL_SERVER_ERROR,
            "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: "
                + routingResult.getResourceMethod());
      }
      List<CreateCollectionResponseEnvelope.CollectionCreateResponseItem> collectionCreateList =
          new ArrayList<CreateCollectionResponseEnvelope.CollectionCreateResponseItem>(
              list.getResults().size());

      for (CreateKVResponse e : list.getResults()) {
        if (e == null) {
          throw new RestLiServiceException(
              HttpStatus.S_500_INTERNAL_SERVER_ERROR,
              "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: "
                  + routingResult.getResourceMethod());
        } else {
          Object id =
              ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
          if (e.getError() == null) {
            CreateIdEntityStatus<Object, RecordTemplate> entry =
                new CreateIdEntityStatus<Object, RecordTemplate>(
                    e.getStatus().getCode(),
                    id,
                    e.getEntity(),
                    null,
                    ProtocolVersionUtil.extractProtocolVersion(headers));
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(entry));
          } else {
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(
                    e.getError(), id));
          }
        }
      }
      return new CreateCollectionResponseEnvelope(collectionCreateList, headers, cookies);
    } else {
      BatchCreateResult<?, ?> list = (BatchCreateResult<?, ?>) result;

      // Verify that a null list was not passed into the BatchCreateResult. If so, this is a
      // developer error.
      if (list.getResults() == null) {
        throw new RestLiServiceException(
            HttpStatus.S_500_INTERNAL_SERVER_ERROR,
            "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: "
                + routingResult.getResourceMethod());
      }

      List<CreateCollectionResponseEnvelope.CollectionCreateResponseItem> collectionCreateList =
          new ArrayList<CreateCollectionResponseEnvelope.CollectionCreateResponseItem>(
              list.getResults().size());
      for (CreateResponse e : list.getResults()) {
        // Verify that a null element was not passed into the BatchCreateResult list. If so, this is
        // a developer error.
        if (e == null) {
          throw new RestLiServiceException(
              HttpStatus.S_500_INTERNAL_SERVER_ERROR,
              "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: "
                  + routingResult.getResourceMethod());
        } else {
          Object id =
              ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
          if (e.getError() == null) {
            CreateIdStatus<Object> entry =
                new CreateIdStatus<Object>(
                    e.getStatus().getCode(),
                    id,
                    null,
                    ProtocolVersionUtil.extractProtocolVersion(headers));
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(entry));
          } else {
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(
                    e.getError(), id));
          }
        }
      }

      return new CreateCollectionResponseEnvelope(collectionCreateList, headers, cookies);
    }
  }