use of cz.metacentrum.perun.openapi.PerunException in project perun by CESNET.
the class PerunCLI method call.
private static void call(PerunCommand command, String[] cliArgs) throws ParseException {
// prepare CLI options
// first options common to all commands
Options options = new Options();
options.addOption(Option.builder(PERUN_URL_OPTION).required(false).hasArg().longOpt(PERUN_URL_VARIABLE).desc("Perun base URL").build());
options.addOption(Option.builder(PERUN_USER_OPTION).required(false).hasArg().longOpt(PERUN_USER_VARIABLE).desc("HTTP Basic Auth user/password").build());
options.addOption(Option.builder(DEBUG_OPTION).required(false).hasArg(false).longOpt("debug").desc("debugging output").build());
options.addOption(Option.builder(HELP_OPTION).required(false).hasArg(false).longOpt("help").desc("print options").build());
// then options specific to the command
command.addOptions(options);
// parse options
CommandLine commandLine;
try {
commandLine = new DefaultParser().parse(options, cliArgs);
} catch (MissingOptionException ex) {
printHelp(command, options);
return;
}
if (commandLine.hasOption(HELP_OPTION)) {
printHelp(command, options);
return;
}
if (commandLine.hasOption(DEBUG_OPTION)) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(Level.DEBUG);
}
// find URL
String perunUrl = System.getenv(PERUN_URL_VARIABLE);
if (commandLine.hasOption(PERUN_URL_OPTION)) {
perunUrl = commandLine.getOptionValue(PERUN_URL_OPTION);
}
if (perunUrl == null)
perunUrl = "https://perun.cesnet.cz/krb/rpc";
// find user and password
String user = System.getenv(PERUN_USER_VARIABLE);
if (commandLine.hasOption(PERUN_USER_OPTION)) {
user = commandLine.getOptionValue(PERUN_USER_OPTION);
}
PerunRPC perunRPC;
if (user == null) {
perunRPC = new PerunRPC(perunUrl, null, null, new KerberosRestTemplate(null, "-"));
} else {
int slash = user.indexOf('/');
if (slash == -1) {
System.err.println("the username and password must be separated by the '/' character");
System.exit(1);
}
String username = user.substring(0, slash);
String password = user.substring(slash + 1);
perunRPC = new PerunRPC(perunUrl, username, password);
}
// execute the command
HttpClientErrorException hce = null;
try {
command.executeCommand(new CommandContext(perunRPC, commandLine));
} catch (HttpClientErrorException e1) {
// normal RestTemplate throws this exception on status 400
hce = e1;
} catch (RestClientException e2) {
if (e2.getCause() instanceof HttpClientErrorException) {
// KerberosRestTemplate throws the exception wrapped
hce = (HttpClientErrorException) e2.getCause();
} else {
// something other went wrong
throw e2;
}
}
if (hce != null) {
PerunException pe = PerunException.to(hce);
System.err.println(pe.getMessage());
System.exit(1);
}
}
Aggregations