forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OkHttpClientImpl.java
393 lines (344 loc) · 13.1 KB
/
OkHttpClientImpl.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.okhttp;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.http.AsyncBody;
import io.fabric8.kubernetes.client.http.AsyncBody.Consumer;
import io.fabric8.kubernetes.client.http.HttpRequest;
import io.fabric8.kubernetes.client.http.HttpResponse;
import io.fabric8.kubernetes.client.http.StandardHttpClient;
import io.fabric8.kubernetes.client.http.StandardHttpHeaders;
import io.fabric8.kubernetes.client.http.StandardHttpRequest;
import io.fabric8.kubernetes.client.http.StandardHttpRequest.BodyContent;
import io.fabric8.kubernetes.client.http.StandardHttpRequest.ByteArrayBodyContent;
import io.fabric8.kubernetes.client.http.StandardHttpRequest.InputStreamBodyContent;
import io.fabric8.kubernetes.client.http.StandardHttpRequest.StringBodyContent;
import io.fabric8.kubernetes.client.http.StandardWebSocketBuilder;
import io.fabric8.kubernetes.client.http.WebSocket.Listener;
import io.fabric8.kubernetes.client.http.WebSocketResponse;
import io.fabric8.kubernetes.client.utils.Utils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import okio.Source;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.Reader;
import java.net.MalformedURLException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.Function;
public class OkHttpClientImpl extends StandardHttpClient<OkHttpClientImpl, OkHttpClientFactory, OkHttpClientBuilderImpl> {
static final transient Logger LOG = LoggerFactory.getLogger(OkHttpClientImpl.class);
static final Map<String, MediaType> MEDIA_TYPES = new ConcurrentHashMap<>();
public static final MediaType JSON = parseMediaType("application/json");
static MediaType parseMediaType(String contentType) {
MediaType result = MediaType.parse(contentType);
MEDIA_TYPES.put(contentType, result);
return result;
}
abstract static class OkHttpAsyncBody<T> implements AsyncBody {
private final AsyncBody.Consumer<T> consumer;
private final BufferedSource source;
private final CompletableFuture<Void> done = new CompletableFuture<>();
private boolean consuming;
private boolean requested;
private Executor executor;
OkHttpAsyncBody(AsyncBody.Consumer<T> consumer, BufferedSource source, Executor executor) {
this.consumer = consumer;
this.source = source;
this.executor = executor;
}
@Override
public void consume() {
synchronized (this) {
requested = true;
if (consuming) {
return;
}
consuming = true;
}
try {
// consume should not block a caller, delegate to the dispatcher thread pool
executor.execute(this::doConsume);
} catch (Exception e) {
// executor is likely shutdown
Utils.closeQuietly(source);
done.completeExceptionally(e);
}
}
private void doConsume() {
try {
while (true) {
synchronized (this) {
if (!requested || done.isDone()) {
consuming = false;
return;
}
requested = false;
}
if (!source.exhausted()) { // this is a blocking call
T value = process(source);
consumer.consume(value, this);
} else {
// even if we've read everything an explicit close is still needed
source.close();
done.complete(null);
}
}
} catch (Exception e) {
Utils.closeQuietly(source);
done.completeExceptionally(e);
}
}
@Override
public CompletableFuture<Void> done() {
return done;
}
protected abstract T process(BufferedSource source) throws IOException;
@Override
public void cancel() {
Utils.closeQuietly(source);
done.cancel(false);
}
}
static class OkHttpResponseImpl<T> implements HttpResponse<T> {
private final Response response;
private T body;
private Class<T> type;
public OkHttpResponseImpl(Response response, T body) throws IOException {
this.response = response;
this.body = body;
}
public OkHttpResponseImpl(Response response, Class<T> type) throws IOException {
this.response = response;
this.type = type;
ResponseBody responseBody = response.body();
if (responseBody != null) {
if (type == null) {
responseBody.close();
} else if (type == String.class) {
body = (T) responseBody.string();
} else if (type == Reader.class) {
body = (T) responseBody.charStream();
} else if (type == byte[].class) {
body = (T) responseBody.bytes();
} else {
body = (T) responseBody.byteStream();
}
}
}
@Override
public int code() {
return response.code();
}
@Override
public T body() {
return body;
}
@Override
public HttpRequest request() {
String bodyString = null;
Request request = response.request();
if (request.body() != null) {
Buffer buffer = new Buffer();
try {
request.body().writeTo(buffer);
bodyString = buffer.readUtf8();
} catch (IOException e) {
// just ignore, the bodyString is only for informational purposes
}
}
return new StandardHttpRequest(
request.headers().toMultimap(),
request.url().uri(),
request.method(),
bodyString);
}
@Override
public Optional<HttpResponse<?>> previousResponse() {
Response previous = response.priorResponse() != null ? response.priorResponse() : response;
previous = previous.networkResponse() != null ? previous.networkResponse() : previous;
try {
return Optional.ofNullable(previous == response ? null : new OkHttpResponseImpl<>(previous, type));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public List<String> headers(String key) {
return response.headers(key);
}
@Override
public Map<String, List<String>> headers() {
return response.headers().toMultimap();
}
}
private final okhttp3.OkHttpClient httpClient;
public OkHttpClientImpl(OkHttpClient client, OkHttpClientBuilderImpl builder) {
super(builder);
this.httpClient = client;
}
@Override
public void close() {
LOG.debug("Shutting down dispatcher " + this.httpClient.dispatcher(), new Exception());
ConnectionPool connectionPool = httpClient.connectionPool();
Dispatcher dispatcher = httpClient.dispatcher();
ExecutorService executorService = httpClient.dispatcher() != null ? httpClient.dispatcher().executorService() : null;
if (dispatcher != null) {
dispatcher.cancelAll();
}
if (connectionPool != null) {
connectionPool.evictAll();
}
if (executorService != null) {
executorService.shutdownNow();
}
}
private CompletableFuture<HttpResponse<AsyncBody>> sendAsync(HttpRequest request,
Function<BufferedSource, AsyncBody> handler) {
CompletableFuture<HttpResponse<AsyncBody>> future = new CompletableFuture<>();
Call call = httpClient.newCall(requestBuilder((StandardHttpRequest) request).build());
try {
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
BufferedSource source = response.body().source();
AsyncBody asyncBody = handler.apply(source);
future.complete(new OkHttpResponseImpl<>(response, asyncBody));
}
@Override
public void onFailure(Call call, IOException e) {
Throwable t = e;
if (e instanceof InterruptedIOException && e.getCause() instanceof RejectedExecutionException) {
t = wrapRejected((RejectedExecutionException) e.getCause());
}
future.completeExceptionally(t);
}
});
} catch (RejectedExecutionException e) {
throw wrapRejected(e);
}
future.whenComplete((r, t) -> {
if (future.isCancelled()) {
call.cancel();
}
});
return future;
}
private KubernetesClientException wrapRejected(RejectedExecutionException e) {
return new KubernetesClientException("The okhttp client executor has been shutdown. "
+ "More than likely this is because the KubernetesClient.close method (see debug logging) has been called "
+ "- please ensure that is intentional. Dispatcher: " + this.httpClient.dispatcher(), e);
}
public okhttp3.OkHttpClient getOkHttpClient() {
return httpClient;
}
private okhttp3.Request.Builder newRequestBuilder() {
return new Request.Builder();
}
@Override
public CompletableFuture<HttpResponse<AsyncBody>> consumeBytesDirect(StandardHttpRequest request,
Consumer<List<ByteBuffer>> consumer) {
Function<BufferedSource, AsyncBody> handler = s -> new OkHttpAsyncBody<List<ByteBuffer>>(consumer, s,
this.httpClient.dispatcher().executorService()) {
@Override
protected List<ByteBuffer> process(BufferedSource source) throws IOException {
// read only what is available otherwise okhttp will block trying to read
// a whole fetch size 8k worth
return Collections.singletonList(ByteBuffer.wrap(source.readByteArray(source.buffer().size())));
}
};
return sendAsync(request, handler);
}
Request.Builder requestBuilder(StandardHttpRequest request) {
okhttp3.Request.Builder requestBuilder = newRequestBuilder();
BodyContent body = request.body();
if (body != null) {
String contentType = request.getContentType();
if (body instanceof StringBodyContent) {
requestBuilder.method(request.method(),
RequestBody.create(OkHttpClientImpl.parseMediaType(contentType), ((StringBodyContent) body).getContent()));
} else if (body instanceof ByteArrayBodyContent) {
requestBuilder.method(request.method(),
RequestBody.create(OkHttpClientImpl.parseMediaType(contentType), ((ByteArrayBodyContent) body).getContent()));
} else if (body instanceof InputStreamBodyContent) {
InputStreamBodyContent bodyContent = (InputStreamBodyContent) body;
requestBuilder.method(request.method(), new RequestBody() {
@Override
public void writeTo(BufferedSink sink) throws IOException {
try (final BufferedInputStream bis = new BufferedInputStream(bodyContent.getContent());
final Source source = Okio.source(bis)) {
sink.writeAll(source);
}
}
@Override
public MediaType contentType() {
return OkHttpClientImpl.parseMediaType(contentType);
}
@Override
public long contentLength() throws IOException {
return bodyContent.getLength();
}
});
} else {
throw new AssertionError("Unsupported body content");
}
}
request.headers().entrySet().stream()
.forEach(e -> e.getValue().stream().forEach(v -> requestBuilder.addHeader(e.getKey(), v)));
try {
requestBuilder.url(request.uri().toURL());
} catch (MalformedURLException e) {
throw new IllegalArgumentException(request.uri().toString(), e);
}
if (request.isExpectContinue()) {
requestBuilder.header(StandardHttpHeaders.EXPECT, StandardHttpHeaders.EXPECT_CONTINUE);
}
return requestBuilder;
}
@Override
public CompletableFuture<WebSocketResponse> buildWebSocketDirect(StandardWebSocketBuilder standardWebSocketBuilder,
Listener listener) {
Request.Builder requestBuilder = requestBuilder(standardWebSocketBuilder.asHttpRequest());
if (standardWebSocketBuilder.getSubprotocol() != null) {
requestBuilder.header("Sec-WebSocket-Protocol", standardWebSocketBuilder.getSubprotocol());
}
return OkHttpWebSocketImpl.buildAsync(httpClient, requestBuilder.build(), listener);
}
}