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