Search in sources :

Example 1 with Downstream

use of ratpack.exec.Downstream in project ratpack by ratpack.

the class RequestBody method read.

@Override
public Promise<ByteBuf> read(long maxContentLength, Block onTooLarge) {
    return Promise.<ByteBuf>async(downstream -> {
        if (read) {
            downstream.error(new RequestBodyAlreadyReadException());
            return;
        }
        read = true;
        this.onTooLarge = onTooLarge;
        if (advertisedLength > maxContentLength || length > maxContentLength) {
            tooLarge(downstream);
        } else if (done) {
            complete(downstream);
        } else {
            this.maxContentLength = maxContentLength;
            this.downstream = downstream;
            if (HttpUtil.is100ContinueExpected(request)) {
                HttpResponse continueResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE, Unpooled.EMPTY_BUFFER);
                ctx.writeAndFlush(continueResponse).addListener(future -> {
                    if (!future.isSuccess()) {
                        ctx.fireExceptionCaught(future.cause());
                    }
                });
            }
            ctx.read();
        }
    });
}
Also used : Downstream(ratpack.exec.Downstream) Promise(ratpack.exec.Promise) RequestBodyAlreadyReadException(ratpack.http.RequestBodyAlreadyReadException) Block(ratpack.func.Block) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) RequestBodyTooLargeException(ratpack.http.RequestBodyTooLargeException) Consumer(java.util.function.Consumer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) io.netty.handler.codec.http(io.netty.handler.codec.http) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Subscription(org.reactivestreams.Subscription) TransformablePublisher(ratpack.stream.TransformablePublisher) RequestBodyAlreadyReadException(ratpack.http.RequestBodyAlreadyReadException) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with Downstream

use of ratpack.exec.Downstream in project ratpack by ratpack.

the class BlockingHttpClient method request.

public ReceivedResponse request(HttpClient httpClient, URI uri, ExecController execController, Duration timeout, Action<? super RequestSpec> action) throws Throwable {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<ExecResult<ReceivedResponse>> result = new AtomicReference<>();
    execController.fork().start(e -> httpClient.request(uri, action.prepend(s -> s.readTimeout(Duration.ofHours(1)))).map(response -> {
        TypedData responseBody = response.getBody();
        ByteBuf responseBuffer = responseBody.getBuffer();
        ByteBuf heapResponseBodyBuffer = unreleasableBuffer(responseBuffer.isDirect() ? TestByteBufAllocators.LEAKING_UNPOOLED_HEAP.heapBuffer(responseBuffer.readableBytes()).writeBytes(responseBuffer) : responseBuffer.retain());
        return new DefaultReceivedResponse(response.getStatus(), response.getHeaders(), new ByteBufBackedTypedData(heapResponseBodyBuffer, responseBody.getContentType()));
    }).connect(new Downstream<ReceivedResponse>() {

        @Override
        public void success(ReceivedResponse value) {
            result.set(ExecResult.of(Result.success(value)));
            latch.countDown();
        }

        @Override
        public void error(Throwable throwable) {
            result.set(ExecResult.of(Result.error(throwable)));
            latch.countDown();
        }

        @Override
        public void complete() {
            result.set(ExecResult.complete());
            latch.countDown();
        }
    }));
    try {
        if (!latch.await(timeout.toNanos(), TimeUnit.NANOSECONDS)) {
            TemporalUnit unit = timeout.getUnits().get(0);
            throw new IllegalStateException("Request to " + uri + " took more than " + timeout.get(unit) + " " + unit.toString() + " to complete");
        }
    } catch (InterruptedException e) {
        throw Exceptions.uncheck(e);
    }
    return result.get().getValueOrThrow();
}
Also used : TypedData(ratpack.http.TypedData) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) TemporalUnit(java.time.temporal.TemporalUnit) DefaultReceivedResponse(ratpack.http.client.internal.DefaultReceivedResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ReceivedResponse(ratpack.http.client.ReceivedResponse) DefaultReceivedResponse(ratpack.http.client.internal.DefaultReceivedResponse) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) Downstream(ratpack.exec.Downstream) ExecResult(ratpack.exec.ExecResult)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 Downstream (ratpack.exec.Downstream)2 Unpooled (io.netty.buffer.Unpooled)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 TemporalUnit (java.time.temporal.TemporalUnit)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1 Subscription (org.reactivestreams.Subscription)1 ExecResult (ratpack.exec.ExecResult)1 Promise (ratpack.exec.Promise)1 Block (ratpack.func.Block)1 RequestBodyAlreadyReadException (ratpack.http.RequestBodyAlreadyReadException)1 RequestBodyTooLargeException (ratpack.http.RequestBodyTooLargeException)1 TypedData (ratpack.http.TypedData)1 ReceivedResponse (ratpack.http.client.ReceivedResponse)1 DefaultReceivedResponse (ratpack.http.client.internal.DefaultReceivedResponse)1