use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.
the class CrtHttpRequestConverter method requestToCrt.
public HttpRequest requestToCrt(SdkHttpFullRequest inputRequest) {
String method = inputRequest.method().name();
String encodedPath = encodedPathToCrtFormat(inputRequest.encodedPath());
String encodedQueryString = SdkHttpUtils.encodeAndFlattenQueryParameters(inputRequest.rawQueryParameters()).map(value -> "?" + value).orElse("");
HttpHeader[] crtHeaderArray = createHttpHeaderArray(inputRequest);
Optional<ContentStreamProvider> contentProvider = inputRequest.contentStreamProvider();
HttpRequestBodyStream crtInputStream = null;
if (contentProvider.isPresent()) {
crtInputStream = new CrtHttpRequestConverter.CrtInputStream(contentProvider.get());
}
return new HttpRequest(method, encodedPath + encodedQueryString, crtHeaderArray, crtInputStream);
}
use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.
the class S3PresignerIntegrationTest method putObject_PresignedHttpRequestCanBeInvokedDirectlyBySdk.
@Test
public void putObject_PresignedHttpRequestCanBeInvokedDirectlyBySdk() throws IOException {
String objectKey = generateRandomObjectKey();
S3TestUtils.addCleanupTask(S3PresignerIntegrationTest.class, () -> client.deleteObject(r -> r.bucket(testBucket).key(objectKey)));
PresignedPutObjectRequest presigned = presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5)).putObjectRequest(por -> por.bucket(testBucket).key(objectKey)));
assertThat(presigned.isBrowserExecutable()).isFalse();
// or UrlConnectionHttpClient.builder().build()
SdkHttpClient httpClient = ApacheHttpClient.builder().build();
ContentStreamProvider requestPayload = () -> new StringInputStream(testObjectContent);
HttpExecuteRequest request = HttpExecuteRequest.builder().request(presigned.httpRequest()).contentStreamProvider(requestPayload).build();
HttpExecuteResponse response = httpClient.prepareRequest(request).call();
assertThat(response.responseBody()).isPresent();
assertThat(response.httpResponse().isSuccessful()).isTrue();
response.responseBody().ifPresent(AbortableInputStream::abort);
String content = client.getObjectAsBytes(r -> r.bucket(testBucket).key(objectKey)).asUtf8String();
assertThat(content).isEqualTo(testObjectContent);
}
use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignatureWithChecksum.
@Test
void test_sign_computesCorrectSignatureWithChecksum() {
Aws4Signer aws4Signer = Aws4Signer.create();
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
byte[] content = "abc".getBytes(StandardCharsets.UTF_8);
ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).checksumParams(SignerChecksumParams.builder().algorithm(Algorithm.SHA256).checksumHeaderName("x-amzn-header").isStreamingRequest(true).build()).build();
List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
final SdkHttpFullRequest sdkHttpFullRequest = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join();
List<String> asyncSignature = sdkHttpFullRequest.headers().get("Authorization");
assertThat(asyncSignature).isEqualTo(syncSignature);
final List<String> stringList = sdkHttpFullRequest.headers().get("x-amzn-header");
assertThat(stringList.stream().findFirst()).hasValue("ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=");
assertThat(sdkHttpFullRequest.firstMatchingHeader(("x-amzn-trailer"))).isNotPresent();
}
use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignature.
@Test
void test_sign_computesCorrectSignature() {
Aws4Signer aws4Signer = Aws4Signer.create();
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
byte[] content = "Hello AWS!".getBytes(StandardCharsets.UTF_8);
ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).build();
List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
List<String> asyncSignature = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join().headers().get("Authorization");
assertThat(asyncSignature).isEqualTo(syncSignature);
}
use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.
the class AbstractAwsS3V4Signer method processRequestPayload.
/**
* Overloads processRequestPayload with sdkChecksum.
* Flexible Checksum for Payload is calculated if sdkChecksum is passed.
*/
@Override
protected void processRequestPayload(SdkHttpFullRequest.Builder mutableRequest, byte[] signature, byte[] signingKey, Aws4SignerRequestParams signerRequestParams, AwsS3V4SignerParams signerParams, SdkChecksum sdkChecksum) {
if (useChunkEncoding(mutableRequest, signerParams)) {
if (mutableRequest.contentStreamProvider() != null) {
ContentStreamProvider streamProvider = mutableRequest.contentStreamProvider();
String headerForTrailerChecksumLocation = signerParams.checksumParams() != null ? signerParams.checksumParams().checksumHeaderName() : null;
mutableRequest.contentStreamProvider(() -> AbstractAwsS3V4Signer.this.asChunkEncodedStream(streamProvider.newStream(), signature, signingKey, signerRequestParams, sdkChecksum, headerForTrailerChecksumLocation));
}
}
}
Aggregations