Search in sources :

Example 1 with BodySubscriber

use of java.net.http.HttpResponse.BodySubscriber in project kubernetes-client by fabric8io.

the class JdkHttpClientImpl method sendAsync.

@Override
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Class<T> type) {
    JdkHttpRequestImpl jdkRequest = (JdkHttpRequestImpl) request;
    JdkHttpRequestImpl.BuilderImpl builderImpl = jdkRequest.newBuilder();
    for (Interceptor interceptor : builder.interceptors.values()) {
        interceptor.before(builderImpl, jdkRequest);
        jdkRequest = builderImpl.build();
    }
    BodyHandler<T> bodyHandler;
    if (type == null) {
        bodyHandler = (BodyHandler<T>) BodyHandlers.discarding();
    } else if (type == InputStream.class) {
        bodyHandler = (BodyHandler<T>) BodyHandlers.ofInputStream();
    } else if (type == String.class) {
        bodyHandler = (BodyHandler<T>) BodyHandlers.ofString();
    } else {
        bodyHandler = responseInfo -> {
            BodySubscriber<InputStream> upstream = BodyHandlers.ofInputStream().apply(responseInfo);
            BodySubscriber<Reader> downstream = BodySubscribers.mapping(upstream, (InputStream is) -> new InputStreamReader(is, StandardCharsets.UTF_8));
            return (BodySubscriber<T>) downstream;
        };
    }
    CompletableFuture<java.net.http.HttpResponse<T>> cf = this.httpClient.sendAsync(builderImpl.build().request, bodyHandler);
    for (Interceptor interceptor : builder.interceptors.values()) {
        cf = cf.thenCompose(response -> {
            if (response != null && !HttpResponse.isSuccessful(response.statusCode()) && interceptor.afterFailure(builderImpl, new JdkHttpResponseImpl<>(response))) {
                return this.httpClient.sendAsync(builderImpl.build().request, bodyHandler);
            }
            return CompletableFuture.completedFuture(response);
        });
    }
    return cf.thenApply(JdkHttpResponseImpl::new);
}
Also used : BodyHandlers(java.net.http.HttpResponse.BodyHandlers) Listener(io.fabric8.kubernetes.client.http.WebSocket.Listener) HttpRequest(io.fabric8.kubernetes.client.http.HttpRequest) BodySubscribers(java.net.http.HttpResponse.BodySubscribers) BodyHandler(java.net.http.HttpResponse.BodyHandler) BodySubscriber(java.net.http.HttpResponse.BodySubscriber) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) HttpResponse(io.fabric8.kubernetes.client.http.HttpResponse) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) InterruptedIOException(java.io.InterruptedIOException) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Interceptor(io.fabric8.kubernetes.client.http.Interceptor) WebSocketHandshakeException(java.net.http.WebSocketHandshakeException) Optional(java.util.Optional) WebSocket(io.fabric8.kubernetes.client.http.WebSocket) URI(java.net.URI) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) InputStream(java.io.InputStream) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) HttpResponse(io.fabric8.kubernetes.client.http.HttpResponse) BodySubscriber(java.net.http.HttpResponse.BodySubscriber) BodyHandler(java.net.http.HttpResponse.BodyHandler) Interceptor(io.fabric8.kubernetes.client.http.Interceptor)

Aggregations

HttpClient (io.fabric8.kubernetes.client.http.HttpClient)1 HttpRequest (io.fabric8.kubernetes.client.http.HttpRequest)1 HttpResponse (io.fabric8.kubernetes.client.http.HttpResponse)1 Interceptor (io.fabric8.kubernetes.client.http.Interceptor)1 WebSocket (io.fabric8.kubernetes.client.http.WebSocket)1 Listener (io.fabric8.kubernetes.client.http.WebSocket.Listener)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 InterruptedIOException (java.io.InterruptedIOException)1 Reader (java.io.Reader)1 URI (java.net.URI)1 BodyHandler (java.net.http.HttpResponse.BodyHandler)1 BodyHandlers (java.net.http.HttpResponse.BodyHandlers)1 BodySubscriber (java.net.http.HttpResponse.BodySubscriber)1 BodySubscribers (java.net.http.HttpResponse.BodySubscribers)1 WebSocketHandshakeException (java.net.http.WebSocketHandshakeException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1 Optional (java.util.Optional)1