|
|
|
@ -16,17 +16,16 @@
|
|
|
|
|
|
|
|
|
|
package cdm.api.android;
|
|
|
|
|
|
|
|
|
|
import cdm.api.android.common.AndroidAgentException;
|
|
|
|
|
import cdm.api.android.util.AndroidAPIUtils;
|
|
|
|
|
import cdm.api.android.util.AndroidConstants;
|
|
|
|
|
import cdm.api.android.util.Message;
|
|
|
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementServiceException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
|
|
|
|
|
|
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
@ -50,82 +49,70 @@ public class Enrollment {
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
@POST
|
|
|
|
|
public Message enrollDevice(Device device) {
|
|
|
|
|
public Message enrollDevice(org.wso2.carbon.device.mgt.common.Device device) throws AndroidAgentException {
|
|
|
|
|
|
|
|
|
|
boolean result = false;
|
|
|
|
|
int status = 0;
|
|
|
|
|
String msg = "";
|
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
|
} finally {
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
|
|
|
|
String errorMsg = "Device management service error";
|
|
|
|
|
log.error(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (dmService != null) {
|
|
|
|
|
device.setType(
|
|
|
|
|
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
|
|
|
|
result = dmService.enrollDevice(device);
|
|
|
|
|
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
|
|
|
|
dmService.enrollDevice(device);
|
|
|
|
|
Response.status(HttpStatus.SC_CREATED);
|
|
|
|
|
responseMsg.setResponseMessage("Device enrollment has succeeded");
|
|
|
|
|
responseMsg.setResponseMessage("Device enrollment succeeded");
|
|
|
|
|
return responseMsg;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
responseMsg.setResponseMessage(
|
|
|
|
|
AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
return responseMsg;
|
|
|
|
|
}
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
responseMsg.setResponseMessage("Error occurred while enrolling the device");
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
return responseMsg;
|
|
|
|
|
} catch (DeviceManagementException deviceMgtEx) {
|
|
|
|
|
String errorMsg = "Error occurred while enrolling the device";
|
|
|
|
|
log.error(errorMsg, deviceMgtEx);
|
|
|
|
|
throw new AndroidAgentException(errorMsg, deviceMgtEx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
@Path("{id}")
|
|
|
|
|
public Message isEnrolled(@PathParam("id") String id) {
|
|
|
|
|
public Message isEnrolled(@PathParam("id") String id) throws AndroidAgentException {
|
|
|
|
|
|
|
|
|
|
boolean result = false;
|
|
|
|
|
String msg = "";
|
|
|
|
|
boolean result;
|
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
|
} finally {
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
|
|
|
|
String errorMsg = "Device management service error";
|
|
|
|
|
log.error(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (dmService != null) {
|
|
|
|
|
result = dmService.isEnrolled(deviceIdentifier);
|
|
|
|
|
if (result) {
|
|
|
|
|
Response.status(HttpStatus.SC_OK);
|
|
|
|
|
responseMsg.setResponseMessage("Device has already enrolled");
|
|
|
|
|
} else {
|
|
|
|
|
Response.status(HttpStatus.SC_NOT_FOUND);
|
|
|
|
|
responseMsg.setResponseMessage("Device has not enrolled");
|
|
|
|
|
}
|
|
|
|
|
return responseMsg;
|
|
|
|
|
} else {
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
responseMsg.setResponseMessage(
|
|
|
|
|
AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
|
|
|
|
|
return responseMsg;
|
|
|
|
|
responseMsg.setResponseMessage("Device not found");
|
|
|
|
|
}
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
msg = "Error occurred while checking the enrollment of the device.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
|
|
return responseMsg;
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementException deviceMgtEx) {
|
|
|
|
|
String errormsg = "Error occurred while enrollment of the device.";
|
|
|
|
|
log.error(errormsg, deviceMgtEx);
|
|
|
|
|
throw new AndroidAgentException(errormsg, deviceMgtEx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -140,42 +127,41 @@ public class Enrollment {
|
|
|
|
|
**/
|
|
|
|
|
@PUT
|
|
|
|
|
@Path("{id}")
|
|
|
|
|
public Message modifyEnrollment(@PathParam("id") String id, Device device) {
|
|
|
|
|
boolean result = false;
|
|
|
|
|
String msg = "";
|
|
|
|
|
public Message modifyEnrollment(@PathParam("id") String id, org.wso2.carbon.device.mgt.common.Device device)
|
|
|
|
|
throws AndroidAgentException {
|
|
|
|
|
|
|
|
|
|
boolean result;
|
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
|
} finally {
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
|
|
|
|
String errorMsg = "Device management service error";
|
|
|
|
|
log.error(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (dmService != null) {
|
|
|
|
|
device.setType(
|
|
|
|
|
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
|
|
|
|
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
|
|
|
|
result = dmService.modifyEnrollment(device);
|
|
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
responseMsg.setResponseMessage("Device enrollment has updated successfully");
|
|
|
|
|
Response.status(HttpStatus.SC_OK);
|
|
|
|
|
} else {
|
|
|
|
|
responseMsg.setResponseMessage("Update enrollment has failed");
|
|
|
|
|
responseMsg.setResponseMessage("device not found for enrollment");
|
|
|
|
|
Response.status(HttpStatus.SC_NOT_MODIFIED);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseMsg;
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
msg = "Error occurred while modifying enrollment of the device";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
String errorMsg = "Error occurred while modifying enrollment of the device";
|
|
|
|
|
log.error(errorMsg, e);
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
|
responseMsg.setResponseMessage(errorMsg);
|
|
|
|
|
return responseMsg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -183,21 +169,24 @@ public class Enrollment {
|
|
|
|
|
|
|
|
|
|
@DELETE
|
|
|
|
|
@Path("{id}")
|
|
|
|
|
public Message disenrollDevice(@PathParam("id") String id) {
|
|
|
|
|
public Message disenrollDevice(@PathParam("id") String id) throws AndroidAgentException {
|
|
|
|
|
|
|
|
|
|
boolean result = false;
|
|
|
|
|
String msg = "";
|
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
|
|
|
|
|
|
boolean result;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
|
} finally {
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
|
|
|
|
String errorMsg = "Device management service error";
|
|
|
|
|
log.error(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
|
|
|
|
}
|
|
|
|
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (dmService != null) {
|
|
|
|
|
result = dmService.disenrollDevice(deviceIdentifier);
|
|
|
|
|
if (result) {
|
|
|
|
|
responseMsg.setResponseMessage("Device has disenrolled successfully");
|
|
|
|
@ -206,18 +195,13 @@ public class Enrollment {
|
|
|
|
|
responseMsg.setResponseMessage("Device not found");
|
|
|
|
|
Response.status(HttpStatus.SC_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseMsg;
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
msg = "Error occurred while disenrolling the device";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
} catch (DeviceManagementException deviceMgtEx) {
|
|
|
|
|
String errorMsg = "Error occurred while dis enrolling the device";
|
|
|
|
|
log.error(errorMsg, deviceMgtEx);
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
|
responseMsg.setResponseMessage(errorMsg);
|
|
|
|
|
return responseMsg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|