1
2
3
4
5
6
7 package nl.mineleni.cbsviewer.servlet;
8
9 import java.io.IOException;
10
11 import javax.servlet.Filter;
12 import javax.servlet.FilterChain;
13 import javax.servlet.FilterConfig;
14 import javax.servlet.ServletException;
15 import javax.servlet.ServletRequest;
16 import javax.servlet.ServletResponse;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.apache.http.HttpHeaders;
21
22
23
24
25
26
27
28 public class GZipFilter implements Filter {
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @Override
52 public void doFilter(final ServletRequest request,
53 final ServletResponse response, final FilterChain chain)
54 throws IOException, ServletException {
55 HttpServletRequest httpRequest = (HttpServletRequest) request;
56 HttpServletResponse httpResponse = (HttpServletResponse) response;
57 String acceptEncoding = httpRequest
58 .getHeader(HttpHeaders.ACCEPT_ENCODING);
59 if (acceptEncoding != null) {
60 if (acceptEncoding.indexOf("gzip") >= 0) {
61 GZIPHttpServletResponseWrapper gzipResponse = new GZIPHttpServletResponseWrapper(
62 httpResponse);
63 chain.doFilter(request, gzipResponse);
64 gzipResponse.finish();
65 return;
66 }
67 }
68 chain.doFilter(request, response);
69 }
70
71
72
73
74
75
76 @Override
77 public void init(final FilterConfig filterConfig) throws ServletException {
78
79 }
80
81
82
83
84
85
86 @Override
87 public void destroy() {
88
89 }
90 }