Search in sources :

Example 11 with AsyncRequestBody

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

the class SigningStage method signRequest.

/**
 * Sign the request if the signer if provided and credentials are present.
 */
private SdkHttpFullRequest signRequest(SdkHttpFullRequest request, RequestExecutionContext context) throws Exception {
    updateInterceptorContext(request, context.executionContext());
    Signer signer = context.signer();
    MetricCollector metricCollector = context.attemptMetricCollector();
    if (shouldSign(signer)) {
        adjustForClockSkew(context.executionAttributes());
        Pair<SdkHttpFullRequest, Duration> measuredSign = MetricUtils.measureDuration(() -> signer.sign(request, context.executionAttributes()));
        metricCollector.reportMetric(CoreMetric.SIGNING_DURATION, measuredSign.right());
        SdkHttpFullRequest signedRequest = measuredSign.left();
        if (signer instanceof AsyncRequestBodySigner) {
            // Transform request body provider with signing operator
            AsyncRequestBody transformedRequestProvider = ((AsyncRequestBodySigner) signer).signAsyncRequestBody(signedRequest, context.requestProvider(), context.executionAttributes());
            context.requestProvider(transformedRequestProvider);
        }
        updateInterceptorContext(signedRequest, context.executionContext());
        return signedRequest;
    }
    return request;
}
Also used : AsyncRequestBodySigner(software.amazon.awssdk.core.signer.AsyncRequestBodySigner) Signer(software.amazon.awssdk.core.signer.Signer) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Duration(java.time.Duration) AsyncRequestBodySigner(software.amazon.awssdk.core.signer.AsyncRequestBodySigner)

Example 12 with AsyncRequestBody

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

the class EventStreamAws4SignerTest method openStreamEventSignaturesCanRollOverBetweenDays.

/**
 * Verify that when an event stream is open from one day to the next, the signature is properly signed for the day of the
 * event.
 */
@Test
public void openStreamEventSignaturesCanRollOverBetweenDays() {
    EventStreamAws4Signer signer = EventStreamAws4Signer.create();
    Region region = Region.US_WEST_2;
    AwsCredentials credentials = AwsBasicCredentials.create("a", "s");
    String signingName = "name";
    AdjustableClock clock = new AdjustableClock();
    clock.time = Instant.parse("2020-01-01T23:59:59Z");
    SdkHttpFullRequest initialRequest = SdkHttpFullRequest.builder().uri(URI.create("http://localhost:8080")).method(SdkHttpMethod.GET).build();
    SdkHttpFullRequest signedRequest = SignerTestUtils.signRequest(signer, initialRequest, credentials, signingName, clock, region.id());
    ByteBuffer event = new Message(Collections.emptyMap(), "foo".getBytes(UTF_8)).toByteBuffer();
    Callable<ByteBuffer> lastEvent = () -> {
        clock.time = Instant.parse("2020-01-02T00:00:00Z");
        return event;
    };
    AsyncRequestBody requestBody = AsyncRequestBody.fromPublisher(Flowable.concatArray(Flowable.just(event), Flowable.fromCallable(lastEvent)));
    AsyncRequestBody signedBody = SignerTestUtils.signAsyncRequest(signer, signedRequest, requestBody, credentials, signingName, clock, region.id());
    List<Message> signedMessages = readMessages(signedBody);
    assertThat(signedMessages.size()).isEqualTo(3);
    Map<String, HeaderValue> firstMessageHeaders = signedMessages.get(0).getHeaders();
    assertThat(firstMessageHeaders.get(":date").getTimestamp()).isEqualTo("2020-01-01T23:59:59Z");
    assertThat(Base64.getEncoder().encodeToString(firstMessageHeaders.get(":chunk-signature").getByteArray())).isEqualTo("EFt7ZU043r/TJE8U+1GxJXscmNxoqmIdGtUIl8wE9u0=");
    Map<String, HeaderValue> lastMessageHeaders = signedMessages.get(2).getHeaders();
    assertThat(lastMessageHeaders.get(":date").getTimestamp()).isEqualTo("2020-01-02T00:00:00Z");
    assertThat(Base64.getEncoder().encodeToString(lastMessageHeaders.get(":chunk-signature").getByteArray())).isEqualTo("UTRGo0D7BQytiVkH1VofR/8f3uFsM4V5QR1A8grr1+M=");
}
Also used : Message(software.amazon.eventstream.Message) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) HeaderValue(software.amazon.eventstream.HeaderValue) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Region(software.amazon.awssdk.regions.Region) Test(org.junit.jupiter.api.Test)

Example 13 with AsyncRequestBody

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

the class ChecksumCalculatingAsyncRequestBodyTest method bytesBufferConstructorHasCorrectContentType.

@Test
public void bytesBufferConstructorHasCorrectContentType() {
    ByteBuffer byteBuffer = ByteBuffer.wrap("hello world".getBytes());
    AsyncRequestBody requestBody = ChecksumCalculatingAsyncRequestBody.builder().asyncRequestBody(AsyncRequestBody.fromByteBuffer(byteBuffer)).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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 14 with AsyncRequestBody

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

the class ChecksumCalculatingAsyncRequestBodyTest method stringConstructorHasCorrectContentType.

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

Example 15 with AsyncRequestBody

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

the class FileAsyncRequestBodyTest method readFully_doesNotRequestPastEndOfFile_receivesComplete.

// If we issue just enough requests to read the file entirely but not more (to go past EOF), we should still receive
// an onComplete
@Test
public void readFully_doesNotRequestPastEndOfFile_receivesComplete() throws Exception {
    int chunkSize = 16384;
    AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder().path(testFile).chunkSizeInBytes(chunkSize).build();
    long totalRequests = TEST_FILE_SIZE / chunkSize;
    CompletableFuture<Void> completed = new CompletableFuture<>();
    asyncRequestBody.subscribe(new Subscriber<ByteBuffer>() {

        private Subscription sub;

        private long requests = 0;

        @Override
        public void onSubscribe(Subscription subscription) {
            this.sub = subscription;
            if (requests++ < totalRequests) {
                this.sub.request(1);
            }
        }

        @Override
        public void onNext(ByteBuffer byteBuffer) {
            if (requests++ < totalRequests) {
                this.sub.request(1);
            }
        }

        @Override
        public void onError(Throwable throwable) {
        }

        @Override
        public void onComplete() {
            completed.complete(null);
        }
    });
    completed.get(5, TimeUnit.SECONDS);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Subscription(org.reactivestreams.Subscription) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.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