Search in sources :

Example 11 with ApplicationVersion

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

the class ModelServiceImplTest method testDelete.

// putting this last because it corrupts the model
public void testDelete() {
    // verify that we 
    Application app = modelService.findByPrimaryKey(Application.class, 1L);
    ApplicationVersion appVer = modelService.findByPrimaryKey(ApplicationVersion.class, 1L);
    modelService.delete(appVer);
    appVer = modelService.findByPrimaryKey(ApplicationVersion.class, 1L);
    Assert.assertTrue(appVer == null);
    app = modelService.findByPrimaryKey(Application.class, 1L);
    modelService.delete(app);
    app = modelService.findByPrimaryKey(Application.class, 1L);
    appVer = modelService.findByPrimaryKey(ApplicationVersion.class, 2L);
    Assert.assertTrue(appVer != null);
}
Also used : ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) Application(com.openmeap.model.dto.Application)

Example 12 with ApplicationVersion

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

the class AddModifyApplicationBacking method process.

/**
	 * With the first of the bean name matching "addModifyApp", there are
	 * three ways to access this:
	 *    - request has applicationId and processTarget - modifying an application
	 *    - request has applicationId only              - pulling up an application to modify
	 *    - request has processTarget only              - submitting a brand new application  
	 *
	 * See the WEB-INF/ftl/form-application-addmodify.ftl for input/output parameters.
	 *    
	 * @param context Not referenced at all, may be null
	 * @param templateVariables Variables output to for the view
	 * @param parameterMap Parameters passed in to drive processing
	 * @return on errors, returns an array of error processingevents
	 * @see TemplatedSectionBacking::process()
	 */
public Collection<ProcessingEvent> process(ProcessingContext context, Map<Object, Object> templateVariables, Map<Object, Object> parameterMap) {
    List<ProcessingEvent> events = new ArrayList<ProcessingEvent>();
    templateVariables.put(FormConstants.PROCESS_TARGET, PROCESS_TARGET);
    Application app = new Application();
    if (ParameterMapUtils.notEmpty(FormConstants.APP_ID, parameterMap)) {
        app = modelManager.getModelService().findByPrimaryKey(Application.class, Long.valueOf(ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap)));
    }
    Boolean mayCreate = modelManager.getAuthorizer().may(Authorizer.Action.CREATE, new Application());
    Boolean mayModify = modelManager.getAuthorizer().may(Authorizer.Action.MODIFY, app);
    Boolean willProcess = mayCreate || mayModify;
    if (!willProcess) {
        events.add(new MessagesEvent("Current user does not have permissions to make changes here"));
    }
    templateVariables.put("willProcess", willProcess);
    // the user is submitting the form for either an add or modify
    if (ParameterMapUtils.notEmpty(FormConstants.PROCESS_TARGET, parameterMap) && PROCESS_TARGET.equals(((String[]) parameterMap.get(FormConstants.PROCESS_TARGET))[0]) && willProcess) {
        app = createApplicationFromParameters(app, parameterMap, events);
        if (ParameterMapUtils.firstValue("submit", parameterMap).equals("true")) {
            if (events.size() == 0) {
                try {
                    app.setLastModifier(firstValue("userPrincipalName", parameterMap));
                    modelManager.begin();
                    app = modelManager.addModify(app, events);
                    modelManager.commit(events);
                    events.add(new MessagesEvent("Application successfully created/modified!"));
                } catch (InvalidPropertiesException e) {
                    events.add(new MessagesEvent(String.format("Application add/modify failed: %s %s", ExceptionUtils.getRootCauseMessage(e), ExceptionUtils.getRootCauseStackTrace(e)[0])));
                    logger.error("Add/Modify application with id " + app.getId() + " failed", e);
                    modelManager.rollback();
                } catch (PersistenceException e) {
                    events.add(new MessagesEvent(String.format("Application add/modify failed: %s %s", ExceptionUtils.getRootCauseMessage(e), ExceptionUtils.getRootCauseStackTrace(e)[0])));
                    logger.error("Add/Modify application with id " + app.getId() + " failed", e);
                    modelManager.rollback();
                }
            }
            if (app == null && ParameterMapUtils.notEmpty(FormConstants.APP_ID, parameterMap))
                app = modelManager.getModelService().findByPrimaryKey(Application.class, Long.valueOf(ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap)));
        }
        if (ParameterMapUtils.notEmpty("delete", parameterMap) && ParameterMapUtils.firstValue("delete", parameterMap).equals("true")) {
            if (!ParameterMapUtils.empty("deleteConfirm", parameterMap) && ParameterMapUtils.firstValue("deleteConfirm", parameterMap).equals(FormConstants.APP_DELETE_CONFIRM_TEXT)) {
                try {
                    modelManager.begin();
                    modelManager.delete(app, events);
                    modelManager.commit(events);
                    events.add(new MessagesEvent("Application successfully deleted!"));
                    app = null;
                    // we remove the applicationId parameter, so that the form can populate empty
                    parameterMap.remove(FormConstants.APP_ID);
                } catch (Exception e) {
                    events.add(new MessagesEvent(String.format("Application delete failed: %s %s", ExceptionUtils.getRootCauseMessage(e), ExceptionUtils.getRootCauseStackTrace(e)[0])));
                    logger.error("Deleting application with id " + app.getId() + " failed", e);
                    modelManager.rollback();
                }
            } else {
                events.add(new MessagesEvent("You must confirm your desire to delete by typing in the delete confirmation message."));
            }
        }
    } else // the user is visiting the page to view or modify an application
    if (ParameterMapUtils.notEmpty(FormConstants.APP_ID, parameterMap)) {
        app = modelManager.getModelService().findByPrimaryKey(Application.class, Long.valueOf(ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap)));
    }
    if (app == null && ParameterMapUtils.notEmpty(FormConstants.APP_ID, parameterMap)) {
        events.add(new MessagesEvent("Application with id " + ParameterMapUtils.firstValue(FormConstants.APP_ID, parameterMap) + " not found"));
    } else if (app != null && app.getId() != null) {
        // in order to create the 
        ApplicationVersion testVer = new ApplicationVersion();
        testVer.setApplication(app);
        Boolean mayCreateVersions = modelManager.getAuthorizer().may(Authorizer.Action.CREATE, testVer);
        if (mayCreateVersions) {
            events.add(new AddSubNavAnchorEvent(new Anchor("?bean=addModifyAppVersionPage&applicationId=" + app.getId(), "Create new version", "Create new version")));
        }
        events.add(new AddSubNavAnchorEvent(new Anchor("?bean=appVersionListingsPage&applicationId=" + app.getId(), "Version Listings", "Version Listings")));
        events.add(new AddSubNavAnchorEvent(new Anchor("?bean=deploymentListingsPage&applicationId=" + app.getId(), "Deployment History", "Deployment History")));
    }
    fillInVariablesFromApplication(templateVariables, app);
    return events;
}
Also used : ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) ArrayList(java.util.ArrayList) AddSubNavAnchorEvent(com.openmeap.admin.web.events.AddSubNavAnchorEvent) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) PersistenceException(javax.persistence.PersistenceException) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) Anchor(com.openmeap.web.html.Anchor) MessagesEvent(com.openmeap.event.MessagesEvent) PersistenceException(javax.persistence.PersistenceException) ProcessingEvent(com.openmeap.event.ProcessingEvent) Application(com.openmeap.model.dto.Application)

Example 13 with ApplicationVersion

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

the class AddModifyApplicationVersionBacking method process.

/**
	 * With the first of the bean name matching "addModifyApp", there are
	 * three ways to access this:
	 *    - request has applicationId and processTarget - modifying an application
	 *    - request has applicationId only              - pulling up an application to modify
	 *    - request has processTarget only              - submitting a brand new application  
	 *
	 * See the WEB-INF/ftl/form-application-addmodify.ftl for input/output parameters.
	 *    
	 * @param context Not referenced at all, may be null
	 * @param templateVariables Variables output to for the view
	 * @param parameterMap Parameters passed in to drive processing
	 * @return on errors, returns an array of error processingevents
	 * @see TemplatedSectionBacking::process()
	 */
public Collection<ProcessingEvent> process(ProcessingContext context, Map<Object, Object> templateVariables, Map<Object, Object> parameterMap) {
    List<ProcessingEvent> events = new ArrayList<ProcessingEvent>();
    Application app = null;
    ApplicationVersion version = null;
    // make sure we're configured to accept uploads, warn otherwise
    validateStorageConfiguration(templateVariables, events);
    // we must have an application in order to add a version
    if (!notEmpty(FormConstants.APP_ID, parameterMap)) {
        return ProcessingUtils.newList(new GenericProcessingEvent<String>(ProcessingTargets.MESSAGES, "An application must be specified in order to add a version"));
    }
    Long appId = Long.valueOf(firstValue(FormConstants.APP_ID, parameterMap));
    app = modelManager.getModelService().findByPrimaryKey(Application.class, appId);
    if (app == null) {
        return ProcessingUtils.newList(new GenericProcessingEvent<String>(ProcessingTargets.MESSAGES, "The application with id " + appId + " could not be found."));
    }
    events.add(new AddSubNavAnchorEvent(new Anchor("?bean=addModifyAppPage&applicationId=" + app.getId(), "View/Modify Application", "View/Modify Application")));
    events.add(new AddSubNavAnchorEvent(new Anchor("?bean=appVersionListingsPage&applicationId=" + app.getId(), "Version Listings", "Version Listings")));
    events.add(new AddSubNavAnchorEvent(new Anchor("?bean=deploymentListingsPage&applicationId=" + app.getId(), "Deployment History", "Deployment History")));
    // at this point, we're committed to form setup at least
    templateVariables.put(FormConstants.PROCESS_TARGET, PROCESS_TARGET);
    version = obtainExistingApplicationVersionFromParameters(app, appId, events, parameterMap);
    if (version == null) {
        version = new ApplicationVersion();
    }
    // determine if the user is allowed to modify application versions
    Boolean willProcess = canUserModifyOrCreate(app, version);
    if (!willProcess) {
        events.add(new MessagesEvent("Current user does not have permissions to make changes here."));
    }
    if (!version.getActiveFlag()) {
        events.add(new MessagesEvent("This version is not currently active."));
        willProcess = false;
    }
    templateVariables.put("willProcess", willProcess);
    if (notEmpty(FormConstants.PROCESS_TARGET, parameterMap) && PROCESS_TARGET.compareTo(firstValue(FormConstants.PROCESS_TARGET, parameterMap)) == 0 && willProcess) {
        // TODO: check to see if the user can delete versions
        if (ParameterMapUtils.notEmpty(FormConstants.DELETE, parameterMap) && ParameterMapUtils.notEmpty("deleteConfirm", parameterMap)) {
            if (ParameterMapUtils.firstValue("deleteConfirm", parameterMap).equals(FormConstants.APPVER_DELETE_CONFIRM_TEXT)) {
                try {
                    modelManager.begin();
                    modelManager.delete(version, events);
                    modelManager.commit(events);
                } catch (Exception e) {
                    modelManager.rollback();
                    String msg = String.format("Unable to delete the version - %s", ExceptionUtils.getRootCauseMessage(e));
                    logger.error(msg, e);
                    events.add(new MessagesEvent(msg));
                }
            } else {
                events.add(new MessagesEvent("You must confirm your desire to delete by typing in the delete confirmation message."));
            }
        } else {
            processApplicationVersionFromParameters(app, version, events, parameterMap);
        }
    }
    if (version != null) {
        templateVariables.put("version", version);
    }
    templateVariables.put("application", app);
    createHashTypes(templateVariables, version != null ? version.getArchive() : null);
    return events;
}
Also used : ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) ArrayList(java.util.ArrayList) AddSubNavAnchorEvent(com.openmeap.admin.web.events.AddSubNavAnchorEvent) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Anchor(com.openmeap.web.html.Anchor) MessagesEvent(com.openmeap.event.MessagesEvent) GenericProcessingEvent(com.openmeap.web.GenericProcessingEvent) ProcessingEvent(com.openmeap.event.ProcessingEvent) Application(com.openmeap.model.dto.Application)

Example 14 with ApplicationVersion

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

the class ApplicationManagementServlet method handleArchiveDownload.

private Result handleArchiveDownload(HttpServletRequest request, HttpServletResponse response) {
    Result res = new Result();
    Error err = new Error();
    res.setError(err);
    GlobalSettings settings = modelManager.getGlobalSettings();
    Map properties = this.getServicesWebProperties();
    String nodeKey = (String) properties.get("clusterNodeUrlPrefix");
    ClusterNode clusterNode = settings.getClusterNode(nodeKey);
    if (nodeKey == null || clusterNode == null) {
        // TODO: create a configuration error code
        err.setCode(ErrorCode.UNDEFINED);
        err.setMessage("A configuration is missing.  Please consult the error logs.");
        logger.error("For each node in the cluster, the property or environment variable OPENMEAP_CLUSTER_NODE_URL_PREFIX must match the \"Service Url Prefix\" value configured in the administrative interface.  This value is currently " + nodeKey + ".");
        return res;
    }
    String pathValidation = clusterNode.validateFileSystemStoragePathPrefix();
    if (pathValidation != null) {
        err.setCode(ErrorCode.UNDEFINED);
        err.setMessage("A configuration is missing.  Please consult the error logs.");
        logger.error("There is an issue with the location at \"File-system Storage Prefix\".  " + pathValidation);
        return res;
    }
    String hash = request.getParameter(UrlParamConstants.APPARCH_HASH);
    String hashAlg = request.getParameter(UrlParamConstants.APPARCH_HASH_ALG);
    String fileName = null;
    if (hash == null || hashAlg == null) {
        // look in the apps directory for the archive specified
        String appName = request.getParameter(UrlParamConstants.APP_NAME);
        String versionId = request.getParameter(UrlParamConstants.APP_VERSION);
        ApplicationVersion appVersion = modelManager.getModelService().findAppVersionByNameAndId(appName, versionId);
        if (appVersion == null) {
            String mesg = "The application version " + versionId + " was not found for application " + appName;
            err.setCode(ErrorCode.APPLICATION_VERSION_NOTFOUND);
            err.setMessage(mesg);
            logger.warn(mesg);
            return res;
        }
        String auth = request.getParameter(UrlParamConstants.AUTH_TOKEN);
        com.openmeap.model.dto.Application app = appVersion.getApplication();
        try {
            if (auth == null || !AuthTokenProvider.validateAuthToken(app.getProxyAuthSalt(), auth)) {
                err.setCode(ErrorCode.AUTHENTICATION_FAILURE);
                err.setMessage("The \"auth\" token presented is not recognized, missing, or empty.");
                return res;
            }
        } catch (DigestException e) {
            throw new GenericRuntimeException(e);
        }
        hash = appVersion.getArchive().getHash();
        hashAlg = appVersion.getArchive().getHashAlgorithm();
        fileName = app.getName() + " - " + appVersion.getIdentifier();
    } else {
        fileName = hashAlg + "-" + hash;
    }
    File file = ApplicationArchive.getFile(clusterNode.getFileSystemStoragePathPrefix(), hashAlg, hash);
    if (!file.exists()) {
        String mesg = "The application archive with " + hashAlg + " hash " + hash + " was not found.";
        // TODO: create an enumeration for this error
        err.setCode(ErrorCode.UNDEFINED);
        err.setMessage(mesg);
        logger.warn(mesg);
        return res;
    }
    try {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(file.toURL().toString());
        response.setContentType(mimeType);
        response.setContentLength(Long.valueOf(file.length()).intValue());
        URLCodec codec = new URLCodec();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".zip\";");
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            outputStream = response.getOutputStream();
            Utils.pipeInputStreamIntoOutputStream(inputStream, outputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        //if(outputStream!=null) {outputStream.close();}
        }
        response.flushBuffer();
    } catch (FileNotFoundException e) {
        logger.error("Exception {}", e);
    } catch (IOException ioe) {
        logger.error("Exception {}", ioe);
    }
    return null;
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) Error(com.openmeap.protocol.dto.Error) GlobalSettings(com.openmeap.model.dto.GlobalSettings) IOException(java.io.IOException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) FileNameMap(java.net.FileNameMap) FileInputStream(java.io.FileInputStream) Result(com.openmeap.protocol.dto.Result) URLCodec(org.apache.commons.codec.net.URLCodec) BufferedInputStream(java.io.BufferedInputStream) DigestException(com.openmeap.digest.DigestException) FileNameMap(java.net.FileNameMap) Map(java.util.Map) File(java.io.File)

Example 15 with ApplicationVersion

use of com.openmeap.model.dto.ApplicationVersion 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)

Aggregations

ApplicationVersion (com.openmeap.model.dto.ApplicationVersion)26 Application (com.openmeap.model.dto.Application)12 ProcessingEvent (com.openmeap.event.ProcessingEvent)7 GlobalSettings (com.openmeap.model.dto.GlobalSettings)6 ApplicationArchive (com.openmeap.model.dto.ApplicationArchive)5 Anchor (com.openmeap.web.html.Anchor)5 HashMap (java.util.HashMap)5 AddSubNavAnchorEvent (com.openmeap.admin.web.events.AddSubNavAnchorEvent)4 MessagesEvent (com.openmeap.event.MessagesEvent)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 PersistenceException (javax.persistence.PersistenceException)4 InvalidPropertiesException (com.openmeap.model.InvalidPropertiesException)3 Deployment (com.openmeap.model.dto.Deployment)3 Result (com.openmeap.protocol.dto.Result)3 AddModifyApplicationVersionBacking (com.openmeap.admin.web.backing.AddModifyApplicationVersionBacking)2 ClusterNotificationException (com.openmeap.cluster.ClusterNotificationException)2 EventNotificationException (com.openmeap.event.EventNotificationException)2 GenericProcessingEvent (com.openmeap.web.GenericProcessingEvent)2 IOException (java.io.IOException)2