Search in sources :

Example 1 with ContentStreamProvider

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);
}
Also used : StringUtils(software.amazon.awssdk.utils.StringUtils) SdkHttpUtils(software.amazon.awssdk.utils.http.SdkHttpUtils) URISyntaxException(java.net.URISyntaxException) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) IOException(java.io.IOException) Math.min(java.lang.Math.min) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) CollectionUtils.isNullOrEmpty(software.amazon.awssdk.utils.CollectionUtils.isNullOrEmpty) Map(java.util.Map) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) AwsSigningResult(software.amazon.awssdk.crt.auth.signing.AwsSigningResult) Optional(java.util.Optional) URI(java.net.URI) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) InputStream(java.io.InputStream) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider)

Example 2 with ContentStreamProvider

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeClass(org.junit.BeforeClass) ApacheHttpClient(software.amazon.awssdk.http.apache.ApacheHttpClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) PresignedAbortMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedAbortMultipartUploadRequest) RequestPayer(software.amazon.awssdk.services.s3.model.RequestPayer) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Duration(java.time.Duration) After(org.junit.After) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) Before(org.junit.Before) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) AfterClass(org.junit.AfterClass) MultipartUpload(software.amazon.awssdk.services.s3.model.MultipartUpload) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) IOException(java.io.IOException) Test(org.junit.Test) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) UUID(java.util.UUID) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) PresignedCompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCompleteMultipartUploadRequest) S3TestUtils(software.amazon.awssdk.services.s3.utils.S3TestUtils) Consumer(java.util.function.Consumer) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) StringInputStream(software.amazon.awssdk.utils.StringInputStream) PresignedCreateMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCreateMultipartUploadRequest) Optional(java.util.Optional) RequestBody(software.amazon.awssdk.core.sync.RequestBody) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) IoUtils(software.amazon.awssdk.utils.IoUtils) InputStream(java.io.InputStream) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) UploadPartResponse(software.amazon.awssdk.services.s3.model.UploadPartResponse) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Test(org.junit.Test)

Example 3 with ContentStreamProvider

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();
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Test(org.junit.jupiter.api.Test)

Example 4 with ContentStreamProvider

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);
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Test(org.junit.jupiter.api.Test)

Example 5 with ContentStreamProvider

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));
        }
    }
}
Also used : ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider)

Aggregations

ContentStreamProvider (software.amazon.awssdk.http.ContentStreamProvider)13 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)8 Optional (java.util.Optional)5 RequestBody (software.amazon.awssdk.core.sync.RequestBody)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Test (org.junit.jupiter.api.Test)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URI (java.net.URI)3 Duration (java.time.Duration)3 ArrayList (java.util.ArrayList)3 PresignedRequest (software.amazon.awssdk.awscore.presigner.PresignedRequest)3 SdkBytes (software.amazon.awssdk.core.SdkBytes)3 StringInputStream (software.amazon.awssdk.utils.StringInputStream)3 HttpURLConnection (java.net.HttpURLConnection)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Consumer (java.util.function.Consumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2