use of org.glassfish.api.admin.CommandModel.ParamModel in project Payara by payara.
the class ProgramOptions method addMetaOption.
/**
* Helper method to define a meta-option.
*
* @param name long option name
* @param sname short option name
* @param type option type (String.class, Boolean.class, etc.)
* @param req is option required?
* @param def default value for option
*/
private static void addMetaOption(Set<ParamModel> opts, String name, char sname, Class type, boolean req, String def) {
ParamModel opt = new ParamModelData(name, type, !req, def, Character.toString(sname));
opts.add(opt);
}
use of org.glassfish.api.admin.CommandModel.ParamModel in project Payara by payara.
the class RemoteRestAdminCommand method initializeDoUpload.
/**
* Search all the parameters that were actually specified to see
* if any of them are FILE type parameters. If so, check for the
* "--upload" option.
*/
private void initializeDoUpload() throws CommandException {
boolean sawFile = false;
boolean sawDirectory = false;
/*
* We don't upload directories, even when asked to upload.
*/
boolean sawUploadableFile = false;
for (Map.Entry<String, List<String>> param : options.entrySet()) {
String paramName = param.getKey();
if (// operands handled below
paramName.equals("DEFAULT"))
continue;
ParamModel opt = commandModel.getModelFor(paramName);
if (opt != null && (opt.getType() == File.class || opt.getType() == File[].class)) {
sawFile = true;
for (String fname : options.get(opt.getName())) {
final File optionFile = new File(fname);
sawDirectory |= optionFile.isDirectory();
sawUploadableFile |= optionFile.isFile();
}
}
}
// now check the operands for files
ParamModel operandParam = getOperandModel();
if (operandParam != null && (operandParam.getType() == File.class || operandParam.getType() == File[].class)) {
sawFile |= !operands.isEmpty();
for (String operandValue : operands) {
final File operandFile = new File(operandValue);
sawDirectory |= operandFile.isDirectory();
sawUploadableFile |= operandFile.isFile();
}
}
if (sawFile) {
logger.finer("Saw a file parameter");
// found a FILE param, is doUpload set?
String upString = getOption("upload");
if (ok(upString)) {
doUpload = Boolean.parseBoolean(upString);
} else {
doUpload = !isLocal(host) && sawUploadableFile;
}
if (prohibitDirectoryUploads && sawDirectory && doUpload) {
// oops, can't upload directories
logger.finer("--upload=" + upString + ", doUpload=" + doUpload);
throw new CommandException(strings.get("CantUploadDirectory"));
}
}
if (addedUploadOption) {
logger.finer("removing --upload option");
// options.remove("upload"); // remove it
// XXX - no remove method, have to copy it
ParameterMap noptions = new ParameterMap();
for (Map.Entry<String, List<String>> e : options.entrySet()) {
if (!e.getKey().equals("upload"))
noptions.set(e.getKey(), e.getValue());
}
options = noptions;
}
logger.finer("doUpload set to " + doUpload);
}
use of org.glassfish.api.admin.CommandModel.ParamModel in project Payara by payara.
the class RemoteRestAdminCommand method processParams.
private ParameterMap processParams(ParameterMap opts) throws CommandException {
if (opts == null) {
opts = new ParameterMap();
}
// first, make sure we have the command model
getCommandModel();
// XXX : This is to take care of camel case from ReST calls that
// do not go through usual CLI path
// XXX : This is not clean; this should be handled the same way
// it is handled for incoming CLI commands
options = new ParameterMap();
for (Map.Entry<String, List<String>> o : opts.entrySet()) {
String key = o.getKey();
List<String> value = o.getValue();
options.set(key.toLowerCase(Locale.ENGLISH), value);
}
// "DEFAULT".toLowerCase()
operands = options.get("default");
try {
initializeDoUpload();
// if uploading, we need a payload
if (doUpload) {
outboundPayload = new RestPayloadImpl.Outbound(true);
} else {
outboundPayload = null;
}
ParameterMap result = new ParameterMap();
ParamModel operandParam = null;
for (ParamModel opt : commandModel.getParameters()) {
if (opt.getParam().primary()) {
operandParam = opt;
continue;
}
String paramName = opt.getName();
List<String> paramValues = new ArrayList<String>(options.get(paramName.toLowerCase(Locale.ENGLISH)));
if (!opt.getParam().alias().isEmpty() && !paramName.equalsIgnoreCase(opt.getParam().alias())) {
paramValues.addAll(options.get(opt.getParam().alias().toLowerCase(Locale.ENGLISH)));
}
if (!opt.getParam().multiple() && paramValues.size() > 1) {
throw new CommandException(strings.get("tooManyOptions", paramName));
}
if (paramValues.isEmpty()) {
// perhaps it's set in the environment?
String envValue = getFromEnvironment(paramName);
if (envValue != null) {
paramValues.add(envValue);
}
}
if (paramValues.isEmpty()) {
/*
* Option still not set. Note that we ignore the default
* value and don't send it explicitly on the assumption
* that the server will supply the default value itself.
*
* If the missing option is required, that's an error,
* which should never happen here because validate()
* should check it first.
*/
if (!opt.getParam().optional()) {
throw new CommandException(strings.get("missingOption", paramName));
}
// optional param not set, skip it
continue;
}
for (String paramValue : paramValues) {
if (opt.getType() == File.class || opt.getType() == File[].class) {
addFileOption(result, paramName, paramValue);
} else {
result.add(paramName, paramValue);
}
}
}
// add operands
for (String operand : operands) {
if (operandParam.getType() == File.class || operandParam.getType() == File[].class) {
addFileOption(result, "DEFAULT", operand);
} else {
result.add("DEFAULT", operand);
}
}
return result;
} catch (IOException ioex) {
// possibly an error caused while reading or writing a file?
throw new CommandException("I/O Error", ioex);
}
}
use of org.glassfish.api.admin.CommandModel.ParamModel in project Payara by payara.
the class RemoteAdminCommand method initializeDoUpload.
/**
* Search all the parameters that were actually specified to see
* if any of them are FILE type parameters. If so, check for the
* "--upload" option.
*/
private void initializeDoUpload() throws CommandException {
boolean sawFile = false;
boolean sawDirectory = false;
/*
* We don't upload directories, even when asked to upload.
*/
boolean sawUploadableFile = false;
for (Map.Entry<String, List<String>> param : options.entrySet()) {
String paramName = param.getKey();
if (// operands handled below
paramName.equals("DEFAULT"))
continue;
ParamModel opt = commandModel.getModelFor(paramName);
if (opt != null && (opt.getType() == File.class || opt.getType() == File[].class)) {
sawFile = true;
for (String fname : options.get(opt.getName())) {
final File optionFile = new File(fname);
sawDirectory |= optionFile.isDirectory();
sawUploadableFile |= optionFile.isFile();
}
}
}
// now check the operands for files
ParamModel operandParam = getOperandModel();
if (operandParam != null && (operandParam.getType() == File.class || operandParam.getType() == File[].class)) {
sawFile |= !operands.isEmpty();
for (String operandValue : operands) {
final File operandFile = new File(operandValue);
sawDirectory |= operandFile.isDirectory();
sawUploadableFile |= operandFile.isFile();
}
}
if (sawFile) {
logger.finer("Saw a file parameter");
// found a FILE param, is doUpload set?
String upString = getOption("upload");
if (ok(upString))
doUpload = Boolean.parseBoolean(upString);
else
doUpload = !isLocal(host) && sawUploadableFile;
if (prohibitDirectoryUploads && sawDirectory && doUpload) {
// oops, can't upload directories
logger.finer("--upload=" + upString + ", doUpload=" + doUpload);
throw new CommandException(strings.get("CantUploadDirectory"));
}
}
if (addedUploadOption) {
logger.finer("removing --upload option");
// options.remove("upload"); // remove it
// XXX - no remove method, have to copy it
ParameterMap noptions = new ParameterMap();
for (Map.Entry<String, List<String>> e : options.entrySet()) {
if (!e.getKey().equals("upload"))
noptions.set(e.getKey(), e.getValue());
}
options = noptions;
}
logger.finer("doUpload set to " + doUpload);
}
use of org.glassfish.api.admin.CommandModel.ParamModel in project Payara by payara.
the class JavaClientClassWriter method generateMethodBody.
@Override
public String generateMethodBody(CommandModel cm, String httpMethod, String resourcePath, boolean includeOptional, boolean needsMultiPart) {
StringBuilder sb = new StringBuilder();
Collection<ParamModel> params = cm.getParameters();
if ((params != null) && (!params.isEmpty())) {
for (ParamModel model : params) {
Param param = model.getParam();
boolean include = true;
if (param.optional() && !includeOptional) {
continue;
}
String key = (!param.alias().isEmpty()) ? param.alias() : model.getName();
String paramName = Util.eleminateHypen(model.getName());
String put = " payload.put(\"" + key + "\", _" + paramName + ");\n";
sb.append(put);
}
}
return TMPL_METHOD_BODY.replace("PUTS", sb.toString()).replace("HTTPMETHOD", httpMethod.toUpperCase(Locale.US)).replace("RESOURCEPATH", resourcePath).replace("NEEDSMULTIPART", Boolean.toString(needsMultiPart));
}
Aggregations