Search in sources :

Example 11 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class ArchiveFileDeleteHandler method handle.

@Override
public <E extends Event<Map>> void handle(E event) throws EventHandlingException {
    if (logger.isTraceEnabled()) {
        logger.trace("entering handle()");
    }
    ApplicationArchive archive = (ApplicationArchive) event.getPayload().get("archive");
    File file = archive.getFile(getFileSystemStoragePathPrefix());
    if (file.exists()) {
        if (!file.delete()) {
            logger.error("Failed to delete archive " + archive.getFile(getFileSystemStoragePathPrefix()));
        }
    } else {
        logger.warn("Failed to find archive " + archive.getFile(getFileSystemStoragePathPrefix()) + ".  It may have yet to be deployed.");
    }
    File directory = archive.getExplodedPath(getFileSystemStoragePathPrefix());
    if (directory.exists()) {
        try {
            FileUtils.deleteDirectory(directory);
        } catch (IOException ioe) {
            String msg = "Unable to delete directory " + directory;
            logger.error(msg);
            throw new EventHandlingException(msg, ioe);
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("exiting handle()");
    }
}
Also used : EventHandlingException(com.openmeap.event.EventHandlingException) IOException(java.io.IOException) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive) File(java.io.File)

Example 12 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class ArchiveFileUploadHandler method handle.

@Override
public <E extends Event<Map>> void handle(E event) throws ClusterHandlingException {
    Map parms = event.getPayload();
    ApplicationArchive arch = (ApplicationArchive) parms.get("archive");
    String hashId = String.format("{%s}%s", arch.getHashAlgorithm(), arch.getHash());
    logger.debug("ArchiveUploadEvent for file {}", hashId);
    File file = arch.getFile(getFileSystemStoragePathPrefix());
    if (file.exists()) {
        logger.warn("ApplicationArchive with {} hash already exists, ignoring ArchiveUploadEvent.", hashId);
        return;
    }
    if (parms.get("file") == null || !(parms.get("file") instanceof FileItem)) {
        logger.error("Expected a FileItem under the \"file\" parameter.  Got " + parms.get("file") + " instead.");
        throw new ClusterHandlingException("Expected a FileItem under the \"file\" parameter.  Got " + parms.get("file") + " instead.");
    }
    FileItem item = (FileItem) parms.get("file");
    try {
        item.write(file);
    } catch (Exception ioe) {
        logger.error("An error occurred writing {}: {}", item.getName(), ioe);
        throw new ClusterHandlingException(ioe);
    }
    item.delete();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ClusterHandlingException(com.openmeap.cluster.ClusterHandlingException) Map(java.util.Map) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive) File(java.io.File) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) ClusterHandlingException(com.openmeap.cluster.ClusterHandlingException)

Example 13 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class ApplicationVersionDeleteNotifier method onAfterOperation.

@Override
public <E extends Event<ApplicationVersion>> void onAfterOperation(E event, List<ProcessingEvent> events) throws EventNotificationException {
    ApplicationArchive archive = archives.get(Thread.currentThread());
    if (archive != null) {
        ArchiveFileHelper.maintainFileSystemCleanliness(modelManager, fileManager, archive, events);
    }
    events.add(new MessagesEvent("Application version successfully deleted!"));
}
Also used : MessagesEvent(com.openmeap.event.MessagesEvent) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive)

Example 14 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class ArchiveFileUploadNotifier method processApplicationArchiveFileUpload.

@SuppressWarnings("unchecked")
private ApplicationArchive processApplicationArchiveFileUpload(ApplicationArchive archive, List<ProcessingEvent> events) {
    GlobalSettings settings = modelManager.getGlobalSettings();
    File tempFile = new File(archive.getNewFileUploaded());
    Long size = tempFile.length();
    String pathError = settings.validateTemporaryStoragePath();
    if (pathError != null) {
        logger.error("There is an issue with the global settings temporary storage path: " + settings.validateTemporaryStoragePath() + "\n {}", pathError);
        events.add(new MessagesEvent("There is an issue with the global settings temporary storage path: " + settings.validateTemporaryStoragePath() + " - " + pathError));
        return null;
    }
    // determine the md5 hash of the uploaded file
    // and rename the temp file by the hash
    FileInputStream is = null;
    File destinationFile = null;
    try {
        // that is, they are not used by any other versions
        if (archive.getId() != null) {
            ArchiveFileHelper.maintainFileSystemCleanliness(modelManager, fileManager, archive, events);
        }
        String hashValue = null;
        ApplicationArchive archiveExists = null;
        try {
            is = new FileInputStream(tempFile);
            hashValue = Utils.hashInputStream("MD5", is);
            archiveExists = modelManager.getModelService().findApplicationArchiveByHashAndAlgorithm(archive.getApplication(), hashValue, "MD5");
            if (archiveExists != null) {
                if (!tempFile.delete()) {
                    String mesg = String.format("Failed to delete temporary file %s", tempFile.getName());
                    logger.error(mesg);
                    events.add(new MessagesEvent(mesg));
                }
                return archiveExists;
            } else {
                // if an archive of the hash/alg doesn't exist, 
                // then we don't want to accidentally change an existing one.
                archive = archive.clone();
                archive.setId(null);
            }
        } catch (DigestException de) {
            throw new PersistenceException(de);
        } finally {
            is.close();
        }
        archive.setHashAlgorithm("MD5");
        archive.setHash(hashValue);
        destinationFile = archive.getFile(settings.getTemporaryStoragePath());
        // even though they are theoretically the same.
        if (destinationFile.exists() && !destinationFile.delete()) {
            String mesg = String.format("Failed to delete old file (theoretically the same anyways, so proceeding) %s", destinationFile.getName());
            logger.error(mesg);
            events.add(new MessagesEvent(mesg));
            if (!tempFile.delete()) {
                mesg = String.format("Failed to delete temporary file %s", tempFile.getName());
                logger.error(mesg);
                events.add(new MessagesEvent(mesg));
            }
        } else // into the web-view directory
        if (tempFile.renameTo(destinationFile)) {
            String mesg = String.format("Uploaded temporary file %s successfully renamed to %s", tempFile.getName(), destinationFile.getName());
            logger.debug(mesg);
            events.add(new MessagesEvent(mesg));
            ArchiveFileHelper.unzipFile(modelManager, fileManager, archive, destinationFile, events);
        } else {
            String mesg = String.format("Failed to renamed file %s to %s", tempFile.getName(), destinationFile.getName());
            logger.error(mesg);
            events.add(new MessagesEvent(mesg));
            return null;
        }
    } catch (IOException ioe) {
        events.add(new MessagesEvent(ioe.getMessage()));
        return null;
    }
    // determine the compressed and uncompressed size of the zip archive
    try {
        archive.setBytesLength(size.intValue());
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(destinationFile);
            Integer uncompressedSize = ZipUtils.getUncompressedSize(zipFile).intValue();
            archive.setBytesLengthUncompressed(new Long(uncompressedSize).intValue());
        } finally {
            if (zipFile != null) {
                zipFile.close();
            }
        }
    } catch (IOException ioe) {
        logger.error("An exception occurred while calculating the uncompressed size of the archive: {}", ioe);
        events.add(new MessagesEvent(String.format("An exception occurred while calculating the uncompressed size of the archive: %s", ioe.getMessage())));
        return null;
    }
    archive.setUrl(ApplicationArchive.URL_TEMPLATE);
    return archive;
}
Also used : ZipFile(java.util.zip.ZipFile) MessagesEvent(com.openmeap.event.MessagesEvent) DigestException(com.openmeap.digest.DigestException) PersistenceException(javax.persistence.PersistenceException) GlobalSettings(com.openmeap.model.dto.GlobalSettings) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive) FileInputStream(java.io.FileInputStream)

Example 15 with ApplicationArchive

use of com.openmeap.model.dto.ApplicationArchive in project OpenMEAP by OpenMEAP.

the class ArchiveFileUploadNotifier method onBeforeOperation.

@Override
public <E extends Event<ApplicationArchive>> void onBeforeOperation(E event, List<ProcessingEvent> events) throws EventNotificationException {
    ApplicationArchive archive = (ApplicationArchive) event.getPayload();
    boolean newUpload = archive.getNewFileUploaded() != null;
    ApplicationArchive ret = archive;
    if (newUpload && (ret = processApplicationArchiveFileUpload(archive, events)) == null) {
        throw new PersistenceException("Zip archive failed to process!");
    } else {
        event.setPayload(ret);
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) ApplicationArchive(com.openmeap.model.dto.ApplicationArchive)

Aggregations

ApplicationArchive (com.openmeap.model.dto.ApplicationArchive)19 File (java.io.File)6 MessagesEvent (com.openmeap.event.MessagesEvent)5 Application (com.openmeap.model.dto.Application)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)5 ApplicationVersion (com.openmeap.model.dto.ApplicationVersion)4 Deployment (com.openmeap.model.dto.Deployment)4 IOException (java.io.IOException)4 PersistenceException (javax.persistence.PersistenceException)4 DigestException (com.openmeap.digest.DigestException)3 EventHandlingException (com.openmeap.event.EventHandlingException)3 InvalidPropertiesException (com.openmeap.model.InvalidPropertiesException)3 ClusterNotificationException (com.openmeap.cluster.ClusterNotificationException)2 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)2 FileInputStream (java.io.FileInputStream)2 ZipFile (java.util.zip.ZipFile)2 FileItem (org.apache.commons.fileupload.FileItem)2 ClusterHandlingException (com.openmeap.cluster.ClusterHandlingException)1 ModelEntity (com.openmeap.model.ModelEntity)1 ModelManager (com.openmeap.model.ModelManager)1