Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically set gzip in Jetty?

I'm writing a web app using Noir and clojure, which uses Jetty. Jetty has two ways of using gzip, one for static, and one for dynamic, they are described in https://stackoverflow.com/a/9113129/104021. I want to turn on both static and dynamic gzipping, but our project doesn't use web.xml files, and doesn't want to start.

How do I programmatically set jetty to use gzip (ie without having a web.xml)?

like image 481
Paul Biggar Avatar asked Apr 13 '12 17:04

Paul Biggar


2 Answers

In a Compojure app I'm working on, I have a Ring/Jetty adapter based on ring-jetty-adapter which programmatically configures Jetty to use a GzipHandler to gzip content dynamically.

(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 sets it up with some handlers. Note that the GzipHandler is a HandlerWrapper, so it takes my (proxied) Ring handler and delegates to it. I also add a logging handler which will be executed after the (gzip-wrapped) Ring handler.

Check out the complete working version.

like image 102
Lyle Avatar answered Sep 24 '22 12:09

Lyle


See the startServer method in here:

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

jetty uses itself extensively for testing so most embedded scenarios people need already exist in the unit tests somewhere, course finding them can be a bit of an issue :)

like image 36
jesse mcconnell Avatar answered Sep 24 '22 12:09

jesse mcconnell