=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManagerService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManagerService.java 2013-05-29 21:29:34 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/appmanager/DefaultAppManagerService.java 2013-06-11 15:13:44 +0000 @@ -26,6 +26,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; @@ -37,7 +38,6 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import static org.hisp.dhis.appmanager.AppManagerService.KEY_APP_FOLDER_PATH; import org.hisp.dhis.datavalue.DefaultDataValueService; import org.hisp.dhis.setting.SystemSettingManager; import org.springframework.beans.factory.annotation.Autowired; === modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java 2013-05-25 14:37:30 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java 2013-06-11 15:13:44 +0000 @@ -593,7 +593,7 @@ { return new File( path ).exists(); } - + /** * Converts an InputStream to String with encoding as UTF-8 * @@ -601,9 +601,9 @@ * @param inputStream the InputStream * @return String after reading the InputStream */ - public static String convertStreamToString(InputStream inputStream) + public static String convertStreamToString( InputStream inputStream ) { - Scanner s = new Scanner(inputStream, ENCODING_UTF8).useDelimiter("\\A"); + Scanner s = new Scanner( inputStream, ENCODING_UTF8 ).useDelimiter( "\\A" ); return s.hasNext() ? s.next() : ""; } } === modified file 'dhis-2/dhis-web/dhis-web-appmanager/src/main/java/org/hisp/dhis/appmanager/action/AddAppAction.java' --- dhis-2/dhis-web/dhis-web-appmanager/src/main/java/org/hisp/dhis/appmanager/action/AddAppAction.java 2013-05-28 15:50:33 +0000 +++ dhis-2/dhis-web/dhis-web-appmanager/src/main/java/org/hisp/dhis/appmanager/action/AddAppAction.java 2013-06-11 15:13:44 +0000 @@ -27,6 +27,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.opensymphony.xwork2.Action; @@ -37,6 +38,8 @@ import javax.servlet.http.HttpServletRequest; import org.apache.ant.compress.taskdefs.Unzip; import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; @@ -54,6 +57,10 @@ public class AddAppAction implements Action { + private static final Log log = LogFactory.getLog( AddAppAction.class ); + + private static final String FAILURE = "failure"; + // ------------------------------------------------------------------------- // Dependencies // ------------------------------------------------------------------------- @@ -115,69 +122,83 @@ public String execute() throws Exception { - if ( null != file ) - { - // TODO: Move to AppManagerService - if ( StreamUtils.isZip( new BufferedInputStream( new FileInputStream( file ) ) ) ) - { - ZipFile zip = new ZipFile( file ); - ZipEntry entry = zip.getEntry( "manifest.webapp" ); - - if ( null != entry ) - { - InputStream inputStream = zip.getInputStream( entry ); - String appManifest = StreamUtils.convertStreamToString( inputStream ); - ObjectMapper mapper = new ObjectMapper(); - mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false ); - App app = mapper.readValue( appManifest, App.class ); - - // Delete if app is already installed - if ( appManagerService.getInstalledApps().contains( app ) ) - { - String folderPath = appManagerService.getAppFolderPath() + File.separator - + appManagerService.getAppFolderName( app ); - FileUtils.forceDelete( new File( folderPath ) ); - } - - String dest = appManagerService.getAppFolderPath() + File.separator - + fileName.substring( 0, fileName.lastIndexOf( '.' ) ); - Unzip unzip = new Unzip(); - unzip.setSrc( file ); - unzip.setDest( new File( dest ) ); - unzip.execute(); - - // Updating Dhis Server Location - File updateManifest = new File( dest + File.separator + "manifest.webapp" ); - App installedApp = mapper.readValue( updateManifest, App.class ); - - if ( installedApp.getActivities().getDhis().getHref().equals( "*" ) ) - { - // TODO: Check why ContextUtils.getContextPath is not working - // String rootPath = ContextUtils.getContextPath(ServletActionContext.getRequest()); - HttpServletRequest req = ServletActionContext.getRequest(); - StringBuffer fullUrl = req.getRequestURL(); - String baseUrl = ContextUtils.getBaseUrl( req ); - String rootPath = fullUrl.substring( 0, fullUrl.indexOf( "/", baseUrl.length() ) ); - - installedApp.getActivities().getDhis().setHref( rootPath ); - mapper.writeValue( updateManifest, installedApp ); - } - - zip.close(); - message = i18n.getString( "appmanager_install_success" ); - } - else - { - zip.close(); - message = i18n.getString( "appmanager_invalid_package" ); - return "failure"; - } - } - else - { - message = i18n.getString( "appmanager_not_zip" ); - return "failure"; - } + if ( file == null ) + { + message = i18n.getString( "appmanager_no_file_specified" ); + log.warn( "No file specified" ); + return FAILURE; + } + + if ( !StreamUtils.isZip( new BufferedInputStream( new FileInputStream( file ) ) ) ) + { + message = i18n.getString( "appmanager_not_zip" ); + log.warn( "App is not a zip archive" ); + return FAILURE; + } + + ZipFile zip = new ZipFile( file ); + ZipEntry entry = zip.getEntry( "manifest.webapp" ); + + 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 + { + InputStream inputStream = zip.getInputStream( entry ); + String appManifest = StreamUtils.convertStreamToString( inputStream ); + ObjectMapper mapper = new ObjectMapper(); + mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false ); + App app = mapper.readValue( appManifest, App.class ); + + // Delete if app is already installed + if ( appManagerService.getInstalledApps().contains( app ) ) + { + String folderPath = appManagerService.getAppFolderPath() + File.separator + + appManagerService.getAppFolderName( app ); + FileUtils.forceDelete( new File( folderPath ) ); + } + + String dest = appManagerService.getAppFolderPath() + File.separator + + fileName.substring( 0, fileName.lastIndexOf( '.' ) ); + Unzip unzip = new Unzip(); + unzip.setSrc( file ); + unzip.setDest( new File( dest ) ); + unzip.execute(); + + // Updating dhis server location + File updateManifest = new File( dest + File.separator + "manifest.webapp" ); + App installedApp = mapper.readValue( updateManifest, App.class ); + + if ( installedApp.getActivities().getDhis().getHref().equals( "*" ) ) + { + // TODO: Check why ContextUtils.getContextPath is not working + // String rootPath = ContextUtils.getContextPath(ServletActionContext.getRequest()); + HttpServletRequest req = ServletActionContext.getRequest(); + StringBuffer fullUrl = req.getRequestURL(); + String baseUrl = ContextUtils.getBaseUrl( req ); + String rootPath = fullUrl.substring( 0, fullUrl.indexOf( "/", baseUrl.length() ) ); + + installedApp.getActivities().getDhis().setHref( rootPath ); + mapper.writeValue( updateManifest, installedApp ); + } + + zip.close(); + message = i18n.getString( "appmanager_install_success" ); + } + catch ( JsonParseException ex ) + { + message = i18n.getString( "appmanager_invalid_json" ); + log.error( "Error parsing JSON in manifest", ex ); + return FAILURE; + } + finally + { + zip.close(); } return SUCCESS; === modified file 'dhis-2/dhis-web/dhis-web-appmanager/src/main/resources/org/hisp/dhis/appmanager/i18n_module.properties' --- dhis-2/dhis-web/dhis-web-appmanager/src/main/resources/org/hisp/dhis/appmanager/i18n_module.properties 2013-06-10 16:36:36 +0000 +++ dhis-2/dhis-web/dhis-web-appmanager/src/main/resources/org/hisp/dhis/appmanager/i18n_module.properties 2013-06-11 15:13:44 +0000 @@ -7,10 +7,11 @@ appmanager_store_url=App Store URL appmanager_upload_app_package=Upload App package (ZIP) appmanager_install_success=App installed successfully -appmanager_invalid_package=Invalid App Package -appmanager_not_zip=Incorrect App Package Format +appmanager_manifest_not_found=Manifest file not found in app +appmanager_not_zip=App is not a valid ZIP archive appmanager_confirm_delete=Are you sure to delete this app? -appmanager_delete_success=App Deleted Successfully +appmanager_invalid_json=Invalid JSON syntax in manifest file +appmanager_delete_success=App deleted successfully appmanager_management=Manage Installed Apps appmanager_appname=Application name appmanager_you_have_no_apps_installed=You have no apps installed at the moment