List of usage examples for org.apache.commons.cli ParseException ParseException
public ParseException(String message)
ParseException
with the specified detail message. From source file:org.glite.authz.pap.ui.cli.policymanagement.Move.java
@Override protected int executeCommand(CommandLine commandLine) throws CLIException, ParseException, RemoteException { String[] args = commandLine.getArgs(); if (args.length != 2) { throw new ParseException("Wrong number of arguments"); }/*from w ww.j av a 2s.c o m*/ String id = args[1]; if (commandLine.hasOption(OPT_PAPALIAS_LONG)) { alias = commandLine.getOptionValue(OPT_PAPALIAS_LONG); } String pivotId = null; boolean moveAfter = false; if (commandLine.hasOption(OPT_BEFORE_ID_LONG)) { pivotId = commandLine.getOptionValue(OPT_BEFORE_ID_LONG); } if (commandLine.hasOption(OPT_AFTER_ID_LONG)) { pivotId = commandLine.getOptionValue(OPT_AFTER_ID_LONG); moveAfter = true; } if (pivotId == null) { throw new ParseException( "One of --" + OPT_BEFORE_ID_LONG + " or --" + OPT_AFTER_ID_LONG + " is required."); } if (id.equals(pivotId)) { return ExitStatus.SUCCESS.ordinal(); } xacmlPolicyMgmtClient.move(alias, id, pivotId, moveAfter); return ExitStatus.SUCCESS.ordinal(); }
From source file:org.glite.authz.pap.ui.cli.policymanagement.Purge.java
@Override protected int executeCommand(CommandLine commandLine) throws CLIException, ParseException, RemoteException { String[] args = commandLine.getArgs(); boolean purgeResources = false; boolean purgeActions = false; if (commandLine.hasOption(OPT_PURGE_RESOURCES_LONG)) { purgeResources = true;/*from www .j a va2s. co m*/ } if (commandLine.hasOption(OPT_PURGE_ACTIONS_LONG)) { purgeActions = true; } if (!(purgeResources || purgeActions)) { purgeActions = true; purgeResources = true; } if (args.length > 1) { throw new ParseException("Wrong number of arguments: no arguments required."); } if (commandLine.hasOption(OPT_PAPALIAS_LONG)) { alias = commandLine.getOptionValue(OPT_PAPALIAS_LONG); } if (purgeResources && purgeActions) { if (verboseMode) { System.out.print("Purging actions and resources... "); } highlevelPolicyMgmtClient.purge(alias, false, true, false, true); if (verboseMode) { System.out.println("ok."); } } else if (purgeResources) { if (verboseMode) { System.out.print("Purging resources... "); } highlevelPolicyMgmtClient.purge(alias, false, false, false, true); if (verboseMode) { System.out.println("ok."); } } else if (purgeActions) { if (verboseMode) { System.out.print("Purging actions... "); } highlevelPolicyMgmtClient.purge(alias, false, true, false, false); if (verboseMode) { System.out.println("ok."); } } return ExitStatus.SUCCESS.ordinal(); }
From source file:org.glite.authz.pap.ui.cli.policymanagement.RemoveAllPolicies.java
@Override protected int executeCommand(CommandLine commandLine) throws ParseException, RemoteException { if (commandLine.getArgs().length != 1) { throw new ParseException("Wrong number of arguments"); }// w ww . j a v a 2 s .co m if (commandLine.hasOption(OPT_PAPALIAS_LONG)) { alias = commandLine.getOptionValue(OPT_PAPALIAS_LONG); } if (verboseMode) { System.out.print("Removing all policies... "); } highlevelPolicyMgmtClient.eraseRepository(alias); if (verboseMode) System.out.println("ok"); return ExitStatus.SUCCESS.ordinal(); }
From source file:org.glite.authz.pap.ui.cli.policymanagement.RemovePolicies.java
@Override protected int executeCommand(CommandLine commandLine) throws CLIException, ParseException, RemoteException { String[] args = commandLine.getArgs(); if (args.length < 2) { throw new ParseException("Wrong number of arguments: specify one or more ids."); }//from w w w. j a v a 2s . co m if (commandLine.hasOption(OPT_PAPALIAS_LONG)) { alias = commandLine.getOptionValue(OPT_PAPALIAS_LONG); } boolean partialSuccess = false; boolean failure = false; try { for (int i = 1; i < args.length; i++) { String policyId = args[i]; if (verboseMode) { System.out.print("Removing \"" + policyId + "\"... "); } boolean policyRemoved = xacmlPolicyMgmtClient.removeObjectByIdAndReferences(alias, policyId); if (!policyRemoved) { if (verboseMode) { System.out.println("error: not found."); } else { System.out.println("Error id not found: " + policyId); } failure = true; continue; } partialSuccess = true; if (verboseMode) { System.out.println("ok."); } } } catch (RemoteException e) { System.out.println("error."); throw e; } if (failure && !partialSuccess) { return ExitStatus.FAILURE.ordinal(); } if (failure && partialSuccess) { return ExitStatus.PARTIAL_SUCCESS.ordinal(); } return ExitStatus.SUCCESS.ordinal(); }
From source file:org.glite.authz.pap.ui.cli.policymanagement.UnBanAttribute.java
@Override protected int executeCommand(CommandLine commandLine) throws CLIException, ParseException, RemoteException { String[] args = commandLine.getArgs(); if (args.length != 3) { throw new ParseException("Wrong number of arguments"); }/* www.j a v a 2 s. c o m*/ String id = args[1]; String value = args[2]; if (commandLine.hasOption(OPT_PAPALIAS_LONG)) { alias = commandLine.getOptionValue(OPT_PAPALIAS_LONG); } String resource = null; String action = null; if (commandLine.hasOption(OPT_RESOURCE)) { resource = commandLine.getOptionValue(OPT_RESOURCE); } else { resource = ".*"; } if (commandLine.hasOption(OPT_ACTION)) { action = commandLine.getOptionValue(OPT_ACTION); } else { action = ".*"; } if (verboseMode) { System.out.print("Removing ban... "); } UnbanResult unbanResult; unbanResult = highlevelPolicyMgmtClient.unban(alias, id, value, resource, action); if (unbanResult.getStatusCode() != 0) { System.out.println("ban policy not found."); return ExitStatus.FAILURE.ordinal(); } else { if (verboseMode) { System.out.println("ok."); } } return ExitStatus.SUCCESS.ordinal(); }
From source file:org.glite.authz.pap.ui.cli.policymanagement.UpdatePolicy.java
@Override protected int executeCommand(CommandLine commandLine) throws CLIException, ParseException, RemoteException { String[] args = commandLine.getArgs(); if (args.length != 3) { throw new ParseException("Wrong number of arguments."); }/*from w w w . j a v a 2 s . co m*/ if (commandLine.hasOption(OPT_PAPALIAS_LONG)) { alias = commandLine.getOptionValue(OPT_PAPALIAS_LONG); } String id = args[1]; String fileName = args[2]; File file = new File(fileName); if (!file.exists()) { System.out.println("Error: file \"" + file.getAbsolutePath() + "\" does not exists."); return ExitStatus.FAILURE.ordinal(); } XACMLPolicyCLIUtils.initOpenSAMLAndAttributeWizard(); List<XACMLWizard> wizardList = new ArrayList<XACMLWizard>(0); try { wizardList = policyFileEncoder.parse(file); } catch (EncodingException e) { System.out.println("Syntax error in file: " + fileName); System.out.println(e.getMessage()); return ExitStatus.FAILURE.ordinal(); } if (wizardList.size() == 0) { System.out.println("Error: no policies has been defined in file \"" + fileName + "\""); return ExitStatus.FAILURE.ordinal(); } if (wizardList.size() > 1) { System.out.println("Error: more than one element has been defined in file \"" + fileName + "\""); return ExitStatus.FAILURE.ordinal(); } XACMLWizard xacmlWizard = wizardList.get(0); int status; if (xacmlWizard instanceof PolicySetWizard) { status = updatePolicySet(id, (PolicySetWizard) xacmlWizard); } else { status = updatePolicy(id, (PolicyWizard) xacmlWizard); } if (verboseMode) { if (status == ExitStatus.SUCCESS.ordinal()) { System.out.println("Success: policy has been updated."); } } return status; }
From source file:org.glite.authz.pap.ui.cli.ServiceCLI.java
public int execute(String[] args) throws ParseException, HelpMessageException, RemoteException { CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption(OPT_HELP)) { throw new HelpMessageException(); }//from w w w. j a va 2s . com if (commandLine.hasOption(OPT_VERBOSE)) { verboseMode = true; } if (commandLine.hasOption(OPT_URL_LONG)) { serviceClient.setTargetEndpoint(commandLine.getOptionValue(OPT_URL_LONG)); } else { String host = Pap.DEFAULT_HOST; String papHostProperty = System.getProperty(PAP_HOST_PROPERTY); if (papHostProperty != null && !"".equals(papHostProperty.trim())) { host = papHostProperty; } String port = Pap.DEFAULT_PORT; String papPortProperty = System.getProperty(PAP_PORT_PROPERTY); if (papPortProperty != null && !"".equals(papPortProperty.trim())) { port = papPortProperty; } if (commandLine.hasOption(OPT_HOST_LONG)) { host = commandLine.getOptionValue(OPT_HOST_LONG); } if (commandLine.hasOption(OPT_PORT)) { port = commandLine.getOptionValue(OPT_PORT); } try { Integer.valueOf(port); } catch (NumberFormatException e) { throw new ParseException(String.format("Invalid port number \"%s\" (option -%s, --%s)", port, OPT_PORT, OPT_PORT_LONG)); } serviceClient.setTargetEndpoint( String.format(DEFAULT_SERVICE_URL, host, port, Pap.DEFAULT_SERVICES_ROOT_PATH)); } boolean credentialsNotRetrieved = true; if (commandLine.hasOption(OPT_PROXY_LONG)) { serviceClient.setClientProxy(commandLine.getOptionValue(OPT_PROXY_LONG)); credentialsNotRetrieved = false; } if (commandLine.hasOption(OPT_CERT_LONG)) { if (commandLine.hasOption(OPT_PROXY_LONG)) { throw new ParseException( String.format("Conflicting options --%s and --%s.", OPT_PROXY_LONG, OPT_CERT_LONG)); } else { if (!commandLine.hasOption(OPT_KEY_LONG)) { throw new ParseException( String.format("Option --%s requires also option --%s.", OPT_CERT_LONG, OPT_KEY_LONG)); } serviceClient.setClientCertificate(commandLine.getOptionValue(OPT_CERT_LONG)); credentialsNotRetrieved = false; } } if (commandLine.hasOption(OPT_KEY_LONG)) { if (commandLine.hasOption(OPT_PROXY_LONG)) { throw new ParseException( String.format("Conflicting options --%s and --%s.", OPT_PROXY_LONG, OPT_KEY_LONG)); } else { if (!commandLine.hasOption(OPT_CERT_LONG)) { throw new ParseException( String.format("Option --%s requires also option --%s.", OPT_KEY_LONG, OPT_CERT_LONG)); } serviceClient.setClientPrivateKey(commandLine.getOptionValue(OPT_KEY_LONG)); credentialsNotRetrieved = false; } } if (credentialsNotRetrieved) { // 1. if running as root take the cert /etc/grid-security/hostcert.pem // 2. check the env variable X509_USER_PROXY // 3. check the env variable X509_USER_CERT (and X509_USER_KEY) // 4. check the proxy /tmp/x509up_u<id_utente> // 5. check the cert $HOME/.globus/usercert.pem and key $HOME/.globus/userkey.pem String euid = getEUID(); if (euid == null) { log.error("Cannot enstabilish user's effective user id."); throw new PAPException(String.format( "Cannot enstabilish user's effective user id, please use the --%s or --%s, --%s options.", OPT_PROXY_LONG, OPT_CERT_LONG, OPT_KEY_LONG)); } String messageString = null; if ("0".equals(euid)) { // user: root if (setCertFromEnvironment()) { messageString = String.format( "Connecting to %s using %s and %s (from environment X509_USER_CERT and X509_USER_KEY)", serviceClient.getTargetEndpoint(), serviceClient.getClientCertificate(), serviceClient.getClientPrivateKey()); } else { serviceClient.setClientCertificate("/etc/grid-security/hostcert.pem"); serviceClient.setClientPrivateKey("/etc/grid-security/hostkey.pem"); messageString = String.format("Connecting to %s using %s and %s", serviceClient.getTargetEndpoint(), serviceClient.getClientCertificate(), serviceClient.getClientPrivateKey()); } } else { if (setProxyFromEnvironment()) { messageString = String.format( "Connecting to %s using proxy (from environment X509_USER_PROXY) %s", serviceClient.getTargetEndpoint(), serviceClient.getClientProxy()); } else if (setCertFromEnvironment()) { messageString = String.format( "Connecting to %s using %s and %s (from environment X509_USER_CERT and X509_USER_KEY)", serviceClient.getTargetEndpoint(), serviceClient.getClientCertificate(), serviceClient.getClientPrivateKey()); } else if (setProxyFromStandardLocation(euid)) { messageString = String.format("Connecting to %s using proxy %s", serviceClient.getTargetEndpoint(), serviceClient.getClientProxy()); } else if (setCertFromHomeDir()) { messageString = String.format("Connecting to %s using %s and %s", serviceClient.getTargetEndpoint(), serviceClient.getClientCertificate(), serviceClient.getClientPrivateKey()); } else { throw new ParseException(String.format( "Unable to find a certificate or a proxy, please specify a proxy file with option --%s or certificate and key with options --%s and --%s", OPT_PROXY_LONG, OPT_CERT_LONG, OPT_KEY_LONG)); } } log.info(messageString); if (verboseMode) { System.out.println(messageString); } } // Ask for certificate password if needed. The default private key (getClientPrivateKey() == // null) // is a host certificate key which doesn't need the password if (serviceClient.getClientPrivateKey() != null) { try { Reader reader = new FileReader(serviceClient.getClientPrivateKey()); String prompt = "Please enter the passphrase for the private key file " + serviceClient.getClientPrivateKey() + ": "; PasswordFinderImpl passwordFinder = new PasswordFinderImpl(prompt); PEMReader pm = new PEMReader(reader, passwordFinder); char[] password = null; try { pm.readObject(); } catch (IOException e) { // doesn't matter certificate stuff is managed later. // the purpose of this is just to set the password (if needed). } password = passwordFinder.getTypedPassword(); if (password != null) { serviceClient.setClientPrivateKeyPassword(new String(password)); } } catch (FileNotFoundException e) { throw new CLIException(e); } } return executeCommandService(commandLine, serviceClient); }
From source file:org.glite.authz.pap.ui.cli.ServiceCLI.java
/** * Set the certificate the the key in {@link ServiceCLI#serviceClient} using the envirinment variables * X509_USER_CERT and X509_USER_KEY./* w w w . jav a2 s .co m*/ * * @return <code>true</code> if the env variables are set, <code>false</code> otherwise. * @throws ParseException if only one of X509_USER_CERT or X509_USER_KEY is set. * */ private boolean setCertFromEnvironment() throws ParseException { String x509UserCert = System.getenv("X509_USER_CERT"); String x509UserKey = System.getenv("X509_USER_KEY"); if (!Utils.isDefined(x509UserCert) && (!Utils.isDefined(x509UserKey))) { return false; } if (!(Utils.isDefined(x509UserCert) && Utils.isDefined(x509UserKey))) { if (Utils.isDefined(x509UserCert)) { throw new ParseException( "Trying to use environment variable X509_USER_CERT for certificate but environment variable X509_USER_KEY is not set."); } throw new ParseException( "Trying to use environment variable X509_USER_KEY for private key but environment variable X509_USER_CERT is not set."); } serviceClient.setClientCertificate(x509UserCert); serviceClient.setClientPrivateKey(x509UserKey); return true; }
From source file:org.globus.workspace.client.WorkspaceCLI.java
protected CommandLine parse(String[] args, Properties defaultOptions) throws Exception { final CommandLineParser parser = new PosixParser(); final CommandLine line = parser.parse(this.options, args, defaultOptions); String eprFile = null;//from w ww . j a v a 2 s. com if (line.hasOption(Opts.EPRFILE2_OPT_STRING)) { eprFile = line.getOptionValue(Opts.EPRFILE2_OPT_STRING); } else if (line.hasOption("e")) { eprFile = line.getOptionValue("e"); } if (eprFile != null) { if (line.hasOption("k")) { throw new ParseException("-e and -k arguments are exclusive"); } if (line.hasOption("s")) { throw new ParseException("-e and -s arguments are exclusive"); } FileInputStream in = null; try { in = new FileInputStream(eprFile); this.endpoint = (EndpointReferenceType) ObjectDeserializer.deserialize(new InputSource(in), EndpointReferenceType.class); } finally { if (in != null) { in.close(); } } } else if (line.hasOption(Opts.SERVICE2_OPT_STRING)) { this.endpoint = new EndpointReferenceType(); this.endpoint.setAddress(new Address(line.getOptionValue(Opts.SERVICE2_OPT_STRING))); } else if (line.hasOption("s")) { this.endpoint = new EndpointReferenceType(); this.endpoint.setAddress(new Address(line.getOptionValue("s"))); } this.debugMode = line.hasOption("d"); // Security mechanism if (line.hasOption("m")) { String value = line.getOptionValue("m"); if (value != null) { if (value.equals("msg")) { this.mechanism = Constants.GSI_SEC_MSG; } else if (value.equals("conv")) { this.mechanism = Constants.GSI_SEC_CONV; } else { throw new ParseException("Unsupported security mechanism: " + value); } } } // Protection if (line.hasOption("p")) { String value = line.getOptionValue("p"); if (value != null) { if (value.equals("sig")) { this.protection = Constants.SIGNATURE; } else if (value.equals("enc")) { this.protection = Constants.ENCRYPTION; } else { throw new ParseException("Unsupported protection mode: " + value); } } } // Delegation if (line.hasOption("g")) { String value = line.getOptionValue("g"); if (value != null) { if (value.equals("limited")) { this.delegation = GSIConstants.GSI_MODE_LIMITED_DELEG; } else if (value.equals("full")) { this.delegation = GSIConstants.GSI_MODE_FULL_DELEG; } else { throw new ParseException("Unsupported delegation mode: " + value); } } } // Authz if (line.hasOption("z")) { String value = line.getOptionValue("z"); if (value != null) { if (value.equals("self")) { this.authorization = SelfAuthorization.getInstance(); } else if (value.equals("host")) { this.authorization = HostAuthorization.getInstance(); } else if (value.equals("none")) { this.authorization = NoAuthorization.getInstance(); } else if (authorization == null) { this.authorization = new IdentityAuthorization(value); } } } // Anonymous if (line.hasOption("a")) { this.anonymous = Boolean.TRUE; } // context lifetime if (line.hasOption("l")) { final String value = line.getOptionValue("l"); if (value != null) { this.contextLifetime = new Integer(value); } } // msg actor if (line.hasOption("x")) { this.msgActor = line.getOptionValue("x"); } // conv actor if (line.hasOption("y")) { this.convActor = line.getOptionValue("y"); } // Server's public key if (line.hasOption("c")) { this.publicKeyFilename = line.getOptionValue("c"); } if (line.hasOption("f")) { this.descriptorFile = line.getOptionValue("f"); } return line; }
From source file:org.globus.workspace.client_common.TempBaseClient.java
protected CommandLine parse(String[] args, Properties defaultOptions) throws Exception { CommandLineParser parser = new PosixParser(); CommandLine line = parser.parse(options, args, defaultOptions); if (defaultOptions != null) { Iterator iter = defaultOptions.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Option opt = options.getOption((String) entry.getKey()); if (opt != null) { String desc = opt.getDescription(); desc += " (Default '" + entry.getValue() + "')"; opt.setDescription(desc); }/*from w w w. j a v a 2 s .c o m*/ } } if (line.hasOption("h")) { displayUsage(); System.exit(0); } if (line.hasOption("e")) { if (line.hasOption("k")) { throw new ParseException("-e and -k arguments are exclusive"); } if (line.hasOption("s")) { throw new ParseException("-e and -s arguments are exclusive"); } FileInputStream in = null; try { in = new FileInputStream(line.getOptionValue("e")); this.endpoint = (EndpointReferenceType) ObjectDeserializer.deserialize(new InputSource(in), EndpointReferenceType.class); } finally { if (in != null) { try { in.close(); } catch (Exception e) { } } } } else if (line.hasOption("s")) { this.endpoint = new EndpointReferenceType(); this.endpoint.setAddress(new Address(line.getOptionValue("s"))); } else { throw new ParseException("-s or -e argument is required"); } if (line.hasOption("k")) { String[] values = line.getOptionValues("k"); if (values.length != 2) { throw new ParseException("-k requires two arguments"); } QName keyName = QName.valueOf(values[0]); ReferencePropertiesType props = new ReferencePropertiesType(); SimpleResourceKey key = new SimpleResourceKey(keyName, values[1]); props.add(key.toSOAPElement()); this.endpoint.setProperties(props); } this.debugMode = line.hasOption("d"); // Security mechanism if (line.hasOption("m")) { String value = line.getOptionValue("m"); if (value != null) { if (value.equals("msg")) { this.mechanism = Constants.GSI_SEC_MSG; } else if (value.equals("conv")) { this.mechanism = Constants.GSI_SEC_CONV; } else { throw new ParseException("Unsupported security mechanism: " + value); } } } // Protection if (line.hasOption("p")) { String value = line.getOptionValue("p"); if (value != null) { if (value.equals("sig")) { this.protection = Constants.SIGNATURE; } else if (value.equals("enc")) { this.protection = Constants.ENCRYPTION; } else { throw new ParseException("Unsupported protection mode: " + value); } } } // Delegation if (line.hasOption("g")) { String value = line.getOptionValue("g"); if (value != null) { if (value.equals("limited")) { this.delegation = GSIConstants.GSI_MODE_LIMITED_DELEG; } else if (value.equals("full")) { this.delegation = GSIConstants.GSI_MODE_FULL_DELEG; } else { throw new ParseException("Unsupported delegation mode: " + value); } } } // Authz if (line.hasOption("z")) { String value = line.getOptionValue("z"); if (value != null) { if (value.equals("self")) { this.authorization = SelfAuthorization.getInstance(); } else if (value.equals("host")) { this.authorization = HostAuthorization.getInstance(); } else if (value.equals("none")) { this.authorization = NoAuthorization.getInstance(); } else if (authorization == null) { this.authorization = new IdentityAuthorization(value); } } } // Anonymous if (line.hasOption("a")) { this.anonymous = Boolean.TRUE; } // context lifetime if (line.hasOption("l")) { String value = line.getOptionValue("l"); if (value != null) this.contextLifetime = new Integer(value); } // msg actor if (line.hasOption("x")) { String value = line.getOptionValue("x"); this.msgActor = value; } // conv actor if (line.hasOption("y")) { String value = line.getOptionValue("y"); this.convActor = value; } // Server's public key if (line.hasOption("c")) { String value = line.getOptionValue("c"); this.publicKeyFilename = value; } if (line.hasOption("f")) { String value = line.getOptionValue("f"); this.descriptorFile = value; } return line; }