Search in sources :

Example 11 with ContentStreamProvider

use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.

the class DefaultAwsCrtS3V4aSigner method enablePayloadSigning.

private SdkHttpFullRequest enablePayloadSigning(SdkSigningResult signingResult, AwsSigningConfig chunkConfig, SignerChecksumParams signerChecksumParams) {
    SdkHttpFullRequest signedRequest = signingResult.getSignedRequest();
    byte[] signature = signingResult.getSignature();
    SdkHttpFullRequest.Builder mutableSignedRequest = signedRequest.toBuilder();
    ContentStreamProvider streamProvider = mutableSignedRequest.contentStreamProvider();
    AwsS3V4aChunkSigner chunkSigner = new AwsS3V4aChunkSigner(signerAdapter, chunkConfig);
    String checksumHeader = signerChecksumParams != null ? signerChecksumParams.checksumHeaderName() : null;
    SdkChecksum sdkChecksum = signerChecksumParams != null ? SdkChecksum.forAlgorithm(signerChecksumParams.algorithm()) : null;
    mutableSignedRequest.contentStreamProvider(() -> AwsSignedChunkedEncodingInputStream.builder().inputStream(streamProvider.newStream()).awsChunkSigner(chunkSigner).checksumHeaderForTrailer(checksumHeader).sdkChecksum(sdkChecksum).headerSignature(new String(signature, StandardCharsets.UTF_8)).awsChunkedEncodingConfig(AwsChunkedEncodingConfig.create()).build());
    return mutableSignedRequest.build();
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkChecksum(software.amazon.awssdk.core.checksums.SdkChecksum) AwsS3V4aChunkSigner(software.amazon.awssdk.authcrt.signer.internal.chunkedencoding.AwsS3V4aChunkSigner) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider)

Example 12 with ContentStreamProvider

use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.

the class QueryParametersToBodyInterceptorTest method postWithContentIsUnaltered.

@Test
public void postWithContentIsUnaltered() throws Exception {
    byte[] contentBytes = "hello".getBytes(StandardCharsets.UTF_8);
    ContentStreamProvider contentProvider = () -> new ByteArrayInputStream(contentBytes);
    SdkHttpFullRequest request = requestBuilder.contentStreamProvider(contentProvider).build();
    SdkHttpFullRequest output = (SdkHttpFullRequest) interceptor.modifyHttpRequest(new HttpRequestOnlyContext(request, null), executionAttributes);
    assertThat(output.rawQueryParameters()).hasSize(1);
    assertThat(output.headers()).hasSize(0);
    assertThat(IoUtils.toByteArray(output.contentStreamProvider().get().newStream())).isEqualTo(contentBytes);
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Test(org.junit.jupiter.api.Test)

Example 13 with ContentStreamProvider

use of software.amazon.awssdk.http.ContentStreamProvider in project aws-sdk-java-v2 by aws.

the class DefaultS3Presigner method marshalRequestAndUpdateContext.

/**
 * Marshal the request and update the execution context with the result.
 */
private <T> void marshalRequestAndUpdateContext(ExecutionContext execCtx, Class<T> requestType, Function<T, SdkHttpFullRequest> requestMarshaller) {
    T sdkRequest = Validate.isInstanceOf(requestType, execCtx.interceptorContext().request(), "Interceptor generated unsupported type (%s) when %s was expected.", execCtx.interceptorContext().request().getClass(), requestType);
    SdkHttpFullRequest marshalledRequest = requestMarshaller.apply(sdkRequest);
    // TODO: The core SDK doesn't put the request body into the interceptor context. That should be fixed.
    Optional<RequestBody> requestBody = marshalledRequest.contentStreamProvider().map(ContentStreamProvider::newStream).map(is -> invokeSafely(() -> IoUtils.toByteArray(is))).map(RequestBody::fromBytes);
    execCtx.interceptorContext(execCtx.interceptorContext().copy(r -> r.httpRequest(marshalledRequest).requestBody(requestBody.orElse(null))));
}
Also used : InterceptorContext(software.amazon.awssdk.core.interceptor.InterceptorContext) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) SdkBytes(software.amazon.awssdk.core.SdkBytes) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) SdkClientOption(software.amazon.awssdk.core.client.config.SdkClientOption) URI(java.net.URI) Validate(software.amazon.awssdk.utils.Validate) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) PresignRequest(software.amazon.awssdk.awscore.presigner.PresignRequest) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) PresignedCompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCompleteMultipartUploadRequest) Instant(java.time.Instant) PutObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.PutObjectRequestMarshaller) UploadPartPresignRequest(software.amazon.awssdk.services.s3.presigner.model.UploadPartPresignRequest) List(java.util.List) Stream(java.util.stream.Stream) PresignedCreateMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCreateMultipartUploadRequest) Optional(java.util.Optional) S3Configuration(software.amazon.awssdk.services.s3.S3Configuration) SdkRequest(software.amazon.awssdk.core.SdkRequest) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) PRESIGNER_EXPIRATION(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute.PRESIGNER_EXPIRATION) CollectionUtils.mergeLists(software.amazon.awssdk.utils.CollectionUtils.mergeLists) AbortMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.AbortMultipartUploadRequestMarshaller) ExecutionInterceptorChain(software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain) ClientType(software.amazon.awssdk.core.ClientType) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) Function(java.util.function.Function) PresignedAbortMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedAbortMultipartUploadRequest) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) ArrayList(java.util.ArrayList) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Presigner(software.amazon.awssdk.core.signer.Presigner) ClasspathInterceptorChainFactory(software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory) GetObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.GetObjectRequestMarshaller) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkDefaultClientBuilder(software.amazon.awssdk.core.client.builder.SdkDefaultClientBuilder) UploadPartRequestMarshaller(software.amazon.awssdk.services.s3.transform.UploadPartRequestMarshaller) CompleteMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CompleteMultipartUploadRequestMarshaller) AwsSignerExecutionAttribute(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) SdkExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkExecutionAttribute) CreateMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CreateMultipartUploadPresignRequest) AwsExecutionContextBuilder(software.amazon.awssdk.awscore.internal.AwsExecutionContextBuilder) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Logger(software.amazon.awssdk.utils.Logger) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) CompleteMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CompleteMultipartUploadPresignRequest) GetObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest) AwsExecutionAttribute(software.amazon.awssdk.awscore.AwsExecutionAttribute) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) AwsS3ProtocolFactory(software.amazon.awssdk.protocols.xml.AwsS3ProtocolFactory) AwsS3V4Signer(software.amazon.awssdk.auth.signer.AwsS3V4Signer) AbortMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.AbortMultipartUploadPresignRequest) CreateMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CreateMultipartUploadRequestMarshaller) AwsDefaultClientBuilder(software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder) DefaultServiceEndpointBuilder(software.amazon.awssdk.awscore.endpoint.DefaultServiceEndpointBuilder) RequestBody(software.amazon.awssdk.core.sync.RequestBody) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) Collections(java.util.Collections) IoUtils(software.amazon.awssdk.utils.IoUtils) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) RequestBody(software.amazon.awssdk.core.sync.RequestBody) 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