Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing Undertow server responses

i have a programmatic Undertow server setup. Static content is served by Undertow as well, without a reverse proxy. Java code for Undertow startup looks like this:

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'm wondering how does one gzip server responses in Undertow?

Thanks, Vitaliy.

like image 224
siphiuel Avatar asked Feb 03 '15 09:02

siphiuel


People also ask

What is server compression enabled?

server.compression.enabled= true. # Minimum response where compression will kick in. server.compression.min-response-size= 4096. # Mime types that should be compressed. server.compression.mime-types=text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json.

How do I enable gzip compression on my server?

Gzip on Windows Servers (IIS Manager)Open up IIS Manager. Click on the site you want to enable compression for. Click on Compression (under IIS) Now Enable static compression and you are done!

What is WildFly undertow?

Undertow is a web server which is able to perform both blocking and non-blocking tasks. Some of its highlights are: You can used it embedded or inside WildFly application server. Features High Performance. Supports Servlet 4.0 and Web Sockets API.


1 Answers

I had to look at GzipContentEncodingTestCase in Undertow's source to get it to work. One has to create an EncodingHandler with appropriate parameters, and then invoke setNext() so that to chain it to 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();
like image 175
siphiuel Avatar answered Sep 28 '22 19:09

siphiuel