use of com.openmeap.model.dto.GlobalSettings in project OpenMEAP by OpenMEAP.
the class AdminTest method testUpdateGlobalSettings.
public void testUpdateGlobalSettings() throws Exception {
// correct location of storage path prefix
GlobalSettings originalSettings = new GlobalSettings();
originalSettings.setExternalServiceUrlPrefix(AdminTestHelper.SERVICES_WEB_URL);
originalSettings.setMaxFileUploadSize(1234550);
originalSettings.setServiceManagementAuthSalt(AdminTestHelper.SERVICES_WEB_AUTH_SALT);
originalSettings.setTemporaryStoragePath(AdminTestHelper.ADMIN_WEB_STORAGE);
// correct cluster node location and path prefix
ClusterNode node = new ClusterNode();
node.setServiceWebUrlPrefix(AdminTestHelper.NODE_01_SERVICES_URL);
node.setFileSystemStoragePathPrefix(AdminTestHelper.NODE_01_STORAGE);
originalSettings.addClusterNode(node);
// validate settings stored in database
String returnBody = Utils.readInputStream(helper.postGlobalSettings(originalSettings).getResponseBody(), FormConstants.CHAR_ENC_DEFAULT);
logger.info(returnBody);
modelManager.getModelService().clearPersistenceContext();
GlobalSettings insertedSettings = modelManager.getGlobalSettings();
JSONObjectBuilder job = new JSONObjectBuilder();
String originalSettingsJSON = job.toJSON(originalSettings).toString(3);
String insertedSettingsJSON = job.toJSON(insertedSettings).toString(3);
logger.info("original: {}", originalSettingsJSON);
logger.info("inserted: {}", insertedSettingsJSON);
Assert.assertEquals(originalSettingsJSON, insertedSettingsJSON);
}
use of com.openmeap.model.dto.GlobalSettings 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;
}
use of com.openmeap.model.dto.GlobalSettings in project OpenMEAP by OpenMEAP.
the class ModelManagerImpl method getGlobalSettings.
@Override
public GlobalSettings getGlobalSettings() {
GlobalSettings settings = modelService.findByPrimaryKey(GlobalSettings.class, (Long) 1L);
boolean update = false;
if (settings == null) {
settings = new GlobalSettings();
settings.setServiceManagementAuthSalt(UUID.randomUUID().toString());
update = true;
}
if (settings.getServiceManagementAuthSalt() == null || settings.getServiceManagementAuthSalt().trim().length() == 0) {
settings.setServiceManagementAuthSalt(UUID.randomUUID().toString());
update = true;
}
if (update) {
try {
modelService.begin();
settings = modelService.saveOrUpdate(settings);
modelService.commit();
} catch (Exception e) {
modelService.rollback();
throw new PersistenceException(e);
}
}
return settings;
}
use of com.openmeap.model.dto.GlobalSettings 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;
}
use of com.openmeap.model.dto.GlobalSettings in project OpenMEAP by OpenMEAP.
the class ApplicationManagementServiceImpl method connectionOpen.
public ConnectionOpenResponse connectionOpen(ConnectionOpenRequest request) throws WebServiceException {
String reqAppArchHashVal = StringUtils.trimToNull(request.getApplication().getHashValue());
String reqAppVerId = StringUtils.trimToNull(request.getApplication().getVersionId());
String reqAppName = StringUtils.trimToNull(request.getApplication().getName());
ConnectionOpenResponse response = new ConnectionOpenResponse();
GlobalSettings settings = modelManager.getGlobalSettings();
if (StringUtils.isBlank(settings.getExternalServiceUrlPrefix()) && logger.isWarnEnabled()) {
logger.warn("The external service url prefix configured in the admin interface is blank. " + "This will probably cause issues downloading application archives.");
}
Application application = getApplication(reqAppName, reqAppVerId);
// Generate a new auth token for the device to present to the proxy
String authToken;
try {
authToken = AuthTokenProvider.newAuthToken(application.getProxyAuthSalt());
} catch (DigestException e) {
throw new GenericRuntimeException(e);
}
response.setAuthToken(authToken);
// If there is a deployment,
// and the version of that deployment differs in hash value or identifier
// then return an update in the response
Deployment lastDeployment = modelManager.getModelService().getLastDeployment(application);
Boolean reqAppVerDiffers = lastDeployment != null && !lastDeployment.getVersionIdentifier().equals(reqAppVerId);
Boolean reqAppArchHashValDiffers = lastDeployment != null && reqAppArchHashVal != null && !lastDeployment.getApplicationArchive().getHash().equals(reqAppArchHashVal);
// the app hash value is different than reported
if (reqAppVerDiffers || reqAppArchHashValDiffers) {
// TODO: I'm not happy with the discrepancies between the model and schema
// ...besides, this update header should be encapsulated somewhere else
ApplicationArchive currentVersionArchive = lastDeployment.getApplicationArchive();
UpdateHeader uh = new UpdateHeader();
uh.setVersionIdentifier(lastDeployment.getVersionIdentifier());
uh.setInstallNeeds(Long.valueOf(currentVersionArchive.getBytesLength() + currentVersionArchive.getBytesLengthUncompressed()));
uh.setStorageNeeds(Long.valueOf(currentVersionArchive.getBytesLengthUncompressed()));
uh.setType(UpdateType.fromValue(lastDeployment.getType().toString()));
uh.setUpdateUrl(currentVersionArchive.getDownloadUrl(settings));
uh.setHash(new Hash());
uh.getHash().setAlgorithm(HashAlgorithm.fromValue(currentVersionArchive.getHashAlgorithm()));
uh.getHash().setValue(currentVersionArchive.getHash());
response.setUpdate(uh);
}
return response;
}
Aggregations