Search in sources :

Example 1 with PathResourceManager

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

Example 2 with PathResourceManager

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

the class DefaultServletCachingTestCase method setup.

@BeforeClass
public static void setup() throws ServletException, IOException {
    tmpDir = Files.createTempDirectory(DIR_NAME);
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassIntrospecter(TestClassIntrospector.INSTANCE).setClassLoader(ServletPathMappingTestCase.class.getClassLoader()).setContextPath("/servletContext").addWelcomePage("index.html").setDeploymentName("servletContext.war").setResourceManager(new CachingResourceManager(100, MAX_FILE_SIZE, dataCache, new PathResourceManager(tmpDir, 10485760, false, false, false), METADATA_MAX_AGE));
    builder.addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class).addMapping("/path/default")).addFilter(Servlets.filter("message", MessageFilter.class).addInitParam(MessageFilter.MESSAGE, "FILTER_TEXT ")).addFilterUrlMapping("message", "*.txt", DispatcherType.REQUEST);
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) PathHandler(io.undertow.server.handlers.PathHandler) CachingResourceManager(io.undertow.server.handlers.resource.CachingResourceManager) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) MessageFilter(io.undertow.servlet.test.util.MessageFilter) ServletPathMappingTestCase(io.undertow.servlet.test.path.ServletPathMappingTestCase) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) BeforeClass(org.junit.BeforeClass)

Example 3 with PathResourceManager

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

the class ComplexSSLTestCase method complexSSLTestCase.

@Test
public void complexSSLTestCase() throws IOException, GeneralSecurityException, URISyntaxException, InterruptedException {
    final PathHandler pathHandler = new PathHandler();
    Path rootPath = Paths.get(FileHandlerTestCase.class.getResource("page.html").toURI()).getParent();
    final NameVirtualHostHandler virtualHostHandler = new NameVirtualHostHandler();
    HttpHandler root = virtualHostHandler;
    root = new SimpleErrorPageHandler(root);
    root = new CanonicalPathHandler(root);
    virtualHostHandler.addHost("default-host", pathHandler);
    virtualHostHandler.setDefaultHandler(pathHandler);
    pathHandler.addPrefixPath("/", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true));
    DefaultServer.setRootHandler(root);
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        // get file list, this works
        HttpGet getFileList = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
        HttpResponse resultList = client.execute(getFileList);
        Assert.assertEquals(StatusCodes.OK, resultList.getStatusLine().getStatusCode());
        String responseList = HttpClientUtils.readResponse(resultList);
        Assert.assertTrue(responseList, responseList.contains("page.html"));
        Header[] headersList = resultList.getHeaders("Content-Type");
        Assert.assertEquals("text/html; charset=UTF-8", headersList[0].getValue());
        // get file itself, breaks
        HttpGet getFile = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/page.html");
        HttpResponse result = client.execute(getFile);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        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();
        DefaultServer.stopSSLServer();
    }
}
Also used : Path(java.nio.file.Path) HttpHandler(io.undertow.server.HttpHandler) 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) FileHandlerTestCase(io.undertow.server.handlers.file.FileHandlerTestCase) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) NameVirtualHostHandler(io.undertow.server.handlers.NameVirtualHostHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Header(org.apache.http.Header) SimpleErrorPageHandler(io.undertow.server.handlers.error.SimpleErrorPageHandler) Test(org.junit.Test)

Example 4 with PathResourceManager

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

Example 5 with PathResourceManager

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

the class FileHandlerTestCase method testDotSuffix.

@Test
public void testDotSuffix() 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.");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) 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)

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