Search in sources :

Example 1 with UpgradeRejectedException

use of io.vertx.core.http.UpgradeRejectedException in project vert.x by eclipse.

the class WebSocketHandshakeInboundHandler method handshakeComplete.

private Future<HeadersAdaptor> handshakeComplete(FullHttpResponse response) {
    int sc = response.status().code();
    if (sc != 101) {
        String msg = "WebSocket upgrade failure: " + sc;
        ByteBuf content = response.content();
        if (content != null && content.readableBytes() > 0) {
            msg += " (" + content.toString(StandardCharsets.UTF_8) + ")";
        }
        UpgradeRejectedException failure = new UpgradeRejectedException(msg, sc);
        return Future.failedFuture(failure);
    } else {
        try {
            handshaker.finishHandshake(chctx.channel(), response);
            return Future.succeededFuture(new HeadersAdaptor(response.headers()));
        } catch (WebSocketHandshakeException e) {
            return Future.failedFuture(e);
        }
    }
}
Also used : HeadersAdaptor(io.vertx.core.http.impl.headers.HeadersAdaptor) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) ByteBuf(io.netty.buffer.ByteBuf) UpgradeRejectedException(io.vertx.core.http.UpgradeRejectedException)

Example 2 with UpgradeRejectedException

use of io.vertx.core.http.UpgradeRejectedException in project ksql by confluentinc.

the class WebsocketUtils method makeWsRequest.

static void makeWsRequest(String request, Map<String, String> clientProps, MultiMap headers, TestKsqlRestApp restApp, boolean tls) throws SSLHandshakeException, UpgradeRejectedException {
    Vertx vertx = Vertx.vertx();
    io.vertx.core.http.HttpClient httpClient = null;
    try {
        HttpClientOptions httpClientOptions = new HttpClientOptions();
        if (tls) {
            httpClientOptions.setSsl(true).setVerifyHost(false);
            final Optional<JksOptions> trustStoreOptions = VertxSslOptionsFactory.getJksTrustStoreOptions(clientProps);
            final String alias = clientProps.get(KsqlClient.SSL_KEYSTORE_ALIAS_CONFIG);
            final Optional<JksOptions> keyStoreOptions = VertxSslOptionsFactory.buildJksKeyStoreOptions(clientProps, Optional.ofNullable(alias));
            trustStoreOptions.ifPresent(options -> httpClientOptions.setTrustStoreOptions(options));
            keyStoreOptions.ifPresent(options -> httpClientOptions.setKeyStoreOptions(options));
        }
        httpClient = vertx.createHttpClient(httpClientOptions);
        URI listener = tls ? restApp.getWssListener() : restApp.getWsListener();
        final URI uri = listener.resolve("/ws/query?request=" + request);
        CompletableFuture<Void> completableFuture = new CompletableFuture<>();
        httpClient.webSocketAbs(uri.toString(), headers, WebsocketVersion.V07, Collections.emptyList(), ar -> {
            if (ar.succeeded()) {
                ar.result().frameHandler(frame -> completableFuture.complete(null));
                ar.result().exceptionHandler(completableFuture::completeExceptionally);
            } else {
                completableFuture.completeExceptionally(ar.cause());
            }
        });
        completableFuture.get(30, TimeUnit.SECONDS);
    } catch (Exception e) {
        if (e instanceof ExecutionException) {
            if (e.getCause() instanceof SSLHandshakeException) {
                throw (SSLHandshakeException) e.getCause();
            } else if (e.getCause() instanceof UpgradeRejectedException) {
                throw (UpgradeRejectedException) e.getCause();
            } else {
                throw new RuntimeException(e);
            }
        } else {
            throw new RuntimeException(e);
        }
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
        vertx.close();
    }
}
Also used : Vertx(io.vertx.core.Vertx) URI(java.net.URI) UpgradeRejectedException(io.vertx.core.http.UpgradeRejectedException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ExecutionException(java.util.concurrent.ExecutionException) UpgradeRejectedException(io.vertx.core.http.UpgradeRejectedException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) CompletableFuture(java.util.concurrent.CompletableFuture) JksOptions(io.vertx.core.net.JksOptions) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with UpgradeRejectedException

use of io.vertx.core.http.UpgradeRejectedException in project ksql by confluentinc.

the class BasicAuthFunctionalTest method makeWsRequest.

private static int makeWsRequest(final Optional<BasicCredentials> creds) throws Exception {
    JsonObject request = new JsonObject().put("ksql", "SELECT * FROM KSQL_PROCESSING_LOG EMIT CHANGES;");
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    creds.ifPresent(c -> {
        final String authHeader = "Basic " + buildBasicAuthHeader(c.username(), c.password());
        headers.add(AUTHORIZATION.toString(), authHeader);
    });
    try {
        String escaped = UrlEscapers.urlFormParameterEscaper().escape(request.toString());
        WebsocketUtils.makeWsRequest(escaped, Collections.emptyMap(), headers, REST_APP, false);
    } catch (UpgradeRejectedException e) {
        return e.getStatus();
    }
    return OK.code();
}
Also used : MultiMap(io.vertx.core.MultiMap) JsonObject(io.vertx.core.json.JsonObject) UpgradeRejectedException(io.vertx.core.http.UpgradeRejectedException)

Aggregations

UpgradeRejectedException (io.vertx.core.http.UpgradeRejectedException)3 ByteBuf (io.netty.buffer.ByteBuf)1 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 MultiMap (io.vertx.core.MultiMap)1 Vertx (io.vertx.core.Vertx)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HeadersAdaptor (io.vertx.core.http.impl.headers.HeadersAdaptor)1 JsonObject (io.vertx.core.json.JsonObject)1 JksOptions (io.vertx.core.net.JksOptions)1 URI (java.net.URI)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1