Search in sources :

Example 1 with ParamsWithPayload

use of com.sun.enterprise.admin.remote.ParamsWithPayload in project Payara by payara.

the class CommandResource method executeCommand.

private Response executeCommand(CommandName commandName, Payload.Inbound inbound, ParameterMap params, boolean supportsMultiparResult, String xIndentHeader, String modelETag, Cookie jSessionId) throws WebApplicationException {
    // Scope support
    if (RestLogging.restLogger.isLoggable(Level.FINEST)) {
        RestLogging.restLogger.log(Level.FINEST, "executeCommand(): ", commandName);
    }
    // Check command model
    CommandModel model = getCommandModel(commandName);
    checkCommandModelETag(model, modelETag);
    // Execute it
    boolean notifyOption = false;
    if (params != null) {
        notifyOption = params.containsKey("notify");
    }
    // new RestActionReporter(); //Must use PropsFileActionReporter because some commands react diferently on it :-(
    ActionReporter ar = new PropsFileActionReporter();
    final RestPayloadImpl.Outbound outbound = new RestPayloadImpl.Outbound(false);
    final CommandRunner.CommandInvocation commandInvocation = getCommandRunner().getCommandInvocation(commandName.getScope(), commandName.getName(), ar, getSubject(), notifyOption);
    if (inbound != null) {
        commandInvocation.inbound(inbound);
    }
    commandInvocation.outbound(outbound).parameters(params).execute();
    ar = (ActionReporter) commandInvocation.report();
    fixActionReporterSpecialCases(ar);
    ActionReport.ExitCode exitCode = ar.getActionExitCode();
    int status = HttpURLConnection.HTTP_OK;
    /*200 - ok*/
    if (exitCode == ActionReport.ExitCode.FAILURE) {
        status = HttpURLConnection.HTTP_INTERNAL_ERROR;
    }
    ResponseBuilder rb = Response.status(status);
    if (xIndentHeader != null) {
        rb.header("X-Indent", xIndentHeader);
    }
    if (supportsMultiparResult && outbound.size() > 0) {
        ParamsWithPayload pwp = new ParamsWithPayload(outbound, ar);
        rb.entity(pwp);
    } else {
        rb.type(MediaType.APPLICATION_JSON_TYPE);
        rb.entity(ar);
    }
    if (isSingleInstanceCommand(model)) {
        rb.cookie(getJSessionCookie(jSessionId));
    }
    return rb.build();
}
Also used : PropsFileActionReporter(com.sun.enterprise.admin.report.PropsFileActionReporter) ActionReport(org.glassfish.api.ActionReport) ActionReporter(com.sun.enterprise.admin.report.ActionReporter) PlainTextActionReporter(com.sun.enterprise.admin.report.PlainTextActionReporter) PropsFileActionReporter(com.sun.enterprise.admin.report.PropsFileActionReporter) CachedCommandModel(com.sun.enterprise.admin.util.CachedCommandModel) ParamsWithPayload(com.sun.enterprise.admin.remote.ParamsWithPayload) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) RestPayloadImpl(com.sun.enterprise.admin.remote.RestPayloadImpl)

Example 2 with ParamsWithPayload

use of com.sun.enterprise.admin.remote.ParamsWithPayload 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 3 with ParamsWithPayload

use of com.sun.enterprise.admin.remote.ParamsWithPayload in project Payara by payara.

the class MultipartProprietaryWriter method writeTo.

@Override
public void writeTo(final Object entity, final HttpURLConnection urlConnection) throws IOException {
    Payload.Outbound payload = null;
    ParameterMap parameters = null;
    ActionReport ar = null;
    if (entity instanceof ParamsWithPayload) {
        ParamsWithPayload pwp = (ParamsWithPayload) entity;
        payload = pwp.getPayloadOutbound();
        parameters = pwp.getParameters();
        ar = pwp.getActionReport();
    } else if (entity instanceof Payload.Outbound) {
        payload = (Payload.Outbound) entity;
    }
    writeTo(payload, parameters, ar, new OutputStream() {

        private OutputStream delegate;

        private OutputStream getDelegate() throws IOException {
            if (delegate == null) {
                delegate = urlConnection.getOutputStream();
            }
            return delegate;
        }

        @Override
        public void write(int b) throws IOException {
            getDelegate().write(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            getDelegate().write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            getDelegate().write(b, off, len);
        }

        @Override
        public void flush() throws IOException {
            getDelegate().flush();
        }

        @Override
        public void close() throws IOException {
            getDelegate().close();
        }
    }, new ContentTypeWriter() {

        @Override
        public void writeContentType(String firstPart, String secondPart, String boundary) {
            StringBuilder ct = new StringBuilder();
            ct.append(firstPart).append('/').append(secondPart);
            if (boundary != null) {
                ct.append("; boundary=").append(boundary);
            }
            urlConnection.addRequestProperty("Content-type", ct.toString());
            urlConnection.setRequestProperty("MIME-Version", "1.0");
        }
    });
}
Also used : OutputStream(java.io.OutputStream) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) Payload(org.glassfish.api.admin.Payload) ParamsWithPayload(com.sun.enterprise.admin.remote.ParamsWithPayload) ParamsWithPayload(com.sun.enterprise.admin.remote.ParamsWithPayload)

Example 4 with ParamsWithPayload

use of com.sun.enterprise.admin.remote.ParamsWithPayload in project Payara by payara.

the class ParameterMapFormProprietaryWriter method writeTo.

@Override
public void writeTo(Object entity, HttpURLConnection urlConnection) throws IOException {
    ParameterMap pm;
    if (entity instanceof ParameterMap) {
        pm = (ParameterMap) entity;
    } else if (entity instanceof ParamsWithPayload) {
        pm = ((ParamsWithPayload) entity).getParameters();
    } else {
        pm = new ParameterMap();
    }
    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    final StringBuilder sb = new StringBuilder();
    for (Map.Entry<String, List<String>> entry : pm.entrySet()) {
        for (String value : entry.getValue()) {
            if (sb.length() > 0) {
                sb.append('&');
            }
            sb.append(URLEncoder.encode(entry.getKey(), UTF8));
            if (value != null) {
                sb.append('=');
                sb.append(URLEncoder.encode(value, UTF8));
            }
        }
    }
    urlConnection.getOutputStream().write(sb.toString().getBytes(UTF8));
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) List(java.util.List) ParamsWithPayload(com.sun.enterprise.admin.remote.ParamsWithPayload) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap)

Aggregations

ParamsWithPayload (com.sun.enterprise.admin.remote.ParamsWithPayload)4 ActionReport (org.glassfish.api.ActionReport)3 ParameterMap (org.glassfish.api.admin.ParameterMap)3 RestPayloadImpl (com.sun.enterprise.admin.remote.RestPayloadImpl)2 IOException (java.io.IOException)2 ActionReporter (com.sun.enterprise.admin.report.ActionReporter)1 PlainTextActionReporter (com.sun.enterprise.admin.report.PlainTextActionReporter)1 PropsFileActionReporter (com.sun.enterprise.admin.report.PropsFileActionReporter)1 CachedCommandModel (com.sun.enterprise.admin.util.CachedCommandModel)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Payload (org.glassfish.api.admin.Payload)1 MIMEConfig (org.jvnet.mimepull.MIMEConfig)1 MIMEMessage (org.jvnet.mimepull.MIMEMessage)1 MIMEPart (org.jvnet.mimepull.MIMEPart)1