1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.handler.codec.compression.JdkZlibEncoder;
20 import org.jboss.netty.handler.codec.compression.ZlibEncoder;
21 import org.jboss.netty.handler.codec.compression.ZlibWrapper;
22 import org.jboss.netty.handler.codec.embedder.EncoderEmbedder;
23 import org.jboss.netty.util.internal.DetectionUtil;
24
25
26
27
28
29
30
31
32 public class HttpContentCompressor extends HttpContentEncoder {
33
34 private final int compressionLevel;
35 private final int windowBits;
36 private final int memLevel;
37
38
39
40
41
42 public HttpContentCompressor() {
43 this(6);
44 }
45
46
47
48
49
50
51
52
53
54
55 public HttpContentCompressor(int compressionLevel) {
56 this(compressionLevel, 15, 8);
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public HttpContentCompressor(int compressionLevel, int windowBits, int memLevel) {
79 if (compressionLevel < 0 || compressionLevel > 9) {
80 throw new IllegalArgumentException(
81 "compressionLevel: " + compressionLevel + " (expected: 0-9)");
82 }
83 if (windowBits < 9 || windowBits > 15) {
84 throw new IllegalArgumentException(
85 "windowBits: " + windowBits + " (expected: 9-15)");
86 }
87 if (memLevel < 1 || memLevel > 9) {
88 throw new IllegalArgumentException(
89 "memLevel: " + memLevel + " (expected: 1-9)");
90 }
91 this.compressionLevel = compressionLevel;
92 this.windowBits = windowBits;
93 this.memLevel = memLevel;
94 }
95
96 @Override
97 protected EncoderEmbedder<ChannelBuffer> newContentEncoder(
98 HttpMessage msg, String acceptEncoding) throws Exception {
99 String contentEncoding = msg.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
100 if (contentEncoding != null &&
101 !HttpHeaders.Values.IDENTITY.equalsIgnoreCase(contentEncoding)) {
102
103 return null;
104 }
105
106 ZlibWrapper wrapper = determineWrapper(acceptEncoding);
107 if (wrapper == null) {
108 return null;
109 }
110
111 if (DetectionUtil.javaVersion() >= 7) {
112 return new EncoderEmbedder<ChannelBuffer>(
113 new JdkZlibEncoder(wrapper, compressionLevel));
114 } else {
115 return new EncoderEmbedder<ChannelBuffer>(
116 new ZlibEncoder(wrapper, compressionLevel, windowBits, memLevel));
117 }
118 }
119
120 @Override
121 protected String getTargetContentEncoding(String acceptEncoding) throws Exception {
122 ZlibWrapper wrapper = determineWrapper(acceptEncoding);
123 if (wrapper == null) {
124 return null;
125 }
126
127 switch (wrapper) {
128 case GZIP:
129 return "gzip";
130 case ZLIB:
131 return "deflate";
132 default:
133 throw new Error();
134 }
135 }
136
137 private static ZlibWrapper determineWrapper(String acceptEncoding) {
138 float starQ = -1.0f;
139 float gzipQ = -1.0f;
140 float deflateQ = -1.0f;
141 for (String encoding : acceptEncoding.split(",")) {
142 float q = 1.0f;
143 int equalsPos = encoding.indexOf('=');
144 if (equalsPos != -1) {
145 try {
146 q = Float.valueOf(encoding.substring(equalsPos + 1));
147 } catch (NumberFormatException e) {
148
149 q = 0.0f;
150 }
151 }
152 if (encoding.indexOf("*") >= 0) {
153 starQ = q;
154 } else if (encoding.indexOf("gzip") >= 0 && q > gzipQ) {
155 gzipQ = q;
156 } else if (encoding.indexOf("deflate") >= 0 && q > deflateQ) {
157 deflateQ = q;
158 }
159 }
160 if (gzipQ > 0.0f || deflateQ > 0.0f) {
161 if (gzipQ >= deflateQ) {
162 return ZlibWrapper.GZIP;
163 } else {
164 return ZlibWrapper.ZLIB;
165 }
166 }
167 if (starQ > 0.0f) {
168 if (gzipQ == -1.0f) {
169 return ZlibWrapper.GZIP;
170 }
171 if (deflateQ == -1.0f) {
172 return ZlibWrapper.ZLIB;
173 }
174 }
175 return null;
176 }
177 }