How do I programmatically set gzip in Jetty?

感情迁移 提交于 2020-02-02 02:59:08

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10145891/how-do-i-programmatically-set-gzip-in-jetty

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