Fix formatting

This commit is contained in:
Madawa Soysa 2019-02-24 16:41:23 +11:00
parent ec9451d444
commit 6470efcb31

View File

@ -57,236 +57,220 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
public class ApplicationOperationsImpl implements ApplicationOperations { public class ApplicationOperationsImpl implements ApplicationOperations {
private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class);
private static final String MEDIA_TYPE_XML = "application/xml";
private static final String INSTALL = "install";
private static final String UNINSTALL = "uninstall";
private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class); /**
public static final String MEDIA_TYPE_XML = "application/xml"; * @param applicationOperationAction holds the information needs to perform an action on mdm.
public static final String INSTALL = "install"; * @throws MobileApplicationException If error occurred while performing the action.
public static final String UNINSTALL = "uninstall"; */
public String performAction(ApplicationOperationAction applicationOperationAction)
/** throws MobileApplicationException {
* @param applicationOperationAction holds the information needs to perform an action on mdm. if (log.isDebugEnabled()) {
* @throws MobileApplicationException log.debug(applicationOperationAction.getAction() + " action is triggered for " +
*/ applicationOperationAction.getType() + ".");
public String performAction(ApplicationOperationAction applicationOperationAction) }
throws MobileApplicationException { Operation operation = null;
if (log.isDebugEnabled()) { List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
log.debug(applicationOperationAction.getAction() + " action is triggered for " + List<org.wso2.carbon.device.mgt.common.Device> deviceList;
applicationOperationAction.getType() +"."); if (MDMAppConstants.USER.equals(applicationOperationAction.getType())) {
} String userName = null;
try {
Operation operation = null; for (String param : applicationOperationAction.getParams()) {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); userName = param;
List<org.wso2.carbon.device.mgt.common.Device> deviceList; deviceList = MDMServiceAPIUtils
if (MDMAppConstants.USER.equals(applicationOperationAction.getType())) { .getDeviceManagementService(applicationOperationAction.getTenantId()).
String userName = null; getDevicesOfUser(userName);
try { for (org.wso2.carbon.device.mgt.common.Device device : deviceList) {
for (String param : applicationOperationAction.getParams()) {
userName = param;
deviceList = MDMServiceAPIUtils
.getDeviceManagementService(applicationOperationAction.getTenantId()).
getDevicesOfUser(userName);
for (org.wso2.carbon.device.mgt.common.Device device : deviceList) {
if (MDMAppConstants.WEBAPP.equals(applicationOperationAction.getApp().getPlatform()) || if (MDMAppConstants.WEBAPP.equals(applicationOperationAction.getApp().getPlatform()) ||
applicationOperationAction.getApp().getPlatform().equalsIgnoreCase(device.getType())) { applicationOperationAction.getApp().getPlatform().equalsIgnoreCase(device.getType())) {
if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo(). if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().
getStatus().toString())) { getStatus().toString())) {
deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); deviceIdentifiers.add(getDeviceIdentifierByDevice(device));
} }
} }
} }
} }
} catch (DeviceManagementException devEx) { } catch (DeviceManagementException devEx) {
String errorMsg = "Error occurred fetch device for user " + userName + String errorMsg = "Error occurred fetch device for user " + userName + " at app installation";
" at app installation";
logError(errorMsg, devEx); logError(errorMsg, devEx);
throw new MobileApplicationException(errorMsg, devEx); throw new MobileApplicationException(errorMsg, devEx);
} }
} else if (MDMAppConstants.ROLE.equals(applicationOperationAction.getType())) { } else if (MDMAppConstants.ROLE.equals(applicationOperationAction.getType())) {
String userRole = null; String userRole = null;
try { try {
for (String param : applicationOperationAction.getParams()) { for (String param : applicationOperationAction.getParams()) {
userRole = param; userRole = param;
deviceList = MDMServiceAPIUtils
deviceList = MDMServiceAPIUtils .getDeviceManagementService(applicationOperationAction.getTenantId()).
.getDeviceManagementService(applicationOperationAction.getTenantId()). getAllDevicesOfRole(userRole);
getAllDevicesOfRole(userRole); for (org.wso2.carbon.device.mgt.common.Device device : deviceList) {
if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) {
for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { deviceIdentifiers.add(getDeviceIdentifierByDevice(device));
if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) { }
deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); }
} }
} } catch (DeviceManagementException devMgtEx) {
} String errorMsg = "Error occurred fetch device for user role " + userRole + " at app installation";
} catch (DeviceManagementException devMgtEx) {
String errorMsg = "Error occurred fetch device for user role " + userRole +
" at app installation";
logError(errorMsg, devMgtEx); logError(errorMsg, devMgtEx);
throw new MobileApplicationException(errorMsg, devMgtEx); throw new MobileApplicationException(errorMsg, devMgtEx);
} }
} else if (MDMAppConstants.DEVICE.equals(applicationOperationAction.getType())) {
DeviceIdentifier deviceIdentifier;
for (String param : applicationOperationAction.getParams()) {
deviceIdentifier = new DeviceIdentifier();
if (isValidJSON(param)) {
JSONParser parser = new JSONParser();
try {
JSONObject parsedObj = (JSONObject) parser.parse(param);
deviceIdentifier.setId((String) parsedObj.get(MDMAppConstants.ID));
deviceIdentifier.setType((String) parsedObj.get(MDMAppConstants.TYPE));
deviceIdentifiers.add(deviceIdentifier);
} catch (ParseException e) {
logError("Device Identifier is not valid json object.", e);
throw new MobileApplicationException(e);
}
}
}
} else {
throw new IllegalStateException("invalid type is received from app store.");
}
App app = applicationOperationAction.getApp();
MobileApp mobileApp = new MobileApp();
mobileApp.setId(app.getId());
mobileApp.setType(MobileAppTypes.valueOf(app.getType().toUpperCase()));
mobileApp.setAppIdentifier(app.getAppIdentifier());
mobileApp.setIconImage(app.getIconImage());
mobileApp.setIdentifier(app.getIdentifier());
mobileApp.setLocation(app.getLocation());
mobileApp.setName(app.getName());
mobileApp.setPackageName(app.getPackageName());
mobileApp.setPlatform(app.getPlatform());
mobileApp.setVersion(app.getVersion());
Properties properties = new Properties();
} else if (MDMAppConstants.DEVICE.equals(applicationOperationAction.getType())) { if (MDMAppConstants.IOS.equals(app.getPlatform())) {
DeviceIdentifier deviceIdentifier; if (MDMAppConstants.ENTERPRISE.equals(app.getType())) {
for (String param : applicationOperationAction.getParams()) { properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true);
deviceIdentifier = new DeviceIdentifier(); properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true);
if (isValidJSON(param)) { } else if (MDMAppConstants.IOSConstants.PUBLIC.equals(app.getType())) {
JSONParser parser = new JSONParser(); properties.put(MDMAppConstants.IOSConstants.I_TUNES_ID, app.getIdentifier());
try { properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true);
JSONObject parsedObj = (JSONObject) parser.parse(param); properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true);
deviceIdentifier.setId((String) parsedObj.get(MDMAppConstants.ID)); } else if (MDMAppConstants.WEBAPP.equals(app.getType())) {
deviceIdentifier.setType((String) parsedObj.get(MDMAppConstants.TYPE)); properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName());
deviceIdentifiers.add(deviceIdentifier); properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true);
} catch (ParseException e) { }
logError("Device Identifier is not valid json object.", e); } else if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) {
throw new MobileApplicationException(e); properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName());
} properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true);
}
} mobileApp.setProperties(properties);
} Activity activity = null;
} else { try {
throw new IllegalStateException("invalid type is received from app store."); if (deviceIdentifiers.size() > 0) {
} if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.ANDROID.toString())) {
App app = applicationOperationAction.getApp(); if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) {
MobileApp mobileApp = new MobileApp(); operation = AndroidApplicationOperationUtil
mobileApp.setId(app.getId()); .createInstallAppOperation(mobileApp, applicationOperationAction.getSchedule());
mobileApp.setType(MobileAppTypes.valueOf(app.getType().toUpperCase())); } else if (MDMAppConstants.UPDATE.equals(applicationOperationAction.getAction())) {
mobileApp.setAppIdentifier(app.getAppIdentifier()); operation = AndroidApplicationOperationUtil
mobileApp.setIconImage(app.getIconImage()); .createUpdateAppOperation(mobileApp, applicationOperationAction.getSchedule());
mobileApp.setIdentifier(app.getIdentifier()); } else {
mobileApp.setLocation(app.getLocation()); operation = AndroidApplicationOperationUtil
mobileApp.setName(app.getName()); .createAppUninstallOperation(mobileApp, applicationOperationAction.getSchedule());
mobileApp.setPackageName(app.getPackageName()); }
mobileApp.setPlatform(app.getPlatform()); } else if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.IOS.toString())) {
mobileApp.setVersion(app.getVersion()); if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) {
Properties properties = new Properties(); operation =
IOSApplicationOperationUtil.createInstallAppOperation(mobileApp);
if (MDMAppConstants.IOS.equals(app.getPlatform())) { } else {
if (MDMAppConstants.ENTERPRISE.equals(app.getType())) { if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) {
properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); operation = IOSApplicationOperationUtil.createWebClipUninstallOperation(mobileApp);
properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); } else {
} else if (MDMAppConstants.IOSConstants.PUBLIC.equals(app.getType())) { operation = IOSApplicationOperationUtil.createAppUninstallOperation(mobileApp);
properties.put(MDMAppConstants.IOSConstants.I_TUNES_ID, app.getIdentifier()); }
properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); }
properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); }
} else if (MDMAppConstants.WEBAPP.equals(app.getType())) {
properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName());
properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true);
}
} else if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) {
properties.put(MDMAppConstants.IOSConstants.LABEL, app.getName());
properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true);
}
mobileApp.setProperties(properties);
Activity activity = null;
try {
if (deviceIdentifiers.size() > 0) {
if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.ANDROID.toString())) {
if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) {
operation = AndroidApplicationOperationUtil
.createInstallAppOperation(mobileApp, applicationOperationAction.getSchedule());
} else if (MDMAppConstants.UPDATE.equals(applicationOperationAction.getAction())) {
operation = AndroidApplicationOperationUtil
.createUpdateAppOperation(mobileApp, applicationOperationAction.getSchedule());
} else {
operation = AndroidApplicationOperationUtil
.createAppUninstallOperation(mobileApp, applicationOperationAction.getSchedule());
}
} else if (deviceIdentifiers.get(0).getType().equalsIgnoreCase(Platform.IOS.toString())) {
if (MDMAppConstants.INSTALL.equals(applicationOperationAction.getAction())) {
operation =
IOSApplicationOperationUtil.createInstallAppOperation(mobileApp);
} else {
if (MDMAppConstants.WEBAPP.equals(app.getPlatform())) {
operation = IOSApplicationOperationUtil.createWebClipUninstallOperation(mobileApp);
} else {
operation = IOSApplicationOperationUtil.createAppUninstallOperation(mobileApp);
}
}
}
activity = MDMServiceAPIUtils.getAppManagementService(applicationOperationAction.getTenantId()) activity = MDMServiceAPIUtils.getAppManagementService(applicationOperationAction.getTenantId())
.installApplicationForDevices(operation, deviceIdentifiers); .installApplicationForDevices(operation, deviceIdentifiers);
}
if (activity != null) {
return activity.getActivityId();
}
return null;
} catch (DeviceApplicationException mdmExce) {
logError("Error in creating operation object using app.", mdmExce);
throw new MobileApplicationException(mdmExce.getMessage());
} catch (ApplicationManagementException appMgtExce) {
logError("Error in app installation.", appMgtExce);
throw new MobileApplicationException(appMgtExce.getErrorMessage());
}
}
/**
* Create a new device identifier from Device object.
*
* @param device device which is to be retrieved type and id
* @return created device identifier
*/
private static DeviceIdentifier getDeviceIdentifierByDevice(org.wso2.carbon.device.mgt.common.Device device) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(device.getDeviceIdentifier());
deviceIdentifier.setType(device.getType());
return deviceIdentifier;
}
} /**
* @param applicationOperationDevice holds the information needs to retrieve device list.
if(activity != null){ * @return List of devices
return activity.getActivityId(); * @throws MobileApplicationException If an error occurred in getting devices or if app platform is not supported.
} */
public List<Device> getDevices(ApplicationOperationDevice applicationOperationDevice)
return null; throws MobileApplicationException {
List<Device> devices;
} catch (DeviceApplicationException mdmExce) { List<org.wso2.carbon.device.mgt.common.Device> deviceList;
logError("Error in creating operation object using app.", mdmExce); try {
throw new MobileApplicationException(mdmExce.getMessage()); DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils
} catch (ApplicationManagementException appMgtExce) { .getDeviceManagementService(applicationOperationDevice.getTenantId());
logError("Error in app installation.", appMgtExce); final String username = applicationOperationDevice.getCurrentUser().getUsername();
throw new MobileApplicationException(appMgtExce.getErrorMessage()); final String platform = applicationOperationDevice.getPlatform();
} switch (platform) {
case MDMAppConstants.WEBAPP:
} deviceList = deviceManagementService.getDevicesOfUser(username);
break;
/** case MDMAppConstants.ANDROID:
* Create a new device identifier from Device object. deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID);
* @param device device which is to be retrieved type and id break;
* @return created device identifier case MDMAppConstants.IOS:
*/ deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS);
private static DeviceIdentifier getDeviceIdentifierByDevice( break;
org.wso2.carbon.device.mgt.common.Device device) { default:
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); String msg = "App platform:" + platform + "is not supported.";
deviceIdentifier.setId(device.getDeviceIdentifier()); log.error(msg);
deviceIdentifier.setType(device.getType()); throw new MobileApplicationException(msg);
}
return deviceIdentifier; devices = new ArrayList<>(deviceList.size());
} if (log.isDebugEnabled()) {
log.debug("device list got from mdm " + deviceList.toString());
/** }
* @param applicationOperationDevice holds the information needs to retrieve device list. for (org.wso2.carbon.device.mgt.common.Device commonDevice : deviceList) {
* @return List of devices if (MDMAppConstants.ACTIVE
* @throws MobileApplicationException If unexpected error occur in getting devices or if app platform is not supported. .equals(commonDevice.getEnrolmentInfo().getStatus().toString().
*/ toLowerCase())) {
public List<Device> getDevices(ApplicationOperationDevice applicationOperationDevice) Device device = new Device();
throws MobileApplicationException { org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier deviceIdentifier =
new org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier();
List<Device> devices; deviceIdentifier.setId(commonDevice.getDeviceIdentifier());
List<org.wso2.carbon.device.mgt.common.Device> deviceList; deviceIdentifier.setType(commonDevice.getType());
try { device.setDeviceIdentifier(deviceIdentifier);
DeviceManagementProviderService deviceManagementService = MDMServiceAPIUtils device.setName(commonDevice.getName());
.getDeviceManagementService(applicationOperationDevice.getTenantId()); device.setModel(commonDevice.getName());
final String username = applicationOperationDevice.getCurrentUser().getUsername(); device.setType(MDMAppConstants.MOBILE_DEVICE);
final String platform = applicationOperationDevice.getPlatform(); String imgUrl;
switch (platform) { if (MDMAppConstants.ANDROID.equalsIgnoreCase(commonDevice.getType())) {
case MDMAppConstants.WEBAPP:
deviceList = deviceManagementService.getDevicesOfUser(username);
break;
case MDMAppConstants.ANDROID:
deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.ANDROID);
break;
case MDMAppConstants.IOS:
deviceList = deviceManagementService.getDevicesOfUser(username, MDMAppConstants.IOS);
break;
default:
throw new MobileApplicationException("App platform:" + platform + "is not supported.");
}
devices = new ArrayList<>(deviceList.size());
if(log.isDebugEnabled()){
log.debug("device list got from mdm "+ deviceList.toString());
}
for (org.wso2.carbon.device.mgt.common.Device commonDevice : deviceList) {
if (MDMAppConstants.ACTIVE
.equals(commonDevice.getEnrolmentInfo().getStatus().toString().
toLowerCase())) {
Device device = new Device();
org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier deviceIdentifier =
new org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier();
deviceIdentifier.setId(commonDevice.getDeviceIdentifier());
deviceIdentifier.setType(commonDevice.getType());
device.setDeviceIdentifier(deviceIdentifier);
device.setName(commonDevice.getName());
device.setModel(commonDevice.getName());
device.setType(MDMAppConstants.MOBILE_DEVICE);
String imgUrl;
if (MDMAppConstants.ANDROID.equalsIgnoreCase(commonDevice.getType())) {
imgUrl = String.format(applicationOperationDevice.getConfigParams() imgUrl = String.format(applicationOperationDevice.getConfigParams()
.get(MDMAppConstants.IMAGE_URL), .get(MDMAppConstants.IMAGE_URL),
MDMAppConstants.NEXUS); MDMAppConstants.NEXUS);
@ -299,38 +283,38 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
.get(MDMAppConstants.IMAGE_URL), .get(MDMAppConstants.IMAGE_URL),
MDMAppConstants.NONE); MDMAppConstants.NONE);
} }
device.setImage(imgUrl); device.setImage(imgUrl);
device.setPlatform(commonDevice.getType()); device.setPlatform(commonDevice.getType());
devices.add(device); devices.add(device);
} }
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
logError("Error While retrieving Device List.", e); logError("Error While retrieving Device List.", e);
throw new MobileApplicationException(e.getMessage()); throw new MobileApplicationException(e.getMessage());
} }
return devices; return devices;
} }
private boolean isValidJSON(String json) { private boolean isValidJSON(String json) {
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
try { try {
parser.parse(json); parser.parse(json);
} catch (ParseException e) { } catch (ParseException e) {
return false; return false;
} }
return true; return true;
} }
private void logError(String errorMessage, Throwable e) { private void logError(String errorMessage, Throwable e) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.error(errorMessage, e); log.error(errorMessage, e);
} else { } else {
log.error(errorMessage); log.error(errorMessage);
} }
} }
public static UserStoreManager getUserStoreManager() throws UserStoreException { private static UserStoreManager getUserStoreManager() throws UserStoreException {
RealmService realmService; RealmService realmService;
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
@ -368,7 +352,6 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
@Override @Override
public void run() { public void run() {
try { try {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
@ -462,6 +445,4 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} }
} }
} }
} }