Search in sources :

Example 36 with PathResourceManager

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

the class PreCompressedResourceTestCase method testCorrectResourceSelected.

@Test
public void testCorrectResourceSelected() throws IOException, URISyntaxException {
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
    try (CloseableHttpClient compClient = HttpClientBuilder.create().build()) {
        DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("gzip", new GzipEncodingProvider(), 50, Predicates.truePredicate())).setNext(new ResourceHandler(new PreCompressedResourceSupplier(new PathResourceManager(rootPath, 10485760)).addEncoding("gzip", ".gzip")).setDirectoryListingEnabled(true)))));
        // assert response without compression
        final String plainResponse = assertResponse(client.execute(get), false);
        // assert compressed response generated by filter
        assertResponse(compClient.execute(get), true, plainResponse);
        // generate resources
        generatePreCompressedResource("gzip");
        generatePreCompressedResource("nonsense");
        generatePreCompressedResource("gzip.nonsense");
        // assert compressed response that was pre compressed
        assertResponse(compClient.execute(get), true, plainResponse, "gzip", "text/html");
    } 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) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) GzipEncodingProvider(io.undertow.server.handlers.encoding.GzipEncodingProvider) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) PreCompressedResourceSupplier(io.undertow.server.handlers.resource.PreCompressedResourceSupplier) Test(org.junit.Test)

Example 37 with PathResourceManager

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

the class PreCompressedResourceTestCase method testContentEncodedJsonResourceWithoutUncompressed.

@Test
public void testContentEncodedJsonResourceWithoutUncompressed() throws IOException, URISyntaxException {
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/data3.json");
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("data2.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))));
        // generate compressed resource with extension .gz and delete the uncompressed
        Path json = rootPath.resolve("data2.json");
        Path jsonFileToBeZippedAndDeleted = rootPath.resolve("data3.json");
        Files.copy(json, jsonFileToBeZippedAndDeleted);
        // data3.json.gz has no corresponding data3.json in the filesystem (UNDERTOW-1950)
        generateGZipFile(jsonFileToBeZippedAndDeleted, rootPath.resolve("data3.json.gz"));
        Files.delete(jsonFileToBeZippedAndDeleted);
        // assert compressed response even with missing uncompressed
        assertResponse(compClient.execute(get), true, null, "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 38 with PathResourceManager

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

the class PreCompressedResourceTestCase method testContentEncodedResource.

@Test
public void testContentEncodedResource() throws IOException, URISyntaxException {
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
    TestHttpClient client = new TestHttpClient();
    Path rootPath = Paths.get(getClass().getResource("page.html").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);
        // assert compressed response, that doesn't exists, so returns plain
        assertResponse(compClient.execute(get), false, plainResponse);
        // generate compressed resource with extension .gz
        generatePreCompressedResource("gz");
        // assert compressed response that was pre compressed
        assertResponse(compClient.execute(get), true, plainResponse, "gz", "text/html");
    } 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 39 with PathResourceManager

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

the class DefaultServletCachingListenerTestCase method setup.

@BeforeClass
public static void setup() throws ServletException, IOException {
    tmpDir = Files.createTempDirectory(DIR_NAME);
    // assume tmp is in the default file system and watch-service is not the slow polling impl
    Assume.assumeTrue("WatchService is going to work OK", FileSystems.getDefault().equals(tmpDir.getFileSystem()) && !FileSystems.getDefault().newWatchService().getClass().getName().equals("sun.nio.fs.PollingWatchService"));
    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, true), -1));
    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 40 with PathResourceManager

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

the class RangeRequestTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    Path rootPath = Paths.get(RangeRequestTestCase.class.getResource("range.txt").toURI()).getParent();
    PathHandler path = Handlers.path();
    path.addPrefixPath("/path", new ByteRangeHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.LAST_MODIFIED, DateUtils.toDateString(new Date(10000)));
            exchange.getResponseHeaders().put(Headers.ETAG, "\"someetag\"");
            exchange.getResponseSender().send("0123456789");
        }
    }, true));
    path.addPrefixPath("/resource", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true));
    path.addPrefixPath("/cachedresource", new ResourceHandler(new CachingResourceManager(1000, 1000000, new DirectBufferCache(1000, 10, 10000), new PathResourceManager(rootPath, 10485760), -1)).setDirectoryListingEnabled(true));
    DefaultServer.setRootHandler(path);
}
Also used : Path(java.nio.file.Path) HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) DirectBufferCache(io.undertow.server.handlers.cache.DirectBufferCache) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) CachingResourceManager(io.undertow.server.handlers.resource.CachingResourceManager) Date(java.util.Date) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) BeforeClass(org.junit.BeforeClass)

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