Search in sources :

Example 1 with VertxException

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

the class HAManager method processFailover.

// Process the failover of a deployment
private void processFailover(JsonObject failedVerticle) {
    if (failDuringFailover) {
        throw new VertxException("Oops!");
    }
    // This method must block until the failover is complete - i.e. the verticle is successfully redeployed
    final String verticleName = failedVerticle.getString("verticle_name");
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> err = new AtomicReference<>();
    // Now deploy this verticle on this node
    ContextImpl ctx = vertx.getContext();
    if (ctx != null) {
        // We could be on main thread in which case we don't want to overwrite tccl
        ContextImpl.setContext(null);
    }
    JsonObject options = failedVerticle.getJsonObject("options");
    try {
        doDeployVerticle(verticleName, new DeploymentOptions(options), result -> {
            if (result.succeeded()) {
                log.info("Successfully redeployed verticle " + verticleName + " after failover");
            } else {
                log.error("Failed to redeploy verticle after failover", result.cause());
                err.set(result.cause());
            }
            latch.countDown();
            Throwable t = err.get();
            if (t != null) {
                throw new VertxException(t);
            }
        });
    } finally {
        if (ctx != null) {
            ContextImpl.setContext(ctx);
        }
    }
    try {
        if (!latch.await(120, TimeUnit.SECONDS)) {
            throw new VertxException("Timed out waiting for redeploy on failover");
        }
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) VertxException(io.vertx.core.VertxException) JsonObject(io.vertx.core.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with VertxException

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

the class FileResolver method unpackFromBundleURL.

/**
   * bundle:// urls are used by OSGi implementations to refer to a file contained in a bundle, or in a fragment. There
   * is not much we can do to get the file from it, except reading it from the url. This method copies the files by
   * reading it from the url.
   *
   * @param url      the url
   * @return the extracted file
   */
private synchronized File unpackFromBundleURL(URL url) {
    try {
        File file = new File(cacheDir, url.getHost() + File.separator + url.getFile());
        file.getParentFile().mkdirs();
        if (url.toExternalForm().endsWith("/")) {
            // Directory
            file.mkdirs();
        } else {
            file.getParentFile().mkdirs();
            try (InputStream is = url.openStream()) {
                if (ENABLE_CACHING) {
                    Files.copy(is, file.toPath());
                } else {
                    Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
                }
            } catch (FileAlreadyExistsException ignore) {
            }
        }
    } catch (IOException e) {
        throw new VertxException(e);
    }
    return new File(cacheDir, url.getHost() + File.separator + url.getFile());
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InputStream(java.io.InputStream) VertxException(io.vertx.core.VertxException) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 3 with VertxException

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

the class ClientConnection method handleClosed.

protected synchronized void handleClosed() {
    super.handleClosed();
    if (ws != null) {
        ws.handleClosed();
    }
    Exception e = new VertxException("Connection was closed");
    // Signal requests failed
    if (metrics.isEnabled()) {
        for (HttpClientRequestImpl req : requests) {
            metrics.requestReset(req.metric());
        }
        if (currentResponse != null) {
            metrics.requestReset(currentResponse.request().metric());
        }
    }
    // Connection was closed - call exception handlers for any requests in the pipeline or one being currently written
    for (HttpClientRequestImpl req : requests) {
        req.handleException(e);
    }
    if (currentRequest != null) {
        currentRequest.handleException(e);
    } else if (currentResponse != null) {
        currentResponse.handleException(e);
    }
    // The connection has been closed - tell the pool about it, this allows the pool to create more
    // connections. Note the pool doesn't actually remove the connection, when the next person to get a connection
    // gets the closed on, they will check if it's closed and if so get another one.
    pool.connectionClosed(this);
}
Also used : VertxException(io.vertx.core.VertxException) VertxException(io.vertx.core.VertxException)

Example 4 with VertxException

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

the class SSLHelper method createContext.

/*
    If you don't specify a trust store, and you haven't set system properties, the system will try to use either a file
    called jsssecacerts or cacerts in the JDK/JRE security directory.
    You can override this by specifying the javax.echo.ssl.trustStore system property

    If you don't specify a key store, and don't specify a system property no key store will be used
    You can override this by specifying the javax.echo.ssl.keyStore system property
     */
private SslContext createContext(VertxInternal vertx) {
    try {
        KeyManagerFactory keyMgrFactory = getKeyMgrFactory(vertx);
        TrustManagerFactory trustMgrFactory = getTrustMgrFactory(vertx);
        SslContextBuilder builder;
        if (client) {
            builder = SslContextBuilder.forClient();
            if (keyMgrFactory != null) {
                builder.keyManager(keyMgrFactory);
            }
        } else {
            if (keyMgrFactory == null) {
                throw new VertxException("Key/certificate is mandatory for SSL");
            }
            builder = SslContextBuilder.forServer(keyMgrFactory);
        }
        Collection<String> cipherSuites = enabledCipherSuites;
        if (openSsl) {
            builder.sslProvider(SslProvider.OPENSSL);
            if (cipherSuites == null || cipherSuites.isEmpty()) {
                cipherSuites = OpenSsl.availableOpenSslCipherSuites();
            }
        } else {
            builder.sslProvider(SslProvider.JDK);
            if (cipherSuites == null || cipherSuites.isEmpty()) {
                cipherSuites = DEFAULT_JDK_CIPHER_SUITE;
            }
        }
        if (trustMgrFactory != null) {
            builder.trustManager(trustMgrFactory);
        }
        if (cipherSuites != null && cipherSuites.size() > 0) {
            builder.ciphers(cipherSuites);
        }
        if (useAlpn && applicationProtocols != null && applicationProtocols.size() > 0) {
            builder.applicationProtocolConfig(new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN, ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE, ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT, applicationProtocols.stream().map(PROTOCOL_NAME_MAPPING::get).collect(Collectors.toList())));
        }
        return builder.build();
    } catch (Exception e) {
        throw new VertxException(e);
    }
}
Also used : VertxException(io.vertx.core.VertxException) SimpleTrustManagerFactory(io.netty.handler.ssl.util.SimpleTrustManagerFactory) VertxException(io.vertx.core.VertxException) CertificateException(java.security.cert.CertificateException)

Example 5 with VertxException

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

the class SSLEngineTest method doTest.

private void doTest(SSLEngineOptions engine, boolean useAlpn, HttpVersion version, String error, String expectedSslContext, boolean expectCause) {
    server.close();
    HttpServerOptions options = new HttpServerOptions().setSslEngineOptions(engine).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setKeyCertOptions(Cert.SERVER_PEM.get()).setSsl(true).setUseAlpn(useAlpn);
    try {
        server = vertx.createHttpServer(options);
    } catch (VertxException e) {
        e.printStackTrace();
        if (error == null) {
            fail(e);
        } else {
            assertEquals(error, e.getMessage());
            if (expectCause) {
                assertNotSame(e, e.getCause());
            }
        }
        return;
    }
    server.requestHandler(req -> {
        assertEquals(req.version(), version);
        assertTrue(req.isSSL());
        req.response().end();
    });
    server.listen(onSuccess(s -> {
        HttpServerImpl impl = (HttpServerImpl) s;
        SSLHelper sslHelper = impl.getSslHelper();
        SslContext ctx = sslHelper.getContext((VertxInternal) vertx);
        switch(expectedSslContext) {
            case "jdk":
                assertTrue(ctx instanceof JdkSslContext);
                break;
            case "openssl":
                assertTrue(ctx instanceof OpenSslContext);
                break;
        }
        client = vertx.createHttpClient(new HttpClientOptions().setSslEngineOptions(engine).setSsl(true).setUseAlpn(useAlpn).setTrustAll(true).setProtocolVersion(version));
        client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            assertEquals(200, resp.statusCode());
            testComplete();
        });
    }));
    await();
}
Also used : VertxException(io.vertx.core.VertxException) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) SSLEngineOptions(io.vertx.core.net.SSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) Test(org.junit.Test) Cert(io.vertx.test.core.tls.Cert) SSLHelper(io.vertx.core.net.impl.SSLHelper) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpTestBase(io.vertx.test.core.HttpTestBase) HttpVersion(io.vertx.core.http.HttpVersion) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) SSLHelper(io.vertx.core.net.impl.SSLHelper) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) VertxException(io.vertx.core.VertxException) OpenSslContext(io.netty.handler.ssl.OpenSslContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext)

Aggregations

VertxException (io.vertx.core.VertxException)10 File (java.io.File)3 IOException (java.io.IOException)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 ZipFile (java.util.zip.ZipFile)3 Http2Exception (io.netty.handler.codec.http2.Http2Exception)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 WebSocketServerHandshaker (io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker)1 WebSocketServerHandshakerFactory (io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)1 Http2Stream (io.netty.handler.codec.http2.Http2Stream)1 JdkSslContext (io.netty.handler.ssl.JdkSslContext)1 OpenSslContext (io.netty.handler.ssl.OpenSslContext)1 SslContext (io.netty.handler.ssl.SslContext)1 SimpleTrustManagerFactory (io.netty.handler.ssl.util.SimpleTrustManagerFactory)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 VertxOptions (io.vertx.core.VertxOptions)1 AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1