use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class DeploymentListingsBacking method 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);
String appId = firstValue(FormConstants.APP_ID, parameterMap);
String appVerId = firstValue("versionId", parameterMap);
String deploymentType = firstValue("deploymentType", parameterMap);
String processTarget = firstValue(FormConstants.PROCESS_TARGET, parameterMap);
Application app = null;
try {
app = modelManager.getModelService().findByPrimaryKey(Application.class, Long.valueOf(appId));
} catch (NumberFormatException nfe) {
events.add(new MessagesEvent("A valid applicationId must be supplied to either view or create deployments."));
}
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")));
// TODO: I'm pretty sure I should create new deployments elsewhere and forward to here from there.
if (deploymentType != null && PROCESS_TARGET.compareTo(processTarget) == 0 && app != null) {
ApplicationVersion version = null;
try {
version = modelManager.getModelService().findByPrimaryKey(ApplicationVersion.class, Long.valueOf(appVerId));
} catch (NumberFormatException nfe) {
events.add(new MessagesEvent("A valid versionId must be supplied to create a deployment."));
}
if (version != null) {
Deployment depl = createDeployment(firstValue("userPrincipalName", parameterMap), version, deploymentType);
try {
modelManager.begin();
depl = modelManager.addModify(depl, events);
modelManager.commit(events);
events.add(new MessagesEvent("Deployment successfully completed."));
} catch (Exception pe) {
modelManager.rollback();
Throwable root = ExceptionUtils.getRootCause(pe);
events.add(new MessagesEvent(String.format("An exception was thrown creating the deployment: %s %s", root.getMessage(), ExceptionUtils.getStackTrace(root))));
}
}
}
// making sure to order the deployments by date
if (app != null && app.getDeployments() != null) {
List<Deployment> deployments = modelManager.getModelService().findDeploymentsByApplication(app);
Collections.sort(deployments, new Deployment.DateComparator());
templateVariables.put("deployments", deployments);
GlobalSettings settings = modelManager.getGlobalSettings();
Map<String, String> urls = new HashMap<String, String>();
for (Deployment depl : deployments) {
urls.put(depl.getApplicationArchive().getHash(), depl.getApplicationArchive().getDownloadUrl(settings));
}
templateVariables.put("deployments", deployments);
templateVariables.put("archiveUrls", urls);
}
return events;
}
use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class GlobalSettingsBacking method process.
@Override
public Collection<ProcessingEvent> process(ProcessingContext context, Map<Object, Object> templateVariables, Map<Object, Object> parameterMap) {
List<ProcessingEvent> events = new ArrayList<ProcessingEvent>();
GlobalSettings settings = modelManager.getGlobalSettings();
// setup the variables required for form render
templateVariables.put(PROCESS_TARGET_PARAM, ProcessingTargets.GLOBAL_SETTINGS);
Boolean hasPerm = modelManager.getAuthorizer().may(Action.MODIFY, settings);
templateVariables.put("mayModify", hasPerm);
if (hasPerm == Boolean.FALSE) {
events.add(new MessagesEvent("The user logged in does not have permissions to change the global settings."));
}
if (!empty(PROCESS_TARGET_PARAM, parameterMap)) {
if (!empty(EXT_SVC_URL_PREFIX_PARAM, parameterMap)) {
String svcUrl = firstValue(EXT_SVC_URL_PREFIX_PARAM, parameterMap);
settings.setExternalServiceUrlPrefix(svcUrl);
}
if (!empty(MAX_FILE_UPLOAD_SIZE_PARAM, parameterMap)) {
Integer maxFileUploadSize = Integer.valueOf(firstValue(MAX_FILE_UPLOAD_SIZE_PARAM, parameterMap));
settings.setMaxFileUploadSize(maxFileUploadSize);
}
// process the storage path parameter
if (!empty(STORAGE_PATH_PARAM, parameterMap)) {
String path = firstValue(STORAGE_PATH_PARAM, parameterMap);
settings.setTemporaryStoragePath(path);
}
// process auth salt
if (!empty(AUTH_SALT_PARAM, parameterMap)) {
if (empty(AUTH_SALT_VERIFY_PARAM, parameterMap) || !equalsEachOther(AUTH_SALT_PARAM, AUTH_SALT_VERIFY_PARAM, parameterMap)) {
events.add(new MessagesEvent("Authentication salt and salt verify must match"));
} else {
settings.setServiceManagementAuthSalt(firstValue(AUTH_SALT_PARAM, parameterMap));
}
}
List<ClusterNode> toDelete = new ArrayList<ClusterNode>();
// process the ClusterNode objects
if (parameterMap.get(CLUSTER_NODE_URLS_PARAM) != null) {
String[] clusterNodeUrls = (String[]) parameterMap.get(CLUSTER_NODE_URLS_PARAM);
String[] clusterNodePaths = (String[]) parameterMap.get(CLUSTER_NODE_PATHS_PARAM);
int end = clusterNodeUrls.length;
// make sure there is a map in cluster nodes
List<ClusterNode> clusterNodes = settings.getClusterNodes();
if (clusterNodes == null) {
clusterNodes = new Vector<ClusterNode>();
settings.setClusterNodes(clusterNodes);
}
// iterate over each node configuration, updating the clusterNodes as per input
boolean warn = false;
for (int i = 0; i < end; i++) {
String thisNodeUrl = clusterNodeUrls[i].trim();
String thisNodePath = clusterNodePaths[i].trim();
if (thisNodeUrl.length() == 0 && warn == false) {
warn = true;
events.add(new MessagesEvent("A cluster node must be specified. The service url must be internally accessible by the administrative service, and should point to the services context. The rest of settings changes will be applied."));
continue;
}
// remove any nodes that no longer appear
List<String> urls = Arrays.asList(clusterNodeUrls);
List<String> urlsToRemove = new ArrayList<String>();
for (ClusterNode node : clusterNodes) {
if (!urls.contains(node.getServiceWebUrlPrefix())) {
urlsToRemove.add(node.getServiceWebUrlPrefix());
}
}
for (String url : urlsToRemove) {
ClusterNode node = settings.getClusterNode(url);
clusterNodes.remove(node);
modelManager.delete(node, events);
}
ClusterNode node = null;
if ((node = settings.getClusterNode(thisNodeUrl)) != null) {
node.setFileSystemStoragePathPrefix(thisNodePath);
} else {
ClusterNode thisNode = new ClusterNode();
thisNode.setServiceWebUrlPrefix(thisNodeUrl);
thisNode.setFileSystemStoragePathPrefix(thisNodePath);
settings.addClusterNode(thisNode);
}
}
// remove any nodes that no longer appear
List<String> urls = Arrays.asList(clusterNodeUrls);
for (ClusterNode node : settings.getClusterNodes()) {
if (!urls.contains(node.getServiceWebUrlPrefix())) {
toDelete.add(node);
}
}
}
try {
modelManager.begin();
if (toDelete != null) {
for (ClusterNode node : toDelete) {
settings.removeClusterNode(node);
modelManager.delete(node, events);
}
}
modelManager.addModify(settings, events);
modelManager.commit(events);
modelManager.refresh(settings, events);
events.add(new MessagesEvent("The settings were successfully modified."));
} catch (InvalidPropertiesException e) {
modelManager.rollback();
logger.info("Invalid properties submitted for an application", e);
events.add(new MessagesEvent(e.getMessage()));
} catch (PersistenceException e) {
modelManager.rollback();
logger.error("An exception occurred commiting the transaction", e);
events.add(new MessagesEvent(e.getMessage()));
}
try {
healthChecker.refreshSettings();
List<Exception> es = healthChecker.checkNowAndWait();
if (es.size() > 0) {
for (Exception e : es) {
events.add(new MessagesEvent(e.getMessage()));
}
}
} catch (InterruptedException e) {
logger.error("Exception occurred waiting on the health check thread after updating global settings", e);
events.add(new MessagesEvent(e.getMessage()));
}
}
if (settings.getExternalServiceUrlPrefix() != null) {
templateVariables.put(EXT_SVC_URL_PREFIX_PARAM, settings.getExternalServiceUrlPrefix());
}
if (settings.getTemporaryStoragePath() != null) {
templateVariables.put(STORAGE_PATH_PARAM, settings.getTemporaryStoragePath());
}
if (settings.getServiceManagementAuthSalt() != null) {
templateVariables.put(AUTH_SALT_PARAM, settings.getServiceManagementAuthSalt());
templateVariables.put(AUTH_SALT_VERIFY_PARAM, settings.getServiceManagementAuthSalt());
}
if (settings.getClusterNodes() != null && settings.getClusterNodes().size() > 0) {
if (healthChecker != null) {
for (ClusterNode node : settings.getClusterNodes()) {
ClusterNode checkerNode = healthChecker.getSettings().getClusterNode(node.getServiceWebUrlPrefix());
if (checkerNode != null) {
synchronized (checkerNode) {
node.setLastStatus(checkerNode.getLastStatus());
Date date = null;
node.setLastStatusCheck((Date) ((date = checkerNode.getLastStatusCheck()) != null ? date.clone() : null));
node.setLastStatusMessage(checkerNode.getLastStatusMessage());
}
}
}
}
templateVariables.put(CLUSTER_NODES_VAR, settings.getClusterNodes());
}
if (settings.getMaxFileUploadSize() != null) {
templateVariables.put(MAX_FILE_UPLOAD_SIZE_PARAM, settings.getMaxFileUploadSize());
}
if (events.size() > 0)
return events;
return null;
}
use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class AddModifyApplicationBackingTest method testCreateAndModifyApplications.
@Test
public void testCreateAndModifyApplications() {
ModelTestUtils.resetTestDb();
ModelTestUtils.createModel(null);
ModelManager mm = ModelTestUtils.createModelManager();
//////////////////////////
// Verify that we can use the backing to create a new application
Map<Object, Object> vars = new HashMap<Object, Object>();
Map<Object, Object> parms = new HashMap<Object, Object>();
AddModifyApplicationBacking amab = null;
parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
parms.put(FormConstants.APP_ID, new String[] {});
parms.put("name", new String[] { "Application.name.3" });
parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.3" });
parms.put("deviceTypes", new String[] { "1" });
amab = new AddModifyApplicationBacking();
amab.setModelManager(mm);
amab.process(null, vars, parms);
Assert.assertTrue(vars.get(FormConstants.PROCESS_TARGET) != null && ((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APP) == 0);
Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getName().compareTo("Application.name.3") == 0);
//////////////////////////
// Verify that inadequate data will throw an exception
// and that exception will manifest as an event returned
// that targets the message backing.
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
parms.put(FormConstants.APP_ID, new String[] {});
parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.4" });
parms.put("deviceTypes", new String[] { "1" });
amab = new AddModifyApplicationBacking();
amab.setModelManager(mm);
Collection<ProcessingEvent> events = amab.process(null, vars, parms);
Assert.assertTrue(events.size() > 0);
Integer numFound = 0;
for (ProcessingEvent event : events) {
if (event.getTargets()[0].compareTo(ProcessingTargets.MESSAGES) == 0) {
numFound++;
}
}
Assert.assertTrue(numFound == 1);
//////////////////////////
// Verify that we can use the backing to modify an application
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.PROCESS_TARGET, new String[] { ProcessingTargets.ADDMODIFY_APP });
// we happen to know that the model creates an application with id 1 that is not named Application.name.3
parms.put(FormConstants.APP_ID, new String[] { "1" });
parms.put("name", new String[] { "Application.name.1" });
parms.put(FormConstants.APP_DESCRIPTION, new String[] { "Application.description.1_modified" });
parms.put("deviceTypes", new String[] { "1", "2" });
amab = new AddModifyApplicationBacking();
amab.setModelManager(mm);
amab.process(null, vars, parms);
Assert.assertTrue(vars.get(FormConstants.PROCESS_TARGET) != null && ((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APP) == 0);
Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getDescription().compareTo("Application.description.1_modified") == 0);
// verify that the applicaiton we modified is otherwise uncorrupted.
Application app = mm.getModelService().findByPrimaryKey(Application.class, 1L);
Assert.assertTrue(app.getName() != null && app.getName().compareTo("Application.name.1") == 0);
}
use of com.openmeap.event.ProcessingEvent in project OpenMEAP by OpenMEAP.
the class AddModifyApplicationVersionsBackingTest method testFormSetup.
@Test
public void testFormSetup() {
ModelManager mm = modelManager;
//////////////////
// Verify the correct templateVariables are produced when no applicationId is passed in
// You cannot modify an app version without an application, so there will be a minimal return
Map<Object, Object> vars = new HashMap<Object, Object>();
Map<Object, Object> parms = new HashMap<Object, Object>();
AddModifyApplicationVersionBacking amab = new AddModifyApplicationVersionBacking();
amab.setModelManager(mm);
Collection<ProcessingEvent> events = amab.process(null, vars, parms);
Assert.assertTrue(events.size() == 1 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
Assert.assertTrue(vars.size() == 1 && vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
///////////////////////
// verify the correct templateVariables are produced with an invalid applcationId is passed
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.APP_ID, new String[] { "666" });
amab = new AddModifyApplicationVersionBacking();
amab.setModelManager(mm);
events = amab.process(null, vars, parms);
Assert.assertTrue(events.size() == 1 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
Assert.assertTrue(vars.size() == 1 && vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
/////////////////////
// verify the correct templateVariables are produced with an valid applcationId,
// but invalid versionId is passed
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.APP_ID, new String[] { "1" });
parms.put("versionId", new String[] { "666" });
amab = new AddModifyApplicationVersionBacking();
amab.setModelManager(mm);
events = amab.process(null, vars, parms);
Assert.assertTrue(events.size() == 4 && ProcessingUtils.containsTarget(events, ProcessingTargets.MESSAGES));
Assert.assertTrue(vars.size() == 6);
Assert.assertTrue(vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getName().compareTo("Application.name") == 0);
Assert.assertTrue(vars.get("version") != null && ((ApplicationVersion) vars.get("version")).getIdentifier() == null);
Assert.assertTrue(vars.get("hashTypes") != null && ((List) vars.get("hashTypes")).size() == HashAlgorithm.values().length);
Assert.assertTrue(((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APPVER) == 0);
//////////////////////
// verify the correct templateVariables are produced when
// both a valid app id and version id are passed in
vars = new HashMap<Object, Object>();
parms = new HashMap<Object, Object>();
parms.put(FormConstants.APP_ID, new String[] { "1" });
parms.put("versionId", new String[] { "1" });
amab = new AddModifyApplicationVersionBacking();
amab.setModelManager(mm);
events = amab.process(null, vars, parms);
Assert.assertTrue(events.size() == 3);
Assert.assertTrue(vars.size() == 6);
Assert.assertTrue(vars.get(FormConstants.ENCODING_TYPE).equals("enctype=\"" + FormConstants.ENCTYPE_MULTIPART_FORMDATA + "\""));
Assert.assertTrue(vars.get("application") != null && ((Application) vars.get("application")).getName().compareTo("Application.name") == 0);
Assert.assertTrue(vars.get("version") != null && ((ApplicationVersion) vars.get("version")).getIdentifier().compareTo("ApplicationVersion.identifier.1") == 0);
Assert.assertTrue(vars.get("hashTypes") != null && ((List) vars.get("hashTypes")).size() == HashAlgorithm.values().length);
Assert.assertTrue(((String) vars.get(FormConstants.PROCESS_TARGET)).compareTo(ProcessingTargets.ADDMODIFY_APPVER) == 0);
}
use of com.openmeap.event.ProcessingEvent 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;
}
Aggregations