Search in sources :

Example 6 with Promise

use of ratpack.exec.Promise 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 7 with Promise

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

the class FlatMapPublisher method subscribe.

@Override
public void subscribe(final Subscriber<? super O> outSubscriber) {
    input.subscribe(new Subscriber<I>() {

        private Subscription subscription;

        private final AtomicBoolean done = new AtomicBoolean();

        @Override
        public void onSubscribe(Subscription subscription) {
            this.subscription = new Subscription() {

                @Override
                public void request(long n) {
                    subscription.request(n);
                }

                @Override
                public void cancel() {
                    subscription.cancel();
                }
            };
            outSubscriber.onSubscribe(this.subscription);
        }

        @Override
        public void onNext(I in) {
            if (done.get()) {
                return;
            }
            Promise<? extends O> out;
            try {
                out = function.apply(in);
            } catch (Throwable throwable) {
                subscription.cancel();
                innerOnError(throwable);
                return;
            }
            out.onError(e -> {
                subscription.cancel();
                innerOnError(e);
            }).then(v -> {
                if (!done.get()) {
                    outSubscriber.onNext(v);
                }
            });
        }

        @Override
        public void onError(Throwable t) {
            Promise.value(t).then(this::innerOnError);
        }

        public void innerOnError(Throwable t) {
            if (done.compareAndSet(false, true)) {
                outSubscriber.onError(t);
            }
        }

        @Override
        public void onComplete() {
            Operation.noop().then(() -> {
                if (done.compareAndSet(false, true)) {
                    outSubscriber.onComplete();
                }
            });
        }
    });
}
Also used : Function(ratpack.func.Function) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) TransformablePublisher(ratpack.stream.TransformablePublisher) Subscriber(org.reactivestreams.Subscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Promise(ratpack.exec.Promise) Subscription(org.reactivestreams.Subscription)

Example 8 with Promise

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

the class DefaultRequestFixture method invoke.

private HandlingResult invoke(Handler handler, Registry registry, DefaultHandlingResult.ResultsHolder results) throws HandlerTimeoutException {
    ServerConfig serverConfig = registry.get(ServerConfig.class);
    DefaultRequest request = new DefaultRequest(Instant.now(), requestHeaders, HttpMethod.valueOf(method.toUpperCase()), HttpVersion.valueOf(protocol), uri, new InetSocketAddress(remoteHostAndPort.getHostText(), remoteHostAndPort.getPort()), new InetSocketAddress(localHostAndPort.getHostText(), localHostAndPort.getPort()), serverConfig, new RequestBodyReader() {

        @Override
        public long getContentLength() {
            return requestBody.readableBytes();
        }

        @Override
        public Promise<? extends ByteBuf> read(long maxContentLength, Block onTooLarge) {
            return Promise.value(requestBody).route(r -> r.readableBytes() > maxContentLength, onTooLarge.action());
        }

        @Override
        public TransformablePublisher<? extends ByteBuf> readStream(long maxContentLength) {
            return Streams.<ByteBuf>yield(r -> {
                if (r.getRequestNum() > 0) {
                    return null;
                } else {
                    return requestBody;
                }
            });
        }
    });
    if (pathBinding != null) {
        handler = Handlers.chain(ctx -> {
            ctx.getExecution().get(PathBindingStorage.TYPE).push(pathBinding);
            ctx.next();
        }, handler);
    }
    try {
        return new DefaultHandlingResult(request, results, responseHeaders, registry, timeout, handler);
    } catch (Exception e) {
        throw Exceptions.uncheck(e);
    } finally {
        registry.get(ExecController.class).close();
    }
}
Also used : RatpackServer(ratpack.server.RatpackServer) HttpVersion(io.netty.handler.codec.http.HttpVersion) ClientErrorHandler(ratpack.error.ClientErrorHandler) RequestFixture(ratpack.test.handling.RequestFixture) RootPathBinding(ratpack.path.internal.RootPathBinding) HandlerTimeoutException(ratpack.test.handling.HandlerTimeoutException) Unpooled.unreleasableBuffer(io.netty.buffer.Unpooled.unreleasableBuffer) ServerRegistry(ratpack.server.internal.ServerRegistry) ByteBuf(io.netty.buffer.ByteBuf) MutableHeaders(ratpack.http.MutableHeaders) PathBindingStorage(ratpack.path.internal.PathBindingStorage) RequestBodyReader(ratpack.server.internal.RequestBodyReader) ServerConfig(ratpack.server.ServerConfig) ServerConfigBuilder(ratpack.server.ServerConfigBuilder) Registry(ratpack.registry.Registry) Map(java.util.Map) CharsetUtil(io.netty.util.CharsetUtil) DefaultPathBinding(ratpack.path.internal.DefaultPathBinding) DefaultRequest(ratpack.http.internal.DefaultRequest) Unpooled.buffer(io.netty.buffer.Unpooled.buffer) DefaultExecController(ratpack.exec.internal.DefaultExecController) Streams(ratpack.stream.Streams) RegistryBuilder(ratpack.registry.RegistryBuilder) ServerErrorHandler(ratpack.error.ServerErrorHandler) ImmutableMap(com.google.common.collect.ImmutableMap) HttpMethod(io.netty.handler.codec.http.HttpMethod) Promise(ratpack.exec.Promise) Block(ratpack.func.Block) Instant(java.time.Instant) HostAndPort(com.google.common.net.HostAndPort) InetSocketAddress(java.net.InetSocketAddress) Chain(ratpack.handling.Chain) RegistrySpec(ratpack.registry.RegistrySpec) Impositions(ratpack.impose.Impositions) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HandlingResult(ratpack.test.handling.HandlingResult) Action(ratpack.func.Action) Handler(ratpack.handling.Handler) Exceptions(ratpack.util.Exceptions) TransformablePublisher(ratpack.stream.TransformablePublisher) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handlers(ratpack.handling.Handlers) NettyHeadersBackedMutableHeaders(ratpack.http.internal.NettyHeadersBackedMutableHeaders) ExecController(ratpack.exec.ExecController) TransformablePublisher(ratpack.stream.TransformablePublisher) DefaultRequest(ratpack.http.internal.DefaultRequest) InetSocketAddress(java.net.InetSocketAddress) DefaultExecController(ratpack.exec.internal.DefaultExecController) ExecController(ratpack.exec.ExecController) ByteBuf(io.netty.buffer.ByteBuf) RequestBodyReader(ratpack.server.internal.RequestBodyReader) HandlerTimeoutException(ratpack.test.handling.HandlerTimeoutException) ServerConfig(ratpack.server.ServerConfig) Promise(ratpack.exec.Promise) Block(ratpack.func.Block)

Aggregations

Promise (ratpack.exec.Promise)8 TransformablePublisher (ratpack.stream.TransformablePublisher)6 List (java.util.List)5 Subscription (org.reactivestreams.Subscription)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Operation (ratpack.exec.Operation)4 Action (ratpack.func.Action)4 BufferingPublisher (ratpack.stream.internal.BufferingPublisher)4 Types (ratpack.util.Types)4 Lists (com.google.common.collect.Lists)3 Iterator (java.util.Iterator)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ExecResult (ratpack.exec.ExecResult)3 Execution (ratpack.exec.Execution)3 ParallelBatch (ratpack.exec.util.ParallelBatch)3 BiAction (ratpack.func.BiAction)3 ByteBuf (io.netty.buffer.ByteBuf)2 Block (ratpack.func.Block)2