Improving application installation/uninstallation handling

4.x.x
prabathabey 9 years ago
parent af58e5ec88
commit 20fd2ec768

@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.jaxrs.beans;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import java.util.ArrayList; import java.util.ArrayList;

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api.admin;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.API;
import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.POST; import javax.ws.rs.POST;
@ -50,11 +51,12 @@ public interface ApplicationManagementAdminService {
tags = "Application Management Administrative Service") tags = "Application Management Administrative Service")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse( @ApiResponse(
code = 200, code = 202,
message = "OK. \n Install application operations have been successfully scheduled upon given devices"), message = "OK. \n Install application operation will be delivered to the given devices"),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be processed does not exist."), message = "Not Found. \n Resource to be processed does not exist."),
@ -65,7 +67,8 @@ public interface ApplicationManagementAdminService {
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while bulk issuing application installation operations upon " + "Server error occurred while bulk issuing application installation operations upon " +
"a given set of devices.") "a given set of devices.",
response = ErrorResponse.class)
}) })
Response installApplication( Response installApplication(
@ApiParam( @ApiParam(
@ -84,11 +87,12 @@ public interface ApplicationManagementAdminService {
tags = "Application Management Administrative Service") tags = "Application Management Administrative Service")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse( @ApiResponse(
code = 200, code = 202,
message = "OK. \n Uninstall application operations have been successfully scheduled upon given devices"), message = "OK. \n Uninstall application operation will be delivered to the provided devices"),
@ApiResponse( @ApiResponse(
code = 400, code = 400,
message = "Bad Request. \n Invalid request or validation error."), message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n Resource to be processed does not exist."), message = "Not Found. \n Resource to be processed does not exist."),
@ -99,7 +103,8 @@ public interface ApplicationManagementAdminService {
code = 500, code = 500,
message = "Internal Server ErrorResponse. \n " + message = "Internal Server ErrorResponse. \n " +
"Server error occurred while bulk issuing application un-installation operations upon " + "Server error occurred while bulk issuing application un-installation operations upon " +
"a given set of devices.") "a given set of devices.",
response = ErrorResponse.class)
}) })
Response uninstallApplication( Response uninstallApplication(
@ApiParam( @ApiParam(

@ -25,8 +25,12 @@ import org.wso2.carbon.device.mgt.common.Platform;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.exception.UnknownApplicationTypeException; import org.wso2.carbon.device.mgt.jaxrs.exception.UnknownApplicationTypeException;
import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.ApplicationManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.ApplicationManagementAdminService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.util.MDMAndroidOperationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.MDMAndroidOperationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.MDMIOSOperationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.MDMIOSOperationUtil;
@ -53,30 +57,45 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem
public Response installApplication(ApplicationWrapper applicationWrapper) { public Response installApplication(ApplicationWrapper applicationWrapper) {
ApplicationManager appManagerConnector; ApplicationManager appManagerConnector;
Operation operation = null; Operation operation = null;
RequestValidationUtil.validateApplicationInstallationContext(applicationWrapper);
try { try {
appManagerConnector = DeviceMgtAPIUtils.getAppManagementService(); appManagerConnector = DeviceMgtAPIUtils.getAppManagementService();
MobileApp mobileApp = applicationWrapper.getApplication(); MobileApp mobileApp = applicationWrapper.getApplication();
if (applicationWrapper.getDeviceIdentifiers() != null) { if (applicationWrapper.getDeviceIdentifiers() != null) {
for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) {
if (deviceIdentifier.getType().equals(Platform.ANDROID.toString())) { if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) {
operation = MDMAndroidOperationUtil.createInstallAppOperation(mobileApp); operation = MDMAndroidOperationUtil.createInstallAppOperation(mobileApp);
} else if (deviceIdentifier.getType().equals(Platform.IOS.toString())) { } else if (Platform.IOS.toString().equals(deviceIdentifier.getType())) {
operation = MDMIOSOperationUtil.createInstallAppOperation(mobileApp); operation = MDMIOSOperationUtil.createInstallAppOperation(mobileApp);
} }
} }
appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); if (applicationWrapper.getRoleNameList() != null && applicationWrapper.getRoleNameList().size() > 0) {
appManagerConnector.installApplicationForUserRoles(operation, applicationWrapper.getRoleNameList());
} else if (applicationWrapper.getUserNameList() != null &&
applicationWrapper.getUserNameList().size() > 0) {
appManagerConnector.installApplicationForUsers(operation, applicationWrapper.getUserNameList());
} else if (applicationWrapper.getDeviceIdentifiers() != null &&
applicationWrapper.getDeviceIdentifiers().size() > 0) {
appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers());
} else {
throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(
"No application installation criteria i.e. user/role/device is given").build());
}
} }
return Response.status(Response.Status.ACCEPTED).entity("Application installation request has been sent " + return Response.status(Response.Status.ACCEPTED).entity("Application installation request has been sent " +
"to the device").build(); "to the device(s)").build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "ErrorResponse occurred while processing application installation request"; String msg = "ErrorResponse occurred while processing application installation request";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} catch (UnknownApplicationTypeException e) { } catch (UnknownApplicationTypeException e) {
String msg = "The type of application requested to be installed is not supported"; String msg = "The type of application requested to be installed is not supported";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
} }
@ -86,30 +105,45 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem
public Response uninstallApplication(ApplicationWrapper applicationWrapper) { public Response uninstallApplication(ApplicationWrapper applicationWrapper) {
ApplicationManager appManagerConnector; ApplicationManager appManagerConnector;
org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation = null; org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation = null;
RequestValidationUtil.validateApplicationInstallationContext(applicationWrapper);
try { try {
appManagerConnector = DeviceMgtAPIUtils.getAppManagementService(); appManagerConnector = DeviceMgtAPIUtils.getAppManagementService();
MobileApp mobileApp = applicationWrapper.getApplication(); MobileApp mobileApp = applicationWrapper.getApplication();
if (applicationWrapper.getDeviceIdentifiers() != null) { if (applicationWrapper.getDeviceIdentifiers() != null) {
for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) {
if (deviceIdentifier.getType().equals(Platform.ANDROID.toString())) { if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) {
operation = MDMAndroidOperationUtil.createAppUninstallOperation(mobileApp); operation = MDMAndroidOperationUtil.createAppUninstallOperation(mobileApp);
} else if (deviceIdentifier.getType().equals(Platform.IOS.toString())) { } else if (deviceIdentifier.getType().equals(Platform.IOS.toString())) {
operation = MDMIOSOperationUtil.createAppUninstallOperation(mobileApp); operation = MDMIOSOperationUtil.createAppUninstallOperation(mobileApp);
} }
} }
appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); if (applicationWrapper.getRoleNameList() != null && applicationWrapper.getRoleNameList().size() > 0) {
appManagerConnector.installApplicationForUserRoles(operation, applicationWrapper.getRoleNameList());
} else if (applicationWrapper.getUserNameList() != null &&
applicationWrapper.getUserNameList().size() > 0) {
appManagerConnector.installApplicationForUsers(operation, applicationWrapper.getUserNameList());
} else if (applicationWrapper.getDeviceIdentifiers() != null &&
applicationWrapper.getDeviceIdentifiers().size() > 0) {
appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers());
} else {
throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(
"No application un-installation criteria i.e. user/role/device is given").build());
}
} }
return Response.status(Response.Status.ACCEPTED).entity("Application un-installation request has " + return Response.status(Response.Status.ACCEPTED).entity("Application un-installation request has " +
"been sent to the device").build(); "been sent to the given device(s)").build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "ErrorResponse occurred while processing application un-installation request"; String msg = "ErrorResponse occurred while processing application un-installation request";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} catch (UnknownApplicationTypeException e) { } catch (UnknownApplicationTypeException e) {
String msg = "The type of application requested to be un-installed is not supported"; String msg = "The type of application requested to be un-installed is not supported";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
} }

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.util;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import java.util.ArrayList; import java.util.ArrayList;
@ -207,4 +208,42 @@ public class RequestValidationUtil {
} }
} }
public static void validateApplicationInstallationContext(ApplicationWrapper installationCtx) {
int count = 0;
if (installationCtx.getDeviceIdentifiers() != null && installationCtx.getDeviceIdentifiers().size() > 0) {
count++;
}
if (installationCtx.getUserNameList() != null && installationCtx.getUserNameList().size() > 0) {
count++;
}
if (installationCtx.getRoleNameList() != null && installationCtx.getRoleNameList().size() > 0) {
count++;
}
if (count > 1) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("The incoming request has " +
"more than one application installation criteria defined").build());
}
}
public static void validateApplicationUninstallationContext(ApplicationWrapper installationCtx) {
int count = 0;
if (installationCtx.getDeviceIdentifiers() != null && installationCtx.getDeviceIdentifiers().size() > 0) {
count++;
}
if (installationCtx.getUserNameList() != null && installationCtx.getUserNameList().size() > 0) {
count++;
}
if (installationCtx.getRoleNameList() != null && installationCtx.getRoleNameList().size() > 0) {
count++;
}
if (count > 1) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("The incoming request has " +
"more than one application un-installation criteria defined").build());
}
}
} }

Loading…
Cancel
Save