Search in sources :

Example 41 with HttpHandler

use of io.undertow.server.HttpHandler 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)

Example 42 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ResumeWritesTestCase method testResumeWritesFixedLength.

@Test
public void testResumeWritesFixedLength() throws IOException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.addResponseWrapper(new ReturnZeroWrapper());
            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, HELLO_WORLD.length());
            exchange.getResponseSender().send(HELLO_WORLD);
        }
    });
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 43 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ResumeWritesTestCase method testResumeWritesChunked.

@Test
public void testResumeWritesChunked() throws IOException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.addResponseWrapper(new ReturnZeroWrapper());
            exchange.getResponseSender().send(HELLO_WORLD);
        }
    });
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 44 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class RoutingHandlerTestCase method setup.

@BeforeClass
public static void setup() {
    RoutingHandler commonHandler = Handlers.routing().add(Methods.GET, "/baz", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("baz");
        }
    }).add(Methods.GET, "/baz/{foo}", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("baz-path" + exchange.getQueryParameters().get("foo"));
        }
    });
    RoutingHandler convienceHandler = Handlers.routing().get("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("GET bar");
        }
    }).put("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("PUT bar");
        }
    }).post("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("POST bar");
        }
    }).delete("/bar", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("DELETE bar");
        }
    });
    DefaultServer.setRootHandler(Handlers.routing().add(Methods.GET, "/wild/{test}/*", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("wild:" + exchange.getQueryParameters().get("test") + ":" + exchange.getQueryParameters().get("*"));
        }
    }).add(Methods.GET, "/wilder/*", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("wilder:" + exchange.getQueryParameters().get("*"));
        }
    }).add(Methods.GET, "/wildest*", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("wildest:" + exchange.getQueryParameters().get("*"));
        }
    }).add(Methods.GET, "/foo", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("foo");
        }
    }).add(Methods.GET, "/foo", Predicates.parse("contains[value=%{i,SomeHeader},search='special'] "), new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("special foo");
        }
    }).add(Methods.POST, "/foo", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("posted foo");
        }
    }).add(Methods.GET, "/foo/{bar}", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("foo-path" + exchange.getQueryParameters().get("bar"));
        }
    }).addAll(commonHandler).addAll(convienceHandler));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) RoutingHandler(io.undertow.server.RoutingHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 45 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class CacheHandlerTestCase method testBasicPathBasedCaching.

@Test
public void testBasicPathBasedCaching() throws IOException {
    final AtomicInteger responseCount = new AtomicInteger();
    final HttpHandler messageHandler = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final ResponseCache cache = exchange.getAttachment(ResponseCache.ATTACHMENT_KEY);
            if (!cache.tryServeResponse()) {
                final String data = "Response " + responseCount.incrementAndGet();
                exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, data.length() + "");
                exchange.getResponseSender().send(data);
            }
        }
    };
    final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(100, 10, 1000), messageHandler);
    DefaultServer.setRootHandler(cacheHandler);
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        //it takes 5 hits to make an entry actually get cached
        for (int i = 1; i <= 5; ++i) {
            HttpResponse result = client.execute(get);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            Assert.assertEquals("Response " + i, HttpClientUtils.readResponse(result));
        }
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 5", HttpClientUtils.readResponse(result));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path2");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Response 6", HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpGet(org.apache.http.client.methods.HttpGet) DirectBufferCache(io.undertow.server.handlers.cache.DirectBufferCache) HttpResponse(org.apache.http.HttpResponse) ResponseCache(io.undertow.server.handlers.cache.ResponseCache) CacheHandler(io.undertow.server.handlers.cache.CacheHandler) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)126 HttpServerExchange (io.undertow.server.HttpServerExchange)72 IOException (java.io.IOException)54 BeforeClass (org.junit.BeforeClass)35 Test (org.junit.Test)25 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 HttpGet (org.apache.http.client.methods.HttpGet)19 PathHandler (io.undertow.server.handlers.PathHandler)17 HttpString (io.undertow.util.HttpString)15 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)13 Undertow (io.undertow.Undertow)12 ArrayList (java.util.ArrayList)11 HandlerWrapper (io.undertow.server.HandlerWrapper)9 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 Header (org.apache.http.Header)9 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)8 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)7 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)7