1
1
package io .arex .inst .httpclient .apache .common ;
2
2
3
3
import io .arex .agent .bootstrap .util .CollectionUtil ;
4
- import io .arex .agent .bootstrap .util .IOUtils ;
5
4
import io .arex .agent .bootstrap .util .StringUtil ;
6
5
import io .arex .inst .httpclient .common .HttpClientAdapter ;
7
6
import io .arex .inst .httpclient .common .HttpResponseWrapper ;
8
7
import io .arex .inst .httpclient .common .HttpResponseWrapper .StringTuple ;
9
8
import io .arex .inst .runtime .log .LogManager ;
10
- import java .io .ByteArrayOutputStream ;
9
+ import java .io .IOException ;
11
10
import org .apache .http .Header ;
12
11
import org .apache .http .HttpEntity ;
13
12
import org .apache .http .HttpEntityEnclosingRequest ;
14
13
import org .apache .http .HttpRequest ;
15
14
import org .apache .http .HttpResponse ;
16
15
import org .apache .http .StatusLine ;
17
- import org .apache .http .client .entity .GzipCompressingEntity ;
18
16
import org .apache .http .client .methods .HttpUriRequest ;
19
17
import org .apache .http .entity .BasicHttpEntity ;
18
+ import org .apache .http .entity .BufferedHttpEntity ;
20
19
import org .apache .http .entity .HttpEntityWrapper ;
21
20
22
21
import java .io .ByteArrayInputStream ;
@@ -31,7 +30,6 @@ public class ApacheHttpClientAdapter implements HttpClientAdapter<HttpRequest, H
31
30
32
31
public ApacheHttpClientAdapter (HttpRequest httpRequest ) {
33
32
this .httpRequest = (HttpUriRequest ) httpRequest ;
34
- wrapHttpEntity (httpRequest );
35
33
}
36
34
37
35
@ Override
@@ -41,27 +39,25 @@ public String getMethod() {
41
39
42
40
@ Override
43
41
public byte [] getRequestBytes () {
44
- HttpEntityEnclosingRequest enclosingRequest = enclosingRequest (httpRequest );
45
- if (enclosingRequest == null ) {
42
+ if (!(httpRequest instanceof HttpEntityEnclosingRequest )) {
46
43
return ZERO_BYTE ;
47
44
}
48
- HttpEntity entity = enclosingRequest . getEntity () ;
49
- if (entity == null ) {
45
+ HttpEntityEnclosingRequest enclosingRequest = ( HttpEntityEnclosingRequest ) httpRequest ;
46
+ if (enclosingRequest . getEntity () == null ) {
50
47
return ZERO_BYTE ;
51
48
}
52
- // getContent will throw UnsupportedOperationException
53
- if (entity instanceof GzipCompressingEntity ) {
54
- return writeTo ((GzipCompressingEntity ) entity );
55
- }
56
- if (entity instanceof CachedHttpEntityWrapper ) {
57
- return ((CachedHttpEntityWrapper ) entity ).getCachedBody ();
58
- }
59
- try {
60
- return IOUtils .copyToByteArray (entity .getContent ());
61
- } catch (Exception e ) {
62
- LogManager .warn ("copyToByteArray" , "getRequestBytes error, uri: " + getUri (), e );
63
- return ZERO_BYTE ;
49
+
50
+ bufferRequestEntity (enclosingRequest );
51
+
52
+ if (enclosingRequest .getEntity () instanceof BufferedHttpEntity ) {
53
+ try {
54
+ return EntityUtils .toByteArray (enclosingRequest .getEntity ());
55
+ } catch (IOException e ) {
56
+ LogManager .warn ("AHC.wrap" , "toByteArray error, uri: " + getUri (), e );
57
+ return ZERO_BYTE ;
58
+ }
64
59
}
60
+ return ZERO_BYTE ;
65
61
}
66
62
67
63
@ Override
@@ -83,13 +79,12 @@ public URI getUri() {
83
79
84
80
@ Override
85
81
public HttpResponseWrapper wrap (HttpResponse response ) {
86
-
87
- List <HttpResponseWrapper .StringTuple > headers = new ArrayList <>(response .getAllHeaders ().length );
82
+ List <StringTuple > headers = new ArrayList <>(response .getAllHeaders ().length );
88
83
for (Header header : response .getAllHeaders ()) {
89
84
if (StringUtil .isEmpty (header .getName ())) {
90
85
continue ;
91
86
}
92
- headers .add (new HttpResponseWrapper . StringTuple (header .getName (), header .getValue ()));
87
+ headers .add (new StringTuple (header .getName (), header .getValue ()));
93
88
}
94
89
95
90
HttpEntity httpEntity = response .getEntity ();
@@ -99,28 +94,21 @@ public HttpResponseWrapper wrap(HttpResponse response) {
99
94
return buildEmptyBodyResponseWrapper (response .getStatusLine ().toString (), locale , headers );
100
95
}
101
96
102
- byte [] responseBody ;
97
+ // Compatible with org.apache.http.impl.client.InternalHttpClient.doExecute
98
+ bufferResponseEntity (response );
99
+
100
+ byte [] responseBody = new byte [0 ];
103
101
try {
104
- responseBody = IOUtils . copyToByteArray ( httpEntity . getContent ());
105
- // For release connection, see PoolingHttpClientConnectionManager#requestConnection,releaseConnection
106
- EntityUtils . consumeQuietly ( httpEntity );
102
+ if ( response . getEntity () instanceof BufferedHttpEntity ) {
103
+ responseBody = EntityUtils . toByteArray ( response . getEntity ());
104
+ }
107
105
} catch (Exception e ) {
108
106
LogManager .warn ("AHC.wrap" , "AHC copyToByteArray error, uri: " + getUri (), e );
109
107
return buildEmptyBodyResponseWrapper (response .getStatusLine ().toString (), locale , headers );
110
108
}
111
109
112
- if (httpEntity instanceof BasicHttpEntity ) {
113
- ((BasicHttpEntity ) httpEntity ).setContent (new ByteArrayInputStream (responseBody ));
114
- response .setEntity (httpEntity );
115
- } else if (httpEntity instanceof HttpEntityWrapper ) {
116
- // Output response normally now, later need to check revert DecompressingEntity
117
- BasicHttpEntity entity = ApacheHttpClientHelper .createHttpEntity (response );
118
- entity .setContent (new ByteArrayInputStream (responseBody ));
119
- response .setEntity (entity );
120
- }
121
-
122
110
return new HttpResponseWrapper (response .getStatusLine ().toString (), responseBody ,
123
- new HttpResponseWrapper . StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
111
+ new StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
124
112
}
125
113
126
114
@ Override
@@ -154,7 +142,7 @@ private HttpResponseWrapper buildEmptyBodyResponseWrapper(String statusLine, Loc
154
142
return null ;
155
143
}
156
144
return new HttpResponseWrapper (statusLine , null ,
157
- new HttpResponseWrapper . StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
145
+ new StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
158
146
}
159
147
160
148
private static boolean check (HttpEntity entity ) {
@@ -169,37 +157,25 @@ private static boolean ignoreUserAgent(String userAgent) {
169
157
return userAgent != null && userAgent .contains ("arex" );
170
158
}
171
159
172
- public static void wrapHttpEntity (HttpRequest httpRequest ) {
173
- HttpEntityEnclosingRequest enclosingRequest = enclosingRequest (httpRequest );
174
- if (enclosingRequest == null ) {
175
- return ;
176
- }
177
- HttpEntity entity = enclosingRequest .getEntity ();
178
- if (entity == null || entity .isRepeatable () || entity instanceof CachedHttpEntityWrapper ) {
160
+ private static void bufferRequestEntity (HttpEntityEnclosingRequest enclosingRequest ) {
161
+ if (enclosingRequest .getEntity () instanceof BufferedHttpEntity ) {
179
162
return ;
180
163
}
181
164
try {
182
- enclosingRequest .setEntity (new CachedHttpEntityWrapper ( entity ));
165
+ enclosingRequest .setEntity (new BufferedHttpEntity ( enclosingRequest . getEntity () ));
183
166
} catch (Exception ignore ) {
184
167
// ignore exception
185
168
}
186
169
}
187
170
188
- private static HttpEntityEnclosingRequest enclosingRequest ( HttpRequest httpRequest ) {
189
- if (httpRequest instanceof HttpEntityEnclosingRequest ) {
190
- return ( HttpEntityEnclosingRequest ) httpRequest ;
171
+ public void bufferResponseEntity ( HttpResponse response ) {
172
+ if (response . getEntity () instanceof BufferedHttpEntity ) {
173
+ return ;
191
174
}
192
- return null ;
193
- }
194
-
195
- private byte [] writeTo (GzipCompressingEntity entity ) {
196
- ByteArrayOutputStream out = new ByteArrayOutputStream ();
197
175
try {
198
- entity .writeTo (out );
199
- return out .toByteArray ();
176
+ EntityUtils .updateEntity (response , new BufferedHttpEntity (response .getEntity ()));
200
177
} catch (Exception e ) {
201
- LogManager .warn ("writeTo" , "getRequestBytes error, uri: " + getUri (), e );
202
- return ZERO_BYTE ;
178
+ // ignore exception
203
179
}
204
180
}
205
181
}
0 commit comments