Search in sources :

Example 1 with AsyncStreamPrepender

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

the class EventStreamInitialRequestInterceptor method modifyAsyncHttpContent.

@Override
public Optional<AsyncRequestBody> modifyAsyncHttpContent(ModifyHttpRequest context, ExecutionAttributes executionAttributes) {
    if (!Boolean.TRUE.equals(executionAttributes.getAttribute(HAS_INITIAL_REQUEST_EVENT))) {
        return context.asyncRequestBody();
    }
    /*
         * At this point in the request execution, the requestBody contains the serialized initial request,
         * and the asyncRequestBody contains the event stream proper. We will prepend the former to the
         * latter.
         */
    byte[] payload = getInitialRequestPayload(context);
    String contentType = context.httpRequest().headers().get(CONTENT_TYPE).get(0);
    Map<String, HeaderValue> initialRequestEventHeaders = new HashMap<>();
    initialRequestEventHeaders.put(":message-type", HeaderValue.fromString("event"));
    initialRequestEventHeaders.put(":event-type", HeaderValue.fromString("initial-request"));
    initialRequestEventHeaders.put(":content-type", HeaderValue.fromString(contentType));
    ByteBuffer initialRequest = new Message(initialRequestEventHeaders, payload).toByteBuffer();
    Publisher<ByteBuffer> asyncRequestBody = context.asyncRequestBody().orElseThrow(() -> new IllegalStateException("This request is an event streaming request and thus " + "should have an asyncRequestBody"));
    Publisher<ByteBuffer> withInitialRequest = new AsyncStreamPrepender<>(asyncRequestBody, initialRequest);
    return Optional.of(AsyncRequestBody.fromPublisher(withInitialRequest));
}
Also used : HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) HashMap(java.util.HashMap) AsyncStreamPrepender(software.amazon.awssdk.core.internal.async.AsyncStreamPrepender) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 AsyncStreamPrepender (software.amazon.awssdk.core.internal.async.AsyncStreamPrepender)1 HeaderValue (software.amazon.eventstream.HeaderValue)1 Message (software.amazon.eventstream.Message)1