How to programmatically install gzip in Jetty?

I am writing a web application using Noir and clojure that uses Jetty. Jetty has two ways of using gzip: one static and one dynamic, they are described in qaru.site/questions/913156 / ... . I want to enable static and dynamic gzipping, but our project does not use web.xml files and does not want to run.

How to programmatically configure jetty to use gzip (i.e. without web.xml)?

+6
source share
2 answers

In the Compojure application I'm working on, I have a Ring / Jetty adapter based on the ring-jetty-adapter that programmatically configures Jetty to use a GzipHandler for dynamic gzip content.

(defn- configurator [server ring-handler] (.setHandler server (doto (new HandlerCollection) (.addHandler (doto (new GzipHandler) (.setHandler (proxy-handler ring-handler)) (.setMimeTypes "text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,text/javascript,image/svg+xml"))) (.addHandler (doto (new RequestLogHandler) (.setRequestLog (NCSARequestLog.))))))) 

This function takes a Server instance and my Ring handler and installs it using some handlers. Note that GzipHandler is a HandlerWrapper , so it takes my (proxied) Ring handler and delegates it. I also add a log handler that will be executed after the ring handler (gzip-wrapped).

Check out the full working version .

+4
source

See here the startServer method:

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipWithPipeliningTest.java

jetty is widely used for testing, so most of the built-in scripts that people already exist in unit tests somewhere, finding them in them can be a bit of a problem :)

+1
source

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


All Articles