Search in sources :

Example 1 with RequestBodyReader

use of ratpack.server.internal.RequestBodyReader 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.getHost(), remoteHostAndPort.getPort()), new InetSocketAddress(localHostAndPort.getHost(), localHostAndPort.getPort()), serverConfig, new RequestBodyReader() {

        private boolean unread = true;

        private long maxContentLength = 8096;

        public boolean isUnread() {
            return unread;
        }

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

        @Override
        public long getMaxContentLength() {
            return maxContentLength;
        }

        @Override
        public void setMaxContentLength(long maxContentLength) {
            this.maxContentLength = maxContentLength;
        }

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

        @Override
        public TransformablePublisher<? extends ByteBuf> readStream() {
            return Streams.publish(Collections.singleton(requestBody)).wiretap(e -> unread = false);
        }
    }, idleTimeout -> {
    }, null);
    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) Optional(java.util.Optional) TransformablePublisher(ratpack.stream.TransformablePublisher) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handlers(ratpack.handling.Handlers) NettyHeadersBackedMutableHeaders(ratpack.http.internal.NettyHeadersBackedMutableHeaders) ExecController(ratpack.exec.ExecController) Collections(java.util.Collections) 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

ImmutableMap (com.google.common.collect.ImmutableMap)1 HostAndPort (com.google.common.net.HostAndPort)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled.buffer (io.netty.buffer.Unpooled.buffer)1 Unpooled.unreleasableBuffer (io.netty.buffer.Unpooled.unreleasableBuffer)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1 CharsetUtil (io.netty.util.CharsetUtil)1 InetSocketAddress (java.net.InetSocketAddress)1 Instant (java.time.Instant)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ClientErrorHandler (ratpack.error.ClientErrorHandler)1 ServerErrorHandler (ratpack.error.ServerErrorHandler)1 ExecController (ratpack.exec.ExecController)1 Promise (ratpack.exec.Promise)1 DefaultExecController (ratpack.exec.internal.DefaultExecController)1