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 package org.apache.http.client.protocol;
28
29 import java.io.IOException;
30 import java.util.Locale;
31
32 import org.apache.http.Header;
33 import org.apache.http.HeaderElement;
34 import org.apache.http.HttpEntity;
35 import org.apache.http.HttpException;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.HttpResponseInterceptor;
38 import org.apache.http.annotation.Contract;
39 import org.apache.http.annotation.ThreadingBehavior;
40 import org.apache.http.client.config.RequestConfig;
41 import org.apache.http.client.entity.DecompressingEntity;
42 import org.apache.http.client.entity.DeflateInputStreamFactory;
43 import org.apache.http.client.entity.GZIPInputStreamFactory;
44 import org.apache.http.client.entity.InputStreamFactory;
45 import org.apache.http.config.Lookup;
46 import org.apache.http.config.RegistryBuilder;
47 import org.apache.http.protocol.HttpContext;
48
49
50
51
52
53
54
55
56
57
58 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
59 public class ResponseContentEncoding implements HttpResponseInterceptor {
60
61 public static final String UNCOMPRESSED = "http.client.response.uncompressed";
62
63 private final Lookup<InputStreamFactory> decoderRegistry;
64 private final boolean ignoreUnknown;
65
66
67
68
69 public ResponseContentEncoding(final Lookup<InputStreamFactory> decoderRegistry, final boolean ignoreUnknown) {
70 this.decoderRegistry = decoderRegistry != null ? decoderRegistry :
71 RegistryBuilder.<InputStreamFactory>create()
72 .register("gzip", GZIPInputStreamFactory.getInstance())
73 .register("x-gzip", GZIPInputStreamFactory.getInstance())
74 .register("deflate", DeflateInputStreamFactory.getInstance())
75 .build();
76 this.ignoreUnknown = ignoreUnknown;
77 }
78
79
80
81
82 public ResponseContentEncoding(final boolean ignoreUnknown) {
83 this(null, ignoreUnknown);
84 }
85
86
87
88
89 public ResponseContentEncoding(final Lookup<InputStreamFactory> decoderRegistry) {
90 this(decoderRegistry, true);
91 }
92
93
94
95
96
97
98
99
100
101 public ResponseContentEncoding() {
102 this(null);
103 }
104
105 @Override
106 public void process(
107 final HttpResponse response,
108 final HttpContext context) throws HttpException, IOException {
109 final HttpEntity entity = response.getEntity();
110
111 final HttpClientContext clientContext = HttpClientContext.adapt(context);
112 final RequestConfig requestConfig = clientContext.getRequestConfig();
113
114
115 if (requestConfig.isContentCompressionEnabled() && entity != null && entity.getContentLength() != 0) {
116 final Header ceheader = entity.getContentEncoding();
117 if (ceheader != null) {
118 final HeaderElement[] codecs = ceheader.getElements();
119 for (final HeaderElement codec : codecs) {
120 final String codecname = codec.getName().toLowerCase(Locale.ROOT);
121 final InputStreamFactory decoderFactory = decoderRegistry.lookup(codecname);
122 if (decoderFactory != null) {
123 response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory));
124 response.removeHeaders("Content-Length");
125 response.removeHeaders("Content-Encoding");
126 response.removeHeaders("Content-MD5");
127 } else {
128 if (!"identity".equals(codecname) && !ignoreUnknown) {
129 throw new HttpException("Unsupported Content-Encoding: " + codec.getName());
130 }
131 }
132 }
133 }
134 }
135 }
136
137 }