use of org.glassfish.api.admin.ParameterMap in project Payara by payara.
the class RemoteAdminCommand method executeCommand.
/**
* Run the command using the specified arguments.
* Return the output of the command.
* @param opts
* @return
* @throws org.glassfish.api.admin.CommandException
*/
public String executeCommand(ParameterMap opts) throws CommandException {
// 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 = PayloadImpl.Outbound.newInstance();
}
StringBuilder uriString = getCommandURI();
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(uriString, paramName, paramValue);
} else if (opt.getParam().password()) {
addPasswordOption(uriString, paramName, paramValue);
} else {
addStringOption(uriString, paramName, paramValue);
}
}
}
// add operands
for (String operand : operands) {
if (operandParam.getType() == File.class || operandParam.getType() == File[].class) {
addFileOption(uriString, "DEFAULT", operand);
} else {
addStringOption(uriString, "DEFAULT", operand);
}
}
// remove the last character, whether it was "?" or "&"
uriString.setLength(uriString.length() - 1);
executeRemoteCommand(uriString.toString());
} catch (IOException ioex) {
// possibly an error caused while reading or writing a file?
throw new CommandException("I/O Error", ioex);
}
return output;
}
use of org.glassfish.api.admin.ParameterMap in project Payara by payara.
the class MultipartProprietaryReader method readFrom.
@Override
public ParamsWithPayload readFrom(final InputStream is, final String contentType) throws IOException {
RestPayloadImpl.Inbound payload = null;
ActionReport actionReport = null;
ParameterMap parameters = null;
Properties mtProps = parseHeaderParams(contentType);
final String boundary = mtProps.getProperty("boundary");
if (!StringUtils.ok(boundary)) {
throw new IOException("ContentType does not define boundary");
}
final MIMEMessage mimeMessage = new MIMEMessage(is, boundary, new MIMEConfig());
// Parse
for (MIMEPart mimePart : mimeMessage.getAttachments()) {
String cd = getFirst(mimePart.getHeader("Content-Disposition"));
if (!StringUtils.ok(cd)) {
cd = "file";
}
cd = cd.trim();
Properties cdParams = parseHeaderParams(cd);
// 3 types of content disposition
if (cd.startsWith("form-data")) {
// COMMAND PARAMETER
if (!StringUtils.ok(cdParams.getProperty("name"))) {
throw new IOException("Form-data Content-Disposition does not contains name parameter.");
}
if (parameters == null) {
parameters = new ParameterMap();
}
parameters.add(cdParams.getProperty("name"), stream2String(mimePart.readOnce()));
} else if (mimePart.getContentType() != null && mimePart.getContentType().startsWith("application/json")) {
// ACTION REPORT
actionReport = actionReportReader.readFrom(mimePart.readOnce(), "application/json");
} else {
// PAYLOAD
String name = "noname";
if (cdParams.containsKey("name")) {
name = cdParams.getProperty("name");
} else if (cdParams.containsKey("filename")) {
name = cdParams.getProperty("filename");
}
if (payload == null) {
payload = new RestPayloadImpl.Inbound();
}
String ct = mimePart.getContentType();
if (!StringUtils.ok(ct) || ct.trim().startsWith("text/plain")) {
payload.add(name, stream2String(mimePart.readOnce()), mimePart.getAllHeaders());
} else {
payload.add(name, mimePart.read(), ct, mimePart.getAllHeaders());
}
}
}
// Result
return new ParamsWithPayload(payload, parameters, actionReport);
}
use of org.glassfish.api.admin.ParameterMap in project Payara by payara.
the class MultipartProprietaryWriter method writeTo.
public void writeTo(final Payload.Outbound payload, final ParameterMap parameters, final ActionReport ar, final OutputStream os, final ContentTypeWriter contentTypeWriter) throws IOException {
final String boundary = getBoundary();
// Content-Type
String ctType = "form-data";
if (parameters == null || parameters.size() == 0) {
ctType = "mixed";
}
contentTypeWriter.writeContentType("multipart", ctType, boundary);
// Write content
final Writer writer = new BufferedWriter(new OutputStreamWriter(os));
// Parameters
if (parameters != null) {
for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
for (String value : entry.getValue()) {
writeParam(writer, os, boundary, entry.getKey(), value);
}
}
}
// ActionReport
if (ar != null) {
writeActionReport(writer, os, boundary, ar);
}
// Payload
if (payload != null) {
Iterator<Part> parts = payload.parts();
while (parts.hasNext()) {
writePayloadPart(writer, os, boundary, parts.next());
}
}
// Write the final boundary string
multiWrite(writer, BOUNDERY_DELIMIT, boundary, BOUNDERY_DELIMIT, EOL);
writer.flush();
}
use of org.glassfish.api.admin.ParameterMap in project Payara by payara.
the class DeleteJdbcResourceTest method tearDown.
@After
public void tearDown() {
// Cleanup any leftover jdbc/foo resource - could be success or failure depending on the test
parameters = new ParameterMap();
parameters.add("DEFAULT", "jdbc/foo");
cr.getCommandInvocation("delete-jdbc-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(deleteCommand);
}
use of org.glassfish.api.admin.ParameterMap in project Payara by payara.
the class CreateJdbcResourceTest method tearDown.
@After
public void tearDown() throws TransactionFailure {
// Delete the created resource
ConfigSupport.apply(new SingleConfigCode<Resources>() {
public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
Resource target = null;
// and removal runs at the same time.
for (Resource resource : param.getResources()) {
if (resource instanceof JdbcResource) {
JdbcResource jr = (JdbcResource) resource;
if (jr.getJndiName().equals("jdbc/foo") || jr.getJndiName().equals("dupRes") || jr.getJndiName().equals("jdbc/sun") || jr.getJndiName().equals("jdbc/alldefaults") || jr.getJndiName().equals("jdbc/junk")) {
target = resource;
break;
}
}
}
if (target != null) {
param.getResources().remove(target);
}
return null;
}
}, resources);
parameters = new ParameterMap();
}
Aggregations