1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.cache.filter; |
35 |
|
|
36 |
|
import info.magnolia.cms.filters.OncePerRequestAbstractMgnlFilter; |
37 |
|
import info.magnolia.cms.util.RequestHeaderUtil; |
38 |
|
import info.magnolia.module.cache.util.GZipUtil; |
39 |
|
|
40 |
|
import java.io.IOException; |
41 |
|
|
42 |
|
import javax.servlet.FilterChain; |
43 |
|
import javax.servlet.ServletException; |
44 |
|
import javax.servlet.ServletOutputStream; |
45 |
|
import javax.servlet.http.HttpServletRequest; |
46 |
|
import javax.servlet.http.HttpServletResponse; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@see |
55 |
|
@author |
56 |
|
@version |
57 |
|
|
|
|
| 84% |
Uncovered Elements: 4 (25) |
Complexity: 6 |
Complexity Density: 0.35 |
|
58 |
|
public class GZipFilter extends OncePerRequestAbstractMgnlFilter { |
59 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
60 |
0 |
@Override... |
61 |
|
public boolean bypasses(HttpServletRequest request) { |
62 |
0 |
return !GZipUtil.isAcceptsGzip(request) || super.bypasses(request); |
63 |
|
} |
64 |
|
|
|
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 5 |
Complexity Density: 0.31 |
|
65 |
4 |
@Override... |
66 |
|
public void doFilter(HttpServletRequest request, final HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
4 |
final GZipCacheResponseWrapper responseWrapper = new GZipCacheResponseWrapper(response, CacheResponseWrapper.DEFAULT_THRESHOLD, true); |
72 |
|
|
73 |
4 |
chain.doFilter(request, responseWrapper); |
74 |
|
|
75 |
|
|
76 |
4 |
responseWrapper.flush(); |
77 |
|
|
78 |
|
|
79 |
4 |
if (!responseWrapper.isGzipResponseDetected()) { |
80 |
4 |
byte[] array = responseWrapper.getBufferedContent(); |
81 |
|
|
82 |
|
|
83 |
4 |
int statusCode = responseWrapper.getStatus(); |
84 |
4 |
if (statusCode == HttpServletResponse.SC_OK && !GZipUtil.isGZipped(array)) { |
85 |
3 |
responseWrapper.replayHeadersAndStatus(response); |
86 |
|
|
87 |
3 |
RequestHeaderUtil.addAndVerifyHeader(response, "Content-Encoding", "gzip"); |
88 |
3 |
RequestHeaderUtil.addAndVerifyHeader(response, "Vary", "Accept-Encoding"); |
89 |
|
|
90 |
3 |
array = GZipUtil.gzip(array); |
91 |
3 |
response.setContentLength(array.length); |
92 |
3 |
if (array.length > 0) { |
93 |
3 |
response.getOutputStream().write(array); |
94 |
|
} |
95 |
|
} |
96 |
|
else { |
97 |
1 |
responseWrapper.replay(response); |
98 |
|
} |
99 |
|
|
100 |
4 |
response.flushBuffer(); |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
@version |
108 |
|
|
|
|
| 45.5% |
Uncovered Elements: 6 (11) |
Complexity: 4 |
Complexity Density: 0.67 |
|
109 |
|
public static final class GZipCacheResponseWrapper extends CacheResponseWrapper { |
110 |
|
|
111 |
|
private final HttpServletResponse response; |
112 |
|
|
113 |
|
private boolean gzipResponseDetected; |
114 |
|
|
115 |
|
private ServletOutputStream deferredOutputStream = new DeferredServletOutputStream(); |
116 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
117 |
|
public boolean isGzipResponseDetected() {... |
118 |
|
return gzipResponseDetected; |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
121 |
4 |
private GZipCacheResponseWrapper(HttpServletResponse response, int threshold, boolean serveIfThresholdReached) {... |
122 |
4 |
super(response, threshold, serveIfThresholdReached, new GzipThresholdingCacheOutputStream(threshold)); |
123 |
4 |
this.response = response; |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
126 |
0 |
@Override... |
127 |
|
public void addHeader(String name, String value) { |
128 |
0 |
if (name.equals("Content-Encoding")) { |
129 |
0 |
gzipResponseDetected = true; |
130 |
|
} |
131 |
0 |
super.addHeader(name, value); |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
134 |
3 |
private ServletOutputStream getWrappedOutputStream() throws IOException {... |
135 |
3 |
return super.getOutputStream(); |
136 |
|
} |
137 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
138 |
|
@Override... |
139 |
|
public ServletOutputStream getOutputStream() { |
140 |
|
return deferredOutputStream; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
|
|
| 70.6% |
Uncovered Elements: 5 (17) |
Complexity: 6 |
Complexity Density: 0.67 |
|
146 |
|
private final class DeferredServletOutputStream extends ServletOutputStream { |
147 |
|
|
148 |
|
private ServletOutputStream stream; |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
150 |
1016217 |
@Override... |
151 |
|
public void write(int b) throws IOException { |
152 |
1016217 |
getStream().write(b); |
153 |
|
} |
154 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
0 |
@Override... |
156 |
|
public void write(byte[] b) throws IOException { |
157 |
0 |
getStream().write(b); |
158 |
|
} |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
160 |
8 |
@Override... |
161 |
|
public void write(byte[] b, int off, int len) throws IOException { |
162 |
8 |
getStream().write(b, off, len); |
163 |
|
} |
164 |
|
|
|
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
165 |
1016225 |
private ServletOutputStream getStream() throws IOException {... |
166 |
1016225 |
if (stream == null) { |
167 |
3 |
if (gzipResponseDetected) { |
168 |
0 |
replayHeadersAndStatus(response); |
169 |
0 |
stream = response.getOutputStream(); |
170 |
|
} else { |
171 |
3 |
stream = getWrappedOutputStream(); |
172 |
|
} |
173 |
|
} |
174 |
1016225 |
return stream; |
175 |
|
} |
176 |
|
} |
177 |
|
} |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
179 |
|
private final static class GzipThresholdingCacheOutputStream extends AbstractThresholdingCacheOutputStream { |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
181 |
4 |
public GzipThresholdingCacheOutputStream(int threshold) {... |
182 |
4 |
super(threshold); |
183 |
|
} |
184 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
185 |
1 |
@Override... |
186 |
|
protected void thresholdReached() throws IOException { |
187 |
|
|
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
} |