Skip to content

Instantly share code, notes, and snippets.

@miere
Created July 26, 2019 02:56
Show Gist options
  • Select an option

  • Save miere/7b8a9314d3d649219964e405bb9b0e2a to your computer and use it in GitHub Desktop.

Select an option

Save miere/7b8a9314d3d649219964e405bb9b0e2a to your computer and use it in GitHub Desktop.
Kikaha GZip Module
import io.undertow.Undertow;
import io.undertow.server.handlers.encoding.EncodingHandler;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import javax.inject.Singleton;
import kikaha.core.modules.Module;
/**
* Compresses all responses sent to the http client (usually the browser)
* using GZip algorithm. It will reduce the amount of data being sent out
* from our servers, improving uses experience and also lowering the
* bandwidth cost in our infrastructure.
*
* It worth notice though it may come with a CPU penalty as a trade off,
* once all requests will its response compressed and the compression is
* a CPU bound operation.
*/
@Singleton
public class GZipModule implements Module {
/**
* This will force the module to be execute at the same level as
* the CORS one. This is strategically useful, so we can guarantee
* that it will be a top level module (one of the first one being
* called).
*/
public String getName(){
return "cors";
}
/**
* This module will make EncodingHandler the root HttpHandler to be executed
* once a request is received by Undertow.
*/
public void load( Undertow.Builder server, DeploymentContext context ) throws IOException {
val rootHandler = context.rootHandler();
val encodingHandler = new EncodingHandler.Builder().build(null).wrap(rootHandler);
context.rootHandler(encodingHandler);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment