简体   繁体   English

如何在Jetty中以编程方式设置gzip?

[英]How do I programmatically set gzip in Jetty?

I'm writing a web app using Noir and clojure, which uses Jetty. 我正在使用Noir和clojure编写一个Web应用程序,它使用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 . Jetty有两种使用gzip的方法,一种用于静态,一种用于动态,它们在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. 我想打开静态和动态gzipping,但我们的项目不使用web.xml文件,也不想启动。

How do I programmatically set jetty to use gzip (ie without having a web.xml)? 如何以编程方式设置jetty以使用gzip(即没有web.xml)?

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. 在我正在研究的Compojure应用程序中,我有一个基于ring-jetty-adapter的Ring / Jetty适配器,它以编程方式配置Jetty以使用GzipHandler动态地gzip内容。

(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. 这个函数接受一个Server实例和我的Ring处理程序,并用一些处理程序设置它。 Note that the GzipHandler is a HandlerWrapper , so it takes my (proxied) Ring handler and delegates to it. 请注意, GzipHandler是一个HandlerWrapper ,所以它需要我的(代理)Ring处理程序和委托给它。 I also add a logging handler which will be executed after the (gzip-wrapped) Ring handler. 我还添加了一个日志处理程序,它将在(gzip-wrapped)Ring处理程序之后执行。

Check out the complete working version . 查看完整的工作版本

See the startServer method in here: 请参阅此处的startServer方法:

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipWithPipeliningTest.java 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 :) jetty广泛用于测试,所以大多数嵌入式场景人们需要已经存在于某个地方的单元测试中,当然找到它们可能有点问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM