Search in sources :

Example 1 with RequestEncodingHandler

use of io.undertow.server.handlers.encoding.RequestEncodingHandler in project oap by oaplatform.

the class NioHttpServer method bind.

public void bind(String prefix, HttpHandler handler, boolean compressionSupport) {
    log.debug("bind {}", prefix);
    Preconditions.checkNotNull(prefix);
    Preconditions.checkArgument(!prefix.isEmpty());
    io.undertow.server.HttpHandler httpHandler = exchange -> handler.handleRequest(new HttpServerExchange(exchange));
    if (!forceCompressionSupport && compressionSupport) {
        httpHandler = new EncodingHandler(httpHandler, contentEncodingRepository);
        httpHandler = new RequestEncodingHandler(httpHandler).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER);
    }
    pathHandler.addPrefixPath(prefix, httpHandler);
}
Also used : BlockingHandler(io.undertow.server.handlers.BlockingHandler) Tags(io.micrometer.core.instrument.Tags) UndertowOptions(io.undertow.UndertowOptions) ConnectorStatistics(io.undertow.server.ConnectorStatistics) IOException(java.io.IOException) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) InetSocketAddress(java.net.InetSocketAddress) Metrics(io.micrometer.core.instrument.Metrics) Undertow(io.undertow.Undertow) Options(org.xnio.Options) GzipStreamSourceConduit(io.undertow.conduits.GzipStreamSourceConduit) InflatingStreamSourceConduit(io.undertow.conduits.InflatingStreamSourceConduit) PathHandler(io.undertow.server.handlers.PathHandler) Slf4j(lombok.extern.slf4j.Slf4j) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) Closeable(java.io.Closeable) Preconditions(com.google.common.base.Preconditions) GracefulShutdownHandler(io.undertow.server.handlers.GracefulShutdownHandler) GzipEncodingProvider(io.undertow.server.handlers.encoding.GzipEncodingProvider) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler)

Example 2 with RequestEncodingHandler

use of io.undertow.server.handlers.encoding.RequestEncodingHandler in project oap by oaplatform.

the class NioHttpServer method start.

public void start() {
    Undertow.Builder builder = Undertow.builder().addHttpListener(port, "0.0.0.0").setSocketOption(Options.REUSE_ADDRESSES, true).setSocketOption(Options.TCP_NODELAY, tcpNodelay).setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, true);
    if (backlog > 0)
        builder.setSocketOption(Options.BACKLOG, backlog);
    if (ioThreads > 0)
        builder.setIoThreads(ioThreads);
    if (workerThreads > 0)
        builder.setWorkerThreads(workerThreads);
    if (idleTimeout > 0)
        builder.setServerOption(UndertowOptions.IDLE_TIMEOUT, (int) idleTimeout);
    if (maxEntitySize > 0)
        builder.setServerOption(UndertowOptions.MAX_ENTITY_SIZE, maxEntitySize);
    if (maxParameters > 0)
        builder.setServerOption(UndertowOptions.MAX_PARAMETERS, maxParameters);
    if (maxHeaders > 0)
        builder.setServerOption(UndertowOptions.MAX_HEADERS, maxHeaders);
    if (maxHeaderSize > 0)
        builder.setServerOption(UndertowOptions.MAX_HEADER_SIZE, maxHeaderSize);
    if (statistics)
        builder.setServerOption(UndertowOptions.ENABLE_STATISTICS, true);
    builder.setServerOption(UndertowOptions.ALWAYS_SET_DATE, alwaysSetDate);
    builder.setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, alwaysSetKeepAlive);
    io.undertow.server.HttpHandler handler = pathHandler;
    if (forceCompressionSupport) {
        handler = new EncodingHandler(handler, contentEncodingRepository);
        handler = new RequestEncodingHandler(handler).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER);
    }
    handler = new BlockingHandler(handler);
    handler = new GracefulShutdownHandler(handler);
    builder.setHandler(handler);
    server = builder.build();
    server.start();
    log.info("port {} statistics {} ioThreads {} workerThreads {}", port, statistics, server.getWorker().getMXBean().getIoThreadCount(), server.getWorker().getMXBean().getMaxWorkerPoolSize());
    if (statistics) {
        for (var listenerInfo : server.getListenerInfo()) {
            var sa = (InetSocketAddress) listenerInfo.getAddress();
            var port = String.valueOf(sa.getPort());
            ConnectorStatistics connectorStatistics = listenerInfo.getConnectorStatistics();
            Metrics.gauge("nio_requests", Tags.of("port", port, "type", "total"), connectorStatistics, ConnectorStatistics::getRequestCount);
            Metrics.gauge("nio_requests", Tags.of("port", port, "type", "active"), connectorStatistics, ConnectorStatistics::getActiveRequests);
            Metrics.gauge("nio_requests", Tags.of("port", port, "type", "errors"), connectorStatistics, ConnectorStatistics::getErrorCount);
            Metrics.gauge("nio_connections", Tags.of("port", port, "type", "active"), connectorStatistics, ConnectorStatistics::getActiveConnections);
            Metrics.gauge("nio_pool_size", Tags.of("port", port, "name", "worker", "type", "active"), server, server -> server.getWorker().getMXBean().getWorkerPoolSize());
            Metrics.gauge("nio_pool_size", Tags.of("port", port, "name", "worker", "type", "core"), server, server -> server.getWorker().getMXBean().getCoreWorkerPoolSize());
            Metrics.gauge("nio_pool_size", Tags.of("port", port, "name", "worker", "type", "max"), server, server -> server.getWorker().getMXBean().getMaxWorkerPoolSize());
            Metrics.gauge("nio_pool_size", Tags.of("port", port, "name", "worker", "type", "busy"), server, server -> server.getWorker().getMXBean().getBusyWorkerThreadCount());
            Metrics.gauge("nio_pool_size", Tags.of("port", port, "name", "worker", "type", "queue"), server, server -> server.getWorker().getMXBean().getWorkerQueueSize());
        }
    }
}
Also used : GracefulShutdownHandler(io.undertow.server.handlers.GracefulShutdownHandler) BlockingHandler(io.undertow.server.handlers.BlockingHandler) ConnectorStatistics(io.undertow.server.ConnectorStatistics) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) InetSocketAddress(java.net.InetSocketAddress) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) Undertow(io.undertow.Undertow)

Aggregations

Undertow (io.undertow.Undertow)2 ConnectorStatistics (io.undertow.server.ConnectorStatistics)2 BlockingHandler (io.undertow.server.handlers.BlockingHandler)2 GracefulShutdownHandler (io.undertow.server.handlers.GracefulShutdownHandler)2 EncodingHandler (io.undertow.server.handlers.encoding.EncodingHandler)2 RequestEncodingHandler (io.undertow.server.handlers.encoding.RequestEncodingHandler)2 InetSocketAddress (java.net.InetSocketAddress)2 Preconditions (com.google.common.base.Preconditions)1 Metrics (io.micrometer.core.instrument.Metrics)1 Tags (io.micrometer.core.instrument.Tags)1 UndertowOptions (io.undertow.UndertowOptions)1 GzipStreamSourceConduit (io.undertow.conduits.GzipStreamSourceConduit)1 InflatingStreamSourceConduit (io.undertow.conduits.InflatingStreamSourceConduit)1 PathHandler (io.undertow.server.handlers.PathHandler)1 ContentEncodingRepository (io.undertow.server.handlers.encoding.ContentEncodingRepository)1 DeflateEncodingProvider (io.undertow.server.handlers.encoding.DeflateEncodingProvider)1 GzipEncodingProvider (io.undertow.server.handlers.encoding.GzipEncodingProvider)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Slf4j (lombok.extern.slf4j.Slf4j)1