Search in sources :

Example 11 with MessagesEvent

use of com.openmeap.event.MessagesEvent in project OpenMEAP by OpenMEAP.

the class ApplicationVersionListingsBacking method setupMayCreateDeployments.

private Boolean setupMayCreateDeployments(Map<Object, Object> templateVariables, Application app, List<ProcessingEvent> events) {
    Deployment authTestDepl = new Deployment();
    authTestDepl.setApplication(app);
    Boolean mayCreateDeployments = modelManager.getAuthorizer().may(Authorizer.Action.CREATE, authTestDepl);
    if (!mayCreateDeployments) {
        events.add(new MessagesEvent("NOTE: Current user does not have permissions to create deployments"));
    }
    authTestDepl = null;
    templateVariables.put("mayCreateDeployments", mayCreateDeployments);
    return mayCreateDeployments;
}
Also used : MessagesEvent(com.openmeap.event.MessagesEvent) Deployment(com.openmeap.model.dto.Deployment)

Example 12 with MessagesEvent

use of com.openmeap.event.MessagesEvent in project OpenMEAP by OpenMEAP.

the class ApplicationVersionListingsBacking method process.

public Collection<ProcessingEvent> process(ProcessingContext context, Map<Object, Object> templateVariables, Map<Object, Object> parameterMap) {
    List<ProcessingEvent> events = new ArrayList<ProcessingEvent>();
    if (ParameterMapUtils.notEmpty(FormConstants.APP_ID, parameterMap)) {
        Application app = modelManager.getModelService().findByPrimaryKey(Application.class, Long.valueOf(ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap)));
        if (app != null) {
            setupMayCreateDeployments(templateVariables, app, events);
            Deployment lastDeployment = modelManager.getModelService().getLastDeployment(app);
            String currentVersionId = null;
            if (lastDeployment != null && lastDeployment.getVersionIdentifier() != null) {
                currentVersionId = lastDeployment.getVersionIdentifier();
            }
            currentVersionId = currentVersionId != null ? currentVersionId : "";
            templateVariables.put("application", app);
            templateVariables.put(FormConstants.PROCESS_TARGET, ProcessingTargets.DEPLOYMENTS);
            templateVariables.put("currentVersionId", currentVersionId);
            if (app.getVersions() != null && app.getVersions().size() > 0) {
                createVersionsDisplayLists(app, templateVariables);
            } else {
                events.add(new MessagesEvent("Application with id " + ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap) + " has no versions associated to it"));
            }
            if (modelManager.getAuthorizer().may(Authorizer.Action.CREATE, new ApplicationVersion())) {
                events.add(new AddSubNavAnchorEvent(new Anchor("?bean=addModifyAppVersionPage&applicationId=" + app.getId(), "Create new version", "Create new version")));
            }
            events.add(new AddSubNavAnchorEvent(new Anchor("?bean=addModifyAppPage&applicationId=" + app.getId(), "View/Modify Application", "View/Modify Application")));
            Anchor deploymentHistoryAnchor = new Anchor("?bean=deploymentListingsPage&applicationId=" + app.getId(), "Deployment History", "Deployment History");
            templateVariables.put("deploymentsAnchor", deploymentHistoryAnchor);
            events.add(new AddSubNavAnchorEvent(deploymentHistoryAnchor));
        } else {
            events.add(new MessagesEvent("Application with id " + ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap) + " not found"));
        }
    } else {
        events.add(new MessagesEvent("An application must be selected"));
    }
    return events;
}
Also used : Anchor(com.openmeap.web.html.Anchor) ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) ArrayList(java.util.ArrayList) MessagesEvent(com.openmeap.event.MessagesEvent) AddSubNavAnchorEvent(com.openmeap.admin.web.events.AddSubNavAnchorEvent) Deployment(com.openmeap.model.dto.Deployment) ProcessingEvent(com.openmeap.event.ProcessingEvent) Application(com.openmeap.model.dto.Application)

Example 13 with MessagesEvent

use of com.openmeap.event.MessagesEvent 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 MessagesEvent

use of com.openmeap.event.MessagesEvent 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 MessagesEvent

use of com.openmeap.event.MessagesEvent in project OpenMEAP by OpenMEAP.

the class ArchiveFileHelper method unzipFile.

/**
 * @param archive
 * @param zipFile
 * @param events
 * @return TRUE if the file successfully is exploded, FALSE otherwise.
 */
public static Boolean unzipFile(ModelManager modelManager, FileOperationManager fileManager, ApplicationArchive archive, File zipFile, List<ProcessingEvent> events) {
    try {
        GlobalSettings settings = modelManager.getGlobalSettings();
        File dest = archive.getExplodedPath(settings.getTemporaryStoragePath());
        if (dest.exists()) {
            fileManager.delete(dest.getAbsolutePath());
        }
        ZipFile file = null;
        try {
            file = new ZipFile(zipFile);
            fileManager.unzipFile(file, archive.getHash());
        } finally {
            file.close();
        }
        return Boolean.TRUE;
    } catch (Exception e) {
        logger.error("An exception occurred unzipping the archive to the viewing location: {}", e);
        events.add(new MessagesEvent(String.format("An exception occurred unzipping the archive to the viewing location: %s", e.getMessage())));
    }
    return Boolean.FALSE;
}
Also used : ZipFile(java.util.zip.ZipFile) MessagesEvent(com.openmeap.event.MessagesEvent) GlobalSettings(com.openmeap.model.dto.GlobalSettings) File(java.io.File) ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) FileOperationException(com.openmeap.file.FileOperationException)

Aggregations

MessagesEvent (com.openmeap.event.MessagesEvent)15 PersistenceException (javax.persistence.PersistenceException)9 InvalidPropertiesException (com.openmeap.model.InvalidPropertiesException)6 ProcessingEvent (com.openmeap.event.ProcessingEvent)5 Application (com.openmeap.model.dto.Application)5 ApplicationArchive (com.openmeap.model.dto.ApplicationArchive)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 AddSubNavAnchorEvent (com.openmeap.admin.web.events.AddSubNavAnchorEvent)4 ApplicationVersion (com.openmeap.model.dto.ApplicationVersion)4 Anchor (com.openmeap.web.html.Anchor)4 File (java.io.File)4 Deployment (com.openmeap.model.dto.Deployment)3 ZipFile (java.util.zip.ZipFile)3 FileOperationException (com.openmeap.file.FileOperationException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 DigestException (com.openmeap.digest.DigestException)1 ClusterNode (com.openmeap.model.dto.ClusterNode)1 GenericProcessingEvent (com.openmeap.web.GenericProcessingEvent)1