Search in sources :

Example 11 with Undertow

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

the class IndyDeployer method deploy.

@Override
public void deploy(BootOptions bootOptions) throws DeployException {
    final DeploymentInfo di = indyDeployment.getDeployment(bootOptions.getContextPath()).setContextPath("/");
    final DeploymentManager dm = Servlets.defaultContainer().addDeployment(di);
    Collection<String> list = Servlets.defaultContainer().listDeployments();
    logger.info("List deployments: {}", list);
    dm.deploy();
    try {
        Integer port = bootOptions.getPort();
        if (port < 1) {
            logger.info("Looking for open Undertow port...");
            final AtomicReference<Exception> errorHolder = new AtomicReference<>();
            final AtomicReference<Integer> usingPort = new AtomicReference<>();
            server = PortFinder.findPortFor(16, (foundPort) -> {
                usingPort.set(foundPort);
                try {
                    return buildAndStartUndertow(dm, foundPort, bootOptions.getBind(), restConfig);
                } catch (Exception e) {
                    errorHolder.set(e);
                }
                return null;
            });
            Exception e = errorHolder.get();
            if (e != null) {
                throw e;
            }
            bootOptions.setPort(usingPort.get());
        } else {
            logger.info("Start Undertow server, bind: {}, port: {}", bootOptions.getBind(), port);
            server = buildAndStartUndertow(dm, port, bootOptions.getBind(), restConfig);
        }
        logger.info("Indy listening on {}:{}\n\n", bootOptions.getBind(), bootOptions.getPort());
    } catch (Exception e) {
        logger.error("Deploy failed", e);
        throw new DeployException("Deploy failed", e);
    }
}
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) DeploymentManager(io.undertow.servlet.api.DeploymentManager) DeployException(org.commonjava.propulsor.deploy.DeployException) AtomicReference(java.util.concurrent.atomic.AtomicReference) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ServletException(javax.servlet.ServletException) DeployException(org.commonjava.propulsor.deploy.DeployException)

Example 12 with Undertow

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

the class AjpClientTestCase method beforeClass.

@BeforeClass
public static void beforeClass() throws IOException {
    // Create xnio worker
    worker = Xnio.getInstance().createWorker(null, DEFAULT_OPTIONS);
    undertow = Undertow.builder().addListener(new Undertow.ListenerBuilder().setType(Undertow.ListenerType.AJP).setPort(AJP_PORT)).setHandler(new PathHandler().addExactPath(MESSAGE, AjpClientTestCase::sendMessage).addExactPath(POST, exchange -> exchange.getRequestReceiver().receiveFullString((exchange1, message) -> exchange1.getResponseSender().send(message)))).build();
    undertow.start();
}
Also used : BeforeClass(org.junit.BeforeClass) StopServerWithExternalWorkerUtils.stopWorker(io.undertow.testutils.StopServerWithExternalWorkerUtils.stopWorker) Xnio(org.xnio.Xnio) URISyntaxException(java.net.URISyntaxException) HttpOneOnly(io.undertow.testutils.HttpOneOnly) HttpServerExchange(io.undertow.server.HttpServerExchange) RunWith(org.junit.runner.RunWith) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) Undertow(io.undertow.Undertow) OptionMap(org.xnio.OptionMap) PathHandler(io.undertow.server.handlers.PathHandler) ClientExchange(io.undertow.client.ClientExchange) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) URI(java.net.URI) ClientResponse(io.undertow.client.ClientResponse) StatusCodes(io.undertow.util.StatusCodes) UndertowClient(io.undertow.client.UndertowClient) ChannelListeners(org.xnio.ChannelListeners) AfterClass(org.junit.AfterClass) Sender(io.undertow.io.Sender) XnioWorker(org.xnio.XnioWorker) DefaultServer(io.undertow.testutils.DefaultServer) AttachmentKey(io.undertow.util.AttachmentKey) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Options(org.xnio.Options) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) ClientConnection(io.undertow.client.ClientConnection) FutureResult(org.xnio.FutureResult) ClientRequest(io.undertow.client.ClientRequest) Headers(io.undertow.util.Headers) Methods(io.undertow.util.Methods) Assert(org.junit.Assert) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IoUtils(org.xnio.IoUtils) PathHandler(io.undertow.server.handlers.PathHandler) BeforeClass(org.junit.BeforeClass)

Example 13 with Undertow

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

the class StopTestCase method testStopUndertowAfterExceptionDuringStart.

@Test
public void testStopUndertowAfterExceptionDuringStart() {
    // Making the NioXnioWorker constructor throw an exception, resulting in the Undertow.worker field not getting set.
    Undertow undertow = Undertow.builder().setWorkerOption(Options.WORKER_IO_THREADS, -1).build();
    try {
        undertow.start();
    } catch (RuntimeException ignore) {
    }
    undertow.stop();
}
Also used : Undertow(io.undertow.Undertow) Test(org.junit.Test)

Example 14 with Undertow

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

the class WebSocketServer method main.

public static void main(final String[] args) {
    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) {
                    WebSockets.sendText(message.getData(), channel, null);
                }
            });
            channel.resumeReceives();
        }
    })).addPrefixPath("/", resource(new ClassPathResourceManager(WebSocketServer.class.getClassLoader(), WebSocketServer.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 15 with Undertow

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

the class ReverseProxyServer method main.

public static void main(final String[] args) {
    try {
        final Undertow server1 = Undertow.builder().addHttpListener(8081, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send("Server1");
            }
        }).build();
        server1.start();
        final Undertow server2 = Undertow.builder().addHttpListener(8082, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send("Server2");
            }
        }).build();
        server2.start();
        final Undertow server3 = Undertow.builder().addHttpListener(8083, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send("Server3");
            }
        }).build();
        server3.start();
        LoadBalancingProxyClient loadBalancer = new LoadBalancingProxyClient().addHost(new URI("http://localhost:8081")).addHost(new URI("http://localhost:8082")).addHost(new URI("http://localhost:8083")).setConnectionsPerThread(20);
        Undertow reverseProxy = Undertow.builder().addHttpListener(8080, "localhost").setIoThreads(4).setHandler(ProxyHandler.builder().setProxyClient(loadBalancer).setMaxRequestTime(30000).build()).build();
        reverseProxy.start();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Undertow(io.undertow.Undertow) LoadBalancingProxyClient(io.undertow.server.handlers.proxy.LoadBalancingProxyClient)

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