王朝网络
分享
 
 
 

利用Filter压缩HTTP响应

王朝other·作者佚名  2008-05-31
宽屏版  字体: |||超大  

/*

* Copyright 2004 The Apache Software Foundation

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may oBTain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either eXPress or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

import Java.io.IOException;

import java.util.Enumeration;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Implementation of <code>javax.servlet.Filter</code> used to compress

* the ServletResponse if it is bigger than a threshold.

*

* @author Amy Roh

* @author Dmitri Valdin

* @version $Revision: 1.2 $, $Date: 2004/03/18 16:40:33 $

*/

public class CompressionFilter implements Filter{

/**

* The filter configuration object we are associated with. If this value

* is null, this filter instance is not currently configured.

*/

private FilterConfig config = null;

/**

* Minimal reasonable threshold

*/

private int minThreshold = 128;

/**

* The threshold number to compress

*/

protected int compressionThreshold;

/**

* Debug level for this filter

*/

private int debug = 0;

/**

* Place this filter into service.

*

* @param filterConfig The filter configuration object

*/

public void init(FilterConfig filterConfig) {

config = filterConfig;

if (filterConfig != null) {

String value = filterConfig.getInitParameter("debug");

if (value!=null) {

debug = Integer.parseInt(value);

} else {

debug = 0;

}

String str = filterConfig.getInitParameter("compressionThreshold");

if (str!=null) {

compressionThreshold = Integer.parseInt(str);

if (compressionThreshold != 0 && compressionThreshold < minThreshold) { if (debug > 0) {

System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold);

System.out.println("compressionThreshold set to " + minThreshold);

}

compressionThreshold = minThreshold;

}

} else {

compressionThreshold = 0;

}

} else {

compressionThreshold = 0;

}

}

/**

* Take this filter out of service.

*/

public void destroy() {

this.config = null;

}

/**

* The <code>doFilter</code> method of the Filter is called by the container

* each time a request/response pair is passed through the chain due

* to a client request for a resource at the end of the chain.

* The FilterChain passed into this method allows the Filter to pass on the

* request and response to the next entity in the chain.<p>

* This method first examines the request to check whether the client support

* compression. <br>

* It simply just pass the request and response if there is no support for

* compression.<br>

* If the compression support is available, it creates a

* CompressionServletResponseWrapper object which compresses the content and

* modifies the header if the content length is big enough.

* It then invokes the next entity in the chain using the FilterChain object

* (<code>chain.doFilter()</code>), <br>

**/

public void doFilter ( ServletRequest request, ServletResponse response,

FilterChain chain ) throws IOException, ServletException {

if (debug > 0) {

System.out.println("@doFilter");

}

if (compressionThreshold == 0) {

if (debug > 0) {

System.out.println("doFilter gets called, but compressionTreshold is set to 0 - no compression");

}

chain.doFilter(request, response);

return;

}

boolean supportCompression = false;

if (request instanceof HttpServletRequest) {

if (debug > 1) {

System.out.println("requestURI = " + ((HttpServletRequest)request).getRequestURI());

}

// Are we allowed to compress ?

String s = (String) ((HttpServletRequest)request).getParameter("gzip");

if ("false".equals(s)) {

if (debug > 0) {

System.out.println("got parameter gzip=false --> don't compress, just chain filter");

}

chain.doFilter(request, response);

return;

}

Enumeration e =

((HttpServletRequest)request).getHeaders("Accept-Encoding");

while (e.hasMoreElements()) {

String name = (String)e.nextElement();

if (name.indexOf("gzip") != -1) {

if (debug > 0) {

System.out.println("supports compression");

}

supportCompression = true;

} else {

if (debug > 0) {

System.out.println("no support for compresion");

}

}

}

}

if (!supportCompression) {

if (debug > 0) {

System.out.println("doFilter gets called wo compression");

}

chain.doFilter(request, response);

return;

} else {

if (response instanceof HttpServletResponse) {

CompressionServletResponseWrapper wrappedResponse =

new CompressionServletResponseWrapper((HttpServletResponse)response);

wrappedResponse.setDebugLevel(debug);

wrappedResponse.setCompressionThreshold(compressionThreshold);

if (debug > 0) {

System.out.println("doFilter gets called with compression");

}

try {

chain.doFilter(request, wrappedResponse);

} finally {

wrappedResponse.finishResponse();

}

return;

}

}

}

/**

* Set filter config

* This function is equivalent to init. Required by Weblogic 6.1

*

* @param filterConfig The filter configuration object

*/

public void setFilterConfig(FilterConfig filterConfig) {

init(filterConfig);

}

/**

* Return filter config

* Required by Weblogic 6.1

*/

public FilterConfig getFilterConfig() {

return config;

}

}

/*

* Copyright 2004 The Apache Software Foundation

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Enumeration;

import javax.servlet.*;

import javax.servlet.http.*;

/**

* Very Simple test servlet to test compression filter

* @author Amy Roh

* @version $Revision: 1.2 $, $Date: 2004/03/18 16:40:33 $

*/

public class CompressionFilterTestServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

ServletOutputStream out = response.getOutputStream();

response.setContentType("text/plain");

Enumeration e = ((HttpServletRequest)request).getHeaders("Accept-Encoding");

while (e.hasMoreElements()) {

String name = (String)e.nextElement();

out.println(name);

if (name.indexOf("gzip") != -1) {

out.println("gzip supported -- able to compress");

}

else {

out.println("gzip not supported");

}

}

out.println("Compression Filter Test Servlet");

out.close();

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有