How do I programmatically set gzip in Jetty?

狂风中的少年 提交于 2019-12-05 07:45:05

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.

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!