Search in sources :

Example 1 with CliActionReport

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

the class RemoteRestAdminCommand method executeRemoteCommand.

/**
 * Actually execute the remote command.
 */
private void executeRemoteCommand(final ParameterMap params) throws CommandException {
    doHttpCommand(getCommandURI(), "POST", new HttpCommand() {

        @Override
        public void prepareConnection(final HttpURLConnection urlConnection) throws IOException {
            try {
                if (useSse()) {
                    urlConnection.addRequestProperty("Accept", MEDIATYPE_SSE);
                } else {
                    urlConnection.addRequestProperty("Accept", MEDIATYPE_JSON + "; q=0.8, " + MEDIATYPE_MULTIPART + "; q=0.9");
                }
            } catch (CommandException cex) {
                throw new IOException(cex.getLocalizedMessage(), cex);
            }
            // add any user-specified headers
            for (Header h : requestHeaders) {
                urlConnection.addRequestProperty(h.getName(), h.getValue());
            }
            // Write data
            ParamsWithPayload pwp;
            if (doUpload) {
                urlConnection.setChunkedStreamingMode(0);
                pwp = new ParamsWithPayload(outboundPayload, params);
            } else {
                pwp = new ParamsWithPayload(null, params);
            }
            ProprietaryWriter writer = ProprietaryWriterFactory.getWriter(pwp);
            if (logger.isLoggable(Level.FINER)) {
                logger.log(Level.FINER, "Writer to use {0}", writer.getClass().getName());
            }
            writer.writeTo(pwp, urlConnection);
        }

        @Override
        public void useConnection(final HttpURLConnection urlConnection) throws CommandException, IOException {
            String resultMediaType = urlConnection.getContentType();
            if (logger.isLoggable(Level.FINER)) {
                logger.log(Level.FINER, "Result type is {0}", resultMediaType);
                logger.log(Level.FINER, "URL connection is {0}", urlConnection.getClass().getName());
            }
            if (resultMediaType != null && resultMediaType.startsWith(MEDIATYPE_SSE)) {
                String instanceId = null;
                boolean retryableCommand = false;
                try {
                    logger.log(Level.FINEST, "Response is SSE - about to read events");
                    closeSse = false;
                    ProprietaryReader<GfSseEventReceiver> reader = new GfSseEventReceiverProprietaryReader();
                    GfSseEventReceiver eventReceiver = reader.readFrom(urlConnection.getInputStream(), resultMediaType);
                    GfSseInboundEvent event;
                    do {
                        event = eventReceiver.readEvent();
                        if (event != null) {
                            if (logger.isLoggable(Level.FINEST)) {
                                logger.log(Level.FINEST, "Event: {0}", event.getName());
                            }
                            fireEvent(event.getName(), event);
                            if (AdminCommandState.EVENT_STATE_CHANGED.equals(event.getName())) {
                                AdminCommandState acs = event.getData(AdminCommandState.class, MEDIATYPE_JSON);
                                if (acs.getId() != null) {
                                    instanceId = acs.getId();
                                    if (logger.isLoggable(Level.FINEST)) {
                                        logger.log(Level.FINEST, "Command instance ID: {0}", instanceId);
                                    }
                                }
                                switch(acs.getState()) {
                                    case COMPLETED:
                                    case RECORDED:
                                    case REVERTED:
                                        if (acs.getActionReport() != null) {
                                            setActionReport(acs.getActionReport());
                                        }
                                        closeSse = true;
                                        if (!acs.isOutboundPayloadEmpty()) {
                                            logger.log(Level.FINEST, "Romote command holds data. Must load it");
                                            downloadPayloadFromManaged(instanceId);
                                        }
                                        break;
                                    case FAILED_RETRYABLE:
                                        logger.log(Level.INFO, STRINGS.get("remotecommand.failedretryable", acs.getId()));
                                        if (acs.getActionReport() != null) {
                                            setActionReport(acs.getActionReport());
                                        }
                                        closeSse = true;
                                        break;
                                    case RUNNING_RETRYABLE:
                                        logger.log(Level.FINEST, "Command stores checkpoint and is retryable");
                                        retryableCommand = true;
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    } while (event != null && !eventReceiver.isClosed() && !closeSse);
                    if (closeSse) {
                        try {
                            eventReceiver.close();
                        } catch (Exception exc) {
                        }
                    }
                } catch (IOException ioex) {
                    if (instanceId != null && "Premature EOF".equals(ioex.getMessage())) {
                        if (retryableCommand) {
                            throw new CommandException(STRINGS.get("remotecommand.lostConnection.retryableCommand", new Object[] { instanceId }), ioex);
                        } else {
                            throw new CommandException(STRINGS.get("remotecommand.lostConnection", new Object[] { instanceId }), ioex);
                        }
                    } else {
                        throw new CommandException(ioex.getMessage(), ioex);
                    }
                } catch (Exception ex) {
                    throw new CommandException(ex.getMessage(), ex);
                }
            } else {
                ProprietaryReader<ParamsWithPayload> reader = ProprietaryReaderFactory.getReader(ParamsWithPayload.class, resultMediaType);
                if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
                    ActionReport report;
                    if (reader == null) {
                        report = new CliActionReport();
                        report.setActionExitCode(ExitCode.FAILURE);
                        report.setMessage(urlConnection.getResponseMessage());
                    } else {
                        report = reader.readFrom(urlConnection.getErrorStream(), resultMediaType).getActionReport();
                    }
                    setActionReport(report);
                } else {
                    ParamsWithPayload pwp = reader.readFrom(urlConnection.getInputStream(), resultMediaType);
                    if (pwp.getPayloadInbound() == null) {
                        setActionReport(pwp.getActionReport());
                    } else if (resultMediaType.startsWith("multipart/")) {
                        RestPayloadImpl.Inbound inbound = pwp.getPayloadInbound();
                        setActionReport(pwp.getActionReport());
                        if (logger.isLoggable(Level.FINER)) {
                            logger.log(Level.FINER, "------ PAYLOAD ------");
                            Iterator<Payload.Part> parts = inbound.parts();
                            while (parts.hasNext()) {
                                Payload.Part part = parts.next();
                                logger.log(Level.FINER, " - {0} [{1}]", new Object[] { part.getName(), part.getContentType() });
                            }
                            logger.log(Level.FINER, "---- END PAYLOAD ----");
                        }
                        PayloadFilesManager downloadedFilesMgr = new PayloadFilesManager.Perm(fileOutputDir, null, logger, null);
                        try {
                            downloadedFilesMgr.processParts(inbound);
                        } catch (CommandException cex) {
                            throw cex;
                        } catch (Exception ex) {
                            throw new CommandException(ex.getMessage(), ex);
                        }
                    }
                }
            }
        }
    });
    if (actionReport == null) {
        this.output = null;
        throw new CommandException(STRINGS.get("emptyResponse"));
    }
    if (actionReport.getActionExitCode() == ExitCode.FAILURE) {
        throw new CommandException(STRINGS.getString("remote.failure.prefix", "remote failure:") + " " + this.output);
    }
}
Also used : CliActionReport(com.sun.enterprise.admin.remote.reader.CliActionReport) CliActionReport(com.sun.enterprise.admin.remote.reader.CliActionReport) ActionReport(org.glassfish.api.ActionReport) ProprietaryWriter(com.sun.enterprise.admin.remote.writer.ProprietaryWriter) GfSseEventReceiverProprietaryReader(com.sun.enterprise.admin.remote.sse.GfSseEventReceiverProprietaryReader) GfSseEventReceiverProprietaryReader(com.sun.enterprise.admin.remote.sse.GfSseEventReceiverProprietaryReader) ProprietaryReader(com.sun.enterprise.admin.remote.reader.ProprietaryReader) GfSseInboundEvent(com.sun.enterprise.admin.remote.sse.GfSseInboundEvent) GfSseEventReceiver(com.sun.enterprise.admin.remote.sse.GfSseEventReceiver) SSLException(javax.net.ssl.SSLException) JsonObject(javax.json.JsonObject) Payload(org.glassfish.api.admin.Payload) PayloadFilesManager(org.glassfish.admin.payload.PayloadFilesManager)

Example 2 with CliActionReport

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

the class RemoteRestAdminCommand method closeSse.

public void closeSse(String message, ActionReport.ExitCode exitCode) {
    ActionReport report = new CliActionReport();
    report.setMessage(message);
    report.setActionExitCode(exitCode);
    setActionReport(report);
    this.closeSse = true;
}
Also used : CliActionReport(com.sun.enterprise.admin.remote.reader.CliActionReport) CliActionReport(com.sun.enterprise.admin.remote.reader.CliActionReport) ActionReport(org.glassfish.api.ActionReport)

Example 3 with CliActionReport

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

the class RemoteRestAdminCommand method addCombinedMessages.

private static void addCombinedMessages(CliActionReport aReport, StringBuilder sb) {
    if (aReport == null || sb == null) {
        return;
    }
    // this is the message related to the topMessage
    String mainMsg = "";
    // this is the message related to failure cause
    String failMsg;
    // and also set report.setFailureCause(exception). We need to avoid the duplicate message.
    if (aReport.getMessage() != null && aReport.getMessage().length() != 0) {
        if (sb.length() > 0) {
            sb.append(EOL);
        }
        sb.append(aReport.getMessage());
    }
    if (aReport.getFailureCause() != null && aReport.getFailureCause().getMessage() != null && aReport.getFailureCause().getMessage().length() != 0) {
        failMsg = aReport.getFailureCause().getMessage();
        if (!failMsg.equals(mainMsg)) {
            if (sb.length() > 0) {
                sb.append(EOL);
            }
        }
        sb.append(failMsg);
    }
    for (CliActionReport sub : aReport.getSubActionsReport()) {
        addCombinedMessages(sub, sb);
    }
}
Also used : CliActionReport(com.sun.enterprise.admin.remote.reader.CliActionReport)

Aggregations

CliActionReport (com.sun.enterprise.admin.remote.reader.CliActionReport)3 ActionReport (org.glassfish.api.ActionReport)2 ProprietaryReader (com.sun.enterprise.admin.remote.reader.ProprietaryReader)1 GfSseEventReceiver (com.sun.enterprise.admin.remote.sse.GfSseEventReceiver)1 GfSseEventReceiverProprietaryReader (com.sun.enterprise.admin.remote.sse.GfSseEventReceiverProprietaryReader)1 GfSseInboundEvent (com.sun.enterprise.admin.remote.sse.GfSseInboundEvent)1 ProprietaryWriter (com.sun.enterprise.admin.remote.writer.ProprietaryWriter)1 JsonObject (javax.json.JsonObject)1 SSLException (javax.net.ssl.SSLException)1 PayloadFilesManager (org.glassfish.admin.payload.PayloadFilesManager)1 Payload (org.glassfish.api.admin.Payload)1