Search in sources :

Example 1 with PreCompressedResourceSupplier

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

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

the class DefaultServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ServletContextImpl sc = (ServletContextImpl) config.getServletContext();
    this.deployment = sc.getDeployment();
    DefaultServletConfig defaultServletConfig = deployment.getDeploymentInfo().getDefaultServletConfig();
    if (defaultServletConfig != null) {
        defaultAllowed = defaultServletConfig.isDefaultAllowed();
        allowed = new HashSet<>();
        if (defaultServletConfig.getAllowed() != null) {
            allowed.addAll(defaultServletConfig.getAllowed());
        }
        disallowed = new HashSet<>();
        if (defaultServletConfig.getDisallowed() != null) {
            disallowed.addAll(defaultServletConfig.getDisallowed());
        }
    }
    if (config.getInitParameter(DEFAULT_ALLOWED) != null) {
        defaultAllowed = Boolean.parseBoolean(config.getInitParameter(DEFAULT_ALLOWED));
    }
    if (config.getInitParameter(ALLOWED_EXTENSIONS) != null) {
        String extensions = config.getInitParameter(ALLOWED_EXTENSIONS);
        allowed = new HashSet<>(Arrays.asList(extensions.split(",")));
    }
    if (config.getInitParameter(DISALLOWED_EXTENSIONS) != null) {
        String extensions = config.getInitParameter(DISALLOWED_EXTENSIONS);
        disallowed = new HashSet<>(Arrays.asList(extensions.split(",")));
    }
    if (config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT) != null) {
        resolveAgainstContextRoot = Boolean.parseBoolean(config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT));
    }
    if (config.getInitParameter(ALLOW_POST) != null) {
        allowPost = Boolean.parseBoolean(config.getInitParameter(ALLOW_POST));
    }
    if (deployment.getDeploymentInfo().getPreCompressedResources().isEmpty()) {
        this.resourceSupplier = new DefaultResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
    } else {
        PreCompressedResourceSupplier preCompressedResourceSupplier = new PreCompressedResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
        for (Map.Entry<String, String> entry : deployment.getDeploymentInfo().getPreCompressedResources().entrySet()) {
            preCompressedResourceSupplier.addEncoding(entry.getKey(), entry.getValue());
        }
        this.resourceSupplier = preCompressedResourceSupplier;
    }
    String listings = config.getInitParameter(DIRECTORY_LISTING);
    if (Boolean.valueOf(listings)) {
        this.directoryListingEnabled = true;
    }
}
Also used : ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) DefaultServletConfig(io.undertow.servlet.api.DefaultServletConfig) DefaultResourceSupplier(io.undertow.server.handlers.resource.DefaultResourceSupplier) PreCompressedResourceSupplier(io.undertow.server.handlers.resource.PreCompressedResourceSupplier) Map(java.util.Map)

Example 3 with PreCompressedResourceSupplier

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

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

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

Aggregations

PreCompressedResourceSupplier (io.undertow.server.handlers.resource.PreCompressedResourceSupplier)5 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)4 PathHandler (io.undertow.server.handlers.PathHandler)4 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)4 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)4 TestHttpClient (io.undertow.testutils.TestHttpClient)4 Path (java.nio.file.Path)4 HttpGet (org.apache.http.client.methods.HttpGet)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 Test (org.junit.Test)4 ContentEncodingRepository (io.undertow.server.handlers.encoding.ContentEncodingRepository)1 EncodingHandler (io.undertow.server.handlers.encoding.EncodingHandler)1 GzipEncodingProvider (io.undertow.server.handlers.encoding.GzipEncodingProvider)1 DefaultResourceSupplier (io.undertow.server.handlers.resource.DefaultResourceSupplier)1 DefaultServletConfig (io.undertow.servlet.api.DefaultServletConfig)1 ServletContextImpl (io.undertow.servlet.spec.ServletContextImpl)1 Map (java.util.Map)1