use of org.hisp.dhis.cache.Cache in project dhis2-core by dhis2.
the class JCloudsAppStorageService method installApp.
@Override
public App installApp(File file, String filename, Cache<App> appCache) {
App app = new App();
log.info("Installing new app: " + filename);
try (ZipFile zip = new ZipFile(file)) {
// -----------------------------------------------------------------
// Parse ZIP file and it's manifest.webapp file.
// -----------------------------------------------------------------
ZipEntry entry = zip.getEntry(MANIFEST_FILENAME);
if (entry == null) {
log.error("Failed to install app: Missing manifest.webapp in zip");
app.setAppState(AppStatus.MISSING_MANIFEST);
return app;
}
InputStream inputStream = zip.getInputStream(entry);
app = jsonMapper.readValue(inputStream, App.class);
app.setFolderName(APPS_DIR + File.separator + filename.substring(0, filename.lastIndexOf('.')));
app.setAppStorageSource(AppStorageSource.JCLOUDS);
if (!this.validateApp(app, appCache)) {
return app;
}
String namespace = app.getActivities().getDhis().getNamespace();
// -----------------------------------------------------------------
// Unzip the app
// -----------------------------------------------------------------
String dest = APPS_DIR + File.separator + filename.substring(0, filename.lastIndexOf('.'));
zip.stream().forEach((Consumer<ZipEntry>) zipEntry -> {
log.debug("Uploading zipEntry: " + zipEntry);
try {
InputStream input = zip.getInputStream(zipEntry);
Blob blob = blobStore.blobBuilder(dest + File.separator + zipEntry.getName()).payload(input).contentLength(zipEntry.getSize()).build();
blobStore.putBlob(config.container, blob);
input.close();
} catch (IOException e) {
log.error("Unable to store app file '" + zipEntry.getName() + "'", e);
}
});
log.info(String.format("" + "New app '%s' installed" + "\n\tInstall path: %s" + (namespace != null && !namespace.isEmpty() ? "\n\tNamespace reserved: %s" : ""), app.getName(), dest, namespace));
// -----------------------------------------------------------------
// Installation complete.
// -----------------------------------------------------------------
app.setAppState(AppStatus.OK);
return app;
} catch (ZipException e) {
log.error("Failed to install app: Invalid ZIP format", e);
app.setAppState(AppStatus.INVALID_ZIP_FORMAT);
} catch (JsonParseException e) {
log.error("Failed to install app: Invalid manifest.webapp", e);
app.setAppState(AppStatus.INVALID_MANIFEST_JSON);
} catch (IOException e) {
log.error("Failed to install app: Could not save app", e);
app.setAppState(AppStatus.INSTALLATION_FAILED);
}
return app;
}
Aggregations