1
1
package io .arex .foundation .util .httpclient ;
2
2
3
+ import io .arex .agent .bootstrap .constants .ConfigConstants ;
3
4
import io .arex .agent .bootstrap .util .MapUtils ;
5
+ import io .arex .agent .bootstrap .util .StringUtil ;
4
6
import io .arex .foundation .config .ConfigManager ;
5
7
import io .arex .foundation .model .HttpClientResponse ;
6
8
import io .arex .foundation .util .CompressUtil ;
7
9
import io .arex .foundation .util .httpclient .async .AutoCleanedPoolingNHttpClientConnectionManager ;
8
10
import io .arex .foundation .util .httpclient .async .ThreadFactoryImpl ;
9
11
import io .arex .inst .runtime .log .LogManager ;
12
+ import org .apache .http .Header ;
10
13
import org .apache .http .HttpEntity ;
11
14
import org .apache .http .HttpHeaders ;
12
15
import org .apache .http .client .config .RequestConfig ;
15
18
import org .apache .http .entity .ByteArrayEntity ;
16
19
import org .apache .http .impl .nio .client .CloseableHttpAsyncClient ;
17
20
import org .apache .http .impl .nio .client .HttpAsyncClients ;
21
+ import org .apache .http .message .BasicHeader ;
18
22
import org .slf4j .Logger ;
19
23
import org .slf4j .LoggerFactory ;
20
24
30
34
public class AsyncHttpClientUtil {
31
35
32
36
private static final Logger LOGGER = LoggerFactory .getLogger (AsyncHttpClientUtil .class );
33
- private static final String USER_AGENT = String .format ("arex-async-http-client-%s" ,
34
- ConfigManager .INSTANCE .getAgentVersion ());
37
+
35
38
/**
36
39
* the compressed size of the sent httpEntity is limited to less than 5MB
37
40
*/
@@ -95,7 +98,7 @@ private static HttpUriRequest createHttpPost(String uri, HttpEntity httpEntity,
95
98
ClientConfig .DEFAULT_SOCKET_TIMEOUT );
96
99
httpPost .setEntity (httpEntity );
97
100
98
- if (requestHeaders != null && requestHeaders . size () > 0 ) {
101
+ if (MapUtils . isNotEmpty ( requestHeaders ) ) {
99
102
requestHeaders .forEach (httpPost ::addHeader );
100
103
}
101
104
return httpPost ;
@@ -107,8 +110,9 @@ private static HttpPost prepareHttpRequest(String uri, int connectTimeout, int s
107
110
RequestConfig requestConfig = createRequestConfig (connectTimeout , socketTimeout );
108
111
109
112
httpPost .setConfig (requestConfig );
110
- httpPost .addHeader (HttpHeaders .ACCEPT , "*" );
111
- httpPost .addHeader (HttpHeaders .USER_AGENT , USER_AGENT );
113
+ httpPost .addHeader (ClientConfig .HEADER_ACCEPT );
114
+ httpPost .addHeader (ClientConfig .HEADER_USER_AGENT );
115
+ httpPost .addHeader (ClientConfig .HEADER_API_TOKEN );
112
116
113
117
return httpPost ;
114
118
}
@@ -141,5 +145,11 @@ static class ClientConfig {
141
145
private static final int DEFAULT_SOCKET_TIMEOUT = 5000 ;
142
146
private static final String APPLICATION_ZSTD_JSON = "application/zstd-json;charset=UTF-8" ;
143
147
private static final String APPLICATION_JSON = "application/json;charset=UTF-8" ;
148
+
149
+ private static final Header HEADER_ACCEPT = new BasicHeader (HttpHeaders .ACCEPT , "*/*" );
150
+ private static final Header HEADER_USER_AGENT = new BasicHeader (HttpHeaders .USER_AGENT ,
151
+ String .format ("arex-async-http-client-%s" , ConfigManager .INSTANCE .getAgentVersion ()));
152
+ private static final Header HEADER_API_TOKEN = new BasicHeader ("arex-api-token" ,
153
+ System .getProperty (ConfigConstants .API_TOKEN , StringUtil .EMPTY ));
144
154
}
145
155
}
0 commit comments