Search in sources :

Example 1 with Undertow

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

the class JaxRsBooter method deploy.

@Override
public boolean deploy() {
    boolean started;
    final IndyDeployment indyDeployment = container.instance().select(IndyDeployment.class).get();
    final DeploymentInfo di = indyDeployment.getDeployment(bootOptions.getContextPath()).setContextPath("/");
    final DeploymentManager dm = Servlets.defaultContainer().addDeployment(di);
    dm.deploy();
    status = new BootStatus();
    try {
        Integer port = bootOptions.getPort();
        if (port < 1) {
            System.out.println("Looking for open port...");
            final ThreadLocal<ServletException> errorHolder = new ThreadLocal<>();
            ThreadLocal<Integer> usingPort = new ThreadLocal<>();
            server = PortFinder.findPortFor(16, (foundPort) -> {
                Undertow undertow = null;
                try {
                    undertow = Undertow.builder().setHandler(dm.start()).addHttpListener(foundPort, bootOptions.getBind()).build();
                    undertow.start();
                    usingPort.set(foundPort);
                } catch (ServletException e) {
                    errorHolder.set(e);
                }
                return undertow;
            });
            ServletException e = errorHolder.get();
            if (e != null) {
                throw e;
            }
            bootOptions.setPort(usingPort.get());
        } else {
            server = Undertow.builder().setHandler(dm.start()).addHttpListener(port, bootOptions.getBind()).build();
            server.start();
        }
        System.out.println("Using: " + bootOptions.getPort());
        status.markSuccess();
        started = true;
        System.out.printf("Indy listening on %s:%s\n\n", bootOptions.getBind(), bootOptions.getPort());
    } catch (ServletException | RuntimeException e) {
        status.markFailed(ERR_CANT_LISTEN, e);
        started = false;
    }
    return started;
}
Also used : IndyDeployment(org.commonjava.indy.bind.jaxrs.IndyDeployment) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) ServletException(javax.servlet.ServletException) PortFinder(org.commonjava.indy.boot.PortFinder) LoggerFactory(org.slf4j.LoggerFactory) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) WeldBootInterface(org.commonjava.indy.boot.WeldBootInterface) Undertow(io.undertow.Undertow) IndyConfigFactory(org.commonjava.indy.conf.IndyConfigFactory) Servlets(io.undertow.servlet.Servlets) IndyLifecycleManager(org.commonjava.indy.action.IndyLifecycleManager) BootInterface(org.commonjava.indy.boot.BootInterface) ConfigurationException(org.commonjava.web.config.ConfigurationException) Service(org.commonjava.atservice.annotation.Service) Weld(org.jboss.weld.environment.se.Weld) Logger(org.slf4j.Logger) BootStatus(org.commonjava.indy.boot.BootStatus) BootOptions(org.commonjava.indy.boot.BootOptions) WeldContainer(org.jboss.weld.environment.se.WeldContainer) DeploymentManager(io.undertow.servlet.api.DeploymentManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) IndyBootException(org.commonjava.indy.boot.IndyBootException) Holder(javax.xml.ws.Holder) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) BeanManager(javax.enterprise.inject.spi.BeanManager) DeploymentManager(io.undertow.servlet.api.DeploymentManager) BootStatus(org.commonjava.indy.boot.BootStatus) IndyDeployment(org.commonjava.indy.bind.jaxrs.IndyDeployment) ServletException(javax.servlet.ServletException) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Undertow(io.undertow.Undertow)

Example 2 with Undertow

use of io.undertow.Undertow in project cxf by apache.

the class UndertowHTTPServerEngine method createServer.

private Undertow createServer(URL url, UndertowHTTPHandler undertowHTTPHandler) throws Exception {
    Undertow.Builder result = Undertow.builder();
    result.setServerOption(UndertowOptions.IDLE_TIMEOUT, getMaxIdleTime());
    if (tlsServerParameters != null) {
        if (this.sslContext == null) {
            this.sslContext = createSSLContext();
        }
        result = result.addHttpsListener(getPort(), getHost(), this.sslContext);
    } else {
        result = result.addHttpListener(getPort(), getHost());
    }
    path = Handlers.path(new NotFoundHandler());
    if (url.getPath().length() == 0) {
        result = result.setHandler(Handlers.trace(undertowHTTPHandler));
    } else {
        if (undertowHTTPHandler.isContextMatchExact()) {
            path.addExactPath(url.getPath(), undertowHTTPHandler);
        } else {
            path.addPrefixPath(url.getPath(), undertowHTTPHandler);
        }
        result = result.setHandler(wrapHandler(path));
    }
    result = decorateUndertowSocketConnection(result);
    result = disableSSLv3(result);
    result = configureThreads(result);
    return result.build();
}
Also used : Builder(io.undertow.Undertow.Builder) Undertow(io.undertow.Undertow)

Example 3 with Undertow

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

the class UndertowApp method main.

public static void main(String[] args) throws IOException {
    String cfgFile = StringUtils.defaultIfBlank(System.getenv("MXGWD_CONFIG_FILE"), "mxgwd.yaml");
    Config cfg = Value.get(YamlConfigLoader.loadFromFile(cfgFile), Config::new);
    Gateway gw = new Gateway(cfg);
    CatchAllHandler allHandler = new CatchAllHandler(gw);
    ActivePoliciesListingHandler activePolicies = new ActivePoliciesListingHandler(gw);
    Undertow server = Undertow.builder().addHttpListener(cfg.getServer().getPort(), "0.0.0.0").setHandler(Handlers.path().addExactPath("/_matrix/client/r0/policy/policies", activePolicies).addPrefixPath("/", allHandler)).build();
    server.start();
}
Also used : Config(io.kamax.mxgwd.config.Config) Gateway(io.kamax.mxgwd.model.Gateway) Undertow(io.undertow.Undertow)

Example 4 with Undertow

use of io.undertow.Undertow in project runwar by cfmlprojects.

the class Start method main.

public static void main(String[] args) throws Exception {
    System.setProperty("log4j.configuration", "log4j.filelog.xml");
    ServerOptions serverOptions = CommandLineHandler.parseLogArguments(args);
    LoggerFactory.init(serverOptions);
    if (args.length == 0) {
        if (new File("server.json").exists()) {
            serverOptions = new ConfigParser(new File("server.json")).getServerOptions();
        } else {
            // print usage
            serverOptions = CommandLineHandler.parseArguments(args);
        }
    } else {
        serverOptions = CommandLineHandler.parseArguments(args);
    }
    if (serverOptions.getLoadBalance() != null && serverOptions.getLoadBalance().length > 0) {
        final List<String> balanceHosts = new ArrayList<String>();
        RunwarLogger.LOG.info("Initializing...");
        final LoadBalancingProxyClient loadBalancer = new LoadBalancingProxyClient();
        for (String balanceHost : serverOptions.getLoadBalance()) {
            if (serverOptions.getWarFile() != null) {
                RunwarLogger.LOG.info("Starting instance of " + serverOptions.getWarFile().getParent() + "...");
                launchServer(balanceHost, serverOptions);
            }
            loadBalancer.addHost(new URI(balanceHost));
            balanceHosts.add(balanceHost);
            RunwarLogger.LOG.info("Added balanced host: " + balanceHost);
            Thread.sleep(3000);
        }
        int port = serverOptions.getPortNumber();
        loadBalancer.setConnectionsPerThread(20);
        RunwarLogger.LOG.info("Hosts loaded");
        RunwarLogger.LOG.info("Starting load balancer on 127.0.0.1 port " + port + "...");
        ProxyHandler proxyHandler = ProxyHandler.builder().setProxyClient(loadBalancer).setMaxRequestTime(30000).setNext(ResponseCodeHandler.HANDLE_404).build();
        Undertow reverseProxy = Undertow.builder().addHttpListener(port, "localhost").setIoThreads(4).setHandler(proxyHandler).build();
        reverseProxy.start();
        RunwarLogger.LOG.info("View balancer admin on http://127.0.0.1:9080");
        Undertow adminServer = Undertow.builder().addHttpListener(9080, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
                if (exchange.getQueryParameters().get("addHost") != null) {
                    String balanceHost = URLDecoder.decode(exchange.getQueryParameters().get("addHost").toString(), "UTF-8");
                    balanceHost = balanceHost.replaceAll("]|\\[", "");
                    loadBalancer.addHost(new URI(balanceHost));
                    balanceHosts.add(balanceHost);
                }
                if (exchange.getQueryParameters().get("removeHost") != null) {
                    String balanceHost = URLDecoder.decode(exchange.getQueryParameters().get("removeHost").toString(), "UTF-8");
                    balanceHost = balanceHost.replaceAll("]|\\[", "");
                    loadBalancer.removeHost(new URI(balanceHost));
                    balanceHosts.remove(balanceHost);
                }
                String response = "";
                // response += URLDecoder.decode(exchange.getQueryString(),"UTF-8");
                for (String balanceHost : balanceHosts) {
                    String[] schemeHostAndPort = balanceHost.split(":");
                    String host = schemeHostAndPort[1].replaceAll("^//", "");
                    int port = schemeHostAndPort.length > 2 ? Integer.parseInt(schemeHostAndPort[2]) : 80;
                    response += balanceHost + " <a href='?removeHost=" + balanceHost + "'>remove</a> Listening: " + serverListening(host, port) + "<br/>";
                }
                exchange.getResponseSender().send("<h3>Balanced Hosts</h3><form action='?'> Add Host:<input type='text' name='addHost' placeholder='http://127.0.0.1:7070'><input type='submit'></form>" + response);
            }
        }).build();
        adminServer.start();
        RunwarLogger.LOG.info("Started load balancer.");
    } else {
        Server server = new Server();
        try {
            server.startServer(serverOptions);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ProxyHandler(io.undertow.server.handlers.proxy.ProxyHandler) ArrayList(java.util.ArrayList) ConfigParser(runwar.options.ConfigParser) URI(java.net.URI) LoadBalancingProxyClient(io.undertow.server.handlers.proxy.LoadBalancingProxyClient) HttpServerExchange(io.undertow.server.HttpServerExchange) ServerOptions(runwar.options.ServerOptions) File(java.io.File) Undertow(io.undertow.Undertow)

Example 5 with Undertow

use of io.undertow.Undertow in project tutorials by eugenp.

the class FileServer method main.

public static void main(String[] args) {
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100)).setDirectoryListingEnabled(true)).build();
    server.start();
}
Also used : Undertow(io.undertow.Undertow) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager)

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