如何以编程方式在Jetty中设置gzip? [英] How do I programmatically set gzip in Jetty?

查看:518
本文介绍了如何以编程方式在Jetty中设置gzip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Noir和clojure编写了一个Web应用程序,它使用Jetty。 Jetty有两种使用gzip的方法,一种用于静态,一种用于动态,它们在 http://stackoverflow.com/a中有所描述/ 9113129/104021 。我想打开静态和动态gzipping,但我们的项目不使用web.xml文件,不想开始。

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 http://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.

我如何 设置jetty以使用gzip(即没有web.xml)?

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

推荐答案

应用程序我正在工作,我有一个环/ Jetty适配器基于环-jetty-adapter ,它以编程方式配置Jetty以使用 GzipHandler 动态gzip内容。

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

此函数需要服务器实例和我的环处理程序,并设置它与一些处理程序。注意, GzipHandler HandlerWrapper ,所以它需要我的(代理)Ring处理程序和委托。我还添加了一个日志处理程序,将在(gzip-wrapped)Ring处理程序后执行。

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.

查看完成工作版本

这篇关于如何以编程方式在Jetty中设置gzip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆