Search in sources :

Example 1 with Cache

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;
}
Also used : HttpRequest(org.jclouds.http.HttpRequest) Arrays(java.util.Arrays) ConfigurationKey(org.hisp.dhis.external.conf.ConfigurationKey) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) LocalBlobRequestSigner(org.jclouds.blobstore.LocalBlobRequestSigner) PreDestroy(javax.annotation.PreDestroy) BlobStoreContext(org.jclouds.blobstore.BlobStoreContext) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Blob(org.jclouds.blobstore.domain.Blob) ZipFile(java.util.zip.ZipFile) URI(java.net.URI) ZipEntry(java.util.zip.ZipEntry) JsonParseException(com.fasterxml.jackson.core.JsonParseException) HttpResponseException(org.jclouds.http.HttpResponseException) Resource(org.springframework.core.io.Resource) Builder.prefix(org.jclouds.blobstore.options.ListContainerOptions.Builder.prefix) ZipException(java.util.zip.ZipException) RequestSigningUnsupported(org.jclouds.blobstore.internal.RequestSigningUnsupported) FileSystemResource(org.springframework.core.io.FileSystemResource) LocationBuilder(org.jclouds.domain.LocationBuilder) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Cache(org.hisp.dhis.cache.Cache) Credentials(org.jclouds.domain.Credentials) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Minutes(org.joda.time.Minutes) LocationManager(org.hisp.dhis.external.location.LocationManager) UrlResource(org.springframework.core.io.UrlResource) HashMap(java.util.HashMap) S3Constants(org.jclouds.s3.reference.S3Constants) Location(org.jclouds.domain.Location) ArrayList(java.util.ArrayList) AuthorizationException(org.jclouds.rest.AuthorizationException) Service(org.springframework.stereotype.Service) Properties(java.util.Properties) BlobStore(org.jclouds.blobstore.BlobStore) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) File(java.io.File) ContextBuilder(org.jclouds.ContextBuilder) Consumer(java.util.function.Consumer) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) BlobRequestSigner(org.jclouds.blobstore.BlobRequestSigner) LocationScope(org.jclouds.domain.LocationScope) FilesystemConstants(org.jclouds.filesystem.reference.FilesystemConstants) StorageMetadata(org.jclouds.blobstore.domain.StorageMetadata) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException) InputStream(java.io.InputStream) Blob(org.jclouds.blobstore.domain.Blob) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Consumer (java.util.function.Consumer)1 Pattern (java.util.regex.Pattern)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1