Search in sources :

Example 6 with ContentEncodingRepository

use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project undertow by undertow-io.

the class ContentEncodedResourceTestCase method setup.

@BeforeClass
public static void setup() throws IOException {
    tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), DIR_NAME);
    final PathResourceManager resourceManager = new PathResourceManager(tmpDir, 10485760);
    DefaultServer.setRootHandler(new ResourceHandler(resourceManager).setContentEncodedResourceManager(new ContentEncodedResourceManager(tmpDir, new CachingResourceManager(100, 10000, null, resourceManager, -1), new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50, null), 0, 100000, null)));
}
Also used : ContentEncodedResourceManager(io.undertow.server.handlers.encoding.ContentEncodedResourceManager) DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) CachingResourceManager(io.undertow.server.handlers.resource.CachingResourceManager) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) BeforeClass(org.junit.BeforeClass)

Example 7 with ContentEncodingRepository

use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project undertow by undertow-io.

the class ServerSentEventTestCase method testProgressiveSSEWithCompression.

@Test
public void testProgressiveSSEWithCompression() throws IOException {
    final AtomicReference<ServerSentEventConnection> connectionReference = new AtomicReference<>();
    DecompressingHttpClient client = new DecompressingHttpClient(new TestHttpClient());
    try {
        DefaultServer.setRootHandler(new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50)).setNext(new ServerSentEventHandler(new ServerSentEventConnectionCallback() {

            @Override
            public void connected(ServerSentEventConnection connection, String lastEventId) {
                connectionReference.set(connection);
                connection.send("msg 1", new ServerSentEventConnection.EventCallback() {

                    @Override
                    public void done(ServerSentEventConnection connection, String data, String event, String id) {
                    }

                    @Override
                    public void failed(ServerSentEventConnection connection, String data, String event, String id, IOException e) {
                        e.printStackTrace();
                        IoUtils.safeClose(connection);
                    }
                });
            }
        })));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        InputStream stream = result.getEntity().getContent();
        assertData(stream, "data:msg 1\n\n");
        connectionReference.get().send("msg 2");
        assertData(stream, "data:msg 2\n\n");
        connectionReference.get().close();
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) HttpResponse(org.apache.http.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) IOException(java.io.IOException) DecompressingHttpClient(org.apache.http.impl.client.DecompressingHttpClient) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 8 with ContentEncodingRepository

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

use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project indy by Commonjava.

the class IndyDeployer method getGzipEncodeHandler.

private EncodingHandler getGzipEncodeHandler(final DeploymentManager dm) throws ServletException {
    // FROM: https://stackoverflow.com/questions/28295752/compressing-undertow-server-responses#28329810
    final Predicate sizePredicate = Predicates.parse("max-content-size[" + Long.toString(5 * 1024) + "]");
    // For firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=610679
    final Predicate fileTypePredicate = v -> {
        final String path = v.getRequestPath();
        return !NO_NEED_GZIPPED_CONTENT.contains(mimeTyper.getContentType(path).toLowerCase());
    };
    final Predicate mixePredicate = v -> sizePredicate.resolve(v) && fileTypePredicate.resolve(v);
    EncodingHandler eh = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("gzip", new GzipEncodingProvider(), 50, mixePredicate)).setNext(dm.start());
    // sizePredicate ) ).setNext( dm.start() );
    return eh;
}
Also used : Predicates(io.undertow.predicate.Predicates) IndyDeployment(org.commonjava.indy.bind.jaxrs.IndyDeployment) ServletException(javax.servlet.ServletException) HttpServerExchange(io.undertow.server.HttpServerExchange) LoggerFactory(org.slf4j.LoggerFactory) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) AtomicReference(java.util.concurrent.atomic.AtomicReference) Undertow(io.undertow.Undertow) DeployException(org.commonjava.propulsor.deploy.DeployException) Servlets(io.undertow.servlet.Servlets) Inject(javax.inject.Inject) HashSet(java.util.HashSet) BootOptions(org.commonjava.propulsor.boot.BootOptions) DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) GzipEncodingProvider(io.undertow.server.handlers.encoding.GzipEncodingProvider) Deployer(org.commonjava.propulsor.deploy.Deployer) UndertowOptions(io.undertow.UndertowOptions) Logger(org.slf4j.Logger) Collection(java.util.Collection) PortFinder(org.commonjava.propulsor.boot.PortFinder) Set(java.util.Set) MimeTyper(org.commonjava.indy.util.MimeTyper) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ApplicationContent(org.commonjava.indy.util.ApplicationContent) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) Predicate(io.undertow.predicate.Predicate) ApplicationScoped(javax.enterprise.context.ApplicationScoped) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) GzipEncodingProvider(io.undertow.server.handlers.encoding.GzipEncodingProvider) Predicate(io.undertow.predicate.Predicate)

Example 10 with ContentEncodingRepository

use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project oap by oaplatform.

the class NioHttpServer method bind.

public void bind(String prefix, HttpHandler handler, boolean compressionSupport) {
    log.debug("bind {}", prefix);
    Preconditions.checkNotNull(prefix);
    Preconditions.checkArgument(!prefix.isEmpty());
    io.undertow.server.HttpHandler httpHandler = exchange -> handler.handleRequest(new HttpServerExchange(exchange));
    if (!forceCompressionSupport && compressionSupport) {
        httpHandler = new EncodingHandler(httpHandler, contentEncodingRepository);
        httpHandler = new RequestEncodingHandler(httpHandler).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER);
    }
    pathHandler.addPrefixPath(prefix, httpHandler);
}
Also used : BlockingHandler(io.undertow.server.handlers.BlockingHandler) Tags(io.micrometer.core.instrument.Tags) UndertowOptions(io.undertow.UndertowOptions) ConnectorStatistics(io.undertow.server.ConnectorStatistics) IOException(java.io.IOException) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) InetSocketAddress(java.net.InetSocketAddress) Metrics(io.micrometer.core.instrument.Metrics) Undertow(io.undertow.Undertow) Options(org.xnio.Options) GzipStreamSourceConduit(io.undertow.conduits.GzipStreamSourceConduit) InflatingStreamSourceConduit(io.undertow.conduits.InflatingStreamSourceConduit) PathHandler(io.undertow.server.handlers.PathHandler) Slf4j(lombok.extern.slf4j.Slf4j) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler) DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) Closeable(java.io.Closeable) Preconditions(com.google.common.base.Preconditions) GracefulShutdownHandler(io.undertow.server.handlers.GracefulShutdownHandler) GzipEncodingProvider(io.undertow.server.handlers.encoding.GzipEncodingProvider) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) RequestEncodingHandler(io.undertow.server.handlers.encoding.RequestEncodingHandler) EncodingHandler(io.undertow.server.handlers.encoding.EncodingHandler)

Aggregations

ContentEncodingRepository (io.undertow.server.handlers.encoding.ContentEncodingRepository)11 EncodingHandler (io.undertow.server.handlers.encoding.EncodingHandler)10 GzipEncodingProvider (io.undertow.server.handlers.encoding.GzipEncodingProvider)9 DeflateEncodingProvider (io.undertow.server.handlers.encoding.DeflateEncodingProvider)5 Undertow (io.undertow.Undertow)3 PathHandler (io.undertow.server.handlers.PathHandler)3 IOException (java.io.IOException)3 UndertowOptions (io.undertow.UndertowOptions)2 HttpServerExchange (io.undertow.server.HttpServerExchange)2 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)2 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)2 TestHttpClient (io.undertow.testutils.TestHttpClient)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 HttpGet (org.apache.http.client.methods.HttpGet)2 Test (org.junit.Test)2 Preconditions (com.google.common.base.Preconditions)1 Metrics (io.micrometer.core.instrument.Metrics)1 Tags (io.micrometer.core.instrument.Tags)1 Builder (io.undertow.Undertow.Builder)1