diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java index eb92f07bc0..0dd283b3fe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java @@ -26,6 +26,7 @@ public class SubscribingDeviceIdHolder { private Map appInstalledDevices = new HashMap<>(); private Map appInstallableDevices = new HashMap<>(); private Map appReInstallableDevices = new HashMap<>(); + private Map appReUnInstallableDevices = new HashMap<>(); private Map skippedDevices = new HashMap<>(); public Map getAppInstalledDevices() { @@ -57,4 +58,12 @@ public class SubscribingDeviceIdHolder { public void setSkippedDevices(Map skippedDevices) { this.skippedDevices = skippedDevices; } + + public Map getAppReUnInstallableDevices() { + return appReUnInstallableDevices; + } + + public void setAppReUnInstallableDevices(Map appReUnInstallableDevices) { + this.appReUnInstallableDevices = appReUnInstallableDevices; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index b4b55e6edb..3c8a1d844e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -83,6 +83,23 @@ public interface SubscriptionManager { */ void updateScheduledSubscriptionStatus(int id, ExecutionStatus status) throws SubscriptionManagementException; + /** + * Perform google enterprise app install + * @param applicationUUID UUID of the application to subscribe/unsubscribe + * @param params list of subscribers. This list can be of either + * {@link org.wso2.carbon.device.mgt.common.DeviceIdentifier} if {@param subType} is equal + * to DEVICE or {@link String} if {@param subType} is USER, ROLE or GROUP + * @param subType subscription type. E.g. DEVICE, USER, ROLE, GROUP {@see { + * @param action subscription action. E.g. INSTALL/UNINSTALL {@see { + * @param generic type of the method. + * @return {@link ApplicationInstallResponse} + * @throws ApplicationManagementException ApplicationManagementException if error occurs when subscribing to the + * given application + * @link org.wso2.carbon.device.application.mgt.common.SubscriptionType}} + */ + void performEntAppSubscription(String applicationUUID, List params, String subType, String action) + throws ApplicationManagementException; + /*** * This method used to get the app id ,device ids and pass them to DM service method. * @@ -106,7 +123,7 @@ public interface SubscriptionManager { * @return {@link PaginationResult} pagination result of the category details. * @throws {@link ApplicationManagementException} Exception of the application management */ - PaginationResult getAppInstalledCategories(int offsetValue, int limitValue, String appUUID, + PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, String subType) throws ApplicationManagementException; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 33fafe57db..5b7f9a00cb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -33,7 +33,7 @@ import java.util.Map; */ public interface SubscriptionDAO { - List addDeviceSubscription(String subscribedBy, List deviceIds, String subscribedFrom, + void addDeviceSubscription(String subscribedBy, List deviceIds, String subscribedFrom, String installStatus, int releaseId, int tenantId ) throws ApplicationManagementDAOException; void updateDeviceSubscription(String updateBy, List deviceIds, String action, String actionTriggeredFrom, @@ -66,13 +66,13 @@ public interface SubscriptionDAO { Map getDeviceSubscriptions(List deviceIds, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getSubscribedUserNames(List users, int tenantId) throws + List getAppSubscribedUserNames(List users, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getSubscribedRoleNames(List roles, int tenantId) throws + List getAppSubscribedRoleNames(List roles, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getSubscribedGroupNames(List groups, int tenantId) throws + List getAppSubscribedGroupNames(List groups, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; void updateSubscriptions(int tenantId, String updateBy, List paramList, @@ -81,7 +81,8 @@ public interface SubscriptionDAO { List getDeviceSubIds(List deviceIds, int applicationReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getDeviceSubIdsForOperation (int operationId, int tenantId) throws ApplicationManagementDAOException; + List getDeviceSubIdsForOperation(int operationId, int deviceID, int tenantId) + throws ApplicationManagementDAOException; boolean updateDeviceSubStatus(int deviceId, List deviceSubIds, String status, int tenantcId) throws ApplicationManagementDAOException; 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/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 444445a288..46621cb9d4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -49,7 +49,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc private static Log log = LogFactory.getLog(GenericSubscriptionDAOImpl.class); @Override - public List addDeviceSubscription(String subscribedBy, List deviceIds, + public void addDeviceSubscription(String subscribedBy, List deviceIds, String subscribedFrom, String installStatus, int releaseId, int tenantId) throws ApplicationManagementDAOException { String sql = "INSERT INTO " @@ -82,13 +82,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } stmt.executeBatch(); - try (ResultSet rs = stmt.getGeneratedKeys()) { - List deviceSubIds = new ArrayList<>(); - while (rs.next()) { - deviceSubIds.add(rs.getInt(1)); - } - return deviceSubIds; - } } } catch (DBConnectionException e) { String msg = "Error occured while obtaining database connection to add device subscription for application " @@ -425,8 +418,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } - @Override - public List getSubscribedUserNames(List users, int tenantId) + public List getAppSubscribedUserNames(List users, int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get already subscribed users for given list of user names."); @@ -438,13 +430,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc StringJoiner joiner = new StringJoiner(",", "SELECT US.USER_NAME AS USER_NAME " + "FROM AP_USER_SUBSCRIPTION US " - + "WHERE US.USER_NAME IN (", ") AND TENANT_ID = ?"); + + "WHERE US.USER_NAME IN (", ") AND AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"); users.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { for (String username : users) { ps.setObject(index++, username); } + ps.setInt(index++, appReleaseId); ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -465,8 +458,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } - @Override - public List getSubscribedRoleNames(List roles, int tenantId) + public List getAppSubscribedRoleNames(List roles, int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get already subscribed role names for given list of roles."); @@ -478,13 +470,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc StringJoiner joiner = new StringJoiner(",", "SELECT RS.ROLE_NAME AS ROLE " + "FROM AP_ROLE_SUBSCRIPTION RS " - + "WHERE RS.ROLE_NAME IN (", ") AND TENANT_ID = ?"); + + "WHERE RS.ROLE_NAME IN (", ") AND AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"); roles.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { for (String roleName : roles) { ps.setObject(index++, roleName); } + ps.setInt(index++, appReleaseId); ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -506,7 +499,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getSubscribedGroupNames(List groups, int tenantId) + public List getAppSubscribedGroupNames(List groups, int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get already subscribed groups for given list of groups."); @@ -518,13 +511,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc StringJoiner joiner = new StringJoiner(",", "SELECT GS.GROUP_NAME AS GROUP_NAME " + "FROM AP_GROUP_SUBSCRIPTION GS " - + "WHERE GS.GROUP_NAME IN (", ") AND TENANT_ID = ?"); + + "WHERE GS.GROUP_NAME IN (", ") AND AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"); groups.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { for (String groupName : groups) { ps.setObject(index++, groupName); } + ps.setInt(index++, appReleaseId); ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -647,20 +641,22 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getDeviceSubIdsForOperation(int operationId, int tenantId) + public List getDeviceSubIdsForOperation(int operationId, int deviceId, int tenantId) throws ApplicationManagementDAOException { try { Connection conn = this.getDBConnection(); List deviceSubIds = new ArrayList<>(); - String sql = "SELECT " - + "AP_DEVICE_SUBSCRIPTION_ID " - + "FROM AP_APP_SUB_OP_MAPPING " - + "WHERE " - + "OPERATION_ID = ? AND " - + "TENANT_ID = ?"; + String sql = "SELECT AP_APP_SUB_OP_MAPPING.AP_DEVICE_SUBSCRIPTION_ID " + + "FROM " + + "AP_APP_SUB_OP_MAPPING INNER JOIN AP_DEVICE_SUBSCRIPTION " + + "ON AP_APP_SUB_OP_MAPPING.AP_DEVICE_SUBSCRIPTION_ID = AP_DEVICE_SUBSCRIPTION.ID " + + "WHERE AP_APP_SUB_OP_MAPPING.OPERATION_ID = ? AND " + + "AP_DEVICE_SUBSCRIPTION.DM_DEVICE_ID = ? AND " + + "AP_APP_SUB_OP_MAPPING.TENANT_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, operationId); - stmt.setInt(2, tenantId); + stmt.setInt(2, deviceId); + stmt.setInt(3, tenantId); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { deviceSubIds.add(rs.getInt("AP_DEVICE_SUBSCRIPTION_ID")); 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/subscription/SQLServerSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/SQLServerSubscriptionDAOImpl.java index 2c725dde07..26a498af1b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/SQLServerSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/SQLServerSubscriptionDAOImpl.java @@ -159,58 +159,4 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { throw new ApplicationManagementDAOException(msg, e); } } - - @Override - public List addDeviceSubscription(String subscribedBy, List deviceIds, - String subscribedFrom, String installStatus, int releaseId, int tenantId) - throws ApplicationManagementDAOException { - String sql = "INSERT INTO " - + "AP_DEVICE_SUBSCRIPTION(" - + "SUBSCRIBED_BY, " - + "SUBSCRIBED_TIMESTAMP, " - + "ACTION_TRIGGERED_FROM, " - + "STATUS, " - + "DM_DEVICE_ID, " - + "AP_APP_RELEASE_ID," - + "TENANT_ID) " - + "VALUES (?, ?, ?, ?, ?, ?, ?)"; - try { - Connection conn = this.getDBConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { - Calendar calendar = Calendar.getInstance(); - Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); - List deviceSubIds = new ArrayList<>(); - for (Integer deviceId : deviceIds) { - stmt.setString(1, subscribedBy); - stmt.setTimestamp(2, timestamp); - stmt.setString(3, subscribedFrom); - stmt.setString(4, installStatus); - stmt.setInt(5, deviceId); - stmt.setInt(6, releaseId); - stmt.setInt(7, tenantId); - if (log.isDebugEnabled()) { - log.debug("Adding a device subscription for device id " + deviceId + " and application " - + "release which has release id" + releaseId); - } - stmt.executeUpdate(); - try (ResultSet rs = stmt.getGeneratedKeys()) { - if (rs.next()) { - deviceSubIds.add(rs.getInt(1)); - } - } - } - return deviceSubIds; - } - } catch (DBConnectionException e) { - String msg = "Error occured while obtaining database connection to add device subscription for application " - + "release which has release Id" + releaseId; - log.error(msg, e); - throw new ApplicationManagementDAOException(msg, e); - } catch (SQLException e) { - String msg = "Error occured when processing SQL to add device subscription for application release which" - + " has release Id " + releaseId; - log.error(msg, e); - throw new ApplicationManagementDAOException(msg, e); - } - } } 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 b7f63a1f79..a937169b83 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 @@ -3163,7 +3163,7 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.beginDBTransaction(); - List deviceSubIds = subscriptionDAO.getDeviceSubIdsForOperation(operationId, tenantId); + List deviceSubIds = subscriptionDAO.getDeviceSubIdsForOperation(operationId, deviceId, tenantId); if (deviceSubIds.isEmpty()){ ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Couldn't find device subscription for operation id " + operationId; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 9dc7a152db..51409bef70 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -38,6 +38,7 @@ import org.wso2.carbon.device.application.mgt.common.SubscriptionType; import org.wso2.carbon.device.application.mgt.common.SubscribingDeviceIdHolder; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationPolicyDTO; +import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.dto.ScheduledSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -174,7 +175,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { + if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType()) && !SubscriptionType.DEVICE + .toString().equals(subType)) { DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); deviceTypeName = deviceType.getName(); //filter devices by device type @@ -316,6 +318,207 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + @Override + public void performEntAppSubscription(String applicationUUID, List params, String subType, String action) + throws ApplicationManagementException { + if (log.isDebugEnabled()) { + log.debug("Google Ent app Install operation is received to application which has UUID " + + applicationUUID + " to perform on " + params.size() + " params."); + } + try { + if (params.isEmpty()) { + String msg = "In order to subscribe/unsubscribe application release, you should provide list of " + + "subscribers. But found an empty list of subscribers."; + log.error(msg); + throw new BadRequestException(msg); + } + + ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); + ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + //todo need to check application release status if it is not in installable state send forbidden exception + int applicationReleaseId = applicationReleaseDTO.getId(); + if (!ApplicationType.PUBLIC.toString().equals(applicationDTO.getType())) { + String msg = "Application type is not public. Hence you can't perform google ent.install operation on " + + "this application. Application name " + applicationDTO.getName() + " Application Type " + + applicationDTO.getType(); + log.error(msg); + throw new BadRequestException(msg); + } + + List categories = getApplicationCategories(applicationDTO.getId()); + if (!categories.contains("GooglePlaySyncedApp")) { + String msg = "This is not google play store synced application. Hence can't perform enteprise app " + + "installation."; + log.error(msg); + throw new BadRequestException(msg); + } + + DeviceManagementProviderService deviceManagementProviderService = HelperUtil + .getDeviceManagementProviderService(); + + List devices = new ArrayList<>(); + List subscribers = new ArrayList<>(); + List appSubscribingDeviceIds; + List appReSubscribingDeviceIds = new ArrayList<>(); + List deviceIdentifiers = new ArrayList<>(); + + //todo validate users, groups and roles + if (SubscriptionType.DEVICE.toString().equals(subType)) { + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + for (T param : params) { + DeviceIdentifier deviceIdentifier = (DeviceIdentifier) param; + if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils + .isEmpty(deviceIdentifier.getType())) { + log.warn("Found a device identifier which has either empty identity of the device or empty" + + " device type. Hence ignoring the device identifier. "); + continue; + } + if (!deviceType.getName().equals(deviceIdentifier.getType())) { + log.warn("Found a device identifier which is not matched with the supported device type " + + "of the application release which has UUID " + applicationUUID + " Application " + + "supported device type is " + deviceType.getName() + " and the " + + "identifier of which has a different device type is " + deviceIdentifier.getId()); + continue; + } + deviceIdentifiers.add(deviceIdentifier); + devices.add(deviceManagementProviderService.getDevice(deviceIdentifier, false)); + } + } else if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { + for (T param : params) { + String username = (String) param; + subscribers.add(username); + devices.addAll(deviceManagementProviderService.getDevicesOfUser(username)); + } + } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { + for (T param : params) { + String roleName = (String) param; + subscribers.add(roleName); + devices.addAll(deviceManagementProviderService.getAllDevicesOfRole(roleName)); + } + } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { + GroupManagementProviderService groupManagementProviderService = HelperUtil + .getGroupManagementProviderService(); + for (T param : params) { + String groupName = (String) param; + subscribers.add(groupName); + devices.addAll(groupManagementProviderService.getAllDevicesOfGroup(groupName)); + } + } else { + String msg = "Found invalid subscription type " + subType+ " to install application release" ; + log.error(msg); + throw new BadRequestException(msg); + } + + /*If subscription type is not device we need to crete device identifiers object list by referring retrieved + list of devices.*/ + if (!SubscriptionType.DEVICE.toString().equalsIgnoreCase(subType)) { + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + String deviceTypeName = deviceType.getName(); + //filter devices by device type + devices.removeIf(device -> !deviceTypeName.equals(device.getType())); + devices.forEach(device -> { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentifier()); + deviceIdentifier.setType(device.getType()); + deviceIdentifiers.add(deviceIdentifier); + }); + } + + //Installing the application + ApplicationPolicyDTO applicationPolicyDTO = new ApplicationPolicyDTO(); + applicationPolicyDTO.setApplicationDTO(applicationDTO); + applicationPolicyDTO.setDeviceIdentifierList(deviceIdentifiers); + applicationPolicyDTO.setAction(action); + installEnrollmentApplications(applicationPolicyDTO); + + appSubscribingDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); + Map deviceSubscriptions = getDeviceSubscriptions(appSubscribingDeviceIds, + applicationReleaseId); + for (Map.Entry deviceSubscription: deviceSubscriptions.entrySet()){ + appReSubscribingDeviceIds.add(deviceSubscription.getKey()); + appSubscribingDeviceIds.remove(deviceSubscription.getKey()); + } + + updateSubscriptionsForEntInstall(applicationReleaseId, appSubscribingDeviceIds, appReSubscribingDeviceIds, + subscribers, subType, action); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting devices of given users or given roles."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (GroupManagementException e) { + String msg = "Error occurred while getting devices of given groups"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } + + /** + * This method is responsible to update subscription data for google enterprise install. + * + * @param applicationReleaseId Application release Id + * @param params subscribers. If subscription is performed via user, group or role, params is a list of + * {@link String} + * @param subType Subscription type. i.e USER, GROUP, ROLE or DEVICE + * @param action Performing action. (i.e INSTALL or UNINSTALL) + * @throws ApplicationManagementException if error occurred while getting or updating subscription data. + */ + private void updateSubscriptionsForEntInstall(int applicationReleaseId, List appSubscribingDeviceIds, + List appReSubscribingDeviceIds, List params, String subType, String action) + throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + try { + ConnectionManagerUtil.beginDBTransaction(); + List subscribedEntities = new ArrayList<>(); + if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { + subscribedEntities = subscriptionDAO.getAppSubscribedUserNames(params, applicationReleaseId, tenantId); + } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { + subscribedEntities = subscriptionDAO.getAppSubscribedRoleNames(params, applicationReleaseId, tenantId); + } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { + subscribedEntities = subscriptionDAO.getAppSubscribedGroupNames(params, applicationReleaseId, tenantId); + } + + params.removeAll(subscribedEntities); + if (!params.isEmpty()) { + subscriptionDAO.addUserSubscriptions(tenantId, username, params, applicationReleaseId); + } + if (!subscribedEntities.isEmpty()) { + subscriptionDAO + .updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, + action); + } + + if (SubAction.INSTALL.toString().equalsIgnoreCase(action) && !appSubscribingDeviceIds.isEmpty()) { + subscriptionDAO.addDeviceSubscription(username, appSubscribingDeviceIds, subType, + Operation.Status.COMPLETED.toString(), applicationReleaseId, tenantId); + } + if (!appReSubscribingDeviceIds.isEmpty()) { + subscriptionDAO.updateDeviceSubscription(username, appReSubscribingDeviceIds, action, subType, + Operation.Status.COMPLETED.toString(), applicationReleaseId, tenantId); + } + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = + "Error occurred when adding subscription data for application release ID: " + applicationReleaseId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred when getting database connection to add new device subscriptions to application."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "SQL Error occurred when adding new device subscription to application release which has ID: " + + applicationReleaseId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + + /** * THis method is responsible to validate application install or uninstall request. * @@ -327,8 +530,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { */ private void validateRequest(List params, String subType, String action) throws BadRequestException { if (params.isEmpty()) { - String msg = "In order to install application release, you should provide list of subscribers. " - + "But found an empty list of users."; + String msg = "In order to subscribe/unsubscribe application release, you should provide list of " + + "subscribers. But found an empty list of subscribers."; log.error(msg); throw new BadRequestException(msg); } @@ -375,10 +578,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstallableDevices().keySet())); deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppReInstallableDevices().keySet())); - ignoredDeviceIdentifiers - .addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet())); + deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet())); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet())); + deviceIdentifiers + .addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppReUnInstallableDevices().keySet())); ignoredDeviceIdentifiers .addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstallableDevices().keySet())); } @@ -408,18 +612,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { entry.getKey(), action); activityList.add(activity); } - } else if (applicationDTO.getType().equals(ApplicationType.PUBLIC.toString())) { - List categories = getApplicationCategories(applicationDTO.getId()); - if (categories.contains("GooglePlaySyncedApp")) { - ApplicationPolicyDTO applicationPolicyDTO = new ApplicationPolicyDTO(); - applicationPolicyDTO.setApplicationDTO(applicationDTO); - applicationPolicyDTO.setDeviceIdentifierList(deviceIdentifiers); - applicationPolicyDTO.setAction(action); - installEnrollmentApplications(applicationPolicyDTO); - } else { - Activity activity = addAppOperationOnDevices(applicationDTO, deviceIdentifiers, deviceType, action); - activityList.add(activity); - } } else { Activity activity = addAppOperationOnDevices(applicationDTO, deviceIdentifiers, deviceType, action); activityList.add(activity); @@ -445,10 +637,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { */ private SubscribingDeviceIdHolder getSubscribingDeviceIdHolder(List devices, int appReleaseId) throws ApplicationManagementException { - Map appInstalledDevices = new HashMap<>(); - Map appInstallableDevices = new HashMap<>(); - Map appReInstallableDevices = new HashMap<>(); - Map skippedDevices = new HashMap<>(); + + SubscribingDeviceIdHolder subscribingDeviceIdHolder = new SubscribingDeviceIdHolder(); + subscribingDeviceIdHolder.setAppInstallableDevices(new HashMap<>()); + subscribingDeviceIdHolder.setAppInstalledDevices(new HashMap<>()); + subscribingDeviceIdHolder.setAppReInstallableDevices(new HashMap<>()); + subscribingDeviceIdHolder.setAppReUnInstallableDevices(new HashMap<>()); + subscribingDeviceIdHolder.setSkippedDevices(new HashMap<>()); List deviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); //get device subscriptions for given device id list. @@ -459,23 +654,20 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (deviceSubscriptionDTO != null) { if (!deviceSubscriptionDTO.isUnsubscribed() && Operation.Status.COMPLETED.toString() .equals(deviceSubscriptionDTO.getStatus())) { - appInstalledDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getAppInstalledDevices().put(deviceIdentifier, device.getId()); + } else if (deviceSubscriptionDTO.isUnsubscribed() && !Operation.Status.COMPLETED.toString() + .equals(deviceSubscriptionDTO.getStatus())) { + subscribingDeviceIdHolder.getAppReUnInstallableDevices().put(deviceIdentifier, device.getId()); } else if (Operation.Status.PENDING.toString().equals(deviceSubscriptionDTO.getStatus()) || Operation.Status.IN_PROGRESS.toString().equals(deviceSubscriptionDTO.getStatus())) { - skippedDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getSkippedDevices().put(deviceIdentifier, device.getId()); } else { - appReInstallableDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getAppReInstallableDevices().put(deviceIdentifier, device.getId()); } } else { - appInstallableDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getAppInstallableDevices().put(deviceIdentifier, device.getId()); } } - - SubscribingDeviceIdHolder subscribingDeviceIdHolder = new SubscribingDeviceIdHolder(); - subscribingDeviceIdHolder.setAppInstallableDevices(appInstallableDevices); - subscribingDeviceIdHolder.setAppInstalledDevices(appInstalledDevices); - subscribingDeviceIdHolder.setAppReInstallableDevices(appReInstallableDevices); - subscribingDeviceIdHolder.setSkippedDevices(skippedDevices); return subscribingDeviceIdHolder; } @@ -565,7 +757,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { try { ConnectionManagerUtil.beginDBTransaction(); if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO.getSubscribedUserNames(params, tenantId); + List subscribedEntities = subscriptionDAO + .getAppSubscribedUserNames(params, applicationReleaseId, tenantId); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { params.removeAll(subscribedEntities); subscriptionDAO.addUserSubscriptions(tenantId, username, params, applicationReleaseId); @@ -573,7 +766,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { subscriptionDAO.updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, action); } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO.getSubscribedRoleNames(params, tenantId); + List subscribedEntities = subscriptionDAO + .getAppSubscribedRoleNames(params, applicationReleaseId, tenantId); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { params.removeAll(subscribedEntities); subscriptionDAO.addRoleSubscriptions(tenantId, username, params, applicationReleaseId); @@ -581,7 +775,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { subscriptionDAO.updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, action); } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO.getSubscribedGroupNames(params, tenantId); + List subscribedEntities = subscriptionDAO + .getAppSubscribedGroupNames(params, applicationReleaseId, tenantId); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { params.removeAll(subscribedEntities); subscriptionDAO.addGroupSubscriptions(tenantId, username, params, applicationReleaseId); @@ -594,32 +789,31 @@ public class SubscriptionManagerImpl implements SubscriptionManager { int operationId = Integer.parseInt(activity.getActivityId().split("ACTIVITY_")[1]); List subUpdatingDeviceIds = new ArrayList<>(); List subInsertingDeviceIds = new ArrayList<>(); - List deviceSubIds = new ArrayList<>(); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { subUpdatingDeviceIds.addAll(getOperationAddedDeviceIds(activity, subscribingDeviceIdHolder.getAppReInstallableDevices())); + subUpdatingDeviceIds.addAll(getOperationAddedDeviceIds(activity, + subscribingDeviceIdHolder.getAppInstalledDevices())); subInsertingDeviceIds.addAll(getOperationAddedDeviceIds(activity, subscribingDeviceIdHolder.getAppInstallableDevices())); - } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { subUpdatingDeviceIds.addAll(getOperationAddedDeviceIds(activity, subscribingDeviceIdHolder.getAppInstalledDevices())); } - List subscribingDevices = subscriptionDAO - .addDeviceSubscription(username, subInsertingDeviceIds, subType, + subscriptionDAO.addDeviceSubscription(username, subInsertingDeviceIds, subType, Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); - subscriptionDAO.updateDeviceSubscription(username, subUpdatingDeviceIds, action, subType, - Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); - if (!subUpdatingDeviceIds.isEmpty()) { - deviceSubIds.addAll(subscriptionDAO - .getDeviceSubIds(subUpdatingDeviceIds, applicationReleaseId, tenantId)); + subscriptionDAO.updateDeviceSubscription(username, subUpdatingDeviceIds, action, subType, + Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); + } + subUpdatingDeviceIds.addAll(subInsertingDeviceIds); + if (!subUpdatingDeviceIds.isEmpty()) { + List deviceSubIds = new ArrayList<>( + subscriptionDAO.getDeviceSubIds(subUpdatingDeviceIds, applicationReleaseId, tenantId)); + subscriptionDAO.addOperationMapping(operationId, deviceSubIds, tenantId); } - deviceSubIds.addAll(subscribingDevices); - - subscriptionDAO.addOperationMapping(operationId, deviceSubIds, tenantId); } ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { @@ -822,7 +1016,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { String payload = gson.toJson(applicationPolicyDTO); StringRequestEntity requestEntity = new StringRequestEntity(payload, MediaType.APPLICATION_JSON - , Constants.ApplicationInstall.ENCODING);; + , Constants.ApplicationInstall.ENCODING); httpClient = new HttpClient(); request = new PostMethod(requestUrl); request.addRequestHeader(Constants.ApplicationInstall.AUTHORIZATION @@ -869,14 +1063,21 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List deviceSubscriptionDTOS = subscriptionDAO .getDeviceSubscriptions(applicationReleaseId, tenantId); if (deviceSubscriptionDTOS.isEmpty()) { - String msg = "Couldn't found an subscribed devices for application release id: " - + applicationReleaseId; - log.info(msg); + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(new ArrayList<>()); + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + return paginationResult; } List deviceIdList = new ArrayList<>(); - for (DeviceSubscriptionDTO deviceIds : deviceSubscriptionDTOS) { - deviceIdList.add(deviceIds.getDeviceId()); - } + deviceSubscriptionDTOS.forEach(deviceSubscriptionDTO -> { + if ((!deviceSubscriptionDTO.isUnsubscribed() && Operation.Status.COMPLETED.toString() + .equalsIgnoreCase(deviceSubscriptionDTO.getStatus())) || (deviceSubscriptionDTO.isUnsubscribed() + && !Operation.Status.COMPLETED.toString() + .equalsIgnoreCase(deviceSubscriptionDTO.getStatus()))) { + deviceIdList.add(deviceSubscriptionDTO.getDeviceId()); + } + }); if (deviceIdList.isEmpty()){ PaginationResult paginationResult = new PaginationResult(); @@ -919,7 +1120,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppInstalledCategories(int offsetValue, int limitValue, String appUUID, String subType) + public PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, String subType) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -988,18 +1189,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List deviceSubscriptionDTOS = subscriptionDAO .getDeviceSubscriptions(applicationReleaseId, tenantId); if (deviceSubscriptionDTOS.isEmpty()) { - String msg = "Couldn't found an subscribed devices for application release id: " + applicationReleaseId; - log.info(msg); - } - List deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId) - .collect(Collectors.toList()); - if (deviceIdList.isEmpty()) { PaginationResult paginationResult = new PaginationResult(); - paginationResult.setData(deviceIdList); + paginationResult.setData(new ArrayList<>()); paginationResult.setRecordsFiltered(0); paginationResult.setRecordsTotal(0); return paginationResult; } + List deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId) + .collect(Collectors.toList()); try { //pass the device id list to device manager service method PaginationResult paginationResult = deviceManagementProviderService diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index 5e0e765239..ae864cb52e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -954,7 +954,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); throw new BadRequestException(msg); } - applicationArtifact.setInstallerName(installerFileName); + applicationArtifact.setInstallerName(installerFileName.replaceAll("\\s", "")); applicationArtifact.setInstallerStream(installerStream); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java index 95edf14420..897154efc3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java @@ -185,6 +185,106 @@ public interface SubscriptionManagementAPI { @QueryParam("timestamp") String timestamp ); + @POST + @Path("/{uuid}/devices/ent-app-install/{action}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Install an application for devices via google enterprise app installing service", + notes = "This will install an application to a given list of devices", + tags = "Subscription Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:install") + }) + } + ) + @ApiResponses( + value = { + + }) + Response performEntAppSubscriptionOnDevices( + @ApiParam( + name = "UUID", + value = "The application UUID", + required = true + ) + @PathParam("uuid") String uuid, + @ApiParam( + name = "action", + value = "Performing action.", + required = true + ) + @PathParam("action") String action, + @ApiParam( + name = "installationDetails", + value = "The list of device identifiers", + required = true + ) + @Valid List deviceIdentifiers, + @ApiParam( + name = "timestamp", + value = "Timestamp of scheduled ent. install operation" + ) + @QueryParam("timestamp") String timestamp + ); + + @POST + @Path("/{uuid}/{subType}/ent-app-install/{action}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Install an application for subscription type via google enterprise install.", + notes = "This will install an application to a given subscription type and this is bulk app installation.", + tags = "Subscription Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:install") + }) + } + ) + @ApiResponses( + value = { + + }) + Response performBulkEntAppSubscription( + @ApiParam( + name = "uuid", + value = "The application release UUID", + required = true + ) + @PathParam("uuid") String uuid, + @ApiParam( + name = "subType", + value = "Subscription type of the app installing operation.", + required = true + ) + @PathParam("subType") String subType, + @ApiParam( + name = "action", + value = "Performing action.", + required = true + ) + @PathParam("action") String action, + @ApiParam( + name = "subscribers", + value = "Subscriber list of the application release.", + required = true + ) + @Valid List subscribers, + @ApiParam( + name = "timestamp", + value = "Timestamp of scheduled ent app install operation" + ) + @QueryParam("timestamp") String timestamp + ); + @GET @Path("/{uuid}/devices") @Produces(MediaType.APPLICATION_JSON) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java index 904e6acdd6..7fb357e4fe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java @@ -92,7 +92,7 @@ public interface SubscriptionManagementAdminAPI { tags = "Subscription Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:view") + @ExtensionProperty(name = SCOPE, value = "perm:admin:app:subscription:view") }) } ) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 62af127d18..ba83ba655f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -89,13 +89,13 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (BadRequestException e) { - String msg = "Found invalid payload for installing application which has UUID: " + uuid - + ". Hence verify the payload"; + String msg = "Found invalid payload for installing application which has UUID: " + uuid + ". Hence verify " + + "the payload"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ForbiddenException e) { - String msg = "Application release is not in the installable state. Hence you are not permitted to install " - + "the application."; + String msg = "Application release is not in the installable state. Hence you are not permitted to perform " + + "the action on the application."; log.error(msg, e); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); } catch (ApplicationManagementException e) { @@ -106,7 +106,6 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ } } - @Override @POST @Path("/{uuid}/{subType}/{action}") @@ -132,13 +131,13 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (BadRequestException e) { - String msg = "Found invalid payload for installing application which has UUID: " + uuid - + ". Hence verify the payload"; + String msg = "Found invalid payload for installing application which has UUID: " + uuid + ". Hence verify " + + "the payload"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ForbiddenException e) { - String msg = "Application release is not in the installable state. Hence you are not permitted to install " - + "the application."; + String msg = "Application release is not in the installable state. Hence you are not permitted to perform " + + "the action on the application."; log.error(msg, e); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); } catch (ApplicationManagementException e) { @@ -149,6 +148,93 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ } } + @Override + @POST + @Path("/{uuid}/devices/ent-app-install/{action}") + public Response performEntAppSubscriptionOnDevices( + @PathParam("uuid") String uuid, + @PathParam("action") String action, + @Valid List deviceIdentifiers, + @QueryParam("timestamp") String timestamp) { + try { + if (StringUtils.isEmpty(timestamp)) { + SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); + subscriptionManager + .performEntAppSubscription(uuid, deviceIdentifiers, SubscriptionType.DEVICE.toString(), action); + String msg = "Application release which has UUID " + uuid + " is installed to given valid device " + + "identifiers."; + return Response.status(Response.Status.OK).entity(msg).build(); + } else { + return scheduleApplicationOperationTask(uuid, deviceIdentifiers, SubscriptionType.DEVICE, + SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp); + } + } catch (NotFoundException e) { + String msg = "Couldn't found an application release for UUI: " + uuid + " to perform ent app installation " + + "on subscriber's devices"; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (BadRequestException e) { + String msg = "Found invalid payload when performing ent app installation on application which has UUID: " + + uuid + ". Hence verify the payload of the request."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } catch (ForbiddenException e) { + String msg = "Application release is not in the installable state. Hence you are not permitted to install " + + "the application."; + log.error(msg, e); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = + "Error occurred while performing ent app installation on the application release which has UUID: " + + uuid + " for devices"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + @POST + @Path("/{uuid}/{subType}/ent-app-install/{action}") + public Response performBulkEntAppSubscription( + @PathParam("uuid") String uuid, + @PathParam("subType") String subType, + @PathParam("action") String action, + @Valid List subscribers, + @QueryParam("timestamp") String timestamp) { + try { + if (StringUtils.isEmpty(timestamp)) { + SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); + subscriptionManager.performEntAppSubscription(uuid, subscribers, subType, action); + String msg = "Application release which has UUID " + uuid + " is installed to subscriber's valid device" + + " identifiers."; + return Response.status(Response.Status.OK).entity(msg).build(); + } else { + return scheduleApplicationOperationTask(uuid, subscribers, + SubscriptionType.valueOf(subType.toUpperCase()), + SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp); + } + } catch (NotFoundException e) { + String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload"; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (BadRequestException e) { + String msg = "Found invalid payload when performing ent app installation on application which has UUID: " + + uuid + ". Hence verify the payload of the request."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } catch (ForbiddenException e) { + String msg = "Application release is not in the installable state. Hence you are not permitted to install " + + "the application."; + log.error(msg, e); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while performing ent app installation on the application release which has " + + "UUID: " + uuid + " for user devices"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + /** * Schedule the application subscription for the given timestamp * @@ -183,24 +269,20 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Consumes("application/json") @Produces("application/json") @Path("/{uuid}/devices") - public Response getAppInstalledDevices(@PathParam("uuid") String uuid, - @DefaultValue("0") - @QueryParam("offset") int offset, - @DefaultValue("5") - @QueryParam("limit") int limit, - @QueryParam("status") String status) { - + public Response getAppInstalledDevices( + @PathParam("uuid") String uuid, + @DefaultValue("0") + @QueryParam("offset") int offset, + @DefaultValue("5") + @QueryParam("limit") int limit, + @QueryParam("status") String status) { try { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); - PaginationResult subscribedDeviceDetails = subscriptionManager .getAppInstalledDevices(offset, limit, uuid, status); - DeviceList devices = new DeviceList(); - devices.setList((List) subscribedDeviceDetails.getData()); devices.setCount(subscribedDeviceDetails.getRecordsTotal()); - return Response.status(Response.Status.OK).entity(devices).build(); } catch (NotFoundException e) { String msg = "Application with application release UUID: " + uuid + " is not found"; @@ -228,18 +310,18 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Consumes("application/json") @Produces("application/json") @Path("/{uuid}/{subType}") - public Response getAppInstalledCategories(@PathParam("uuid") String uuid, - @PathParam("subType") String subType, - @DefaultValue("0") - @QueryParam("offset") int offset, - @DefaultValue("5") - @QueryParam("limit") int limit) { - + public Response getAppInstalledCategories( + @PathParam("uuid") String uuid, + @PathParam("subType") String subType, + @DefaultValue("0") + @QueryParam("offset") int offset, + @DefaultValue("5") + @QueryParam("limit") int limit) { try { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); PaginationResult subscribedCategoryDetails = subscriptionManager - .getAppInstalledCategories(offset, limit, uuid, subType); + .getAppInstalledSubscribers(offset, limit, uuid, subType); if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { BasicUserInfoList users = new BasicUserInfoList(); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java index 81041d9e9c..6f59f28fe8 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java @@ -103,7 +103,8 @@ public class UserHandler extends HttpServlet { } ProxyResponse proxyResponse = new ProxyResponse(); proxyResponse.setCode(HttpStatus.SC_OK); - proxyResponse.setData(jTokenResultAsJsonObject.get("username").getAsString()); + proxyResponse.setData( + jTokenResultAsJsonObject.get("username").getAsString().replaceAll("@carbon.super", "")); HandlerUtil.handleSuccess(req, resp, serverUrl, platform, proxyResponse); } } catch (IOException e) {