Content implements Payload { public static void extractFromAsset(final Asset asset, final Iterable<HashAlgorithm> hashAlgorithms, final AttributesMap contentAttributes) { checkNotNull(asset); checkNotNull(hashAlgorithms); final NestedAttributesMap assetAttributes = asset.attributes().child(CONTENT); final DateTime lastModified = toDateTime(assetAttributes.get(P_LAST_MODIFIED, Date.class)); final String etag = assetAttributes.get(P_ETAG, String.class); final Map<HashAlgorithm, HashCode> checksums = asset.getChecksums(hashAlgorithms); contentAttributes.set(Asset.class, asset); contentAttributes.set(Content.CONTENT_LAST_MODIFIED, lastModified); contentAttributes.set(Content.CONTENT_ETAG, etag); contentAttributes.set(Content.CONTENT_HASH_CODES_MAP, checksums); contentAttributes.set(CacheInfo.class, CacheInfo.extractFromAsset(asset)); } Content(final Payload payload); protected  Content(final Payload payload, final AttributesMap attributes); @Override InputStream openInputStream(); @Override long getSize(); @Nullable @Override String getContentType(); @Override void close(); @Override void copy(final InputStream inputStream, final OutputStream outputStream); @Nonnull AttributesMap getAttributes(); @Override String toString(); static void extractFromAsset(final Asset asset,
                                      final Iterable<HashAlgorithm> hashAlgorithms,
                                      final AttributesMap contentAttributes); static void applyToAsset(final Asset asset, final AttributesMap contentAttributes); @Nullable static Asset findAsset(final StorageTx tx, final Bucket bucket, Content content); @Nonnull static AttributesMap maintainLastModified(final Asset asset,
                                                   @Nullable final AttributesMap contentAttributes); @VisibleForTesting
  static final String CONTENT; static final String CONTENT_LAST_MODIFIED; static final String CONTENT_ETAG; static final String CONTENT_HASH_CODES_MAP; static final TypeToken<Map<HashAlgorithm, HashCode>> T_CONTENT_HASH_CODES_MAP; @VisibleForTesting
  static final String P_LAST_MODIFIED; @VisibleForTesting
  static final String P_ETAG; }
@Test public void nothingToExtract() { final NestedAttributesMap attributes = nestedAttributesMap(); Asset asset = mock(Asset.class); when(asset.attributes()).thenReturn(attributes); AttributesMap contentAttributes = new AttributesMap(); Content.extractFromAsset(asset, ALGORITHMS, contentAttributes); assertThat(contentAttributes.contains(Content.CONTENT_LAST_MODIFIED), is(false)); assertThat(contentAttributes.contains(Content.CONTENT_ETAG), is(false)); assertThat(contentAttributes.contains(Content.CONTENT_HASH_CODES_MAP), is(true)); }
@Test public void lastModifiedOnlyExtract() { final DateTime now = DateTime.now(); final NestedAttributesMap attributes = nestedAttributesMap(); attributes.child(Content.CONTENT).set(Content.P_LAST_MODIFIED, now.toDate()); Asset asset = mock(Asset.class); when(asset.attributes()).thenReturn(attributes); AttributesMap contentAttributes = new AttributesMap(); Content.extractFromAsset(asset, ALGORITHMS, contentAttributes); assertThat(contentAttributes.get(Content.CONTENT_LAST_MODIFIED, DateTime.class), equalTo(now)); assertThat(contentAttributes.contains(Content.CONTENT_ETAG), is(false)); assertThat(contentAttributes.contains(Content.CONTENT_HASH_CODES_MAP), is(true)); }
@Test public void etagOnlyExtract() { final String etag = "foo-bar"; final NestedAttributesMap attributes = nestedAttributesMap(); attributes.child(Content.CONTENT).set(Content.P_ETAG, etag); Asset asset = mock(Asset.class); when(asset.attributes()).thenReturn(attributes); AttributesMap contentAttributes = new AttributesMap(); Content.extractFromAsset(asset, ALGORITHMS, contentAttributes); assertThat(contentAttributes.contains(Content.CONTENT_LAST_MODIFIED), is(false)); assertThat(contentAttributes.get(Content.CONTENT_ETAG, String.class), equalTo(etag)); assertThat(contentAttributes.contains(Content.CONTENT_HASH_CODES_MAP), is(true)); }
@Test public void lastModifiedAndEtagExtract() { final DateTime now = DateTime.now(); final String etag = "foo-bar"; final NestedAttributesMap attributes = nestedAttributesMap(); attributes.child(Content.CONTENT).set(Content.P_LAST_MODIFIED, now.toDate()); attributes.child(Content.CONTENT).set(Content.P_ETAG, etag); Asset asset = mock(Asset.class); when(asset.attributes()).thenReturn(attributes); AttributesMap contentAttributes = new AttributesMap(); Content.extractFromAsset(asset, ALGORITHMS, contentAttributes); assertThat(contentAttributes.get(Content.CONTENT_LAST_MODIFIED, DateTime.class), equalTo(now)); assertThat(contentAttributes.get(Content.CONTENT_ETAG, String.class), equalTo(etag)); assertThat(contentAttributes.contains(Content.CONTENT_HASH_CODES_MAP), is(true)); }