Search in sources :

Example 6 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager 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 7 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager 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 8 with PathResourceManager

use of io.undertow.server.handlers.resource.PathResourceManager 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 9 with PathResourceManager

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

the class PreCompressedResourceTestCase method testContentEncodedJsonResource.

@Test
public void testContentEncodedJsonResource() throws IOException, URISyntaxException {
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/data1.json");
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("data1.json").toURI()).getParent();
    try (CloseableHttpClient compClient = HttpClientBuilder.create().build()) {
        DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PreCompressedResourceSupplier(new PathResourceManager(rootPath, 10485760)).addEncoding("gzip", ".gz")).setDirectoryListingEnabled(true))));
        // assert response without compression
        final String plainResponse = assertResponse(client.execute(get), false, null, "web", "application/json");
        // assert compressed response, that doesn't exists, so returns plain
        assertResponse(compClient.execute(get), false, plainResponse, "web", "application/json");
        // generate compressed resource with extension .gz
        Path json = rootPath.resolve("data1.json");
        generateGZipFile(json, rootPath.resolve("data1.json.gz"));
        // assert compressed response that was pre compressed
        assertResponse(compClient.execute(get), true, plainResponse, "gz", "application/json");
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) 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) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) PreCompressedResourceSupplier(io.undertow.server.handlers.resource.PreCompressedResourceSupplier) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 10 with PathResourceManager

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

the class PathResourceManagerTestCase method testGetResource.

@Test
public void testGetResource() throws Exception {
    final Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    final PathResourceManager resourceManager = new PathResourceManager(rootPath, 1024 * 1024);
    Assert.assertNotNull(resourceManager.getResource("page.html"));
    Assert.assertNotNull(resourceManager.getResource("./page.html"));
    Assert.assertNotNull(resourceManager.getResource("../file/page.html"));
}
Also used : Path(java.nio.file.Path) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Aggregations

PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)41 Path (java.nio.file.Path)34 Test (org.junit.Test)30 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)28 PathHandler (io.undertow.server.handlers.PathHandler)27 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)25 TestHttpClient (io.undertow.testutils.TestHttpClient)24 HttpGet (org.apache.http.client.methods.HttpGet)23 HttpResponse (org.apache.http.HttpResponse)20 Header (org.apache.http.Header)12 HttpHandler (io.undertow.server.HttpHandler)5 UnitTest (io.undertow.testutils.category.UnitTest)5 Undertow (io.undertow.Undertow)4 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)4 PreCompressedResourceSupplier (io.undertow.server.handlers.resource.PreCompressedResourceSupplier)4 Resource (io.undertow.server.handlers.resource.Resource)3 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 ServletInfo (io.undertow.servlet.api.ServletInfo)3 IOException (java.io.IOException)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3