package examples;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.cli.Argument;
import io.vertx.core.cli.CLI;
import io.vertx.core.cli.CommandLine;
import io.vertx.core.cli.Option;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.JksOptions;
import io.vertx.ext.shell.Shell;
import io.vertx.ext.shell.ShellServer;
import io.vertx.ext.shell.ShellService;
import io.vertx.ext.shell.ShellServiceOptions;
import io.vertx.ext.shell.command.CommandBuilder;
import io.vertx.ext.shell.command.CommandRegistry;
import io.vertx.ext.shell.command.CommandResolver;
import io.vertx.ext.shell.session.Session;
import io.vertx.ext.shell.system.Job;
import io.vertx.ext.shell.term.*;
import io.vertx.ext.web.Router;
public class ShellExamples {
public void deployTelnetService(Vertx vertx) throws Exception {
vertx.deployVerticle("maven:{maven-groupId}:{maven-artifactId}:{maven-version}",
new DeploymentOptions().setConfig(
new JsonObject().put("telnetOptions",
new JsonObject().
put("host", "localhost").
put("port", 4000))
)
);
}
public void deploySSHServiceWithShiro(Vertx vertx) throws Exception {
vertx.deployVerticle("maven:{maven-groupId}:{maven-artifactId}:{maven-version}",
new DeploymentOptions().setConfig(new JsonObject().
put("sshOptions", new JsonObject().
put("host", "localhost").
put("port", 5000).
put("keyPairOptions", new JsonObject().
put("path", "src/test/resources/ssh.jks").
put("password", "wibble")).
put("authOptions", new JsonObject().
put("provider", "shiro").
put("config", new JsonObject().
put("properties_path", "file:/path/to/my/auth.properties"))))
)
);
}
public void deploySSHServiceWithJDBC(Vertx vertx) throws Exception {
vertx.deployVerticle("maven:{maven-groupId}:{maven-artifactId}:{maven-version}",
new DeploymentOptions().setConfig(new JsonObject().
put("sshOptions", new JsonObject().
put("host", "localhost").
put("port", 5000).
put("keyPairOptions", new JsonObject().
put("path", "src/test/resources/ssh.jks").
put("password", "wibble")).
put("authOptions", new JsonObject().
put("provider", "jdbc").
put("config", new JsonObject()
.put("url", "jdbc:hsqldb:mem:test?shutdown=true")
.put("driver_class", "org.hsqldb.jdbcDriver"))))
)
);
}
public void deploySSHServiceWithMongo(Vertx vertx) throws Exception {
vertx.deployVerticle("maven:{maven-groupId}:{maven-artifactId}:{maven-version}",
new DeploymentOptions().setConfig(new JsonObject().
put("sshOptions", new JsonObject().
put("host", "localhost").
put("port", 5000).
put("keyPairOptions", new JsonObject().
put("path", "src/test/resources/ssh.jks").
put("password", "wibble")).
put("authOptions", new JsonObject().
put("provider", "mongo").
put("config", new JsonObject().
put("connection_string", "mongodb://localhost:27018"))))
)
);
}
public void deployHttpServiceWithShiro(Vertx vertx) throws Exception {
vertx.deployVerticle("maven:{maven-groupId}:{maven-artifactId}:{maven-version}",
new DeploymentOptions().setConfig(new JsonObject().
put("httpOptions", new JsonObject().
put("host", "localhost").
put("port", 8080).
put("ssl", true).
put("keyPairOptions", new JsonObject().
put("path", "src/test/resources/server-keystore.jks").
put("password", "wibble")).
put("authOptions", new JsonObject().
put("provider", "shiro").
put("config", new JsonObject().
put("properties_path", "file:/path/to/my/auth.properties"))))
)
);
}
public void deployHttpServiceWithJDBC(Vertx vertx) throws Exception {
vertx.deployVerticle("maven:{maven-groupId}:{maven-artifactId}:{maven-version}",
new DeploymentOptions().setConfig(new JsonObject().
put("httpOptions", new JsonObject().
put("host", "localhost").
put("port", 8080).
put("ssl", true).
put("keyPairOptions", new JsonObject().
put("path", "src/test/resources/server-keystore.jks").
put("password", "wibble")).
put("authOptions", new JsonObject().
put("provider", "jdbc").
put("config", new JsonObject()
.put("url", "jdbc:hsqldb:mem:test?shutdown=true")
.put("driver_class", "org.hsqldb.jdbcDriver"))))
)
);
}
public void deployHttpServiceWithMongo(Vertx vertx) throws Exception {
vertx.deployVerticle("maven:{maven-groupId}:{maven-artifactId}:{maven-version}",
new DeploymentOptions().setConfig(new JsonObject().
put("httpOptions", new JsonObject().
put("host", "localhost").
put("port", 8080).
put("ssl", true).
put("keyPairOptions", new JsonObject().
put("path", "src/test/resources/server-keystore.jks").
put("password", "wibble")).
put("authOptions", new JsonObject().
put("provider", "mongo").
put("config", new JsonObject().
put("connection_string", "mongodb://localhost:27018"))))
)
);
}
public void runTelnetService(Vertx vertx) throws Exception {
ShellService service = ShellService.create(vertx,
new ShellServiceOptions().setTelnetOptions(
new TelnetTermOptions().
setHost("localhost").
setPort(4000)
)
);
service.start();
}
public void runSSHServiceWithShiro(Vertx vertx) throws Exception {
ShellService service = ShellService.create(vertx,
new ShellServiceOptions().setSSHOptions(
new SSHTermOptions().
setHost("localhost").
setPort(5000).
setKeyPairOptions(new JksOptions().
setPath("server-keystore.jks").
setPassword("wibble")
).
setAuthOptions(
new JsonObject()
.put("provider", "shiro")
.put("type", "PROPERTIES")
.put("config", new JsonObject().
put("properties_path", "file:/path/to/my/auth.properties"))
)
)
);
service.start();
}
public void runSSHServiceWithMongo(Vertx vertx) throws Exception {
ShellService service = ShellService.create(vertx,
new ShellServiceOptions().setSSHOptions(
new SSHTermOptions().
setHost("localhost").
setPort(5000).
setKeyPairOptions(new JksOptions().
setPath("server-keystore.jks").
setPassword("wibble")
).
setAuthOptions(new JsonObject()
.put("provider", "mongo")
.put("config", new JsonObject().put("connection_string", "mongodb://localhost:27018"))
)
)
);
service.start();
}
public void runSSHServiceWithJDBC(Vertx vertx) throws Exception {
ShellService service = ShellService.create(vertx,
new ShellServiceOptions().setSSHOptions(
new SSHTermOptions().
setHost("localhost").
setPort(5000).
setKeyPairOptions(new JksOptions().
setPath("server-keystore.jks").
setPassword("wibble")
).
setAuthOptions(new JsonObject()
.put("provider", "jdbc")
.put("config", new JsonObject()
.put("url", "jdbc:hsqldb:mem:test?shutdown=true")
.put("driver_class", "org.hsqldb.jdbcDriver"))
)
)
);
service.start();
}
public void runHttpService(Vertx vertx) throws Exception {
ShellService service = ShellService.create(vertx,
new ShellServiceOptions().setHttpOptions(
new HttpTermOptions().
setHost("localhost").
setPort(8080)
)
);
service.start();
}
public void runHTTPServiceWithMongo(Vertx vertx) throws Exception {
ShellService service = ShellService.create(vertx,
new ShellServiceOptions().setHttpOptions(
new HttpTermOptions().
setHost("localhost").
setPort(8080).
setAuthOptions(new JsonObject()
.put("provider", "mongo")
.put("config", new JsonObject()
.put("connection_string", "mongodb://localhost:27018"))
)
)
);
service.start();
}
public void runHTTPServiceWithJDBC(Vertx vertx) throws Exception {
ShellService service = ShellService.create(vertx,
new ShellServiceOptions().setHttpOptions(
new HttpTermOptions().
setHost("localhost").
setPort(8080).
setAuthOptions(new JsonObject()
.put("provider", "jdbc")
.put("config", new JsonObject()
.put("url", "jdbc:hsqldb:mem:test?shutdown=true")
.put("driver_class", "org.hsqldb.jdbcDriver"))
)
)
);
service.start();
}
public void shellServer(Vertx vertx, Router router) {
ShellServer server = ShellServer.create(vertx);
Router shellRouter = Router.router(vertx);
router.mountSubRouter("/shell", shellRouter);
TermServer httpTermServer = TermServer.createHttpTermServer(vertx, router);
TermServer sshTermServer = TermServer.createSSHTermServer(vertx);
server.registerTermServer(httpTermServer);
server.registerTermServer(sshTermServer);
server.registerCommandResolver(CommandResolver.baseCommands(vertx));
server.listen();
}
public void helloWorld(Vertx vertx) {
CommandBuilder builder = CommandBuilder.command("my-command");
builder.processHandler(process -> {
process.write("Hello World");
process.end();
});
CommandRegistry registry = CommandRegistry.getShared(vertx);
registry.registerCommand(builder.build(vertx));
}
public void cliCommand() {
CLI cli = CLI.create("my-command").
addArgument(new Argument().setArgName("my-arg")).
addOption(new Option().setShortName("m").setLongName("my-option"));
CommandBuilder command = CommandBuilder.command(cli);
command.processHandler(process -> {
CommandLine commandLine = process.commandLine();
String argValue = commandLine.getArgumentValue(0);
String optValue = commandLine.getOptionValue("my-option");
process.write("The argument is " + argValue + " and the option is " + optValue);
process.end();
});
}
public void cliCommandWithHelp() {
CLI cli = CLI.create("my-command").
addArgument(new Argument().setArgName("my-arg")).
addOption(new Option().setArgName("help").setShortName("h").setLongName("help"));
CommandBuilder command = CommandBuilder.command(cli);
command.processHandler(process -> {
});
}
public void commandArgs(CommandBuilder command) {
command.processHandler(process -> {
for (String arg : process.args()) {
process.write("Argument " + arg);
}
process.end();
});
}
public void session(CommandBuilder command) {
command.processHandler(process -> {
Session session = process.session();
if (session.get("my_key") == null) {
session.put("my key", "my value");
}
process.end();
});
}
public void readStdin(Tty tty) {
tty.stdinHandler(data -> {
System.out.println("Received " + data);
});
}
public void writeStdout(Tty tty) {
tty.write("Hello World");
}
public void terminalSize(Tty tty) {
tty.write("Current terminal size: (" + tty.width() + ", " + tty.height() + ")");
}
public void resizeHandlerTerminal(Tty tty) {
tty.resizehandler(v -> {
System.out.println("terminal resized : " + tty.width() + " " + tty.height());
});
}
public void terminalType(Tty tty) {
System.out.println("terminal type : " + tty.type());
}
public void asyncCommand(CommandBuilder command) {
command.processHandler(process -> {
Vertx vertx = process.vertx();
vertx.setTimer(1000, id -> {
process.end();
});
});
}
public void interruptHandler(CommandBuilder command) {
command.processHandler(process -> {
Vertx vertx = process.vertx();
long periodicId = vertx.setPeriodic(1000, id -> {
process.write("tick\n");
});
process.interruptHandler(v -> {
vertx.cancelTimer(periodicId);
process.end();
});
});
}
public void suspendResumeHandler(CommandBuilder command) {
command.processHandler(process -> {
process.suspendHandler(v -> {
System.out.println("Suspended");
});
process.resumeHandler(v -> {
System.out.println("Resumed");
});
});
}
public void endHandler(CommandBuilder command) {
command.processHandler(process -> {
process.endHandler(v -> {
System.out.println("Terminated");
});
});
}
public void telnetEchoTerminal(Vertx vertx) {
TermServer server = TermServer.createTelnetTermServer(vertx, new TelnetTermOptions().setPort(5000).setHost("localhost"));
server.termHandler(term -> {
term.stdinHandler(line -> {
term.write(line);
});
});
server.listen();
}
public void sshEchoTerminal(Vertx vertx) {
TermServer server = TermServer.createSSHTermServer(vertx, new SSHTermOptions().setPort(5000).setHost("localhost"));
server.termHandler(term -> {
term.stdinHandler(line -> {
term.write(line);
});
});
server.listen();
}
public void httpEchoTerminal(Vertx vertx) {
TermServer server = TermServer.createHttpTermServer(vertx, new HttpTermOptions().setPort(5000).setHost("localhost"));
server.termHandler(term -> {
term.stdinHandler(line -> {
term.write(line);
});
});
server.listen();
}
public void httpEchoTerminalUsingRouter(Vertx vertx, Router router) {
TermServer server = TermServer.createHttpTermServer(vertx, router, new HttpTermOptions().setPort(5000).setHost("localhost"));
server.termHandler(term -> {
term.stdinHandler(line -> {
term.write(line);
});
});
server.listen();
}
public void creatingShell(ShellServer shellServer) {
Shell shell = shellServer.createShell();
}
public void runningShellCommand(ShellServer shellServer) {
Shell shell = shellServer.createShell();
Job job = shell.createJob("my-command 1234");
Pty pty = Pty.create();
pty.stdoutHandler(data -> {
System.out.println("Command wrote " + data);
});
job.setTty(pty.slave());
job.statusUpdateHandler(status -> {
System.out.println("Command terminated with status " + status);
});
}
}