Search in sources :

Example 6 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class FileHandlerTestCase method testFileIsServed.

@Test
public void testFileIsServed() throws IOException, URISyntaxException {
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    try {
        DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true))));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Header[] headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html", headers[0].getValue());
        Assert.assertTrue(response, response.contains("A web page"));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 7 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class FileHandlerTestCase method testRangeRequests.

@Test
public void testRangeRequests() throws IOException, URISyntaxException {
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    try {
        DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 1)).setDirectoryListingEnabled(true))));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
        get.addHeader("range", "bytes=2-3");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Header[] headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html", headers[0].getValue());
        Assert.assertEquals("--", response);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
        get.addHeader("range", "bytes=-7");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html", headers[0].getValue());
        Assert.assertEquals("</html>", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 8 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class FileHandlerIndexTestCase method testDirectoryIndex.

@Test
public void testDirectoryIndex() throws IOException, URISyntaxException {
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    try {
        DefaultServer.setRootHandler(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true)));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Header[] headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html; charset=UTF-8", headers[0].getValue());
        Assert.assertTrue(response, response.contains("page.html"));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/.");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html; charset=UTF-8", headers[0].getValue());
        Assert.assertTrue(response, response.contains("page.html"));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 9 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class FileHandlerIndexTestCase method testWelcomeFile.

@Test
public void testWelcomeFile() throws IOException, URISyntaxException {
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    try {
        DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true).addWelcomeFiles("page.html"))));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Header[] headers = result.getHeaders("Content-Type");
        Assert.assertEquals("text/html", headers[0].getValue());
        Assert.assertTrue(response, response.contains("A web page"));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) Header(org.apache.http.Header) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 10 with ResourceHandler

use of io.undertow.server.handlers.resource.ResourceHandler in project undertow by undertow-io.

the class FileHandlerStressTestCase method simpleFileStressTest.

@Test
public void simpleFileStressTest() throws IOException, ExecutionException, InterruptedException, URISyntaxException {
    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    try {
        Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
        final ResourceHandler handler = new ResourceHandler(new PathResourceManager(rootPath, 10485760));
        final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(1024, 10, 10480), handler);
        final PathHandler path = new PathHandler();
        path.addPrefixPath("/path", cacheHandler);
        final CanonicalPathHandler root = new CanonicalPathHandler();
        root.setNext(path);
        DefaultServer.setRootHandler(root);
        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; ++i) {
            futures.add(executor.submit(new Runnable() {

                @Override
                public void run() {
                    TestHttpClient client = new TestHttpClient();
                    try {
                        for (int i = 0; i < NUM_REQUESTS; ++i) {
                            HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
                            HttpResponse result = client.execute(get);
                            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                            final String response = HttpClientUtils.readResponse(result);
                            Assert.assertTrue(response, response.contains("A web page"));
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }
        for (Future<?> future : futures) {
            future.get();
        }
    } finally {
        executor.shutdown();
    }
}
Also used : Path(java.nio.file.Path) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) DirectBufferCache(io.undertow.server.handlers.cache.DirectBufferCache) ArrayList(java.util.ArrayList) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) IOException(java.io.IOException) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CacheHandler(io.undertow.server.handlers.cache.CacheHandler) Test(org.junit.Test)

Aggregations

ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)24 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)23 Path (java.nio.file.Path)22 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)21 PathHandler (io.undertow.server.handlers.PathHandler)21 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Header (org.apache.http.Header)12 HttpHandler (io.undertow.server.HttpHandler)3 DirectBufferCache (io.undertow.server.handlers.cache.DirectBufferCache)2 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)2 IOException (java.io.IOException)2 BeforeClass (org.junit.BeforeClass)2 Undertow (io.undertow.Undertow)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 NameVirtualHostHandler (io.undertow.server.handlers.NameVirtualHostHandler)1 CacheHandler (io.undertow.server.handlers.cache.CacheHandler)1 ContentEncodedResourceManager (io.undertow.server.handlers.encoding.ContentEncodedResourceManager)1