1
1
package io .arex .inst .httpclient .apache .common ;
2
2
3
- import io .arex .agent .bootstrap .util .CollectionUtil ;
4
- import io .arex .agent .bootstrap .util .IOUtils ;
3
+ import io .arex .agent .bootstrap .util .ArrayUtils ;
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,17 @@ 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 ) {
50
- return ZERO_BYTE ;
51
- }
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 );
45
+ HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest ) httpRequest ;
46
+ if (enclosingRequest .getEntity () == null ) {
63
47
return ZERO_BYTE ;
64
48
}
49
+
50
+ bufferRequestEntity (enclosingRequest );
51
+
52
+ return getEntityBytes (enclosingRequest .getEntity ());
65
53
}
66
54
67
55
@ Override
@@ -83,44 +71,32 @@ public URI getUri() {
83
71
84
72
@ Override
85
73
public HttpResponseWrapper wrap (HttpResponse response ) {
74
+ Header [] responseHeaders = response .getAllHeaders ();
75
+ if (ArrayUtils .isEmpty (responseHeaders )) {
76
+ return null ;
77
+ }
86
78
87
- List <HttpResponseWrapper . StringTuple > headers = new ArrayList <>(response . getAllHeaders () .length );
88
- for (Header header : response . getAllHeaders () ) {
79
+ List <StringTuple > headers = new ArrayList <>(responseHeaders .length );
80
+ for (Header header : responseHeaders ) {
89
81
if (StringUtil .isEmpty (header .getName ())) {
90
82
continue ;
91
83
}
92
- headers .add (new HttpResponseWrapper . StringTuple (header .getName (), header .getValue ()));
84
+ headers .add (new StringTuple (header .getName (), header .getValue ()));
93
85
}
94
86
95
- HttpEntity httpEntity = response .getEntity ();
96
87
Locale locale = response .getLocale ();
97
88
98
- if (!check (httpEntity )) {
99
- return buildEmptyBodyResponseWrapper (response .getStatusLine ().toString (), locale , headers );
89
+ if (!check (response .getEntity ())) {
90
+ return new HttpResponseWrapper (response .getStatusLine ().toString (), null ,
91
+ new StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
100
92
}
101
93
102
- byte [] responseBody ;
103
- try {
104
- responseBody = IOUtils .copyToByteArray (httpEntity .getContent ());
105
- // For release connection, see PoolingHttpClientConnectionManager#requestConnection,releaseConnection
106
- EntityUtils .consumeQuietly (httpEntity );
107
- } catch (Exception e ) {
108
- LogManager .warn ("AHC.wrap" , "AHC copyToByteArray error, uri: " + getUri (), e );
109
- return buildEmptyBodyResponseWrapper (response .getStatusLine ().toString (), locale , headers );
110
- }
111
-
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
- }
94
+ // Compatible with org.apache.http.impl.client.InternalHttpClient.doExecute
95
+ bufferResponseEntity (response );
121
96
97
+ byte [] responseBody = getEntityBytes (response .getEntity ());
122
98
return new HttpResponseWrapper (response .getStatusLine ().toString (), responseBody ,
123
- new HttpResponseWrapper . StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
99
+ new StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
124
100
}
125
101
126
102
@ Override
@@ -147,16 +123,6 @@ private static void appendHeaders(HttpResponse response, List<StringTuple> heade
147
123
}
148
124
}
149
125
150
- private HttpResponseWrapper buildEmptyBodyResponseWrapper (String statusLine , Locale locale ,
151
- List <StringTuple > headers ) {
152
- if (CollectionUtil .isEmpty (headers )) {
153
- LogManager .warn ("AHC.wrap" , "AHC response wrap failed, uri: " + getUri ());
154
- return null ;
155
- }
156
- return new HttpResponseWrapper (statusLine , null ,
157
- new HttpResponseWrapper .StringTuple (locale .getLanguage (), locale .getCountry ()), headers );
158
- }
159
-
160
126
private static boolean check (HttpEntity entity ) {
161
127
return entity instanceof BasicHttpEntity || entity instanceof HttpEntityWrapper ;
162
128
}
@@ -169,36 +135,36 @@ private static boolean ignoreUserAgent(String userAgent) {
169
135
return userAgent != null && userAgent .contains ("arex" );
170
136
}
171
137
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 ) {
138
+ public static void bufferRequestEntity (HttpEntityEnclosingRequest enclosingRequest ) {
139
+ if (enclosingRequest .getEntity () == null || enclosingRequest .getEntity () instanceof BufferedHttpEntity ) {
179
140
return ;
180
141
}
181
142
try {
182
- enclosingRequest .setEntity (new CachedHttpEntityWrapper ( entity ));
143
+ enclosingRequest .setEntity (new BufferedHttpEntity ( enclosingRequest . getEntity () ));
183
144
} catch (Exception ignore ) {
184
145
// ignore exception
185
146
}
186
147
}
187
148
188
- private static HttpEntityEnclosingRequest enclosingRequest (HttpRequest httpRequest ) {
189
- if (httpRequest instanceof HttpEntityEnclosingRequest ) {
190
- return (HttpEntityEnclosingRequest ) httpRequest ;
149
+ public static void bufferResponseEntity (HttpResponse response ) {
150
+ if (response .getEntity () == null || response .getEntity () instanceof BufferedHttpEntity ) {
151
+ return ;
152
+ }
153
+ try {
154
+ EntityUtils .updateEntity (response , new BufferedHttpEntity (response .getEntity ()));
155
+ } catch (Exception e ) {
156
+ // ignore exception
191
157
}
192
- return null ;
193
158
}
194
159
195
- private byte [] writeTo (GzipCompressingEntity entity ) {
196
- ByteArrayOutputStream out = new ByteArrayOutputStream ();
160
+ private byte [] getEntityBytes (HttpEntity entity ) {
161
+ if (!(entity instanceof BufferedHttpEntity )) {
162
+ return ZERO_BYTE ;
163
+ }
197
164
try {
198
- entity .writeTo (out );
199
- return out .toByteArray ();
200
- } catch (Exception e ) {
201
- LogManager .warn ("writeTo" , "getRequestBytes error, uri: " + getUri (), e );
165
+ return EntityUtils .toByteArray (entity );
166
+ } catch (IOException e ) {
167
+ LogManager .warn ("AHC.getEntityBytes" , "getEntityBytes error, uri: " + getUri (), e );
202
168
return ZERO_BYTE ;
203
169
}
204
170
}
0 commit comments