Search in sources :

Example 1 with StreamedHttpResponse

use of software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse in project aws-sdk-java-v2 by aws.

the class PublisherAdapterTest method errorOccurred_shouldInvokeResponseHandler.

@Test
public void errorOccurred_shouldInvokeResponseHandler() {
    RuntimeException exception = new RuntimeException("boom");
    Flowable<HttpContent> testPublisher = Flowable.error(exception);
    StreamedHttpResponse streamedHttpResponse = new DefaultStreamedHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED, testPublisher);
    ResponseHandler.PublisherAdapter publisherAdapter = new ResponseHandler.PublisherAdapter(streamedHttpResponse, ctx, requestContext, executeFuture);
    TestSubscriber subscriber = new TestSubscriber();
    publisherAdapter.subscribe(subscriber);
    verify(ctx, times(0)).read();
    verify(ctx).close();
    assertThat(subscriber.errorOccurred).isEqualTo(true);
    verify(channelPool).release(channel);
    assertThat(executeFuture).isCompletedExceptionally();
    verify(responseHandler).onError(exception);
}
Also used : DefaultStreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.DefaultStreamedHttpResponse) StreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse) DefaultStreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.DefaultStreamedHttpResponse) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Example 2 with StreamedHttpResponse

use of software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse in project aws-sdk-java-v2 by aws.

the class ResponseHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext channelContext, HttpObject msg) throws Exception {
    RequestContext requestContext = channelContext.channel().attr(REQUEST_CONTEXT_KEY).get();
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        SdkHttpResponse sdkResponse = SdkHttpFullResponse.builder().headers(fromNettyHeaders(response.headers())).statusCode(response.status().code()).statusText(response.status().reasonPhrase()).build();
        channelContext.channel().attr(RESPONSE_STATUS_CODE).set(response.status().code());
        channelContext.channel().attr(RESPONSE_CONTENT_LENGTH).set(responseContentLength(response));
        channelContext.channel().attr(KEEP_ALIVE).set(shouldKeepAlive(response));
        requestContext.handler().onHeaders(sdkResponse);
    }
    CompletableFuture<Void> ef = executeFuture(channelContext);
    if (msg instanceof StreamedHttpResponse) {
        requestContext.handler().onStream(new DataCountingPublisher(channelContext, new PublisherAdapter((StreamedHttpResponse) msg, channelContext, requestContext, ef)));
    } else if (msg instanceof FullHttpResponse) {
        ByteBuf fullContent = null;
        try {
            // Be prepared to take care of (ignore) a trailing LastHttpResponse
            // from the HttpClientCodec if there is one.
            channelContext.pipeline().replace(HttpStreamsClientHandler.class, channelContext.name() + "-LastHttpContentSwallower", LastHttpContentSwallower.getInstance());
            fullContent = ((FullHttpResponse) msg).content();
            ByteBuffer bb = copyToByteBuffer(fullContent);
            requestContext.handler().onStream(new DataCountingPublisher(channelContext, new FullResponseContentPublisher(channelContext, bb, ef)));
            try {
                validateResponseContentLength(channelContext);
                finalizeResponse(requestContext, channelContext);
            } catch (IOException e) {
                exceptionCaught(channelContext, e);
            }
        } finally {
            Optional.ofNullable(fullContent).ifPresent(ByteBuf::release);
        }
    }
}
Also used : StreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) StreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) HttpStreamsClientHandler(software.amazon.awssdk.http.nio.netty.internal.nrs.HttpStreamsClientHandler) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 3 with StreamedHttpResponse

use of software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse in project aws-sdk-java-v2 by aws.

the class PublisherAdapterTest method successfulStreaming_shouldNotInvokeChannelRead.

@Test
public void successfulStreaming_shouldNotInvokeChannelRead() {
    Flowable<HttpContent> testPublisher = Flowable.just(fullHttpResponse);
    StreamedHttpResponse streamedHttpResponse = new DefaultStreamedHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED, testPublisher);
    ResponseHandler.PublisherAdapter publisherAdapter = new ResponseHandler.PublisherAdapter(streamedHttpResponse, ctx, requestContext, executeFuture);
    TestSubscriber subscriber = new TestSubscriber();
    publisherAdapter.subscribe(subscriber);
    verify(ctx, times(0)).read();
    verify(ctx, times(0)).close();
    assertThat(subscriber.isCompleted).isEqualTo(true);
    verify(channelPool).release(channel);
    executeFuture.join();
    assertThat(executeFuture).isCompleted();
}
Also used : DefaultStreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.DefaultStreamedHttpResponse) StreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse) DefaultStreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.DefaultStreamedHttpResponse) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Aggregations

StreamedHttpResponse (software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)2 HttpContent (io.netty.handler.codec.http.HttpContent)2 Test (org.junit.Test)2 SdkAsyncHttpResponseHandler (software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler)2 DefaultStreamedHttpResponse (software.amazon.awssdk.http.nio.netty.internal.nrs.DefaultStreamedHttpResponse)2 ByteBuf (io.netty.buffer.ByteBuf)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 SdkHttpResponse (software.amazon.awssdk.http.SdkHttpResponse)1 HttpStreamsClientHandler (software.amazon.awssdk.http.nio.netty.internal.nrs.HttpStreamsClientHandler)1