问题
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