From 34f1b25c846e64e57dfd17668f46825514b2143e Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 16 Sep 2019 15:39:22 +0530 Subject: [PATCH 1/3] Fix app-install operation adding issue for custom apps --- .../core/impl/SubscriptionManagerImpl.java | 116 +++++++++++------- 1 file changed, 71 insertions(+), 45 deletions(-) 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 cf3ba51bde..0c3fb21deb 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 @@ -463,68 +463,94 @@ public class SubscriptionManagerImpl implements SubscriptionManager { throws ApplicationManagementException { try { //todo rethink and modify the {@link App} usage - App app = new App(); - MobileAppTypes mobileAppType = MobileAppTypes.valueOf(application.getType()); - if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(deviceType)) { + if (ApplicationType.CUSTOM.toString().equalsIgnoreCase(application.getType())) { + ProfileOperation operation = new ProfileOperation(); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - app.setType(mobileAppType); - app.setLocation(application.getApplicationReleases().get(0).getInstallerPath()); - return MDMAndroidOperationUtil.createInstallAppOperation(app); + operation.setCode(MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION); + operation.setType(Operation.Type.PROFILE); + CustomApplication customApplication = new CustomApplication(); + customApplication.setType(application.getType()); + customApplication.setUrl(application.getApplicationReleases().get(0).getInstallerPath()); + operation.setPayLoad(customApplication.toJSON()); + return operation; } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { - return MDMAndroidOperationUtil.createAppUninstallOperation(app); - } else { - String msg = "Invalid Action is found. Action: " + action; - log.error(msg); - throw new ApplicationManagementException(msg); - } - } else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) { - if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - String plistDownloadEndpoint = APIUtil.getArtifactDownloadBaseURL() - + MDMAppConstants.IOSConstants.PLIST + Constants.FORWARD_SLASH - + application.getApplicationReleases().get(0).getUuid(); - app.setType(mobileAppType); - app.setLocation(plistDownloadEndpoint); - Properties properties = new Properties(); - properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); - properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); - app.setProperties(properties); - return MDMIOSOperationUtil.createInstallAppOperation(app); - } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { - return MDMIOSOperationUtil.createAppUninstallOperation(app); + operation.setCode(MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION); + operation.setType(Operation.Type.PROFILE); + CustomApplication customApplication = new CustomApplication(); + customApplication.setType(application.getType()); + //todo get application package name and set + operation.setPayLoad(customApplication.toJSON()); + return operation; } else { String msg = "Invalid Action is found. Action: " + action; log.error(msg); throw new ApplicationManagementException(msg); } } else { - if (ApplicationType.CUSTOM.toString().equalsIgnoreCase(application.getType())) { + App app = new App(); + MobileAppTypes mobileAppType = MobileAppTypes.valueOf(application.getType()); + if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(deviceType)) { if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - ProfileOperation operation = new ProfileOperation(); - operation.setCode(MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION); - operation.setType(Operation.Type.PROFILE); - CustomApplication customApplication = new CustomApplication(); - customApplication.setType(application.getType()); - customApplication.setUrl(application.getApplicationReleases().get(0).getInstallerPath()); - operation.setPayLoad(customApplication.toJSON()); - return operation; + app.setType(mobileAppType); + app.setLocation(application.getApplicationReleases().get(0).getInstallerPath()); + return MDMAndroidOperationUtil.createInstallAppOperation(app); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { - ProfileOperation operation = new ProfileOperation(); - operation.setCode(MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION); - operation.setType(Operation.Type.PROFILE); - CustomApplication customApplication = new CustomApplication(); - customApplication.setType(application.getType()); - //todo get application package name and set - operation.setPayLoad(customApplication.toJSON()); return MDMAndroidOperationUtil.createAppUninstallOperation(app); } else { String msg = "Invalid Action is found. Action: " + action; log.error(msg); throw new ApplicationManagementException(msg); } + } else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) { + if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { + String plistDownloadEndpoint = + APIUtil.getArtifactDownloadBaseURL() + MDMAppConstants.IOSConstants.PLIST + + Constants.FORWARD_SLASH + application.getApplicationReleases().get(0) + .getUuid(); + app.setType(mobileAppType); + app.setLocation(plistDownloadEndpoint); + Properties properties = new Properties(); + properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); + properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); + app.setProperties(properties); + return MDMIOSOperationUtil.createInstallAppOperation(app); + } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + return MDMIOSOperationUtil.createAppUninstallOperation(app); + } else { + String msg = "Invalid Action is found. Action: " + action; + log.error(msg); + throw new ApplicationManagementException(msg); + } } else { - String msg = "Invalid device type is found. Device Type: " + deviceType; - log.error(msg); - throw new ApplicationManagementException(msg); + if (ApplicationType.CUSTOM.toString().equalsIgnoreCase(application.getType())) { + if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { + ProfileOperation operation = new ProfileOperation(); + operation.setCode(MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION); + operation.setType(Operation.Type.PROFILE); + CustomApplication customApplication = new CustomApplication(); + customApplication.setType(application.getType()); + customApplication.setUrl(application.getApplicationReleases().get(0).getInstallerPath()); + operation.setPayLoad(customApplication.toJSON()); + return operation; + } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + ProfileOperation operation = new ProfileOperation(); + operation.setCode(MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION); + operation.setType(Operation.Type.PROFILE); + CustomApplication customApplication = new CustomApplication(); + customApplication.setType(application.getType()); + //todo get application package name and set + operation.setPayLoad(customApplication.toJSON()); + return MDMAndroidOperationUtil.createAppUninstallOperation(app); + } else { + String msg = "Invalid Action is found. Action: " + action; + log.error(msg); + throw new ApplicationManagementException(msg); + } + } else { + String msg = "Invalid device type is found. Device Type: " + deviceType; + log.error(msg); + throw new ApplicationManagementException(msg); + } } } } catch (UnknownApplicationTypeException e) { From 5cf89ec24cd4b3252a9b59261b8211121d122c16 Mon Sep 17 00:00:00 2001 From: Charitha Goonetilleke Date: Mon, 16 Sep 2019 11:49:35 +0000 Subject: [PATCH 2/3] Add application installation response via pull notification --- .../pom.xml | 9 +++++- .../PullNotificationSubscriberImpl.java | 18 ++++++++++- .../internal/PullNotificationDataHolder.java | 10 ++++++ .../PullNotificationServiceComponent.java | 15 +++++++++ .../pom.xml | 10 +++++- .../jaxrs/beans/analytics/AttributeType.java | 31 +++++++++++-------- .../pom.xml | 5 +++ 7 files changed, 82 insertions(+), 16 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml index ff9eca7c0f..b297b30933 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml @@ -54,6 +54,11 @@ org.wso2.carbon.devicemgt org.wso2.carbon.policy.mgt.core + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.application.mgt.common + provided + @@ -85,7 +90,9 @@ org.wso2.carbon.policy.mgt.core.*, org.wso2.carbon.policy.mgt.core, com.google.gson, - org.wso2.carbon.device.mgt.core.service.* + org.wso2.carbon.device.mgt.core.service.*, + org.wso2.carbon.device.application.mgt.common.*, + org.wso2.carbon.device.application.mgt.common.services.* diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java index bcf8dece8a..532c89dea0 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java @@ -24,7 +24,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonParser; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; @@ -44,6 +48,7 @@ public class PullNotificationSubscriberImpl implements PullNotificationSubscribe throw new AssertionError(); } public static final String POLICY_MONITOR = "POLICY_MONITOR"; + public static final String INSTALL_APPLICATION = "INSTALL_APPLICATION"; } @@ -68,8 +73,12 @@ public class PullNotificationSubscriberImpl implements PullNotificationSubscribe } else { PullNotificationDataHolder.getInstance().getDeviceManagementProviderService().updateOperation( deviceIdentifier, operation); + if (OperationCodes.INSTALL_APPLICATION.equals(operation.getCode()) + && Operation.Status.COMPLETED == operation.getStatus()) { + updateAppSubStatus(deviceIdentifier, operation.getId(), operation.getCode()); + } } - } catch (OperationManagementException e) { + } catch (OperationManagementException | DeviceManagementException | ApplicationManagementException e) { throw new PullNotificationExecutionFailedException(e); } catch (PolicyComplianceException e) { throw new PullNotificationExecutionFailedException("Invalid payload format compliant feature", e); @@ -99,4 +108,11 @@ public class PullNotificationSubscriberImpl implements PullNotificationSubscribe } return complianceFeatures; } + + private void updateAppSubStatus(DeviceIdentifier deviceIdentifier, int operationId, String status) + throws DeviceManagementException, ApplicationManagementException { + ApplicationManager applicationManager = PullNotificationDataHolder.getInstance().getApplicationManager(); + Device device = PullNotificationDataHolder.getInstance().getDeviceManagementProviderService().getDevice(deviceIdentifier); + applicationManager.updateSubsStatus(device.getId(), operationId, status); + } } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java index a9f7888c43..fd257ea28c 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java @@ -18,6 +18,7 @@ */ package org.wso2.carbon.device.mgt.extensions.pull.notification.internal; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -25,6 +26,7 @@ public class PullNotificationDataHolder { private DeviceManagementProviderService deviceManagementProviderService; private PolicyManagerService policyManagerService; + private ApplicationManager applicationManager; private static PullNotificationDataHolder thisInstance = new PullNotificationDataHolder(); @@ -47,4 +49,12 @@ public class PullNotificationDataHolder { public void setPolicyManagerService(PolicyManagerService policyManagerService) { this.policyManagerService = policyManagerService; } + + public ApplicationManager getApplicationManager() { + return applicationManager; + } + + public void setApplicationManager(ApplicationManager applicationManager) { + this.applicationManager = applicationManager; + } } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java index 351e514706..4c3971bf7d 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.extensions.pull.notification.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -38,6 +39,12 @@ import org.wso2.carbon.policy.mgt.core.PolicyManagerService; * policy="dynamic" * bind="setPolicyManagerService" * unbind="unsetPolicyManagerService" + * @scr.reference name="org.wso2.carbon.application.mgt.service" + * interface="org.wso2.carbon.device.application.mgt.common.services.ApplicationManager" + * cardinality="1..1" + * policy="dynamic" + * bind="setApplicationManagerService" + * unbind="unsetApplicationManagerService" */ public class PullNotificationServiceComponent { @@ -77,4 +84,12 @@ public class PullNotificationServiceComponent { PullNotificationDataHolder.getInstance().setPolicyManagerService(null); } + protected void setApplicationManagerService(ApplicationManager applicationManagerService){ + PullNotificationDataHolder.getInstance().setApplicationManager(applicationManagerService); + } + + protected void unsetApplicationManagerService(ApplicationManager applicationManagerService){ + PullNotificationDataHolder.getInstance().setApplicationManager(null); + } + } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml index 81fcbf86cb..5c686d6420 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml @@ -111,6 +111,11 @@ org.wso2.carbon.analytics-common org.wso2.carbon.event.output.adapter.core + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.application.mgt.common + provided + org.testng testng @@ -148,12 +153,15 @@ org.wso2.carbon.device.mgt.common.operation.mgt, org.wso2.carbon.device.mgt.common.push.notification, org.wso2.carbon.device.mgt.common, + org.wso2.carbon.device.mgt.common.exceptions, org.wso2.carbon.device.mgt.core.service, org.wso2.carbon.event.output.adapter.core, org.wso2.carbon.event.output.adapter.core.exception, org.osgi.framework, org.wso2.carbon.device.mgt.core.operation.mgt, - org.wso2.carbon.core + org.wso2.carbon.core, + org.wso2.carbon.device.application.mgt.common.*, + org.wso2.carbon.device.application.mgt.common.services.* diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java index 1fde19f8a9..32302986e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java @@ -1,19 +1,20 @@ /* - * Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * - * Entgra (pvt) Ltd. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. */ package org.wso2.carbon.device.mgt.jaxrs.beans.analytics; @@ -22,5 +23,9 @@ package org.wso2.carbon.device.mgt.jaxrs.beans.analytics; */ public enum AttributeType { STRING, LONG, BOOL, INT, FLOAT, DOUBLE; -} + @Override + public String toString() { + return super.toString().toLowerCase(); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml index 3e77a56e72..106a2942b5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml @@ -77,6 +77,11 @@ org.wso2.carbon.device.mgt.extensions.pull.notification test + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.application.mgt.common + test + com.h2database.wso2 h2-database-engine From 85fe985a94800cf13bf0cbdbd37ac44f7bfaf99f Mon Sep 17 00:00:00 2001 From: Jayasanka Weerasinghe Date: Mon, 16 Sep 2019 12:14:32 +0000 Subject: [PATCH 3/3] Fix server starting issue This issue is occurred as a result of having java8 codes in the source. Hence remove and replace the code. --- .../react-app/src/components/apps/AppList.js | 16 ++-------- .../components/apps/release/DetailedRating.js | 7 ++-- .../components/apps/release/ReleaseView.js | 16 ++-------- .../apps/release/install/DeviceInstall.js | 16 ++-------- .../apps/release/install/GroupInstall.js | 16 ++-------- .../apps/release/install/RoleInstall.js | 16 ++-------- .../apps/release/install/UserInstall.js | 14 ++------ .../apps/release/review/AddReview.js | 20 ++++-------- .../apps/release/review/CurrentUsersReview.js | 13 ++------ .../components/apps/release/review/Reviews.js | 12 ++----- .../review/singleReview/SingleReview.js | 13 ++------ .../singleReview/editReview/EditReview.js | 21 ++++-------- .../react-app/src/js/Utils.js | 32 +++++++++++++++++++ .../react-app/src/pages/Login.js | 4 ++- .../src/pages/dashboard/Dashboard.js | 12 ++----- .../pages/dashboard/apps/release/Release.js | 16 ++-------- .../src/pages/dashboard/logout/Logout.js | 15 ++------- 17 files changed, 76 insertions(+), 183 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/js/Utils.js diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js index e6e459e3dc..241ea12acd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/AppList.js @@ -21,6 +21,7 @@ import AppCard from "./AppCard"; import {Col, message, notification, Row, Result, Skeleton} from "antd"; import axios from "axios"; import {withConfigContext} from "../../context/ConfigContext"; +import {handleApiError} from "../../js/Utils"; class AppList extends React.Component { constructor(props) { @@ -72,20 +73,7 @@ class AppList extends React.Component { } }).catch((error) => { - console.log(error.response); - if (error.hasOwnProperty("response") && error.response.status === 401) { - //todo display a popup with error - message.error('You are not logged in'); - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load apps.", - }); - } - + handleApiError(error,"Error occurred while trying to load apps."); this.setState({loading: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js index 0536e966cf..aac62976b7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/DetailedRating.js @@ -17,11 +17,12 @@ */ import React from "react"; -import {Row, Typography, Icon} from "antd"; +import {Row, Typography, Icon, notification} from "antd"; import StarRatings from "react-star-ratings"; import "./DetailedRating.css"; import axios from "axios"; import {withConfigContext} from "../../../context/ConfigContext"; +import {handleApiError} from "../../../js/Utils"; const { Text } = Typography; @@ -62,9 +63,7 @@ class DetailedRating extends React.Component{ } }).catch(function (error) { - if (error.response.status === 401) { - window.location.href = window.location.origin+'/store/login'; - } + handleApiError(error,"Error occurred while trying to load ratings."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js index 6d55b45361..d2b45a191c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/ReleaseView.js @@ -27,6 +27,7 @@ import axios from "axios"; import AppInstallModal from "./install/AppInstallModal"; import CurrentUsersReview from "./review/CurrentUsersReview"; import {withConfigContext} from "../../../context/ConfigContext"; +import {handleApiError} from "../../../js/Utils"; const {Title, Text, Paragraph} = Typography; @@ -78,20 +79,7 @@ class ReleaseView extends React.Component { } }).catch((error) => { - if (error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - this.setState({ - loading: false, - visible: false - }); - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while installing the app.", - }); - } + handleApiError(error,"Error occurred while installing the app."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js index 2af6be7a74..4b7f11bbd2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/DeviceInstall.js @@ -24,6 +24,7 @@ import TimeAgo from 'javascript-time-ago' // Load locale-specific relative date/time formatting rules. import en from 'javascript-time-ago/locale/en' import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const columns = [ @@ -166,20 +167,7 @@ class DeviceInstall extends React.Component { } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("status") && error.response.status === 401) { - //todo display a popop with error - message.error('You are not logged in'); - window.location.href = window.location.origin + '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load devices.", - }); - } - + handleApiError(error,"Error occurred while trying to load devices."); this.setState({loading: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js index a8af7482c9..44024687a2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/GroupInstall.js @@ -21,6 +21,7 @@ import {Typography, Select, Spin, message, notification, Button} from "antd"; import debounce from 'lodash.debounce'; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const {Option} = Select; @@ -64,19 +65,8 @@ class GroupInstall extends React.Component { this.setState({data, fetching: false}); } - }).catch((error) => { console.log(error); - if (error.hasOwnProperty("status") && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+'/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load groups.", - }); - } - + }).catch((error) => { + handleApiError(error,"Error occurred while trying to load groups."); this.setState({fetching: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js index 877fd3a4cb..0f6da0f2d7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/RoleInstall.js @@ -21,6 +21,7 @@ import {Typography, Select, Spin, message, notification, Button} from "antd"; import debounce from 'lodash.debounce'; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const {Option} = Select; @@ -64,19 +65,8 @@ class RoleInstall extends React.Component { this.setState({data, fetching: false}); } - }).catch((error) => { console.log(error); - if (error.hasOwnProperty("status") && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+'/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load roles.", - }); - } - + }).catch((error) => { + handleApiError(error,"Error occurred while trying to load roles."); this.setState({fetching: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js index 8e592d52cf..d04d11a24c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/install/UserInstall.js @@ -21,6 +21,7 @@ import {Typography, Select, Spin, message, notification, Button} from "antd"; import debounce from 'lodash.debounce'; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text} = Typography; const {Option} = Select; @@ -67,18 +68,7 @@ class UserInstall extends React.Component { } }).catch((error) => { - if (error.response.hasOwnProperty(status) && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load users.", - }); - } - + handleApiError(error,"Error occurred while trying to load users."); this.setState({fetching: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js index ee2b8f65ff..e30cac30bb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/AddReview.js @@ -21,6 +21,7 @@ import {Drawer, Button, Icon, Row, Col, Typography, Divider, Input, Spin, notifi import StarRatings from "react-star-ratings"; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Title} = Typography; const {TextArea} = Input; @@ -103,20 +104,11 @@ class AddReview extends React.Component { } }).catch((error) => { - if (error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - this.setState({ - loading: false, - visible: false - }); - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "We are unable to add your review right now.", - }); - } + handleApiError(error,"We are unable to add your review right now."); + this.setState({ + loading: false, + visible: false + }); }); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js index ed1f14711c..2173db1921 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/CurrentUsersReview.js @@ -22,6 +22,7 @@ import SingleReview from "./singleReview/SingleReview"; import axios from "axios"; import AddReview from "./AddReview"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const {Text, Paragraph} = Typography; @@ -52,17 +53,7 @@ class CurrentUsersReview extends React.Component { } }).catch((error) => { - if (error.response.hasOwnProperty(status) && error.response.status === 401) { - message.error('You are not logged in'); - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to get your review.", - }); - } + handleApiError(error,"Error occurred while trying to get your review."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js index ca0e84567c..59020a26d6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/Reviews.js @@ -24,6 +24,7 @@ import InfiniteScroll from 'react-infinite-scroller'; import SingleReview from "./singleReview/SingleReview"; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; +import {handleApiError} from "../../../../js/Utils"; const limit = 5; @@ -60,16 +61,7 @@ class Reviews extends React.Component { } }).catch(function (error) { - if (error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load reviews.", - }); - } + handleApiError(error,"Error occurred while trying to load reviews."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js index 29f44b1a68..fe6ae73b9e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/SingleReview.js @@ -25,6 +25,7 @@ import "./SingleReview.css"; import EditReview from "./editReview/EditReview"; import axios from "axios"; import {withConfigContext} from "../../../../../context/ConfigContext"; +import {handleApiError} from "../../../../../js/Utils"; const {Text, Paragraph} = Typography; const colorList = ['#f0932b', '#badc58', '#6ab04c', '#eb4d4b', '#0abde3', '#9b59b6', '#3498db', '#22a6b3', '#e84393', '#f9ca24']; @@ -78,17 +79,7 @@ class SingleReview extends React.Component { this.props.deleteCallback(id); } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("response") && error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "We were unable to delete the review..", - }); - } + handleApiError(error,"We were unable to delete the review.."); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js index f18bac5079..67dd8c7c2d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/components/apps/release/review/singleReview/editReview/EditReview.js @@ -22,6 +22,7 @@ import StarRatings from "react-star-ratings"; import axios from "axios"; import "./EditReview.css"; import {withConfigContext} from "../../../../../../context/ConfigContext"; +import {handleApiError} from "../../../../../../js/Utils"; const {Title} = Typography; const {TextArea} = Input; @@ -115,21 +116,11 @@ class EditReview extends React.Component { } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("response") && error.response.status === 401) { - window.location.href = window.location.origin+ '/store/login'; - } else { - this.setState({ - loading: false, - visible: false - }); - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "We are unable to add your review right now.", - }); - } + handleApiError(error,"We are unable to add your review right now."); + this.setState({ + loading: false, + visible: false + }); }); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/js/Utils.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/js/Utils.js new file mode 100644 index 0000000000..7a51083453 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/js/Utils.js @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import {notification} from "antd"; + +export const handleApiError = (error, message) => { + console.log(error); + if (error.hasOwnProperty("response") && error.response.status === 401) { + window.location.href = window.location.origin + '/store/login'; + } else { + notification["error"]({ + message: "There was a problem", + duration: 0, + description: message, + }); + } +}; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js index 5aed47a3b4..ec615bfedf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/Login.js @@ -21,6 +21,7 @@ import {Typography, Row, Col, Form, Icon, Input, Button, Checkbox} from 'antd'; import './Login.css'; import axios from 'axios'; import {withConfigContext} from "../context/ConfigContext"; +import {handleApiError} from "../js/Utils"; const {Title} = Typography; const {Text} = Typography; @@ -104,7 +105,8 @@ class NormalLoginForm extends React.Component { window.location = window.location.origin+ "/store"; } }).catch(function (error) { - if (error.response.status === 400) { + handleApiError(error,"Error occurred while trying to load groups."); + if (error.hasOwnProperty("response") && error.response.status === 400) { thisForm.setState({ inValid: true, loading: false diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js index e59b58d990..044dae5378 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/Dashboard.js @@ -26,6 +26,7 @@ import axios from "axios"; import "./Dashboard.css"; import {withConfigContext} from "../../context/ConfigContext"; import Logout from "./logout/Logout"; +import {handleApiError} from "../../js/Utils"; const {SubMenu} = Menu; @@ -58,16 +59,7 @@ class Dashboard extends React.Component { } }).catch((error) => { - if (error.hasOwnProperty("response") && error.response.status === 401) { - window.location.href = window.location.origin + '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load device types.", - }); - } + handleApiError(error,"Error occurred while trying to load device types."); this.setState({ loading: false }); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js index b1eb744b22..adac67e737 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/apps/release/Release.js @@ -23,6 +23,7 @@ import ReleaseView from "../../../../components/apps/release/ReleaseView"; import axios from "axios"; import {withConfigContext} from "../../../../context/ConfigContext"; import {Link} from "react-router-dom"; +import {handleApiError} from "../../../../js/Utils"; const {Title} = Typography; @@ -71,20 +72,7 @@ class Release extends React.Component { } }).catch((error) => { - console.log(error); - if (error.hasOwnProperty("response") && error.response.status === 401) { - //todo display a popop with error - message.error('You are not logged in'); - window.location.href = window.location.origin + '/store/login'; - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to load releases.", - }); - } - + handleApiError(error,"Error occurred while trying to load releases."); this.setState({loading: false}); }); }; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js index 35068cc581..87c57bc014 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/react-app/src/pages/dashboard/logout/Logout.js @@ -20,6 +20,7 @@ import React from "react"; import {notification, Menu, Icon} from 'antd'; import axios from 'axios'; import {withConfigContext} from "../../../context/ConfigContext"; +import {handleApiError} from "../../../js/Utils"; /* This class for call the logout api by sending request @@ -52,19 +53,7 @@ class Logout extends React.Component { window.location = window.location.origin + "/store/login"; } }).catch(function (error) { - - if (error.hasOwnProperty("response") && error.response.status === 400) { - thisForm.setState({ - inValid: true - }); - } else { - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to logout.", - }); - } + handleApiError(error,"Error occurred while trying to get your review."); }); };