Undertow server response compression

I have a software setup for the Undertow server. Static content also serves Undertow, without a reverse proxy. The Java code for running Undertow is as follows:

ResourceManager resourceManager = new FileResourceManager(new File("deploymentDir"), 100); DeploymentInfo servletBuilder = Servlets.deployment() .setResourceManager(resourceManager) .setDeploymentName("testDeployment") .setContextPath("/"); DeploymentManager manager = Servlets.defaultContainer() .addDeployment(servletBuilder); manager.deploy(); Undertow.Builder builder = Undertow.builder(); builder.addHttpListener(8080, domainName); PathHandler path = Handlers.path(Handlers.redirect("/")) .addPrefixPath("/", manager.start()); Undertow server = builder.setHandler(path).build(); server.start(); 

I am wondering how is one gzip server response in Undertow?

Thank you, Vitaliy.

+5
source share
3 answers

I had to look at the GzipContentEncodingTestCase in the Undertow source to get it working. You need to create an EncodingHandler with the appropriate parameters, and then call setNext() to associate it with the PathHandler :

 PathHandler path = Handlers.path(Handlers.redirect("/")) .addPrefixPath("/", manager.start()); final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository() .addEncodingHandler("gzip", new GzipEncodingProvider(), 50, Predicates.parse("max-content-size[5]"))) .setNext(path); // ... Undertow server = builder.setHandler(handler).build(); 
+8
source

The answer from @siphiuel looks correct to me.

However, a GZIP-encoded EncodingHandler can also be created as shown below:

  HttpHandler pathHandler = Handlers.path(Handlers.redirect("/")) .addPrefixPath("/", exchange -> exchange.getResponseSender().send("echo")); HttpHandler encodingHandler = new EncodingHandler.Builder().build(null) .wrap(pathHandler); Undertow server = Undertow.builder() .addHttpListener(8080, "localhost") .setHandler(encodingHandler).build(); server.start(); 

EncodingHandler provides a HandlerBuilder, which by default adds GzipEncodingProvider and DeflateEncodingProvider using the default configuration. Thus, your code is not related to the constructor and its parameters for creating the EncodingHandler.

In addition, HandlerBuilder # build returns a HandlerWrapper for smooth HttpHandler wrapping / chaining.

+1
source

A very interesting link that helped me personally: Add Server-Sent event compression using Undertow . All parameters used by EncodingHandler provided by @siphiuel are explained one after another.

Here is a brief summary for gzip compression, for example. addEncodingHandler(...) parameters will be:

  • type ("gzip")
  • EncodingProvider to use (Undertow provides implementations for deflate and gzip)
  • priority (multiple providers may apply, so priority will be used to select a supplier)
  • predicate for activating encoding (in the @siphiuel example, only responses with a content size> 5 bytes will be encoded). The link I shared mentioned 5K, but my tests proved that it was 5 bytes).
0
source

Source: https://habr.com/ru/post/1212515/


All Articles