From 2e0bacbd18640120a9fb48a4b8623761c359130a Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 22 Sep 2018 15:19:44 +0530 Subject: [PATCH] Bug fixes in app creation logic --- .../application/mgt/core/dao/common/Util.java | 47 ++++++++++--------- .../GenericApplicationDAOImpl.java | 2 +- .../GenericApplicationReleaseDAOImpl.java | 3 +- .../mgt/core/impl/ApplicationManagerImpl.java | 31 +++++------- .../impl/ApplicationStorageManagerImpl.java | 39 +++++++++++++-- 5 files changed, 74 insertions(+), 48 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index a850452445..7c0d414a5e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -119,31 +119,32 @@ public class Util { Application application = null; int applicatioId; int iteration = 0; + if (rs != null) { + while (rs.next()) { + if (iteration == 0) { + application = new Application(); + applicatioId = rs.getInt("APP_ID"); + application.setId(applicatioId); + application.setName(rs.getString("APP_NAME")); + application.setType(rs.getString("APP_TYPE")); + application.setAppCategory(rs.getString("APP_CATEGORY")); + application.setSubType(rs.getString("SUB_TYPE")); + application.setPaymentCurrency(rs.getString("CURRENCY")); + application.setIsRestricted(rs.getBoolean("RESTRICTED")); + } - while (rs.next()) { - if (iteration == 0) { - application = new Application(); - applicatioId = rs.getInt("APP_ID"); - application.setId(applicatioId); - application.setName(rs.getString("APP_NAME")); - application.setType(rs.getString("APP_TYPE")); - application.setAppCategory(rs.getString("APP_CATEGORY")); - application.setSubType(rs.getString("SUB_TYPE")); - application.setPaymentCurrency(rs.getString("CURRENCY")); - application.setIsRestricted(rs.getBoolean("RESTRICTED")); - } - - Tag tag = new Tag(); - tag.setTagName(rs.getString("APP_TAG")); - UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); - unrestrictedRole.setRole(rs.getString("ROLE")); - if (application.getTags().contains(tag)) { - application.getTags().add(tag); - } - if (application.getUnrestrictedRoles().contains(unrestrictedRole)) { - application.getUnrestrictedRoles().add(unrestrictedRole); + Tag tag = new Tag(); + tag.setTagName(rs.getString("APP_TAG")); + UnrestrictedRole unrestrictedRole = new UnrestrictedRole(); + unrestrictedRole.setRole(rs.getString("ROLE")); + if (application.getTags().contains(tag)) { + application.getTags().add(tag); + } + if (application.getUnrestrictedRoles().contains(unrestrictedRole)) { + application.getUnrestrictedRoles().add(unrestrictedRole); + } + iteration++; } - iteration++; } return application; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 633e8b8ccb..70248a15ad 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -66,7 +66,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic try { conn = this.getDBConnection(); stmt = conn.prepareStatement("INSERT INTO AP_APP (NAME, TYPE, APP_CATEGORY, SUB_TYPE, RESTRICTED, " - + "TENANT_ID, DM_DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + + "TENANT_ID, DM_DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); stmt.setString(1, application.getName()); stmt.setString(2, application.getType()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index 824a709016..ecd793fb2d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -66,7 +66,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE," + "STORED_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, APP_HASH_VALUE," + "SHARED_WITH_ALL_TENANTS, APP_META_INFO,CREATED_BY,AP_APP_ID) " - + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; + + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; int index = 0; String generatedColumns[] = {"ID"}; @@ -80,6 +80,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setString(++index, String.valueOf(applicationRelease.getPackageName())); statement.setDouble(++index, applicationRelease.getPrice()); statement.setString(++index, applicationRelease.getAppStoredLoc()); + statement.setString(++index, applicationRelease.getBannerLoc()); statement.setString(++index, applicationRelease.getScreenshotLoc1()); statement.setString(++index, applicationRelease.getScreenshotLoc2()); statement.setString(++index, applicationRelease.getScreenshotLoc3()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index f2e851f5ee..ce7b68229d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -50,6 +50,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.ValidationException import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.DeviceType; @@ -105,8 +106,10 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationRelease applicationRelease; List applicationReleases = new ArrayList<>(); try { + ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.beginDBTransaction(); - deviceType = this.deviceTypeDAO.getDeviceType(application.getDeviceType(), tenantId); + MAMDeviceConnectorImpl mamDeviceConnector = new MAMDeviceConnectorImpl(); + deviceType = mamDeviceConnector.getDeviceManagementService().getDeviceType(application.getDeviceType()); if (deviceType == null) { log.error("Device type is not matched with application type"); @@ -136,12 +139,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } applicationRelease = application.getApplicationReleases().get(0); applicationRelease = this.applicationReleaseDAO - .createRelease(applicationRelease, application.getId(), tenantId); + .createRelease(applicationRelease, appId, tenantId); LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); + changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -152,19 +155,17 @@ public class ApplicationManagerImpl implements ApplicationManager { return application; - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting device type id of " + application.getType(); - log.error(msg, e); - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementException e) { String msg = "Error occurred while adding application"; log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + e.printStackTrace(); } finally { - ConnectionManagerUtil.closeDBConnection(); + //ConnectionManagerUtil.closeDBConnection(); //todo: check this again } + return null; } @Override @@ -546,14 +547,13 @@ public class ApplicationManagerImpl implements ApplicationManager { Application application; boolean isAppAllowed = false; try { - ConnectionManagerUtil.openDBConnection(); application = ApplicationManagementDAOFactory.getApplicationDAO() .getApplicationById(applicationId, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { return application; } - if (!application.getUnrestrictedRoles().isEmpty()) { + if (application != null && !application.getUnrestrictedRoles().isEmpty()) { if (isRoleExists(application.getUnrestrictedRoles(), userName)) { isAppAllowed = true; } @@ -569,8 +569,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } catch (UserStoreException e) { throw new ApplicationManagementException( "User-store exception while getting application with the " + "application id " + applicationId, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @@ -707,7 +705,6 @@ public class ApplicationManagerImpl implements ApplicationManager { public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws ApplicationManagementException { try { - ConnectionManagerUtil.openDBConnection(); Application application = getApplicationIfAccessible(applicationId); ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -728,8 +725,6 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException("Failed to add lifecycle state", e); } catch (ApplicationManagementException e) { throw new ApplicationManagementException("Lifecycle State Validation failed", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @@ -808,10 +803,6 @@ public class ApplicationManagerImpl implements ApplicationManager { private Filter validateFilter(Filter filter) { if (filter != null) { - if (!SortingOrder.ASC.toString().equals(filter.getSortBy()) && - !SortingOrder.DESC.toString().equals(filter.getSortBy())) { - return null; - } if (filter.getAppType() != null) { boolean isValidRequest = false; for (ApplicationType applicationType : ApplicationType.values()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index e6d034ee94..ccce9dddfe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -193,6 +193,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager String artifactDirectoryPath; String md5OfApp; + InputStream[] cloneInputStream = cloneInputStream(binaryFile); md5OfApp = getMD5(binaryFile); if (md5OfApp == null) { @@ -202,7 +203,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } if (DeviceType.ANDROID.toString().equalsIgnoreCase(deviceType)) { - ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(binaryFile); + ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(cloneInputStream[2]); applicationRelease.setVersion(apkMeta.getVersionName()); applicationRelease.setPackageName(apkMeta.getPackageName()); } else if (DeviceType.IOS.toString().equalsIgnoreCase(deviceType)) { @@ -223,8 +224,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager + "application UUID " + applicationRelease.getUuid() + " is " + artifactDirectoryPath); } - String artifactPath = artifactDirectoryPath + Constants.RELEASE_ARTIFACT; - saveFile(binaryFile, artifactPath); + String artifactPath = artifactDirectoryPath + File.separator + Constants.RELEASE_ARTIFACT +".apk"; + saveFile(cloneInputStream[1], artifactPath); applicationRelease.setAppStoredLoc(artifactPath); applicationRelease.setAppHashValue(md5OfApp); @@ -241,6 +242,38 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager return applicationRelease; } + public InputStream[] cloneInputStream(InputStream inputStream) throws ApplicationStorageManagementException { + + ByteArrayOutputStream byteArrayOutputStream = null; + + try { + byteArrayOutputStream = new ByteArrayOutputStream(); + + byte[] buffer = new byte[BUFFER_SIZE]; + int len; + while ((len = inputStream.read(buffer)) > -1 ) { + byteArrayOutputStream.write(buffer, 0, len); + } + byteArrayOutputStream.flush(); + + InputStream stream1 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + InputStream stream2 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + InputStream stream3 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + + return new InputStream[]{stream1, stream2, stream3}; + } catch (IOException e) { + throw new ApplicationStorageManagementException("Error occurred while cloning input stream ", e); + } finally { + if (byteArrayOutputStream != null) { + try { + byteArrayOutputStream.close(); + } catch (IOException e) { + + } + } + } + } + @Override public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType, String deviceType, InputStream binaryFile) throws ApplicationStorageManagementException,