DecompressingMethod.java
9.02 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.httpclient.Header
* org.apache.commons.httpclient.HeaderElement
* org.apache.commons.httpclient.HostConfiguration
* org.apache.commons.httpclient.HttpConnection
* org.apache.commons.httpclient.HttpException
* org.apache.commons.httpclient.HttpMethod
* org.apache.commons.httpclient.HttpState
* org.apache.commons.httpclient.NameValuePair
* org.apache.commons.httpclient.StatusLine
* org.apache.commons.httpclient.URI
* org.apache.commons.httpclient.URIException
* org.apache.commons.httpclient.auth.AuthState
* org.apache.commons.httpclient.params.HttpMethodParams
* org.apache.commons.io.IOUtils
*/
package com.day.cq.searchpromote.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.zip.GZIPInputStream;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HeaderElement;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.StatusLine;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.auth.AuthState;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
public class DecompressingMethod
implements HttpMethod {
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
private static final Header DEFAULT_HEADER = new Header("Accept-Encoding", "gzip;q=1.0, *;q=0");
private final HttpMethod decorated;
public DecompressingMethod(HttpMethod decorated) {
this.decorated = decorated;
}
public byte[] getResponseBody() throws IOException {
byte[] raw = this.decorated.getResponseBody();
if (this.isGzipped()) {
ByteArrayOutputStream decompressed = new ByteArrayOutputStream();
IOUtils.copy((InputStream)new GZIPInputStream(new ByteArrayInputStream(raw)), (OutputStream)decompressed);
return decompressed.toByteArray();
}
return raw;
}
public InputStream getResponseBodyAsStream() throws IOException {
return this.decorated.getResponseBodyAsStream();
}
public String getResponseBodyAsString() throws IOException {
return IOUtils.toString((InputStream)new ByteArrayInputStream(this.getResponseBody()), (String)this.getResponseEncoding());
}
public int execute(HttpState httpState, HttpConnection httpConnection) throws HttpException, IOException {
this.decorated.setRequestHeader(DEFAULT_HEADER);
return this.decorated.execute(httpState, httpConnection);
}
public Header[] getRequestHeaders() {
return this.getDecompressingRequestHeaders();
}
public Header[] getRequestHeaders(String s) {
ArrayList<Header> headers = new ArrayList<Header>();
if (s != null) {
for (Header header : this.getDecompressingRequestHeaders()) {
if (!s.equalsIgnoreCase(header.getName())) continue;
headers.add(header);
}
}
return headers.toArray((T[])new Header[headers.size()]);
}
public Header getRequestHeader(String s) {
if (s != null) {
for (Header header : this.getDecompressingRequestHeaders()) {
if (!s.equalsIgnoreCase(header.getName())) continue;
return header;
}
}
return null;
}
public String getName() {
return "DecompressingMethod";
}
private Header[] getDecompressingRequestHeaders() {
ArrayList<Header> headers = new ArrayList<Header>(Arrays.asList(this.decorated.getRequestHeaders()));
for (Header header : headers) {
if (!"Accept-Encoding".equalsIgnoreCase(header.getName())) continue;
headers.remove((Object)header);
}
headers.add(DEFAULT_HEADER);
return headers.toArray((T[])new Header[headers.size()]);
}
private boolean isGzipped() {
Header ceh = this.decorated.getResponseHeader("Content-Encoding");
if (ceh != null) {
for (HeaderElement he : ceh.getElements()) {
String elt = he.getName().toLowerCase(Locale.US);
if (!"gzip".equalsIgnoreCase(elt) && !"x-gzip".equalsIgnoreCase(elt)) continue;
return true;
}
}
return false;
}
private String getResponseEncoding() {
int charsetIndex;
String defaultEncoding = "8859_1";
Header hdr = this.decorated.getResponseHeader("Content-Type");
if (hdr != null && hdr.getValue() != null && (charsetIndex = hdr.getValue().indexOf("charset=")) != -1) {
return hdr.getValue().substring(charsetIndex + "charset=".length()).trim();
}
return "8859_1";
}
public void setRequestHeader(String s, String s1) {
this.decorated.setRequestHeader(s, s1);
}
public void setRequestHeader(Header header) {
this.decorated.setRequestHeader(header);
}
public void addRequestHeader(String s, String s1) {
this.decorated.addRequestHeader(s, s1);
}
public void addRequestHeader(Header header) {
this.decorated.addRequestHeader(header);
}
public void removeRequestHeader(String s) {
this.decorated.removeRequestHeader(s);
}
public void removeRequestHeader(Header header) {
this.decorated.removeRequestHeader(header);
}
public Header[] getResponseHeaders() {
return this.decorated.getResponseHeaders();
}
public Header getResponseHeader(String s) {
return this.decorated.getResponseHeader(s);
}
public Header[] getResponseHeaders(String s) {
return this.decorated.getResponseHeaders(s);
}
public boolean hasBeenUsed() {
return this.decorated.hasBeenUsed();
}
public void abort() {
this.decorated.abort();
}
@Deprecated
public void recycle() {
this.decorated.recycle();
}
public void releaseConnection() {
this.decorated.releaseConnection();
}
public void addResponseFooter(Header header) {
this.decorated.addResponseFooter(header);
}
public StatusLine getStatusLine() {
return this.decorated.getStatusLine();
}
public boolean getDoAuthentication() {
return this.decorated.getDoAuthentication();
}
public void setDoAuthentication(boolean b) {
this.decorated.setDoAuthentication(b);
}
public HttpMethodParams getParams() {
return this.decorated.getParams();
}
public void setParams(HttpMethodParams httpMethodParams) {
this.decorated.setParams(httpMethodParams);
}
public AuthState getHostAuthState() {
return this.decorated.getHostAuthState();
}
public AuthState getProxyAuthState() {
return this.decorated.getProxyAuthState();
}
public boolean isRequestSent() {
return this.decorated.isRequestSent();
}
@Deprecated
public HostConfiguration getHostConfiguration() {
return this.decorated.getHostConfiguration();
}
public void setPath(String s) {
this.decorated.setPath(s);
}
public String getPath() {
return this.decorated.getPath();
}
public URI getURI() throws URIException {
return this.decorated.getURI();
}
public void setURI(URI uri) throws URIException {
this.decorated.setURI(uri);
}
@Deprecated
public void setStrictMode(boolean b) {
this.decorated.setStrictMode(b);
}
@Deprecated
public boolean isStrictMode() {
return this.decorated.isStrictMode();
}
public boolean getFollowRedirects() {
return this.decorated.getFollowRedirects();
}
public void setFollowRedirects(boolean b) {
this.decorated.setFollowRedirects(b);
}
public void setQueryString(String s) {
this.decorated.setQueryString(s);
}
public void setQueryString(NameValuePair[] nameValuePairs) {
this.decorated.setQueryString(nameValuePairs);
}
public String getQueryString() {
return this.decorated.getQueryString();
}
public boolean validate() {
return this.decorated.validate();
}
public int getStatusCode() {
return this.decorated.getStatusCode();
}
public String getStatusText() {
return this.decorated.getStatusText();
}
public Header[] getResponseFooters() {
return this.decorated.getResponseFooters();
}
public Header getResponseFooter(String s) {
return this.decorated.getResponseFooter(s);
}
}