Search in sources :

Example 16 with Undertow

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

the class HelloWorldServer method main.

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

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
            exchange.getResponseSender().send("Hello World");
        }
    }).build();
    server.start();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Undertow(io.undertow.Undertow)

Example 17 with Undertow

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

the class JSRWebSocketServer method main.

public static void main(final String[] args) {
    PathHandler path = Handlers.path();
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path).build();
    server.start();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(JSRWebSocketServer.class.getClassLoader()).setContextPath("/").addWelcomePage("index.html").setResourceManager(new ClassPathResourceManager(JSRWebSocketServer.class.getClassLoader(), JSRWebSocketServer.class.getPackage())).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(new DefaultByteBufferPool(true, 100)).addEndpoint(JsrChatWebSocketEndpoint.class)).setDeploymentName("chat.war");
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    try {
        path.addPrefixPath("/", manager.start());
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) PathHandler(io.undertow.server.handlers.PathHandler) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Undertow(io.undertow.Undertow)

Example 18 with Undertow

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

the class ModClusterProxyServer method main.

public static void main(final String[] args) throws IOException {
    final XnioWorker worker = Xnio.getInstance().createWorker(OptionMap.EMPTY);
    final Undertow server;
    final ModCluster modCluster = ModCluster.builder(worker).build();
    try {
        if (chost == null) {
            // We are going to guess it.
            chost = java.net.InetAddress.getLocalHost().getHostName();
            System.out.println("Using: " + chost + ":" + cport);
        }
        modCluster.start();
        // Create the proxy and mgmt handler
        final HttpHandler proxy = modCluster.createProxyHandler();
        final MCMPConfig config = MCMPConfig.webBuilder().setManagementHost(chost).setManagementPort(cport).enableAdvertise().getParent().build();
        final HttpHandler mcmp = config.create(modCluster, proxy);
        server = Undertow.builder().addHttpListener(cport, chost).addHttpListener(pport, phost).setHandler(mcmp).build();
        server.start();
        // Start advertising the mcmp handler
        modCluster.advertise(config);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ModCluster(io.undertow.server.handlers.proxy.mod_cluster.ModCluster) XnioWorker(org.xnio.XnioWorker) MCMPConfig(io.undertow.server.handlers.proxy.mod_cluster.MCMPConfig) Undertow(io.undertow.Undertow) IOException(java.io.IOException)

Example 19 with Undertow

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

the class SimpleBenchmarkState method before.

@Setup
public final void before() {
    Undertow.Builder builder = Undertow.builder().setIoThreads(4).setWorkerThreads(64).setServerOption(UndertowOptions.SHUTDOWN_TIMEOUT, 10000).setSocketOption(Options.SSL_CLIENT_AUTH_MODE, SslClientAuthMode.NOT_REQUESTED).setHandler(Handlers.routing().get("/blocking", new BlockingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            String value = exchange.getQueryParameters().get("size").getFirst();
            int bytes = Integer.parseInt(value);
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/octet-stream").put(Headers.CONTENT_LENGTH, value);
            OutputStream out = exchange.getOutputStream();
            for (int i = 0; i < bytes; i++) {
                out.write(1);
            }
        }
    })).post("/blocking", new BlockingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            InputStream stream = exchange.getInputStream();
            long length = BenchmarkUtils.length(stream);
            String stringValue = Long.toString(length);
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain").put(Headers.CONTENT_LENGTH, stringValue.length());
            exchange.getResponseSender().send(stringValue);
        }
    })));
    switch(listenerType) {
        case HTTP:
            builder.addHttpListener(PORT, "0.0.0.0");
            break;
        case HTTPS:
            builder.addHttpsListener(PORT, "0.0.0.0", TLSUtils.newServerContext());
            break;
        default:
            throw new IllegalStateException("Unknown protocol: " + listenerType);
    }
    undertow = builder.build();
    undertow.start();
    client = HttpClients.custom().disableConnectionState().disableAutomaticRetries().setSSLContext(TLSUtils.newClientContext()).setMaxConnPerRoute(100).setMaxConnTotal(100).build();
    baseUri = (listenerType == ListenerType.HTTP ? "http" : "https") + "://localhost:" + PORT;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) BlockingHandler(io.undertow.server.handlers.BlockingHandler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Undertow(io.undertow.Undertow) IOException(java.io.IOException) Setup(org.openjdk.jmh.annotations.Setup)

Example 20 with Undertow

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

the class FileHandlerTestCase method main.

/*
    Starts simple file server, it is useful for testing directory browsing
     */
public static void main(String[] args) throws URISyntaxException {
    Path rootPath = Paths.get(FileHandlerTestCase.class.getResource("page.html").toURI()).getParent().getParent();
    HttpHandler root = new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 1)).setDirectoryListingEnabled(true)));
    Undertow undertow = Undertow.builder().addHttpListener(8888, "localhost").setHandler(root).build();
    undertow.start();
}
Also used : Path(java.nio.file.Path) HttpHandler(io.undertow.server.HttpHandler) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) Undertow(io.undertow.Undertow)

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