forked from Hakky54/mutual-tls-ssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApacheHttpAsyncClientService.java
46 lines (34 loc) 路 1.56 KB
/
ApacheHttpAsyncClientService.java
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
package nl.altindag.client.service;
import nl.altindag.client.ClientType;
import nl.altindag.client.model.ClientResponse;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static nl.altindag.client.ClientType.APACHE_HTTP_ASYNC_CLIENT;
import static nl.altindag.client.Constants.HEADER_KEY_CLIENT_TYPE;
@Service
public class ApacheHttpAsyncClientService implements RequestService {
private static final int TIMEOUT_AMOUNT_IN_SECONDS = 5;
private final CloseableHttpAsyncClient httpClient;
public ApacheHttpAsyncClientService(CloseableHttpAsyncClient httpClient) {
this.httpClient = httpClient;
}
@Override
public ClientResponse executeRequest(String url) throws Exception {
HttpGet request = new HttpGet(url);
request.addHeader(HEADER_KEY_CLIENT_TYPE, getClientType().getValue());
Future<HttpResponse> responseFuture = httpClient.execute(request, null);
HttpResponse response = responseFuture.get(TIMEOUT_AMOUNT_IN_SECONDS, TimeUnit.SECONDS);
String responseBody = EntityUtils.toString(response.getEntity());
int statusCode = response.getStatusLine().getStatusCode();
return new ClientResponse(responseBody, statusCode);
}
@Override
public ClientType getClientType() {
return APACHE_HTTP_ASYNC_CLIENT;
}
}