Search in sources :

Example 26 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class Aws4EventStreamSignerTest method testBackPressure.

/**
 * Test that without demand from subscriber, trailing empty frame is not delivered
 */
@Test
public void testBackPressure() {
    TestVector testVector = generateTestVector();
    SdkHttpFullRequest.Builder request = testVector.httpFullRequest();
    AwsBasicCredentials credentials = AwsBasicCredentials.create("access", "secret");
    SdkHttpFullRequest signedRequest = SignerTestUtils.signRequest(signer, request.build(), credentials, "demo", signingClock(), "us-east-1");
    AsyncRequestBody transformedPublisher = SignerTestUtils.signAsyncRequest(signer, signedRequest, testVector.requestBodyPublisher(), credentials, "demo", signingClock(), "us-east-1");
    Subscriber<Object> subscriber = Mockito.spy(new Subscriber<Object>() {

        @Override
        public void onSubscribe(Subscription s) {
            // Only request the number of request body (excluding trailing empty frame)
            s.request(testVector.requestBody().size());
        }

        @Override
        public void onNext(Object o) {
        }

        @Override
        public void onError(Throwable t) {
            fail("onError should never been called");
        }

        @Override
        public void onComplete() {
            fail("onComplete should never been called");
        }
    });
    Flowable.fromPublisher(transformedPublisher).flatMap(new Function<ByteBuffer, Publisher<?>>() {

        Queue<Message> messages = new LinkedList<>();

        MessageDecoder decoder = new MessageDecoder(message -> messages.offer(message));

        @Override
        public Publisher<?> apply(ByteBuffer byteBuffer) throws Exception {
            decoder.feed(byteBuffer.array());
            List<Message> messageList = new ArrayList<>();
            while (!messages.isEmpty()) {
                messageList.add(messages.poll());
            }
            return Flowable.fromIterable(messageList);
        }
    }).subscribe(subscriber);
    // The number of events equal to the size of request body (excluding trailing empty frame)
    verify(subscriber, times(testVector.requestBody().size())).onNext(any());
    // subscriber is not terminated (no onError/onComplete) since trailing empty frame is not delivered yet
    verify(subscriber, never()).onError(any());
    verify(subscriber, never()).onComplete();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BiFunction(io.reactivex.functions.BiFunction) SignerTestUtils(software.amazon.awssdk.auth.signer.internal.SignerTestUtils) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Message(software.amazon.eventstream.Message) EVENT_STREAM_DATE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_DATE) ArrayList(java.util.ArrayList) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BinaryUtils(software.amazon.awssdk.utils.BinaryUtils) TestSubscriber(io.reactivex.subscribers.TestSubscriber) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) ZoneOffset(java.time.ZoneOffset) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) Mockito.times(org.mockito.Mockito.times) EVENT_STREAM_SIGNATURE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_SIGNATURE) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MessageDecoder(software.amazon.eventstream.MessageDecoder) Mockito.never(org.mockito.Mockito.never) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) Function(io.reactivex.functions.Function) Lists(org.assertj.core.util.Lists) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Subscription(org.reactivestreams.Subscription) Clock(java.time.Clock) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) ArrayList(java.util.ArrayList) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) MessageDecoder(software.amazon.eventstream.MessageDecoder) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Subscription(org.reactivestreams.Subscription) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.jupiter.api.Test)

Example 27 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class NonStreamingAsyncBodyAws4SignerTest method test_sign_futureCancelled_propagatedToPublisher.

@Test
void test_sign_futureCancelled_propagatedToPublisher() {
    SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").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();
    AsyncRequestBody mockRequestBody = mock(AsyncRequestBody.class);
    Subscription mockSubscription = mock(Subscription.class);
    doAnswer((Answer<Void>) invocationOnMock -> {
        Subscriber subscriber = invocationOnMock.getArgument(0, Subscriber.class);
        subscriber.onSubscribe(mockSubscription);
        return null;
    }).when(mockRequestBody).subscribe(any(Subscriber.class));
    AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
    CompletableFuture<SdkHttpFullRequest> signedRequestFuture = asyncAws4Signer.signWithBody(httpRequest, mockRequestBody, signerParams);
    signedRequestFuture.cancel(true);
    verify(mockSubscription).cancel();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) CompletableFuture(java.util.concurrent.CompletableFuture) ByteBuffer(java.nio.ByteBuffer) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Flowable(io.reactivex.Flowable) ByteArrayInputStream(java.io.ByteArrayInputStream) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Region(software.amazon.awssdk.regions.Region) Subscriber(org.reactivestreams.Subscriber) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Instant(java.time.Instant) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) Algorithm(software.amazon.awssdk.core.checksums.Algorithm) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Subscription(org.reactivestreams.Subscription) Clock(java.time.Clock) Optional(java.util.Optional) SignerChecksumParams(software.amazon.awssdk.auth.signer.params.SignerChecksumParams) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Mockito.mock(org.mockito.Mockito.mock) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Subscriber(org.reactivestreams.Subscriber) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 28 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class ExecutionInterceptorChain method modifyHttpRequestAndHttpContent.

public InterceptorContext modifyHttpRequestAndHttpContent(InterceptorContext context, ExecutionAttributes executionAttributes) {
    InterceptorContext result = context;
    for (ExecutionInterceptor interceptor : interceptors) {
        AsyncRequestBody asyncRequestBody = interceptor.modifyAsyncHttpContent(result, executionAttributes).orElse(null);
        RequestBody requestBody = interceptor.modifyHttpContent(result, executionAttributes).orElse(null);
        SdkHttpRequest interceptorResult = interceptor.modifyHttpRequest(result, executionAttributes);
        validateInterceptorResult(result.httpRequest(), interceptorResult, interceptor, "modifyHttpRequest");
        result = applySdkHttpFullRequestHack(result);
        result = result.copy(b -> b.httpRequest(interceptorResult).asyncRequestBody(asyncRequestBody).requestBody(requestBody));
    }
    return result;
}
Also used : SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) Validate(software.amazon.awssdk.utils.Validate) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Logger(software.amazon.awssdk.utils.Logger) Publisher(org.reactivestreams.Publisher) DefaultFailedExecutionContext(software.amazon.awssdk.core.internal.interceptor.DefaultFailedExecutionContext) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) SdkProtectedApi(software.amazon.awssdk.annotations.SdkProtectedApi) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Optional(java.util.Optional) SdkResponse(software.amazon.awssdk.core.SdkResponse) RequestBody(software.amazon.awssdk.core.sync.RequestBody) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkRequest(software.amazon.awssdk.core.SdkRequest) InputStream(java.io.InputStream) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) RequestBody(software.amazon.awssdk.core.sync.RequestBody)

Example 29 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class SignedAsyncRequestBodyUploadIntegrationTest method setup.

@BeforeClass
public static void setup() throws Exception {
    S3IntegrationTestBase.setUp();
    // Use a mock so we can introspect easily to verify that the signer was used for the request
    mockSigner = mock(TestSigner.class);
    AsyncAws4Signer realSigner = AsyncAws4Signer.create();
    when(mockSigner.sign(any(SdkHttpFullRequest.class), any(AsyncRequestBody.class), any(ExecutionAttributes.class))).thenAnswer(i -> {
        SdkHttpFullRequest request = i.getArgument(0, SdkHttpFullRequest.class);
        AsyncRequestBody body = i.getArgument(1, AsyncRequestBody.class);
        ExecutionAttributes executionAttributes = i.getArgument(2, ExecutionAttributes.class);
        return realSigner.sign(request, body, executionAttributes);
    });
    testClient = s3AsyncClientBuilder().overrideConfiguration(o -> o.putAdvancedOption(SIGNER, mockSigner).addExecutionInterceptor(capturingInterceptor)).build();
    createBucket(BUCKET);
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) AsyncAws4Signer(software.amazon.awssdk.auth.signer.AsyncAws4Signer) BeforeClass(org.junit.BeforeClass)

Example 30 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class ChecksumCalculatingAsyncRequestBodyTest method fileConstructorHasCorrectContentType.

@Test
public void fileConstructorHasCorrectContentType() {
    AsyncRequestBody requestBody = ChecksumCalculatingAsyncRequestBody.builder().asyncRequestBody(AsyncRequestBody.fromFile(path)).algorithm(Algorithm.CRC32).trailerHeader("x-amz-checksum-crc32").build();
    assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_OCTET_STREAM);
}
Also used : AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Test(org.junit.Test)

Aggregations

AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)51 ByteBuffer (java.nio.ByteBuffer)25 Test (org.junit.jupiter.api.Test)24 List (java.util.List)17 Test (org.junit.Test)13 Subscriber (org.reactivestreams.Subscriber)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)11 Publisher (org.reactivestreams.Publisher)9 Flowable (io.reactivex.Flowable)8 Consumer (java.util.function.Consumer)8 AsyncAws4Signer (software.amazon.awssdk.auth.signer.AsyncAws4Signer)8 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)8 Signer (software.amazon.awssdk.core.signer.Signer)8 MetricCollector (software.amazon.awssdk.metrics.MetricCollector)8 Collections (java.util.Collections)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 Generated (software.amazon.awssdk.annotations.Generated)7 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)7