/** * <p> * Adds response headers parameters to the request given, if non-null. * </p> * * @param request * The request to add the response header parameters to. * @param responseHeaders * The full set of response headers to add, or null for none. */ private static void addResponseHeaderParameters(Request<?> request, ResponseHeaderOverrides responseHeaders) { if ( responseHeaders != null ) { if ( responseHeaders.getCacheControl() != null ) { request.addParameter(ResponseHeaderOverrides.RESPONSE_HEADER_CACHE_CONTROL, responseHeaders.getCacheControl()); } if ( responseHeaders.getContentDisposition() != null ) { request.addParameter(ResponseHeaderOverrides.RESPONSE_HEADER_CONTENT_DISPOSITION, responseHeaders.getContentDisposition()); } if ( responseHeaders.getContentEncoding() != null ) { request.addParameter(ResponseHeaderOverrides.RESPONSE_HEADER_CONTENT_ENCODING, responseHeaders.getContentEncoding()); } if ( responseHeaders.getContentLanguage() != null ) { request.addParameter(ResponseHeaderOverrides.RESPONSE_HEADER_CONTENT_LANGUAGE, responseHeaders.getContentLanguage()); } if ( responseHeaders.getContentType() != null ) { request.addParameter(ResponseHeaderOverrides.RESPONSE_HEADER_CONTENT_TYPE, responseHeaders.getContentType()); } if ( responseHeaders.getExpires() != null ) { request.addParameter(ResponseHeaderOverrides.RESPONSE_HEADER_EXPIRES, responseHeaders.getExpires()); } } }
public PersistableDownload( @JsonProperty(value = "bucketName") String bucketName, @JsonProperty(value = "key") String key, @JsonProperty(value = "versionId") String versionId, @JsonProperty(value = "range") long[] range, @JsonProperty(value = "responseHeaders") ResponseHeaderOverrides responseHeaders, @JsonProperty(value = "isRequesterPays") boolean isRequesterPays, @JsonProperty(value = "file") String file, @JsonProperty(value = "lastFullyDownloadedPartNumber") Integer lastFullyDownloadedPartNumber, @JsonProperty(value = "lastModifiedTime") long lastModifiedTime) { this.bucketName = bucketName; this.key = key; this.versionId = versionId; this.range = range == null ? null : range.clone(); this.responseHeaders = responseHeaders; this.isRequesterPays = isRequesterPays; this.file = file; this.lastFullyDownloadedPartNumber = lastFullyDownloadedPartNumber; this.lastModifiedTime = lastModifiedTime; }
@Test public void testAwsV2SignatureWithOverrideParameters() throws Exception { client = AmazonS3ClientBuilder.standard() .withClientConfiguration(V2_SIGNER_CONFIG) .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withEndpointConfiguration(s3EndpointConfig).build(); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); client.putObject(containerName, "foo", BYTE_SOURCE.openStream(), metadata); String blobName = "foo"; ResponseHeaderOverrides headerOverride = new ResponseHeaderOverrides(); String expectedContentDisposition = "attachment; " + blobName; headerOverride.setContentDisposition(expectedContentDisposition); String expectedContentType = "text/plain"; headerOverride.setContentType(expectedContentType); GetObjectRequest request = new GetObjectRequest(containerName, blobName); request.setResponseHeaders(headerOverride); S3Object object = client.getObject(request); assertThat(object.getObjectMetadata().getContentLength()).isEqualTo( BYTE_SOURCE.size()); assertThat(object.getObjectMetadata().getContentDisposition()) .isEqualTo(expectedContentDisposition); assertThat(object.getObjectMetadata().getContentType()).isEqualTo( expectedContentType); try (InputStream actual = object.getObjectContent(); InputStream expected = BYTE_SOURCE.openStream()) { assertThat(actual).hasContentEqualTo(expected); } }
/** * Returns the optional response headers. */ ResponseHeaderOverrides getResponseHeaders() { return responseHeaders; }
@Test public void testAwsV2UrlSigningWithOverrideParameters() throws Exception { client = AmazonS3ClientBuilder.standard() .withClientConfiguration(V2_SIGNER_CONFIG) .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withEndpointConfiguration(s3EndpointConfig).build(); String blobName = "foo"; ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); client.putObject(containerName, blobName, BYTE_SOURCE.openStream(), metadata); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(containerName, blobName); generatePresignedUrlRequest.setMethod(HttpMethod.GET); ResponseHeaderOverrides headerOverride = new ResponseHeaderOverrides(); headerOverride.setContentDisposition("attachment; " + blobName); headerOverride.setContentType("text/plain"); generatePresignedUrlRequest.setResponseHeaders(headerOverride); Date expiration = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)); generatePresignedUrlRequest.setExpiration(expiration); URL url = client.generatePresignedUrl(generatePresignedUrlRequest); URLConnection connection = url.openConnection(); try (InputStream actual = connection.getInputStream(); InputStream expected = BYTE_SOURCE.openStream()) { String value = connection.getHeaderField("Content-Disposition"); assertThat(value).isEqualTo(headerOverride.getContentDisposition()); value = connection.getHeaderField("Content-Type"); assertThat(value).isEqualTo(headerOverride.getContentType()); assertThat(actual).hasContentEqualTo(expected); } }
@Test public void testOverrideResponseHeader() throws Exception { String blobName = "foo"; ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); client.putObject(containerName, blobName, BYTE_SOURCE.openStream(), metadata); String cacheControl = "no-cache"; String contentDisposition = "attachment; filename=foo.html"; String contentEncoding = "gzip"; String contentLanguage = "en"; String contentType = "text/html; charset=UTF-8"; String expires = "Wed, 13 Jul 2016 21:23:51 GMT"; long expiresTime = 1468445031000L; GetObjectRequest getObjectRequest = new GetObjectRequest(containerName, blobName); getObjectRequest.setResponseHeaders( new ResponseHeaderOverrides() .withCacheControl(cacheControl) .withContentDisposition(contentDisposition) .withContentEncoding(contentEncoding) .withContentLanguage(contentLanguage) .withContentType(contentType) .withExpires(expires)); S3Object object = client.getObject(getObjectRequest); try (InputStream is = object.getObjectContent()) { assertThat(is).isNotNull(); ByteStreams.copy(is, ByteStreams.nullOutputStream()); } ObjectMetadata reponseMetadata = object.getObjectMetadata(); assertThat(reponseMetadata.getCacheControl()).isEqualTo( cacheControl); assertThat(reponseMetadata.getContentDisposition()).isEqualTo( contentDisposition); assertThat(reponseMetadata.getContentEncoding()).isEqualTo( contentEncoding); assertThat(reponseMetadata.getContentLanguage()).isEqualTo( contentLanguage); assertThat(reponseMetadata.getContentType()).isEqualTo( contentType); assertThat(reponseMetadata.getHttpExpiresDate().getTime()) .isEqualTo(expiresTime); }