← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20624: AppManagers install method now always returns a AppStatus(enum) instead of throwing exceptions. T...

 

Merge authors:
  Stian Sandvold (stian-sandvold)
------------------------------------------------------------
revno: 20624 [merge]
committer: Stian Sandvold <stian.sandvold@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-10-12 11:10:38 +0200
message:
  AppManagers install method now always returns a AppStatus(enum) instead of throwing exceptions. The same exceptions are caught in the same way, and the same information as before is returned. Updated AppController and AddAppAction to refelct the changes.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/AppStatus.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManager.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/java/org/hisp/dhis/appmanager/action/AddAppAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/resources/org/hisp/dhis/appmanager/i18n_module.properties


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/AppStatus.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/AppStatus.java	2015-10-09 21:43:56 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/AppStatus.java	2015-10-12 08:55:04 +0000
@@ -30,5 +30,5 @@
 
 public enum AppStatus
 {
-    OK, NAMESPACE_TAKEN;
+    OK, NAMESPACE_TAKEN, INVALID_ZIP_FORMAT, INVALID_MANIFEST_JSON, INSTALLATION_FAILED;
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManager.java	2015-10-11 18:48:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManager.java	2015-10-12 09:07:57 +0000
@@ -36,10 +36,13 @@
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 
 import javax.annotation.PostConstruct;
 
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
 import org.apache.ant.compress.taskdefs.Unzip;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -131,66 +134,95 @@
 
     @Override
     public AppStatus installApp( File file, String fileName, String rootPath )
-        throws IOException
     {
-        ZipFile zip = new ZipFile( file );
-        ZipEntry entry = zip.getEntry( "manifest.webapp" );
-
-        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 appNamespace = app.getActivities().getDhis().getNamespace();
-        if ( appNamespace != null && ( this.appNamespaces.containsKey( appNamespace ) && 
-            !app.equals( appNamespaces.get( appNamespace ) ) ) )
+        try
         {
+
+            // ---------------------------------------------------------------------
+            // Parse zip file and it's manifest.webapp file.
+            // ---------------------------------------------------------------------
+
+            ZipFile zip = new ZipFile( file );
+
+            ZipEntry entry = zip.getEntry( "manifest.webapp" );
+            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 appNamespace = app.getActivities().getDhis().getNamespace();
+            if ( appNamespace != null && (this.appNamespaces.containsKey( appNamespace ) &&
+                !app.equals( appNamespaces.get( appNamespace ) )) )
+            {
+                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
+            // ---------------------------------------------------------------------
+
+            String dest = getAppFolderPath() + File.separator + fileName.substring( 0, fileName.lastIndexOf( '.' ) );
+            Unzip unzip = new Unzip();
+            unzip.setSrc( file );
+            unzip.setDest( new File( dest ) );
+            unzip.execute();
+
+            // ---------------------------------------------------------------------
+            // Set dhis server location
+            // ---------------------------------------------------------------------
+
+            File updateManifest = new File( dest + File.separator + "manifest.webapp" );
+            App installedApp = mapper.readValue( updateManifest, App.class );
+
+            if ( installedApp.getActivities() != null && installedApp.getActivities().getDhis() != null )
+            {
+                if ( "*".equals( installedApp.getActivities().getDhis().getHref() ) )
+                {
+                    installedApp.getActivities().getDhis().setHref( rootPath );
+                    mapper.writeValue( updateManifest, installedApp );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // Installation complete. Closing zip, reloading apps and return OK
+            // ---------------------------------------------------------------------
+
             zip.close();
-            return AppStatus.NAMESPACE_TAKEN;
-        }
-
-        // ---------------------------------------------------------------------
-        // Delete if app is already installed
-        // ---------------------------------------------------------------------
-        
-        if ( getApps().contains( app ) )
-        {
-            String folderPath = getAppFolderPath() + File.separator + app.getFolderName();
-            FileUtils.forceDelete( new File( folderPath ) );
-        }
-
-        String dest = getAppFolderPath() + File.separator + fileName.substring( 0, fileName.lastIndexOf( '.' ) );
-        Unzip unzip = new Unzip();
-        unzip.setSrc( file );
-        unzip.setDest( new File( dest ) );
-        unzip.execute();
-
-        // ---------------------------------------------------------------------
-        // Set dhis server location
-        // ---------------------------------------------------------------------
-
-        File updateManifest = new File( dest + File.separator + "manifest.webapp" );
-        App installedApp = mapper.readValue( updateManifest, App.class );
-
-        if ( installedApp.getActivities() != null && installedApp.getActivities().getDhis() != null )
-        {
-            if ( "*".equals( installedApp.getActivities().getDhis().getHref() ) )
-            {
-                installedApp.getActivities().getDhis().setHref( rootPath );
-                mapper.writeValue( updateManifest, installedApp );
-            }
-        }
-
-        zip.close();
-
-        reloadApps(); // Reload app state
-
-        return AppStatus.OK;
+
+            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;
+        }
     }
 
     @Override
@@ -220,10 +252,10 @@
                     FileUtils.forceDelete( new File( folderPath ) );
 
                     // If deleteAppData is true and a namespace associated with the app exists, delete it.
-                    if(deleteAppData && appNamespaces.containsValue( app ))
+                    if ( deleteAppData && appNamespaces.containsValue( app ) )
                     {
                         appNamespaces.forEach( ( namespace, app1 ) -> {
-                            if( app1 == app)
+                            if ( app1 == app )
                             {
                                 keyJsonValueService.deleteNamespace( namespace );
                             }
@@ -310,7 +342,7 @@
     public void reloadApps()
     {
         List<App> appList = new ArrayList<>();
-        HashMap<String, App> appNamespaces = new HashMap<>(  );
+        HashMap<String, App> appNamespaces = new HashMap<>();
         ObjectMapper mapper = new ObjectMapper();
         mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
 
@@ -340,7 +372,7 @@
                                 String appNamespace = app.getActivities().getDhis().getNamespace();
                                 if ( appNamespace != null )
                                 {
-                                    appNamespaces.put(appNamespace, app);
+                                    appNamespaces.put( appNamespace, app );
                                 }
                             }
                             catch ( IOException ex )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java	2015-10-09 21:21:12 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AppController.java	2015-10-12 08:55:04 +0000
@@ -28,20 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.Lists;
 import org.apache.commons.lang3.StringUtils;
 import org.hisp.dhis.appmanager.App;
 import org.hisp.dhis.appmanager.AppManager;
-import org.hisp.dhis.appmanager.AppStatus;
 import org.hisp.dhis.dxf2.render.DefaultRenderService;
 import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.dxf2.webmessage.WebMessageException;
@@ -59,17 +50,18 @@
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.util.StreamUtils;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.context.request.ServletWebRequest;
 import org.springframework.web.multipart.MultipartFile;
 
-import com.fasterxml.jackson.core.JsonParseException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.collect.Lists;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @author Lars Helge Overland
@@ -129,25 +121,22 @@
 
         String contextPath = ContextUtils.getContextPath( request );
 
-        try
-        {
-            AppStatus appStatus = appManager.installApp( tempFile, file.getOriginalFilename(), contextPath );
-
-            if ( appStatus.equals( AppStatus.NAMESPACE_TAKEN ) )
-            {
-                throw new WebMessageException(
-                    WebMessageUtils.conflict( "The namespace defined in manifest.webapp is already protected." ) );
-            }
-
-        }
-        catch ( JsonParseException ex )
-        {
-            throw new WebMessageException( WebMessageUtils.conflict( "Invalid JSON in app manifest file." ) );
-        }
-        catch ( IOException ex )
-        {
-            throw new WebMessageException(
-                WebMessageUtils.conflict( "App could not not be installed on file system, check permissions" ) );
+        switch ( appManager.installApp( tempFile, file.getOriginalFilename(), contextPath ) )
+        {
+        case OK:
+            break;
+        case NAMESPACE_TAKEN:
+            throw new WebMessageException(
+                WebMessageUtils.conflict( "The namespace defined in manifest.webapp is already protected." ) );
+        case INVALID_ZIP_FORMAT:
+            throw new WebMessageException(
+                WebMessageUtils.unprocessableEntity( "Zip-file could not be read." ) );
+        case INVALID_MANIFEST_JSON:
+            throw new WebMessageException(
+                WebMessageUtils.conflict( "Invalid JSON in app manifest file." ) );
+        case INSTALLATION_FAILED:
+            throw new WebMessageException(
+                WebMessageUtils.conflict( "App could not be installed on file system, check permissions." ) );
         }
     }
 
@@ -227,7 +216,9 @@
 
     @RequestMapping( value = "/{app}", method = RequestMethod.DELETE )
     @PreAuthorize( "hasRole('ALL') or hasRole('M_dhis-web-maintenance-appmanager')" )
-    public void deleteApp( @PathVariable( "app" ) String app, @RequestParam(value = "deleteappdata", required = false, defaultValue = "false") boolean deleteAppData, HttpServletRequest request, HttpServletResponse response )
+    public void deleteApp( @PathVariable( "app" ) String app,
+        @RequestParam( value = "deleteappdata", required = false, defaultValue = "false" ) boolean deleteAppData,
+        HttpServletRequest request, HttpServletResponse response )
         throws WebMessageException
     {
         if ( !appManager.exists( app ) )

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/java/org/hisp/dhis/appmanager/action/AddAppAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/java/org/hisp/dhis/appmanager/action/AddAppAction.java	2015-10-09 21:48:42 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/java/org/hisp/dhis/appmanager/action/AddAppAction.java	2015-10-12 09:07:57 +0000
@@ -57,9 +57,9 @@
     implements Action
 {
     private static final Log log = LogFactory.getLog( AddAppAction.class );
-    
+
     private static final String FAILURE = "failure";
-    
+
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -108,7 +108,7 @@
         throws Exception
     {
         HttpServletRequest request = ServletActionContext.getRequest();
-        
+
         if ( file == null )
         {
             message = i18n.getString( "appmanager_no_file_specified" );
@@ -122,48 +122,55 @@
             log.warn( "App is not a zip archive" );
             return FAILURE;
         }
-        
-        try ( ZipFile zip = new ZipFile( file ) )
+
+        try (ZipFile zip = new ZipFile( file ))
         {
             ZipEntry entry = zip.getEntry( "manifest.webapp" );
-    
-            if ( entry == null)
+
+            if ( entry == null )
             {
                 zip.close();
                 message = i18n.getString( "appmanager_manifest_not_found" );
                 log.warn( "Manifest file could not be found in app" );
                 return FAILURE;
             }
-            
-            try
-            {
-                String contextPath = ContextUtils.getContextPath( request );
-                
-                AppStatus appStatus = appManager.installApp( file, fileName, contextPath );
-
-                if ( appStatus == AppStatus.NAMESPACE_TAKEN )
-                {
-                    message = i18n.getString( "appmanager_namespace_taken" );
-                    log.warn( "Namespace in the app's manifest already taken." );
-                    return FAILURE;
-                }
-                
-                message = i18n.getString( "appmanager_install_success" );
-                
-                return SUCCESS;
-            }
-            catch ( JsonParseException ex )
-            {
+
+            String contextPath = ContextUtils.getContextPath( request );
+
+            switch ( appManager.installApp( file, fileName, contextPath ) )
+            {
+            case OK:
+                break;
+
+            case NAMESPACE_TAKEN:
+                message = i18n.getString( "appmanager_namespace_taken" );
+                log.warn( "Namespace in the app's manifest is already protected." );
+                return FAILURE;
+
+            case INVALID_ZIP_FORMAT:
+                message = i18n.getString( "appmanager_zip_unreadable" );
+                log.warn( "The zip uploaded could not be read." );
+                return FAILURE;
+
+            case INVALID_MANIFEST_JSON:
                 message = i18n.getString( "appmanager_invalid_json" );
-                log.error( "Error parsing JSON in manifest", ex );
+                log.error( "Error parsing JSON in manifest");
                 return FAILURE;
-            }
-            catch ( IOException ex )
-            {
+
+            case INSTALLATION_FAILED:
                 message = i18n.getString( "appmanager_could_not_read_file_check_server_permissions" );
-                log.error( "App could not not be read, check server permissions" );
+                log.error( "App could not be read, check server permissions" );
                 return FAILURE;
             }
         }
+        catch ( IOException e )
+        {
+            message = i18n.getString( "appmanager_could_not_read_file_check_server_permissions" );
+            log.error( "App could not be read, check server permissions" );
+            return FAILURE;
+        }
+
+        message = i18n.getString( "appmanager_install_success" );
+        return SUCCESS;
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/resources/org/hisp/dhis/appmanager/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/resources/org/hisp/dhis/appmanager/i18n_module.properties	2015-10-09 17:01:55 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-appmanager/src/main/resources/org/hisp/dhis/appmanager/i18n_module.properties	2015-10-12 08:55:04 +0000
@@ -21,4 +21,5 @@
 appmanager_author=Author
 appmanager_version=Version
 appmanager_set_to_default=Set to default
-appmanager_namespace_taken=The namespace defined in manifest.webapp is already protected by another app
\ No newline at end of file
+appmanager_namespace_taken=The namespace defined in manifest.webapp is already protected by another app
+appmanager_zip_unreadable=The zip-archive uploaded could not be read