Search in sources :

Example 1 with Unzip

use of org.apache.ant.compress.taskdefs.Unzip in project dhis2-core by dhis2.

the class DefaultAppManager method installApp.

@Override
public AppStatus installApp(File file, String fileName) {
    try {
        // -----------------------------------------------------------------
        // Parse ZIP file and it's manifest.webapp file.
        // -----------------------------------------------------------------
        ZipFile zip = new ZipFile(file);
        ZipEntry entry = zip.getEntry(MANIFEST_FILENAME);
        InputStream inputStream = zip.getInputStream(entry);
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        App app = mapper.readValue(inputStream, App.class);
        // -----------------------------------------------------------------
        // Check for namespace and if it's already taken by another app
        // -----------------------------------------------------------------
        String namespace = app.getActivities().getDhis().getNamespace();
        if (namespace != null && (this.appNamespaces.containsKey(namespace) && !app.equals(appNamespaces.get(namespace)))) {
            zip.close();
            return AppStatus.NAMESPACE_TAKEN;
        }
        // -----------------------------------------------------------------
        // Delete if app is already installed, assuming app update so no 
        // data is deleted
        // -----------------------------------------------------------------
        deleteApp(app.getName(), false);
        // -----------------------------------------------------------------
        // Unzip the app
        // -----------------------------------------------------------------
        log.info("Installing app, namespace: " + namespace);
        String dest = getAppFolderPath() + File.separator + fileName.substring(0, fileName.lastIndexOf('.'));
        Unzip unzip = new Unzip();
        unzip.setSrc(file);
        unzip.setDest(new File(dest));
        unzip.execute();
        log.info("Installed app: " + app);
        // -----------------------------------------------------------------
        // Installation complete. Closing zip, reloading apps and return OK
        // -----------------------------------------------------------------
        zip.close();
        reloadApps();
        return AppStatus.OK;
    } catch (ZipException e) {
        return AppStatus.INVALID_ZIP_FORMAT;
    } catch (JsonParseException e) {
        return AppStatus.INVALID_MANIFEST_JSON;
    } catch (JsonMappingException e) {
        return AppStatus.INVALID_MANIFEST_JSON;
    } catch (IOException e) {
        return AppStatus.INSTALLATION_FAILED;
    }
}
Also used : ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Unzip(org.apache.ant.compress.taskdefs.Unzip) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ZipFile(java.util.zip.ZipFile) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1 Unzip (org.apache.ant.compress.taskdefs.Unzip)1