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.util; |
35 |
|
|
36 |
|
import info.magnolia.cms.util.RequestHeaderUtil; |
37 |
|
import info.magnolia.module.cache.CacheModule; |
38 |
|
import info.magnolia.objectfactory.Components; |
39 |
|
|
40 |
|
import java.io.ByteArrayInputStream; |
41 |
|
import java.io.ByteArrayOutputStream; |
42 |
|
import java.io.IOException; |
43 |
|
import java.util.Arrays; |
44 |
|
import java.util.List; |
45 |
|
import java.util.zip.GZIPInputStream; |
46 |
|
import java.util.zip.GZIPOutputStream; |
47 |
|
|
48 |
|
import javax.servlet.http.HttpServletRequest; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@version |
55 |
|
|
|
|
| 90% |
Uncovered Elements: 4 (40) |
Complexity: 10 |
Complexity Density: 0.37 |
|
56 |
|
public class GZipUtil { |
57 |
|
private static final List GZIP_MIMETYPES = Arrays.asList(new String[]{ |
58 |
|
"application/x-compressed", |
59 |
|
"application/x-gzip", |
60 |
|
"application/gnutar", |
61 |
|
"multipart/x-gzip" |
62 |
|
}); |
63 |
|
|
64 |
|
private static final int FOUR_KB = 4196; |
65 |
|
private static final int GZIP_MAGIC_NUMBER_BYTE_1 = 31; |
66 |
|
private static final int GZIP_MAGIC_NUMBER_BYTE_2 = -117; |
67 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
68 |
27 |
public static byte[] gzip(byte[] ungzipped) throws IOException {... |
69 |
27 |
if (isGZipped(ungzipped)) { |
70 |
0 |
throw new IllegalStateException("The byte[] is already gzipped. It should not be gzipped again."); |
71 |
|
} |
72 |
27 |
final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
73 |
27 |
final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes); |
74 |
27 |
gzipOutputStream.write(ungzipped); |
75 |
27 |
gzipOutputStream.close(); |
76 |
27 |
return bytes.toByteArray(); |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
79 |
8 |
public static byte[] ungzip(final byte[] gzipped) throws IOException {... |
80 |
8 |
final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped)); |
81 |
8 |
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length); |
82 |
8 |
final byte[] buffer = new byte[FOUR_KB]; |
83 |
8 |
int bytesRead = 0; |
84 |
36 |
while (bytesRead != -1) { |
85 |
28 |
bytesRead = inputStream.read(buffer, 0, FOUR_KB); |
86 |
28 |
if (bytesRead != -1) { |
87 |
20 |
byteArrayOutputStream.write(buffer, 0, bytesRead); |
88 |
|
} |
89 |
|
} |
90 |
8 |
byte[] ungzipped = byteArrayOutputStream.toByteArray(); |
91 |
8 |
inputStream.close(); |
92 |
8 |
byteArrayOutputStream.close(); |
93 |
8 |
return ungzipped; |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
99 |
57 |
public static boolean isGZipped(byte[] candidate) {... |
100 |
57 |
if (candidate == null || candidate.length < 2) { |
101 |
16 |
return false; |
102 |
|
} else { |
103 |
41 |
return (candidate[0] == GZIP_MAGIC_NUMBER_BYTE_1 && candidate[1] == GZIP_MAGIC_NUMBER_BYTE_2); |
104 |
|
} |
105 |
|
} |
106 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
0 |
public static boolean isGZipMimeType(String contentType) {... |
108 |
0 |
return GZIP_MIMETYPES.contains(contentType); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
114 |
7 |
public static boolean isAcceptsGzip(HttpServletRequest request){... |
115 |
7 |
CacheModule module = Components.getComponent(CacheModule.class); |
116 |
7 |
boolean compressionVote = module.getCompression().getVoters().vote(request)>0; |
117 |
7 |
boolean requestAcceptsGzip = RequestHeaderUtil.acceptsGzipEncoding(request); |
118 |
7 |
return requestAcceptsGzip && compressionVote; |
119 |
|
} |
120 |
|
|
121 |
|
} |