Search in sources :

Example 1 with PayloadFilesManager

use of org.glassfish.admin.payload.PayloadFilesManager in project Payara by payara.

the class DeployerImpl method extractPayload.

/**
 * Extract the payload (client side stub jar files) to the directory specified via
 * --retrieve option.
 *
 * @param outboundPayload Payload to be extracted
 * @param actionReport    ActionReport of the deploy command.
 * @param retrieveDir     Directory where the payload should be extracted to.
 */
private void extractPayload(Payload.Outbound outboundPayload, ActionReport actionReport, File retrieveDir) {
    File payloadZip = null;
    FileOutputStream payloadOutputStream = null;
    FileInputStream payloadInputStream = null;
    try {
        /*
            * Add the report to the payload to mimic what the normal
            * non-embedded server does.
            */
        final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        actionReport.writeReport(baos);
        final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        final Properties reportProps = new Properties();
        reportProps.setProperty("data-request-type", "report");
        outboundPayload.addPart(0, actionReport.getContentType(), "report", reportProps, bais);
        /*
            * Now process the payload as an *inbound* payload as the non-embedded
            * admin client does, by writing the *outbound* payload to a temporary file
            * then reading from that file.
            */
        payloadZip = File.createTempFile("appclient", ".zip");
        payloadOutputStream = new FileOutputStream(payloadZip);
        outboundPayload.writeTo(payloadOutputStream);
        payloadOutputStream.flush();
        payloadOutputStream.close();
        /*
            * Use the temp file's contents as the inbound payload to
            * correctly process the downloaded files.
            */
        final PayloadFilesManager pfm = new PayloadFilesManager.Perm(retrieveDir, null, /* no action report to record extraction results */
        logger);
        payloadInputStream = new FileInputStream(payloadZip);
        final PayloadImpl.Inbound inboundPayload = PayloadImpl.Inbound.newInstance("application/zip", payloadInputStream);
        // explodes the payloadZip.
        pfm.processParts(inboundPayload);
    } catch (Exception ex) {
        // Log error and ignore exception.
        logger.log(Level.WARNING, ex.getMessage(), ex);
    } finally {
        if (payloadOutputStream != null) {
            try {
                payloadOutputStream.close();
            } catch (IOException ioex) {
                logger.log(Level.WARNING, ioex.getMessage());
            }
        }
        if (payloadInputStream != null) {
            try {
                payloadInputStream.close();
            } catch (IOException ioex) {
                logger.log(Level.WARNING, ioex.getMessage());
            }
        }
        if (payloadZip != null && !payloadZip.delete()) {
            logger.log(Level.WARNING, "Cannot delete payload: {0}", payloadZip.toString());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) PayloadFilesManager(org.glassfish.admin.payload.PayloadFilesManager) File(java.io.File) FileInputStream(java.io.FileInputStream) PayloadImpl(org.glassfish.admin.payload.PayloadImpl) CommandException(org.glassfish.api.admin.CommandException) GlassFishException(org.glassfish.embeddable.GlassFishException) IOException(java.io.IOException)

Example 2 with PayloadFilesManager

use of org.glassfish.admin.payload.PayloadFilesManager 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)

Aggregations

PayloadFilesManager (org.glassfish.admin.payload.PayloadFilesManager)2 CliActionReport (com.sun.enterprise.admin.remote.reader.CliActionReport)1 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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 JsonObject (javax.json.JsonObject)1 SSLException (javax.net.ssl.SSLException)1 PayloadImpl (org.glassfish.admin.payload.PayloadImpl)1 ActionReport (org.glassfish.api.ActionReport)1 CommandException (org.glassfish.api.admin.CommandException)1 Payload (org.glassfish.api.admin.Payload)1