Search in sources :

Example 11 with PemKeyCertOptions

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

the class KeyStoreHelper method create.

public static KeyStoreHelper create(VertxInternal vertx, KeyCertOptions options) {
    if (options instanceof JksOptions) {
        JksOptions jks = (JksOptions) options;
        Supplier<Buffer> value;
        if (jks.getPath() != null) {
            value = () -> vertx.fileSystem().readFileBlocking(vertx.resolveFile(jks.getPath()).getAbsolutePath());
        } else if (jks.getValue() != null) {
            value = jks::getValue;
        } else {
            return null;
        }
        return new JKSOrPKCS12("JKS", jks.getPassword(), value);
    } else if (options instanceof PfxOptions) {
        PfxOptions pkcs12 = (PfxOptions) options;
        Supplier<Buffer> value;
        if (pkcs12.getPath() != null) {
            value = () -> vertx.fileSystem().readFileBlocking(vertx.resolveFile(pkcs12.getPath()).getAbsolutePath());
        } else if (pkcs12.getValue() != null) {
            value = pkcs12::getValue;
        } else {
            return null;
        }
        return new JKSOrPKCS12("PKCS12", pkcs12.getPassword(), value);
    } else if (options instanceof PemKeyCertOptions) {
        PemKeyCertOptions keyCert = (PemKeyCertOptions) options;
        Supplier<Buffer> key = () -> {
            if (keyCert.getKeyPath() != null) {
                return vertx.fileSystem().readFileBlocking(vertx.resolveFile(keyCert.getKeyPath()).getAbsolutePath());
            } else if (keyCert.getKeyValue() != null) {
                return keyCert.getKeyValue();
            } else {
                throw new RuntimeException("Missing private key");
            }
        };
        Supplier<Buffer> cert = () -> {
            if (keyCert.getCertPath() != null) {
                return vertx.fileSystem().readFileBlocking(vertx.resolveFile(keyCert.getCertPath()).getAbsolutePath());
            } else if (keyCert.getCertValue() != null) {
                return keyCert.getCertValue();
            } else {
                throw new RuntimeException("Missing X.509 certificate");
            }
        };
        return new KeyCert(DUMMY_PASSWORD, key, cert);
    } else {
        return null;
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) JksOptions(io.vertx.core.net.JksOptions) Supplier(java.util.function.Supplier) PfxOptions(io.vertx.core.net.PfxOptions)

Example 12 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    MqttServerOptions options = new MqttServerOptions().setPort(8883).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")).setSsl(true);
    MqttServer mqttServer = MqttServer.create(vertx, options);
    mqttServer.endpointHandler(endpoint -> {
        // shows main connect info
        System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, " + "clean session = " + endpoint.isCleanSession());
        // accept connection from the remote client
        endpoint.accept(false);
    }).listen(ar -> {
        if (ar.succeeded()) {
            System.out.println("MQTT server is listening on port " + mqttServer.actualPort());
        } else {
            System.err.println("Error on starting the server" + ar.cause().getMessage());
        }
    });
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) MqttServerOptions(io.vertx.mqtt.MqttServerOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) Runner(io.vertx.example.mqtt.util.Runner) MqttServer(io.vertx.mqtt.MqttServer) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) MqttServerOptions(io.vertx.mqtt.MqttServerOptions) MqttServer(io.vertx.mqtt.MqttServer)

Example 13 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    final Image image = new Image(vertx, "coin.png");
    Router router = Router.router(vertx);
    router.get("/").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "text/html").end(image.generateHTML(16));
    });
    router.get("/img/:x/:y").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "image/png").end(image.getPixel(Integer.parseInt(ctx.pathParam("x")), Integer.parseInt(ctx.pathParam("y"))));
    });
    vertx.createHttpServer(new HttpServerOptions().setSsl(true).setUseAlpn(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem"))).requestHandler(router::accept).listen(8443);
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Router(io.vertx.ext.web.Router)

Example 14 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")));
    server.requestHandler(req -> {
        String path = req.path();
        HttpServerResponse resp = req.response();
        if ("/".equals(path)) {
            resp.push(HttpMethod.GET, "/script.js", ar -> {
                if (ar.succeeded()) {
                    System.out.println("sending push");
                    HttpServerResponse pushedResp = ar.result();
                    pushedResp.sendFile("script.js");
                } else {
                // Sometimes Safari forbids push : "Server push not allowed to opposite endpoint."
                }
            });
            resp.sendFile("index.html");
        } else if ("/script.js".equals(path)) {
            resp.sendFile("script.js");
        } else {
            System.out.println("Not found " + path);
            resp.setStatusCode(404).end();
        }
    });
    server.listen(8443, "localhost", ar -> {
        if (ar.succeeded()) {
            System.out.println("Server started");
        } else {
            ar.cause().printStackTrace();
        }
    });
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Example 15 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")));
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        req.customFrameHandler(frame -> {
            System.out.println("Received client frame " + frame.payload().toString("UTF-8"));
            // Write the sam
            resp.writeCustomFrame(10, 0, Buffer.buffer("pong"));
        });
    }).listen(8443);
}
Also used : OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Runner(io.vertx.example.util.Runner) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Aggregations

PemKeyCertOptions (io.vertx.core.net.PemKeyCertOptions)22 Test (org.junit.Test)10 HttpServerOptions (io.vertx.core.http.HttpServerOptions)9 HttpServer (io.vertx.core.http.HttpServer)7 AbstractVerticle (io.vertx.core.AbstractVerticle)4 Buffer (io.vertx.core.buffer.Buffer)4 PfxOptions (io.vertx.core.net.PfxOptions)4 Router (io.vertx.ext.web.Router)4 HttpServerResponse (io.vertx.core.http.HttpServerResponse)3 JsonObject (io.vertx.core.json.JsonObject)3 JksOptions (io.vertx.core.net.JksOptions)3 HttpClient (io.vertx.core.http.HttpClient)2 HttpClientOptions (io.vertx.core.http.HttpClientOptions)2 HttpClientRequest (io.vertx.core.http.HttpClientRequest)2 HttpMethod (io.vertx.core.http.HttpMethod)2 HttpVersion (io.vertx.core.http.HttpVersion)2 JsonArray (io.vertx.core.json.JsonArray)2 OpenSSLEngineOptions (io.vertx.core.net.OpenSSLEngineOptions)2 PemTrustOptions (io.vertx.core.net.PemTrustOptions)2 KeyStoreHelper (io.vertx.core.net.impl.KeyStoreHelper)2