Search in sources :

Example 36 with ParameterMap

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;
}
Also used : ArrayList(java.util.ArrayList) ParamModel(org.glassfish.api.admin.CommandModel.ParamModel) ParameterMap(org.glassfish.api.admin.ParameterMap) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) SmartFile(com.sun.enterprise.universal.io.SmartFile) File(java.io.File)

Example 37 with ParameterMap

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);
}
Also used : MIMEConfig(org.jvnet.mimepull.MIMEConfig) MIMEMessage(org.jvnet.mimepull.MIMEMessage) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) ParamsWithPayload(com.sun.enterprise.admin.remote.ParamsWithPayload) ActionReport(org.glassfish.api.ActionReport) Properties(java.util.Properties) RestPayloadImpl(com.sun.enterprise.admin.remote.RestPayloadImpl) MIMEPart(org.jvnet.mimepull.MIMEPart)

Example 38 with ParameterMap

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();
}
Also used : Part(org.glassfish.api.admin.Payload.Part) OutputStreamWriter(java.io.OutputStreamWriter) List(java.util.List) ParameterMap(org.glassfish.api.admin.ParameterMap) Map(java.util.Map) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 39 with ParameterMap

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);
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) After(org.junit.After)

Example 40 with ParameterMap

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();
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) JdbcResource(org.glassfish.jdbc.config.JdbcResource) JdbcResource(org.glassfish.jdbc.config.JdbcResource) ParameterMap(org.glassfish.api.admin.ParameterMap) After(org.junit.After)

Aggregations

ParameterMap (org.glassfish.api.admin.ParameterMap)149 ActionReport (org.glassfish.api.ActionReport)68 CommandRunner (org.glassfish.api.admin.CommandRunner)37 Test (org.junit.Test)25 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)24 Map (java.util.Map)20 PropsFileActionReporter (com.sun.enterprise.v3.common.PropsFileActionReporter)19 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)18 List (java.util.List)16 ArrayList (java.util.ArrayList)15 CommandRunner (org.glassfish.embeddable.CommandRunner)15 IOException (java.io.IOException)14 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)13 Before (org.junit.Before)13 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)13 CommandException (org.glassfish.api.admin.CommandException)12 File (java.io.File)11 MessagePart (org.glassfish.api.ActionReport.MessagePart)11 Resource (com.sun.enterprise.config.serverbeans.Resource)10 Logger (java.util.logging.Logger)9