Search in sources :

Example 1 with WebSocketHandshakeException

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

the class JdkHttpClientImpl method internalBuildAsync.

/**
 * Convert the invocation of a JDK build async into a holder of both the exception and the response
 */
public CompletableFuture<WebSocketResponse> internalBuildAsync(JdkWebSocketImpl.BuilderImpl webSocketBuilder, Listener listener) {
    java.net.http.HttpRequest request = webSocketBuilder.asRequest();
    java.net.http.WebSocket.Builder newBuilder = this.httpClient.newWebSocketBuilder();
    request.headers().map().forEach((k, v) -> v.forEach(s -> newBuilder.header(k, s)));
    if (webSocketBuilder.subprotocol != null) {
        newBuilder.subprotocols(webSocketBuilder.subprotocol);
    }
    // TODO: this should probably be made clearer in the docs
    if (this.builder.readTimeout != null) {
        newBuilder.connectTimeout(this.builder.readTimeout);
    }
    AtomicLong queueSize = new AtomicLong();
    // use a responseholder to convey both the exception and the websocket
    CompletableFuture<WebSocketResponse> response = new CompletableFuture<>();
    URI uri = request.uri();
    if (uri.getScheme().startsWith("http")) {
        // the jdk logic expects a ws uri
        // after the https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8245245 it just does the reverse of this
        // to convert back to http(s) ...
        uri = URI.create("ws" + uri.toString().substring(4));
    }
    newBuilder.buildAsync(uri, new JdkWebSocketImpl.ListenerAdapter(listener, queueSize)).whenComplete((w, t) -> {
        if (t instanceof CompletionException && t.getCause() != null) {
            t = t.getCause();
        }
        if (t instanceof java.net.http.WebSocketHandshakeException) {
            response.complete(new WebSocketResponse(new JdkWebSocketImpl(queueSize, w), (WebSocketHandshakeException) t));
        } else if (t != null) {
            response.completeExceptionally(t);
        } else {
            response.complete(new WebSocketResponse(new JdkWebSocketImpl(queueSize, w), null));
        }
    });
    return response;
}
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) URI(java.net.URI) WebSocket(io.fabric8.kubernetes.client.http.WebSocket) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) WebSocketHandshakeException(java.net.http.WebSocketHandshakeException)

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