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