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;
}
Aggregations