Search in sources :

Example 51 with Undertow

use of io.undertow.Undertow in project undertow by undertow-io.

the class TLS13HalfCloseHangTestCase method testHang.

@Test
public void testHang() throws IOException, GeneralSecurityException, InterruptedException {
    SSLContext clientSslContext = null;
    try {
        clientSslContext = DefaultServer.createClientSslContext("TLSv1.3");
    } catch (Throwable e) {
        // Don't try to run test if TLS 1.3 is not supported
        Assume.assumeNoException(e);
    }
    Undertow server = Undertow.builder().addHttpsListener(0, "localhost", DefaultServer.getServerSslContext()).setHandler((exchange) -> {
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
        exchange.getResponseSender().send("Hello World!\n");
    }).setSocketOption(Options.SSL_ENABLED_PROTOCOLS, Sequence.of("TLSv1.3")).setIoThreads(1).setWorkerThreads(1).build();
    server.start();
    InetSocketAddress address = (InetSocketAddress) server.getListenerInfo().get(0).getAddress();
    String uri = "https://localhost:" + address.getPort() + "/foo";
    doRequest(clientSslContext, address);
    doRequest(clientSslContext, address);
    server.stop();
    // sleep 1 s to prevent BindException (Address already in use) when running the CI
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ignore) {
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLContext(javax.net.ssl.SSLContext) Undertow(io.undertow.Undertow) Test(org.junit.Test)

Example 52 with Undertow

use of io.undertow.Undertow in project undertow by undertow-io.

the class ChatServer method main.

public static void main(final String[] args) {
    System.out.println("To see chat in action is to open two different browsers and point them at http://localhost:8080");
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/myapp", websocket(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
                    final String messageData = message.getData();
                    for (WebSocketChannel session : channel.getPeerConnections()) {
                        WebSockets.sendText(messageData, session, null);
                    }
                }
            });
            channel.resumeReceives();
        }
    })).addPrefixPath("/", resource(new ClassPathResourceManager(ChatServer.class.getClassLoader(), ChatServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
    server.start();
}
Also used : WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Undertow(io.undertow.Undertow) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage)

Example 53 with Undertow

use of io.undertow.Undertow in project indy by Commonjava.

the class IndyDeployer method buildAndStartUndertow.

private Undertow buildAndStartUndertow(DeploymentManager dm, Integer port, String bind, RestConfig restConfig) throws Exception {
    Undertow.Builder builder = Undertow.builder().setServerOption(UndertowOptions.ENABLE_HTTP2, true).setHandler(getGzipEncodeHandler(dm)).addHttpListener(port, bind);
    restConfig.configureBuilder(builder);
    Undertow t = builder.build();
    t.start();
    return t;
}
Also used : Undertow(io.undertow.Undertow)

Example 54 with Undertow

use of io.undertow.Undertow in project indy by Commonjava.

the class IndyDeployer method getGzipEncodeHandler.

private EncodingHandler getGzipEncodeHandler(final DeploymentManager dm) throws ServletException {
    // FROM: https://stackoverflow.com/questions/28295752/compressing-undertow-server-responses#28329810
    final Predicate sizePredicate = Predicates.parse("max-content-size[" + Long.toString(5 * 1024) + "]");
    // For firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=610679
    final Predicate fileTypePredicate = v -> {
        final String path = v.getRequestPath();
        return !NO_NEED_GZIPPED_CONTENT.contains(mimeTyper.getContentType(path).toLowerCase());
    };
    final Predicate mixePredicate = v -> sizePredicate.resolve(v) && fileTypePredicate.resolve(v);
    EncodingHandler eh = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("gzip", new GzipEncodingProvider(), 50, mixePredicate)).setNext(dm.start());
    // sizePredicate ) ).setNext( dm.start() );
    return eh;
}
Also used : Predicates(io.undertow.predicate.Predicates) IndyDeployment(org.commonjava.indy.bind.jaxrs.IndyDeployment) ServletException(javax.servlet.ServletException) HttpServerExchange(io.undertow.server.HttpServerExchange) LoggerFactory(org.slf4j.LoggerFactory) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) AtomicReference(java.util.concurrent.atomic.AtomicReference) Undertow(io.undertow.Undertow) DeployException(org.commonjava.propulsor.deploy.DeployException) Servlets(io.undertow.servlet.Servlets) Inject(javax.inject.Inject) HashSet(java.util.HashSet) BootOptions(org.commonjava.propulsor.boot.BootOptions) DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) GzipEncodingProvider(io.undertow.server.handlers.encoding.GzipEncodingProvider) Deployer(org.commonjava.propulsor.deploy.Deployer) UndertowOptions(io.undertow.UndertowOptions) Logger(org.slf4j.Logger) Collection(java.util.Collection) PortFinder(org.commonjava.propulsor.boot.PortFinder) Set(java.util.Set) MimeTyper(org.commonjava.indy.util.MimeTyper) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ApplicationContent(org.commonjava.indy.util.ApplicationContent) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) Predicate(io.undertow.predicate.Predicate) ApplicationScoped(javax.enterprise.context.ApplicationScoped) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) GzipEncodingProvider(io.undertow.server.handlers.encoding.GzipEncodingProvider) Predicate(io.undertow.predicate.Predicate)

Example 55 with Undertow

use of io.undertow.Undertow in project spring-boot by spring-projects.

the class UndertowWebServer method start.

@Override
public void start() throws WebServerException {
    synchronized (this.monitor) {
        if (this.started) {
            return;
        }
        try {
            if (!this.autoStart) {
                return;
            }
            if (this.undertow == null) {
                this.undertow = createUndertowServer();
            }
            this.undertow.start();
            this.started = true;
            String message = getStartLogMessage();
            logger.info(message);
        } catch (Exception ex) {
            try {
                PortInUseException.ifPortBindingException(ex, (bindException) -> {
                    List<Port> failedPorts = getConfiguredPorts();
                    failedPorts.removeAll(getActualPorts());
                    if (failedPorts.size() == 1) {
                        throw new PortInUseException(failedPorts.get(0).getNumber());
                    }
                });
                throw new WebServerException("Unable to start embedded Undertow", ex);
            } finally {
                stopSilently();
            }
        }
    }
}
Also used : SocketAddress(java.net.SocketAddress) BoundChannel(org.xnio.channels.BoundChannel) WebServerException(org.springframework.boot.web.server.WebServerException) HttpServerExchange(io.undertow.server.HttpServerExchange) IOException(java.io.IOException) Field(java.lang.reflect.Field) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) ArrayList(java.util.ArrayList) Undertow(io.undertow.Undertow) HttpHandler(io.undertow.server.HttpHandler) WebServer(org.springframework.boot.web.server.WebServer) List(java.util.List) ReflectionUtils(org.springframework.util.ReflectionUtils) Closeable(java.io.Closeable) PortInUseException(org.springframework.boot.web.server.PortInUseException) Log(org.apache.commons.logging.Log) GracefulShutdownHandler(io.undertow.server.handlers.GracefulShutdownHandler) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) GracefulShutdownCallback(org.springframework.boot.web.server.GracefulShutdownCallback) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) PortInUseException(org.springframework.boot.web.server.PortInUseException) WebServerException(org.springframework.boot.web.server.WebServerException) ArrayList(java.util.ArrayList) List(java.util.List) WebServerException(org.springframework.boot.web.server.WebServerException) IOException(java.io.IOException) PortInUseException(org.springframework.boot.web.server.PortInUseException)

Aggregations

Undertow (io.undertow.Undertow)55 HttpHandler (io.undertow.server.HttpHandler)27 HttpServerExchange (io.undertow.server.HttpServerExchange)22 PathHandler (io.undertow.server.handlers.PathHandler)16 IOException (java.io.IOException)13 Test (org.junit.Test)11 ClassPathResourceManager (io.undertow.server.handlers.resource.ClassPathResourceManager)10 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)10 DeploymentManager (io.undertow.servlet.api.DeploymentManager)9 URI (java.net.URI)9 TestHttpClient (io.undertow.testutils.TestHttpClient)6 ServletException (javax.servlet.ServletException)6 HttpGet (org.apache.http.client.methods.HttpGet)6 LoadBalancingProxyClient (io.undertow.server.handlers.proxy.LoadBalancingProxyClient)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 XnioWorker (org.xnio.XnioWorker)5 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)4 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)4 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)4 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)4