public static void cloneObjectMetadata(HeadObjectResponse source,
      Map<String, String> dest, CopyObjectRequest.Builder copyObjectRequestBuilder) {

    // Possibly null attributes
    // Allowing nulls to pass breaks it during later use
    if (source.cacheControl() != null) {
      copyObjectRequestBuilder.cacheControl(source.cacheControl());
    }
    if (source.contentDisposition() != null) {
      copyObjectRequestBuilder.contentDisposition(source.contentDisposition());
    }
    if (source.contentEncoding() != null) {
      copyObjectRequestBuilder.contentEncoding(source.contentEncoding());
    }

    if (source.contentType() != null) {
      copyObjectRequestBuilder.contentType(source.contentType());
    }

    if (source.serverSideEncryption() != null) {
      copyObjectRequestBuilder.serverSideEncryption(source.serverSideEncryption());
    }

    if (source.sseCustomerAlgorithm() != null) {
      copyObjectRequestBuilder.copySourceSSECustomerAlgorithm(source.sseCustomerAlgorithm());
    }
    if (source.sseCustomerKeyMD5() != null) {
      copyObjectRequestBuilder.copySourceSSECustomerKeyMD5(source.sseCustomerKeyMD5());
    }

    // copy user metadata except the magic marker header.
    source.metadata().entrySet().stream()
        .filter(e -> !e.getKey().equals(X_HEADER_MAGIC_MARKER))
        .forEach(e -> dest.put(e.getKey(), e.getValue()));
  }
@Test
  public void testMetadataCopySkipsMagicAttribute() throws Throwable {

    final String owner = "x-header-owner";
    final String root = "root";
    CONTEXT_ACCESSORS.userHeaders.put(owner, root);
    final HeadObjectResponse source = CONTEXT_ACCESSORS
        .getObjectMetadata(MAGIC_KEY);
    final Map<String, String> sourceUserMD = source.metadata();
    Assertions.assertThat(sourceUserMD.get(owner))
        .describedAs("owner header in copied MD")
        .isEqualTo(root);

    Map<String, String> destUserMetadata = new HashMap<>();
    headerProcessing.cloneObjectMetadata(source, destUserMetadata, CopyObjectRequest.builder());

    Assertions.assertThat(destUserMetadata.get(X_HEADER_MAGIC_MARKER))
        .describedAs("Magic marker header in copied MD")
        .isNull();
    Assertions.assertThat(destUserMetadata.get(owner))
        .describedAs("owner header in copied MD")
        .isEqualTo(root);
  }