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;
}
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=");
}
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);
}
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);
}
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);
}
Aggregations