Search in sources :

Example 1 with MemorySize

use of io.quarkus.runtime.configuration.MemorySize in project quarkus by quarkusio.

the class VertxHttpRecorder method createBodyHandler.

public Handler<RoutingContext> createBodyHandler() {
    BodyHandler bodyHandler = BodyHandler.create();
    Optional<MemorySize> maxBodySize = httpConfiguration.getValue().limits.maxBodySize;
    if (maxBodySize.isPresent()) {
        bodyHandler.setBodyLimit(maxBodySize.get().asLongValue());
    }
    final BodyConfig bodyConfig = httpConfiguration.getValue().body;
    bodyHandler.setHandleFileUploads(bodyConfig.handleFileUploads);
    bodyHandler.setUploadsDirectory(bodyConfig.uploadsDirectory);
    bodyHandler.setDeleteUploadedFilesOnEnd(bodyConfig.deleteUploadedFilesOnEnd);
    bodyHandler.setMergeFormAttributes(bodyConfig.mergeFormAttributes);
    bodyHandler.setPreallocateBodyBuffer(bodyConfig.preallocateBodyBuffer);
    return new Handler<RoutingContext>() {

        @Override
        public void handle(RoutingContext event) {
            if (!Context.isOnEventLoopThread()) {
                ((ConnectionBase) event.request().connection()).channel().eventLoop().execute(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            // this can happen if blocking authentication is involved for get requests
                            if (!event.request().isEnded()) {
                                event.request().resume();
                                if (CAN_HAVE_BODY.contains(event.request().method())) {
                                    bodyHandler.handle(event);
                                } else {
                                    event.next();
                                }
                            } else {
                                event.next();
                            }
                        } catch (Throwable t) {
                            event.fail(t);
                        }
                    }
                });
            } else {
                if (!event.request().isEnded()) {
                    event.request().resume();
                }
                if (CAN_HAVE_BODY.contains(event.request().method())) {
                    bodyHandler.handle(event);
                } else {
                    event.next();
                }
            }
        }
    };
}
Also used : MemorySize(io.quarkus.runtime.configuration.MemorySize) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) AccessLogHandler(io.quarkus.vertx.http.runtime.filters.accesslog.AccessLogHandler) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VertxHandler(io.vertx.core.net.impl.VertxHandler) RemoteSyncHandler(io.quarkus.vertx.http.runtime.devmode.RemoteSyncHandler) Handler(io.vertx.core.Handler) ConnectionBase(io.vertx.core.net.impl.ConnectionBase)

Example 2 with MemorySize

use of io.quarkus.runtime.configuration.MemorySize in project quarkus by quarkusio.

the class UndertowDeploymentRecorder method startUndertow.

public Handler<RoutingContext> startUndertow(ShutdownContext shutdown, ExecutorService executorService, DeploymentManager manager, List<HandlerWrapper> wrappers, ServletRuntimeConfig servletRuntimeConfig) throws Exception {
    shutdown.addShutdownTask(new Runnable() {

        @Override
        public void run() {
            try {
                manager.stop();
            } catch (ServletException e) {
                log.error("Failed to stop deployment", e);
            }
            manager.undeploy();
        }
    });
    HttpHandler main = manager.getDeployment().getHandler();
    for (HandlerWrapper i : wrappers) {
        main = i.wrap(main);
    }
    if (!manager.getDeployment().getDeploymentInfo().getContextPath().equals("/")) {
        PathHandler pathHandler = new PathHandler().addPrefixPath(manager.getDeployment().getDeploymentInfo().getContextPath(), main);
        main = pathHandler;
    }
    currentRoot = main;
    DefaultExchangeHandler defaultHandler = new DefaultExchangeHandler(ROOT_HANDLER);
    UndertowBufferAllocator allocator = new UndertowBufferAllocator(servletRuntimeConfig.directBuffers.orElse(DEFAULT_DIRECT_BUFFERS), (int) servletRuntimeConfig.bufferSize.orElse(new MemorySize(BigInteger.valueOf(DEFAULT_BUFFER_SIZE))).asLongValue());
    UndertowOptionMap.Builder undertowOptions = UndertowOptionMap.builder();
    undertowOptions.set(UndertowOptions.MAX_PARAMETERS, servletRuntimeConfig.maxParameters);
    UndertowOptionMap undertowOptionMap = undertowOptions.getMap();
    return new Handler<RoutingContext>() {

        @Override
        public void handle(RoutingContext event) {
            if (!event.request().isEnded()) {
                event.request().pause();
            }
            // we handle auth failure directly
            event.remove(QuarkusHttpUser.AUTH_FAILURE_HANDLER);
            VertxHttpExchange exchange = new VertxHttpExchange(event.request(), allocator, executorService, event, event.getBody());
            exchange.setPushHandler(VertxHttpRecorder.getRootHandler());
            Optional<MemorySize> maxBodySize = httpConfiguration.getValue().limits.maxBodySize;
            if (maxBodySize.isPresent()) {
                exchange.setMaxEntitySize(maxBodySize.get().asLongValue());
            }
            Duration readTimeout = httpConfiguration.getValue().readTimeout;
            exchange.setReadTimeout(readTimeout.toMillis());
            exchange.setUndertowOptions(undertowOptionMap);
            // see https://github.com/quarkusio/quarkus/issues/7782
            if (BlockingOperationControl.isBlockingAllowed()) {
                defaultHandler.handle(exchange);
            } else {
                executorService.submit(new Runnable() {

                    @Override
                    public void run() {
                        defaultHandler.handle(exchange);
                    }
                });
            }
        }
    };
}
Also used : HttpHandler(io.undertow.server.HttpHandler) MemorySize(io.quarkus.runtime.configuration.MemorySize) DefaultExchangeHandler(io.undertow.server.DefaultExchangeHandler) PathHandler(io.undertow.server.handlers.PathHandler) ExceptionHandler(io.undertow.servlet.api.ExceptionHandler) ResponseCodeHandler(io.undertow.server.handlers.ResponseCodeHandler) DefaultExchangeHandler(io.undertow.server.DefaultExchangeHandler) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpHandler(io.undertow.server.HttpHandler) Handler(io.vertx.core.Handler) Duration(java.time.Duration) HandlerWrapper(io.undertow.server.HandlerWrapper) ServletException(javax.servlet.ServletException) RoutingContext(io.vertx.ext.web.RoutingContext) VertxHttpExchange(io.undertow.vertx.VertxHttpExchange) UndertowOptionMap(io.undertow.httpcore.UndertowOptionMap)

Aggregations

MemorySize (io.quarkus.runtime.configuration.MemorySize)2 Handler (io.vertx.core.Handler)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 RemoteSyncHandler (io.quarkus.vertx.http.runtime.devmode.RemoteSyncHandler)1 AccessLogHandler (io.quarkus.vertx.http.runtime.filters.accesslog.AccessLogHandler)1 UndertowOptionMap (io.undertow.httpcore.UndertowOptionMap)1 DefaultExchangeHandler (io.undertow.server.DefaultExchangeHandler)1 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpHandler (io.undertow.server.HttpHandler)1 PathHandler (io.undertow.server.handlers.PathHandler)1 ResponseCodeHandler (io.undertow.server.handlers.ResponseCodeHandler)1 ExceptionHandler (io.undertow.servlet.api.ExceptionHandler)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1 VertxHttpExchange (io.undertow.vertx.VertxHttpExchange)1 ConnectionBase (io.vertx.core.net.impl.ConnectionBase)1 VertxHandler (io.vertx.core.net.impl.VertxHandler)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1 Duration (java.time.Duration)1 ServletException (javax.servlet.ServletException)1