|
17 | 17 | import com.google.api.client.http.LowLevelHttpRequest; |
18 | 18 | import com.google.api.client.http.LowLevelHttpResponse; |
19 | 19 |
|
| 20 | +import java.io.ByteArrayOutputStream; |
20 | 21 | import java.io.IOException; |
21 | 22 | import java.util.concurrent.CompletableFuture; |
22 | 23 | import java.util.concurrent.ExecutionException; |
|
27 | 28 | import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer; |
28 | 29 | import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; |
29 | 30 | import org.apache.hc.client5.http.config.RequestConfig; |
| 31 | +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; |
30 | 32 | import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; |
31 | 33 | import org.apache.hc.core5.concurrent.FutureCallback; |
32 | 34 | import org.apache.hc.core5.http.ContentType; |
|
42 | 44 | import org.apache.hc.core5.util.Timeout; |
43 | 45 | import org.apache.http.HttpEntity; |
44 | 46 |
|
45 | | -/** @author Yaniv Inbar */ |
| 47 | +/** |
| 48 | + * @author Yaniv Inbar |
| 49 | + */ |
46 | 50 | final class ApacheHttpRequest extends LowLevelHttpRequest { |
47 | | - private final HttpAsyncClientBuilder httpClientBuilder; |
48 | | - private final SimpleHttpRequest request; |
| 51 | + private final HttpAsyncClientBuilder httpClientBuilder; |
| 52 | + private final SimpleHttpRequest request; |
49 | 53 |
|
50 | | - private RequestConfig.Builder requestConfig; |
| 54 | + private RequestConfig.Builder requestConfig; |
51 | 55 |
|
52 | | - ApacheHttpRequest(HttpAsyncClientBuilder httpClientBuilder, SimpleHttpRequest request) { |
53 | | - this.httpClientBuilder = httpClientBuilder; |
54 | | - this.request = request; |
55 | | - // disable redirects as google-http-client handles redirects |
56 | | - this.requestConfig = |
57 | | - RequestConfig.custom() |
58 | | - .setRedirectsEnabled(false) |
59 | | - // TODO: enable set these somewhere down the call |
| 56 | + ApacheHttpRequest(HttpAsyncClientBuilder httpClientBuilder, SimpleHttpRequest request) { |
| 57 | + this.httpClientBuilder = httpClientBuilder; |
| 58 | + this.request = request; |
| 59 | + // disable redirects as google-http-client handles redirects |
| 60 | + this.requestConfig = |
| 61 | + RequestConfig.custom() |
| 62 | + .setRedirectsEnabled(false) |
| 63 | + // TODO: enable set these somewhere down the call |
60 | 64 | // .setNormalizeUri(false) |
61 | 65 | // .setStaleConnectionCheckEnabled(false) |
62 | | - ; |
63 | | - } |
| 66 | + ; |
| 67 | + } |
64 | 68 |
|
65 | | - @Override |
66 | | - public void addHeader(String name, String value) { |
67 | | - request.addHeader(name, value); |
68 | | - } |
| 69 | + @Override |
| 70 | + public void addHeader(String name, String value) { |
| 71 | + request.addHeader(name, value); |
| 72 | + } |
69 | 73 |
|
70 | | - @Override |
71 | | - public void setTimeout(int connectTimeout, int readTimeout) throws IOException { |
72 | | - IOReactorConfig newConfig = IOReactorConfig.custom() |
73 | | - .setSoTimeout(Timeout.ofMilliseconds(readTimeout)) |
74 | | - .build(); |
75 | | - requestConfig.setConnectTimeout(Timeout.ofMilliseconds(connectTimeout)); |
76 | | - httpClientBuilder.setIOReactorConfig(newConfig); |
77 | | - } |
| 74 | + @Override |
| 75 | + public void setTimeout(int connectTimeout, int readTimeout) throws IOException { |
| 76 | + IOReactorConfig newConfig = IOReactorConfig.custom() |
| 77 | + .setSoTimeout(Timeout.ofMilliseconds(readTimeout)) |
| 78 | + .build(); |
| 79 | + requestConfig.setConnectTimeout(Timeout.ofMilliseconds(connectTimeout)); |
| 80 | + httpClientBuilder.setIOReactorConfig(newConfig); |
| 81 | + } |
78 | 82 |
|
79 | | - @Override |
80 | | - public LowLevelHttpResponse execute() throws IOException { |
| 83 | + @Override |
| 84 | + public LowLevelHttpResponse execute() throws IOException { |
81 | 85 | ApacheHttpRequestEntityProducer entityProducer = new ApacheHttpRequestEntityProducer(this); |
82 | 86 |
|
83 | | - request.setConfig(requestConfig.build()); |
84 | | - final CompletableFuture<SimpleHttpResponse> responseFuture = new CompletableFuture<>(); |
85 | | - try { |
86 | | - httpClientBuilder.build().execute( |
| 87 | + request.setConfig(requestConfig.build()); |
| 88 | + final CompletableFuture<SimpleHttpResponse> responseFuture = new CompletableFuture<>(); |
| 89 | + try { |
| 90 | + CloseableHttpAsyncClient client = httpClientBuilder.build(); |
| 91 | + client.start(); |
| 92 | + client.execute( |
87 | 93 | new BasicRequestProducer(request, entityProducer), |
88 | | - SimpleResponseConsumer.create(), |
89 | | - new FutureCallback<SimpleHttpResponse>() { |
90 | | - @Override |
91 | | - public void completed(final SimpleHttpResponse response) { |
92 | | - responseFuture.complete(response); |
93 | | - } |
| 94 | + SimpleResponseConsumer.create(), |
| 95 | + new FutureCallback<SimpleHttpResponse>() { |
| 96 | + @Override |
| 97 | + public void completed(final SimpleHttpResponse response) { |
| 98 | + responseFuture.complete(response); |
| 99 | + } |
94 | 100 |
|
95 | | - @Override |
96 | | - public void failed(final Exception exception) { |
97 | | - responseFuture.completeExceptionally(exception); |
98 | | - } |
| 101 | + @Override |
| 102 | + public void failed(final Exception exception) { |
| 103 | + responseFuture.completeExceptionally(exception); |
| 104 | + } |
99 | 105 |
|
100 | | - @Override |
101 | | - public void cancelled() { |
102 | | - responseFuture.cancel(false); |
103 | | - } |
104 | | - } |
105 | | - ); |
106 | | - final SimpleHttpResponse response = responseFuture.get(); |
107 | | - return new ApacheHttpResponse(request, response); |
108 | | - } catch (InterruptedException | ExecutionException e) { |
109 | | - e.printStackTrace(); |
110 | | - throw new IOException("Error making request", e); |
| 106 | + @Override |
| 107 | + public void cancelled() { |
| 108 | + responseFuture.cancel(false); |
| 109 | + } |
| 110 | + } |
| 111 | + ); |
| 112 | + final SimpleHttpResponse response = responseFuture.get(); |
| 113 | + return new ApacheHttpResponse(request, response); |
| 114 | + } catch (InterruptedException | ExecutionException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + throw new IOException("Error making request", e); |
| 117 | + } |
111 | 118 | } |
112 | | - } |
113 | 119 |
|
114 | 120 | } |
0 commit comments