Search in sources :

Example 1 with Message

use of software.amazon.eventstream.Message in project aws-sdk-java-v2 by aws.

the class Aws4EventStreamSignerTest method generateTestVector.

TestVector generateTestVector() {
    return new TestVector() {

        List<String> requestBody = Lists.newArrayList("A", "B", "C");

        @Override
        public List<String> requestBody() {
            return requestBody;
        }

        @Override
        public SdkHttpFullRequest.Builder httpFullRequest() {
            // Signing key: "29dc0a760fed568677d74136ad02d315a07d31b8f321f5c43350f284dac892c";
            return SdkHttpFullRequest.builder().method(SdkHttpMethod.POST).putHeader("Host", "demo.us-east-1.amazonaws.com").putHeader("content-encoding", "application/vnd.amazon.eventstream").putHeader("x-amz-content-sha256", "STREAMING-AWS4-HMAC-SHA256-EVENTS").encodedPath("/streaming").protocol("https").host("demo.us-east-1.amazonaws.com");
        }

        @Override
        public AsyncRequestBody requestBodyPublisher() {
            List<ByteBuffer> bodyBytes = requestBody.stream().map(s -> ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8))).collect(Collectors.toList());
            Publisher<ByteBuffer> bodyPublisher = Flowable.fromIterable(bodyBytes);
            return AsyncRequestBody.fromPublisher(bodyPublisher);
        }

        @Override
        public Flowable<Message> expectedMessagePublisher() {
            Flowable<String> sigsHex = Flowable.just("7aabf85b765e6a4d0d500b6e968657b14726fa3e1eb7e839302728ffd77629a5", "f72aa9642f571d24a6e1ae42f10f073ad9448d8a028b6bcd82da081335adda02", "632af120435b57ec241d8bfbb12e496dfd5e2730a1a02ac0ab6eaa230ae02e9a", "c6f679ddb3af68f5e82f0cf6761244cb2338cf11e7d01a24130aea1b7c17e53e");
            // The Last data frame is empty
            Flowable<String> payloads = Flowable.fromIterable(requestBody).concatWith(Flowable.just(""));
            return sigsHex.zipWith(payloads, new BiFunction<String, String, Message>() {

                // The first Instant was used to sign the request
                private int idx = 1;

                @Override
                public Message apply(String sig, String payload) throws Exception {
                    Map<String, HeaderValue> headers = new HashMap<>();
                    headers.put(EVENT_STREAM_DATE, HeaderValue.fromTimestamp(SIGNING_INSTANTS.get(idx++)));
                    headers.put(EVENT_STREAM_SIGNATURE, HeaderValue.fromByteArray(BinaryUtils.fromHex(sig)));
                    return new Message(headers, payload.getBytes(StandardCharsets.UTF_8));
                }
            });
        }
    };
}
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) ByteBuffer(java.nio.ByteBuffer) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Message

use of software.amazon.eventstream.Message in project aws-sdk-java-v2 by aws.

the class Aws4EventStreamSignerTest method testEventStreamSigning.

@Test
public void testEventStreamSigning() {
    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");
    TestSubscriber testSubscriber = TestSubscriber.create();
    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(testSubscriber);
    testSubscriber.assertNoErrors();
    testSubscriber.assertComplete();
    testSubscriber.assertValueSequence(testVector.expectedMessagePublisher().blockingIterable());
}
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) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.jupiter.api.Test)

Example 3 with Message

use of software.amazon.eventstream.Message in project aws-sdk-java-v2 by aws.

the class EventStreamInitialRequestInterceptorTest method testInitialRequestEvent.

@Test
public void testInitialRequestEvent() {
    ModifyHttpRequest context = buildContext(bytePublisher, payload);
    Optional<AsyncRequestBody> modifiedBody = interceptor.modifyAsyncHttpContent(context, attr);
    List<Message> messages = Flowable.fromPublisher(modifiedBody.get()).map(Message::decode).toList().blockingGet();
    Message initialRequestEvent = messages.get(0);
    assertArrayEquals(payload, initialRequestEvent.getPayload());
    assertEquals(RPC_CONTENT_TYPE, initialRequestEvent.getHeaders().get(":content-type").getString());
}
Also used : Message(software.amazon.eventstream.Message) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ModifyHttpRequest(software.amazon.awssdk.core.interceptor.Context.ModifyHttpRequest) Test(org.junit.jupiter.api.Test)

Example 4 with Message

use of software.amazon.eventstream.Message in project aws-sdk-java-v2 by aws.

the class AwsClientHandlerUtils method encodeEventStreamRequestToByteBuffer.

/**
 * Encodes the request into a flow message and then returns bytebuffer from the message.
 *
 * @param request The request to encode
 * @return A bytebuffer representing the given request
 */
public static ByteBuffer encodeEventStreamRequestToByteBuffer(SdkHttpFullRequest request) {
    Map<String, HeaderValue> headers = request.headers().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> HeaderValue.fromString(firstIfPresent(e.getValue()))));
    byte[] payload = null;
    if (request.contentStreamProvider().isPresent()) {
        try {
            payload = IoUtils.toByteArray(request.contentStreamProvider().get().newStream());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    return new Message(headers, payload).toByteBuffer();
}
Also used : UncheckedIOException(java.io.UncheckedIOException) SdkProtectedApi(software.amazon.awssdk.annotations.SdkProtectedApi) Map(java.util.Map) IOException(java.io.IOException) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) IoUtils(software.amazon.awssdk.utils.IoUtils) Message(software.amazon.eventstream.Message) CollectionUtils.firstIfPresent(software.amazon.awssdk.utils.CollectionUtils.firstIfPresent) HeaderValue(software.amazon.eventstream.HeaderValue) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Map(java.util.Map)

Example 5 with Message

use of software.amazon.eventstream.Message in project aws-sdk-java-v2 by aws.

the class EventStreamAsyncResponseTransformerTest method multipleEventsInChunk_OnlyDeliversOneEvent.

@Test
public void multipleEventsInChunk_OnlyDeliversOneEvent() throws InterruptedException {
    Message eventMessage = new Message(ImmutableMap.of(":message-type", HeaderValue.fromString("event"), ":event-type", HeaderValue.fromString("foo")), new byte[0]);
    CountDownLatch latch = new CountDownLatch(1);
    Flowable<ByteBuffer> bytePublisher = Flowable.just(eventMessage.toByteBuffer(), eventMessage.toByteBuffer()).doOnCancel(latch::countDown);
    AtomicInteger numEvents = new AtomicInteger(0);
    // Request one event then cancel
    Subscriber<Object> requestOneSubscriber = new Subscriber<Object>() {

        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription subscription) {
            this.subscription = subscription;
            subscription.request(1);
        }

        @Override
        public void onNext(Object o) {
            numEvents.incrementAndGet();
            subscription.cancel();
        }

        @Override
        public void onError(Throwable throwable) {
        }

        @Override
        public void onComplete() {
        }
    };
    AsyncResponseTransformer<SdkResponse, Void> transformer = EventStreamAsyncResponseTransformer.builder().eventStreamResponseHandler(onEventStream(p -> p.subscribe(requestOneSubscriber))).eventResponseHandler((r, e) -> new Object()).executor(Executors.newSingleThreadExecutor()).future(new CompletableFuture<>()).build();
    transformer.prepare();
    transformer.onStream(SdkPublisher.adapt(bytePublisher));
    latch.await();
    assertThat(numEvents).as("Expected only one event to be delivered").hasValue(1);
}
Also used : ImmutableMap(software.amazon.awssdk.utils.ImmutableMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) Message(software.amazon.eventstream.Message) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Subscription(org.reactivestreams.Subscription) SdkResponse(software.amazon.awssdk.core.SdkResponse) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) Subscriber(org.reactivestreams.Subscriber) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscriber(org.reactivestreams.Subscriber) SdkResponse(software.amazon.awssdk.core.SdkResponse) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Aggregations

Message (software.amazon.eventstream.Message)20 ByteBuffer (java.nio.ByteBuffer)11 Test (org.junit.jupiter.api.Test)11 HeaderValue (software.amazon.eventstream.HeaderValue)11 HashMap (java.util.HashMap)9 Map (java.util.Map)8 Subscriber (org.reactivestreams.Subscriber)7 Subscription (org.reactivestreams.Subscription)7 Flowable (io.reactivex.Flowable)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)6 Test (org.junit.Test)5 StandardCharsets (java.nio.charset.StandardCharsets)4 Instant (java.time.Instant)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)4 BiFunction (io.reactivex.functions.BiFunction)3