DefaultResource implements Resource { @Override @NotNull public Collection<String> headers() { final ObjectMetadata meta = this.object.getObjectMetadata(); final Collection<String> headers = new LinkedList<String>(); headers.add( DefaultResource.header( HttpHeaders.CONTENT_LENGTH, Long.toString(meta.getContentLength()) ) ); if (meta.getContentType() != null) { headers.add( DefaultResource.header( HttpHeaders.CONTENT_TYPE, meta.getContentType() ) ); } if (meta.getContentEncoding() != null) { headers.add( DefaultResource.header( HttpHeaders.CONTENT_ENCODING, meta.getContentEncoding() ) ); } if (meta.getETag() != null) { headers.add( DefaultResource.header( HttpHeaders.ETAG, meta.getETag() ) ); } headers.add( DefaultResource.header( HttpHeaders.CACHE_CONTROL, StringUtils.defaultString( meta.getCacheControl(), "must-revalidate" ) ) ); headers.add(DefaultResource.header("Accept-Ranges", "bytes")); if (!this.range.equals(Range.ENTIRE)) { headers.add( DefaultResource.header( "Content-Range", String.format( "bytes %d-%d/%d", this.range.first(), this.range.last(), this.size() ) ) ); } return headers; } DefaultResource(@NotNull final AmazonS3 clnt,
        @NotNull final String bckt, @NotNull final String name,
        @NotNull final Range rng, @NotNull final Version ver,
        @NotNull final DomainStatsData dstats); @Override String toString(); @Override int status(); @Override @Loggable( value = Loggable.DEBUG, limit = Integer.MAX_VALUE, ignore = DefaultResource.StreamingException.class ) long writeTo(@NotNull final OutputStream output); @Override @NotNull Collection<String> headers(); @Override @NotNull String etag(); @Override Date lastModified(); @Override String contentType(); @Override void close();  }
@Test public void getsHeadersFromAmazonObject() throws Exception { final AmazonS3 client = Mockito.mock(AmazonS3.class); final S3Object object = Mockito.mock(S3Object.class); Mockito.doReturn(object).when(client) .getObject(Mockito.any(GetObjectRequest.class)); final ObjectMetadata meta = Mockito.mock(ObjectMetadata.class); Mockito.doReturn(meta).when(object).getObjectMetadata(); Mockito.doReturn(1L).when(meta).getContentLength(); final Resource res = new DefaultResource( client, "a", "", Range.ENTIRE, Version.LATEST, Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.headers(), Matchers.hasItem("Content-Length: 1") ); }
@Test public void getsCacheControlHeaderFromAmazonObject() throws Exception { final AmazonS3 client = Mockito.mock(AmazonS3.class); final S3Object object = Mockito.mock(S3Object.class); Mockito.doReturn(object).when(client) .getObject(Mockito.any(GetObjectRequest.class)); final ObjectMetadata meta = Mockito.mock(ObjectMetadata.class); Mockito.doReturn(meta).when(object).getObjectMetadata(); Mockito.doReturn("max-age: 600, public").when(meta).getCacheControl(); final Resource res = new DefaultResource( client, "e", "", Range.ENTIRE, Version.LATEST, Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.headers(), Matchers.hasItem("Cache-Control: max-age: 600, public") ); }
@Test public void getsDefaultCacheControlHeader() throws Exception { final AmazonS3 client = Mockito.mock(AmazonS3.class); final S3Object object = Mockito.mock(S3Object.class); Mockito.doReturn(object).when(client) .getObject(Mockito.any(GetObjectRequest.class)); final ObjectMetadata meta = Mockito.mock(ObjectMetadata.class); Mockito.doReturn(meta).when(object).getObjectMetadata(); Mockito.doReturn(null).when(meta).getCacheControl(); final Resource res = new DefaultResource( client, "f", "", Range.ENTIRE, Version.LATEST, Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.headers(), Matchers.hasItem("Cache-Control: must-revalidate") ); }
@Test public void getsContentEncodingHeaderFromAmazonObject() throws Exception { final AmazonS3 client = Mockito.mock(AmazonS3.class); final S3Object object = Mockito.mock(S3Object.class); Mockito.doReturn(object).when(client) .getObject(Mockito.any(GetObjectRequest.class)); final ObjectMetadata meta = Mockito.mock(ObjectMetadata.class); Mockito.doReturn(meta).when(object).getObjectMetadata(); Mockito.doReturn("gzip").when(meta).getContentEncoding(); final Resource res = new DefaultResource( client, "abcdef", "", Range.ENTIRE, Version.LATEST, Mockito.mock(DomainStatsData.class) ); MatcherAssert.assertThat( res.headers(), Matchers.hasItem("Content-Encoding: gzip") ); }