posts - 19,  comments - 4,  trackbacks - 0

 
  1/**//*
  2 * <p>Company: 凌科软件 www.elingke.com </p>
  3 * @author liubaojun
  4 * @version 1.0
  5 * Created on 2004-11-29
  6 * 来源于 elinkBSP 部分源代码
  7 */

  8package com.elink.control;
  9
 10import java.io.*;
 11import java.util.*;
 12import java.util.zip.*;
 13import javax.servlet.*;
 14import javax.servlet.http.*;
 15//import com.elink.util.*;
 16
 17/** *//**
 18 * @author liubj
 19 */

 20
 21public class BusiFilter implements Filter
 22{
 23 private FilterConfig config  = null;
 24 private HashMap expiresMap  = null;
 25 private String encoding = null;
 26
 27 public void init(FilterConfig filterConfig)
 28 {
 29  this.config = filterConfig;
 30  this.encoding = config.getInitParameter("encoding");
 31  if( encoding == null || encoding.length() == 0 )
 32  {
 33   encoding = "GBK";
 34  }

 35  expiresMap = new HashMap();
 36  Enumeration names = config.getInitParameterNames();
 37  while( names.hasMoreElements() )
 38  {
 39   String paramName = (String)names.nextElement();
 40   if!"encoding".equals(paramName) )
 41   {
 42    String paramValue = config.getInitParameter(paramName);
 43    try
 44    {
 45     Integer expires = Integer.valueOf( config.getInitParameter(paramName) );
 46     expiresMap.put(paramName, expires);
 47    }

 48    catch( Exception ex)
 49    {
 50     //LogUtil.logError( "Filter."+paramValue+"="+paramValue);
 51    }

 52   }

 53  }

 54 }

 55
 56 public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain)
 57  throws IOException, ServletException
 58 {
 59  // 汉字问题
 60  HttpServletRequest httpRequest = (HttpServletRequest)request;
 61  httpRequest.setCharacterEncoding( encoding );
 62//  chain.doFilter(request, response);
 63
 64        // 压缩传输
 65  HttpServletResponse httpResponse = (HttpServletResponse)response;
 66  String uri = httpRequest.getRequestURI();
 67  
 68  String transferEncoding = getGZIPEncoding((HttpServletRequest)request);
 69  if (transferEncoding == null)
 70  {
 71   setResponseHeader( httpResponse, uri, transferEncoding );
 72   chain.doFilter(request, response);
 73  }

 74  else
 75  {
 76   if( uri.endsWith(".ctl"|| uri.endsWith("busictrl") ) // 不处理的 URL
 77   {
 78    chain.doFilter(request, response);
 79   }

 80   else
 81   {
 82    setResponseHeader( httpResponse, uri, transferEncoding );
 83    httpResponse.setHeader("Content-Encoding", transferEncoding);
 84    GZIPEncodableResponse wrappedResponse = new GZIPEncodableResponse((HttpServletResponse)response);
 85    chain.doFilter(request, wrappedResponse);
 86    wrappedResponse.flush();
 87   }

 88  }

 89 }

 90
 91 public void destroy()
 92 {
 93 }

 94 
 95 private void setResponseHeader( HttpServletResponse response, String uri, String transferEncoding )
 96 {
 97  //LogUtil.logDebug( uri + ".Accept-Encoding: "+ transferEncoding);
 98  String ext = null;
 99  int dot =  uri.lastIndexOf(".");
100  if( dot != -1 )
101  {
102   ext = uri.substring( dot+1 );
103  }

104  if( ext!= null && ext.length() > 0 )
105  {
106   Integer expires = (Integer)expiresMap.get(ext);
107   if( expires != null )
108   {
109    //LogUtil.logDebug( uri + ".Expires: "+ expires.intValue());
110    if( expires.intValue() > 0 )
111    {
112     response.setHeader("Cache-Control","max-age="+expires.intValue()); //HTTP 1.1
113    }

114    else
115    {
116     response.setHeader("Cache-Control","no-cache");
117     response.setHeader("Pragma","no-cache"); //HTTP 1.0
118     response.setDateHeader ("Expires", expires.intValue() );
119    }

120   }

121  }

122 }

123
124private static String getGZIPEncoding(HttpServletRequest request)
125 {
126  String acceptEncoding = request.getHeader("Accept-Encoding");
127  if (acceptEncoding == null)
128   return null;
129  acceptEncoding = acceptEncoding.toLowerCase();
130  if (acceptEncoding.indexOf("x-gzip">= 0)
131  {
132   return "x-gzip";
133  }

134  if (acceptEncoding.indexOf("gzip">= 0)
135  {
136   return "gzip";
137  }

138  return null;
139 }

140
141 private class GZIPEncodableResponse extends HttpServletResponseWrapper
142 {
143  private GZIPServletStream wrappedOut;
144
145  public GZIPEncodableResponse(HttpServletResponse response) throws IOException
146  {
147   super(response);
148   wrappedOut = new GZIPServletStream(response.getOutputStream());
149  }

150
151  public ServletOutputStream getOutputStream() throws IOException
152  {
153   return wrappedOut;
154  }

155
156  private PrintWriter wrappedWriter;
157
158  public PrintWriter getWriter() throws IOException
159  {
160   if (wrappedWriter == null)
161   {
162    wrappedWriter = new PrintWriter(new OutputStreamWriter(
163      getOutputStream(), getCharacterEncoding()));
164   }

165   return wrappedWriter;
166  }

167
168  public void flush() throws IOException
169  {
170   if (wrappedWriter != null)
171   {
172    wrappedWriter.flush();
173   }

174   wrappedOut.finish();
175  }

176 }

177
178 private class GZIPServletStream extends ServletOutputStream
179 {
180  private GZIPOutputStream outputStream;
181
182  public GZIPServletStream(OutputStream source) throws IOException
183  {
184   outputStream = new GZIPOutputStream(source);
185  }

186
187  public void finish() throws IOException
188  {
189   outputStream.finish();
190  }

191
192  public void write(byte[] buf) throws IOException
193  {
194   outputStream.write(buf);
195  }

196
197  public void write(byte[] buf, int off, int len) throws IOException
198  {
199   outputStream.write(buf, off, len);
200  }

201
202  public void write(int c) throws IOException
203  {
204   outputStream.write(c);
205  }

206
207  public void flush() throws IOException
208  {
209   outputStream.flush();
210  }

211
212  public void close() throws IOException
213  {
214   outputStream.close();
215  }

216 }

217}

配置文件

 1<filter>
 2 <filter-name>busifilter</filter-name>
 3 <filter-class>com.elink.control.BusiFilter</filter-class>
 4 <init-param>
 5     <param-name>encoding</param-name>
 6     <param-value>GBK</param-value>
 7 </init-param>
 8 <init-param>
 9  <param-name>js</param-name>
10  <param-value>3600</param-value>
11 </init-param>
12 <init-param>
13  <param-name>gif</param-name>
14  <param-value>3600</param-value>
15 </init-param>
16 <init-param>
17  <param-name>jpg</param-name>
18  <param-value>3600</param-value>
19 </init-param>
20 <init-param>
21  <param-name>css</param-name>
22  <param-value>3600</param-value>
23 </init-param>
24 <init-param>
25  <param-name>bo</param-name>
26  <param-value>0</param-value>
27 </init-param>
28 <init-param>
29  <param-name>jsp</param-name>
30  <param-value>0</param-value>
31 </init-param>
32  </filter>
33  <filter-mapping>
34 <filter-name>busifilter</filter-name>
35 <url-pattern>/*</url-pattern>
36  </filter-mapping>
37

 

 

posted on 2007-01-05 13:59 公主她爹 阅读(510) 评论(0)  编辑  收藏 所属分类: Servlet Filter

只有注册用户登录后才能发表评论。


网站导航:
 

<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(1)

随笔分类(19)

随笔档案(19)

相册

娱乐网站

技术网站(Java)

搜索

  •  

最新评论

阅读排行榜

评论排行榜