From 359edfd6dd5ed2687bc34c6bc3bbbbb69add2425 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 18 Nov 2008 02:01:29 +0100 Subject: [PATCH] apps/cli: add ls-auth tool --- .../main/java/remote/apps/cli/Authenticator.java | 59 +++++++++++++++---- apps/cli/src/main/java/remote/apps/cli/LsAuth.java | 66 ++++++++++++++++++++++ .../META-INF/services/remote.apps.cli.Command | 1 + apps/cli/src/site/apt/index.apt | 33 +++++++++-- 4 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 apps/cli/src/main/java/remote/apps/cli/LsAuth.java diff --git a/apps/cli/src/main/java/remote/apps/cli/Authenticator.java b/apps/cli/src/main/java/remote/apps/cli/Authenticator.java index 473ecfe..cb93cd9 100644 --- a/apps/cli/src/main/java/remote/apps/cli/Authenticator.java +++ b/apps/cli/src/main/java/remote/apps/cli/Authenticator.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintStream; import remote.client.authentication.AuthenticationCallback; import remote.client.authentication.AuthenticationCallbackUnsupportedException; import remote.client.authentication.AuthenticationHandler; @@ -18,37 +19,71 @@ import remote.client.spi.service.AuthenticationService; class Authenticator implements AuthenticationHandler, ServiceEventListener { - private final BufferedReader stdin; private final ServiceManager manager; + private final Visitor visitor; - private Authenticator(ServiceManager manager, InputStream input) + protected Authenticator(ServiceManager manager, Visitor visitor) { this.manager = manager; - stdin = new BufferedReader(new InputStreamReader(input)); + this.visitor = visitor; + manager.addEventListener(this); } - static Authenticator create(ServiceManager manager, InputStream input) + static Authenticator attach(ServiceManager manager, Visitor visitor) { - return new Authenticator(manager, input); + return new Authenticator(manager, visitor); } - public void authenticate(AuthenticationCallback[] callbacks) - throws AuthenticationCallbackUnsupportedException - { - for (AuthenticationCallback callback : callbacks) { - System.out.print(callback.getText() + ": "); + interface Visitor { + + String visit(AuthenticationCallback callback); + + void complete(); + + } + + static class StdinVisitor implements Visitor { + + private final BufferedReader stdin; + private final PrintStream stdout; + + protected StdinVisitor(InputStream input, PrintStream output) + { + stdout = output; + stdin = new BufferedReader(new InputStreamReader(input)); + } + + public String visit(AuthenticationCallback callback) + { + stdout.print(callback.getText() + ": "); String input = null; try { input = stdin.readLine(); } catch (IOException ex) { - return; + return null; } + return input; + } + + public void complete() + { + } + + } + public void authenticate(AuthenticationCallback[] callbacks) + throws AuthenticationCallbackUnsupportedException + { + for (AuthenticationCallback callback : callbacks) { + String input = visitor.visit(callback); + if (input == null) + continue; if (callback instanceof AuthenticationNameCallback) ((AuthenticationNameCallback) callback).setName(input); else if (callback instanceof AuthenticationPasswordCallback) ((AuthenticationPasswordCallback) callback).setPassword(input); } + visitor.complete(); } private void authenticate() @@ -74,7 +109,7 @@ class Authenticator implements AuthenticationHandler, ServiceEventListener { authenticate(this). send(callback); } - + public void onEvent(Class source, T event) { if (event instanceof ServiceSession.Event.Init) diff --git a/apps/cli/src/main/java/remote/apps/cli/LsAuth.java b/apps/cli/src/main/java/remote/apps/cli/LsAuth.java new file mode 100644 index 0000000..39ca074 --- /dev/null +++ b/apps/cli/src/main/java/remote/apps/cli/LsAuth.java @@ -0,0 +1,66 @@ +package remote.apps.cli; + +import java.net.MalformedURLException; +import java.net.URL; +import org.kohsuke.args4j.Argument; +import remote.client.authentication.AuthenticationCallback; + +/** + * A command for listing authentication information. + */ +public class LsAuth extends Command { + + @Argument + private String server; + + /** + * Create a command for listing authentication information. + */ + public LsAuth() + { + super("ls-auth", "ls-auth server", "List authentication info for a server"); + } + + protected void run() + { + if (server == null) + error("No server given"); + + URL url = null; + try { + url = new URL(server); + } catch (MalformedURLException ex) { + die("Server URL invalid: " + server); + } + + settings.put("axis.location", url.toString()); + + Authenticator.attach(getServiceManager(), new Authenticator.Visitor() { + + private boolean showHeader = true; + + public String visit(AuthenticationCallback callback) + { + if (showHeader) { + showHeader = false; + showAuth("Label", "Help"); + showAuth("-----", "----"); + } + showAuth(callback.getText(), callback.getHelp()); + return null; + } + + public void complete() + { + System.exit(0); + } + + }); + } + + private void showAuth(String label, String help) + { + System.out.printf("%-20s %-20s%n", label, help); + + } +} diff --git a/apps/cli/src/main/resources/META-INF/services/remote.apps.cli.Command b/apps/cli/src/main/resources/META-INF/services/remote.apps.cli.Command index e336815..e605e74 100644 --- a/apps/cli/src/main/resources/META-INF/services/remote.apps.cli.Command +++ b/apps/cli/src/main/resources/META-INF/services/remote.apps.cli.Command @@ -1 +1,2 @@ +remote.apps.cli.LsAuth remote.apps.cli.LsMotes diff --git a/apps/cli/src/site/apt/index.apt b/apps/cli/src/site/apt/index.apt index 0ad2af2..f5de1d4 100644 --- a/apps/cli/src/site/apt/index.apt +++ b/apps/cli/src/site/apt/index.apt @@ -11,7 +11,7 @@ ReĀ·Mote Command Line Tools found in <<>>. To run for example <<>> use: +----------------------------------------------------------------------------+ -$ sh target/bin/remote-cli ls-data http://example.org/path +$ sh target/bin/remote-cli ls-motes http://example.org/path +----------------------------------------------------------------------------+ Below a list of the tools provided by this project is given. Examples @@ -20,15 +20,36 @@ $ sh target/bin/remote-cli ls-data http://example.org/path %{toc|section=1|fromDepth=1|toDepth=2} -* {Show mote data from host} +* {List subcommands} - <> remote-cli [options] host + <> remote-cli [--help] - The command lists the information from the mote data webservice by dumping - both the header data and the mote data to stdout. + List the available subcommands with both a usage synopsis and a small + description. + +* {Show mote data for a testbed server} + + <> remote-cli ls-motes server + + Lists the information from the mote data webservice by dumping + both the data description header and the mote data to stdout. + + Example: + ++----------------------------------------------------------------------------+ +$ remote-cli ls-motes http://testbed.ekstranet.diku.dk/axis/services/ ++----------------------------------------------------------------------------+ + +* {Show authentication entries for a testbed server} + + <> remote-cli ls-auth server + + The command lists the authentication callback information from the + authentication service by dumping the callback label and help message + to stdout. Example: +----------------------------------------------------------------------------+ -$ remote-cli http://testbed.ekstranet.diku.dk/axis/services/ +$ remote-cli ls-auth http://testbed.ekstranet.diku.dk/axis/services/ +----------------------------------------------------------------------------+ -- 2.11.4.GIT