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