|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package compressionFilters; |
|
19 |
| |
|
20 |
| import java.io.IOException; |
|
21 |
| import java.util.Enumeration; |
|
22 |
| import javax.servlet.Filter; |
|
23 |
| import javax.servlet.FilterChain; |
|
24 |
| import javax.servlet.FilterConfig; |
|
25 |
| import javax.servlet.ServletException; |
|
26 |
| import javax.servlet.ServletRequest; |
|
27 |
| import javax.servlet.ServletResponse; |
|
28 |
| import javax.servlet.http.HttpServletRequest; |
|
29 |
| import javax.servlet.http.HttpServletResponse; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public class CompressionFilter implements Filter{ |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| private FilterConfig config = null; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| private int minThreshold = 128; |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| protected int compressionThreshold; |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| private int debug = 0; |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
0
| public void init(FilterConfig filterConfig) {
|
|
72 |
| |
|
73 |
0
| config = filterConfig;
|
|
74 |
0
| if (filterConfig != null) {
|
|
75 |
0
| String value = filterConfig.getInitParameter("debug");
|
|
76 |
0
| if (value!=null) {
|
|
77 |
0
| debug = Integer.parseInt(value);
|
|
78 |
| } else { |
|
79 |
0
| debug = 0;
|
|
80 |
| } |
|
81 |
0
| String str = filterConfig.getInitParameter("compressionThreshold");
|
|
82 |
0
| if (str!=null) {
|
|
83 |
0
| compressionThreshold = Integer.parseInt(str);
|
|
84 |
0
| if (compressionThreshold != 0 && compressionThreshold < minThreshold) {
|
|
85 |
0
| if (debug > 0) {
|
|
86 |
0
| System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold);
|
|
87 |
0
| System.out.println("compressionThreshold set to " + minThreshold);
|
|
88 |
| } |
|
89 |
0
| compressionThreshold = minThreshold;
|
|
90 |
| } |
|
91 |
| } else { |
|
92 |
0
| compressionThreshold = 0;
|
|
93 |
| } |
|
94 |
| |
|
95 |
| } else { |
|
96 |
0
| compressionThreshold = 0;
|
|
97 |
| } |
|
98 |
| |
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
0
| public void destroy() {
|
|
105 |
| |
|
106 |
0
| this.config = null;
|
|
107 |
| |
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
0
| public void doFilter ( ServletRequest request, ServletResponse response,
|
|
128 |
| FilterChain chain ) throws IOException, ServletException { |
|
129 |
| |
|
130 |
0
| if (debug > 0) {
|
|
131 |
0
| System.out.println("@doFilter");
|
|
132 |
| } |
|
133 |
| |
|
134 |
0
| if (compressionThreshold == 0) {
|
|
135 |
0
| if (debug > 0) {
|
|
136 |
0
| System.out.println("doFilter gets called, but compressionTreshold is set to 0 - no compression");
|
|
137 |
| } |
|
138 |
0
| chain.doFilter(request, response);
|
|
139 |
0
| return;
|
|
140 |
| } |
|
141 |
| |
|
142 |
0
| boolean supportCompression = false;
|
|
143 |
0
| if (request instanceof HttpServletRequest) {
|
|
144 |
0
| if (debug > 1) {
|
|
145 |
0
| System.out.println("requestURI = " + ((HttpServletRequest)request).getRequestURI());
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
0
| String s = (String) ((HttpServletRequest)request).getParameter("gzip");
|
|
150 |
0
| if ("false".equals(s)) {
|
|
151 |
0
| if (debug > 0) {
|
|
152 |
0
| System.out.println("got parameter gzip=false --> don't compress, just chain filter");
|
|
153 |
| } |
|
154 |
0
| chain.doFilter(request, response);
|
|
155 |
0
| return;
|
|
156 |
| } |
|
157 |
| |
|
158 |
0
| Enumeration e =
|
|
159 |
| ((HttpServletRequest)request).getHeaders("Accept-Encoding"); |
|
160 |
0
| while (e.hasMoreElements()) {
|
|
161 |
0
| String name = (String)e.nextElement();
|
|
162 |
0
| if (name.indexOf("gzip") != -1) {
|
|
163 |
0
| if (debug > 0) {
|
|
164 |
0
| System.out.println("supports compression");
|
|
165 |
| } |
|
166 |
0
| supportCompression = true;
|
|
167 |
| } else { |
|
168 |
0
| if (debug > 0) {
|
|
169 |
0
| System.out.println("no support for compresion");
|
|
170 |
| } |
|
171 |
| } |
|
172 |
| } |
|
173 |
| } |
|
174 |
| |
|
175 |
0
| if (!supportCompression) {
|
|
176 |
0
| if (debug > 0) {
|
|
177 |
0
| System.out.println("doFilter gets called wo compression");
|
|
178 |
| } |
|
179 |
0
| chain.doFilter(request, response);
|
|
180 |
0
| return;
|
|
181 |
| } else { |
|
182 |
0
| if (response instanceof HttpServletResponse) {
|
|
183 |
0
| CompressionServletResponseWrapper wrappedResponse =
|
|
184 |
| new CompressionServletResponseWrapper((HttpServletResponse)response); |
|
185 |
0
| wrappedResponse.setDebugLevel(debug);
|
|
186 |
0
| wrappedResponse.setCompressionThreshold(compressionThreshold);
|
|
187 |
0
| if (debug > 0) {
|
|
188 |
0
| System.out.println("doFilter gets called with compression");
|
|
189 |
| } |
|
190 |
0
| try {
|
|
191 |
0
| chain.doFilter(request, wrappedResponse);
|
|
192 |
| } finally { |
|
193 |
0
| wrappedResponse.finishResponse();
|
|
194 |
| } |
|
195 |
0
| return;
|
|
196 |
| } |
|
197 |
| } |
|
198 |
| } |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
0
| public void setFilterConfig(FilterConfig filterConfig) {
|
|
207 |
0
| init(filterConfig);
|
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
0
| public FilterConfig getFilterConfig() {
|
|
215 |
0
| return config;
|
|
216 |
| } |
|
217 |
| |
|
218 |
| } |
|
219 |
| |