mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
JAX-RS refractor
This commit is contained in:
parent
a6dd8875b5
commit
ff2ac51ea3
@ -16,18 +16,17 @@
|
|||||||
|
|
||||||
package cdm.api.android;
|
package cdm.api.android;
|
||||||
|
|
||||||
|
import cdm.api.android.common.AndroidAgentException;
|
||||||
import cdm.api.android.util.AndroidAPIUtils;
|
import cdm.api.android.util.AndroidAPIUtils;
|
||||||
import cdm.api.android.util.AndroidConstants;
|
|
||||||
import cdm.api.android.util.Message;
|
import cdm.api.android.util.Message;
|
||||||
import org.apache.commons.httpclient.HttpStatus;
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
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 org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
|
||||||
|
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -39,141 +38,104 @@ import java.util.List;
|
|||||||
@Consumes({ "application/json", "application/xml" })
|
@Consumes({ "application/json", "application/xml" })
|
||||||
public class Device {
|
public class Device {
|
||||||
|
|
||||||
private static Log log = LogFactory.getLog(Device.class);
|
private static Log log = LogFactory.getLog(Device.class);
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
public List<org.wso2.carbon.device.mgt.common.Device> getAllDevices() {
|
public List<org.wso2.carbon.device.mgt.common.Device> getAllDevices() throws AndroidAgentException {
|
||||||
List<org.wso2.carbon.device.mgt.common.Device> devices = null;
|
|
||||||
String msg = "";
|
|
||||||
DeviceManagementService dmService;
|
|
||||||
|
|
||||||
try {
|
List<org.wso2.carbon.device.mgt.common.Device> devices;
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
String msg;
|
||||||
} finally {
|
DeviceManagementService dmService;
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (dmService != null) {
|
|
||||||
devices = dmService.getAllDevices(
|
|
||||||
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
|
||||||
Response.status(HttpStatus.SC_OK);
|
|
||||||
} else {
|
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
} catch (DeviceManagementException e) {
|
|
||||||
msg = "Error occurred while fetching the device list.";
|
|
||||||
log.error(msg, e);
|
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
return devices;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
try {
|
||||||
@Path("{id}")
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
||||||
public org.wso2.carbon.device.mgt.common.Device getDevice(@PathParam("id") String id) {
|
|
||||||
String msg = "";
|
|
||||||
DeviceManagementService dmService;
|
|
||||||
org.wso2.carbon.device.mgt.common.Device device =
|
|
||||||
new org.wso2.carbon.device.mgt.common.Device();
|
|
||||||
|
|
||||||
try {
|
} catch (DeviceManagementServiceException deviceMgtServiceEx) {
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
String errorMsg = "Device management service error";
|
||||||
} finally {
|
log.error(errorMsg, deviceMgtServiceEx);
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
throw new AndroidAgentException();
|
||||||
}
|
}
|
||||||
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
||||||
try {
|
|
||||||
if (dmService != null) {
|
|
||||||
device = dmService.getDevice(deviceIdentifier);
|
|
||||||
if (device == null) {
|
|
||||||
Response.status(HttpStatus.SC_NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
try {
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (DeviceManagementException e) {
|
devices = dmService.getAllDevices(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
||||||
msg = "Error occurred while fetching the device information.";
|
Response.status(HttpStatus.SC_OK);
|
||||||
log.error(msg, e);
|
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
return device;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PUT
|
} catch (DeviceManagementException e) {
|
||||||
@Path("{id}")
|
msg = "Error occurred while fetching the device list.";
|
||||||
public Message updateDevice(@PathParam("id") String id,
|
log.error(msg, e);
|
||||||
org.wso2.carbon.device.mgt.common.Device device) {
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
boolean result = false;
|
throw new AndroidAgentException(msg, e);
|
||||||
String msg = "";
|
|
||||||
DeviceManagementService dmService;
|
|
||||||
Message responseMessage = new Message();
|
|
||||||
|
|
||||||
try {
|
}
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
return devices;
|
||||||
} finally {
|
}
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (dmService != null) {
|
|
||||||
device.setType(
|
|
||||||
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
|
||||||
result = dmService.updateDeviceInfo(device);
|
|
||||||
if (result) {
|
|
||||||
Response.status(HttpStatus.SC_OK);
|
|
||||||
responseMessage.setResponseMessage("Device information has modified successfully.");
|
|
||||||
} else {
|
|
||||||
Response.status(HttpStatus.SC_NOT_MODIFIED);
|
|
||||||
responseMessage.setResponseMessage("Update device has failed.");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
||||||
responseMessage.setResponseMessage(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (DeviceManagementException e) {
|
@GET
|
||||||
msg = "Error occurred while modifying the device information.";
|
@Path("{id}")
|
||||||
log.error(msg, e);
|
public org.wso2.carbon.device.mgt.common.Device getDevice(@PathParam("id") String id) throws AndroidAgentException {
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
String msg;
|
||||||
responseMessage.setResponseMessage(msg);
|
DeviceManagementService dmService;
|
||||||
|
org.wso2.carbon.device.mgt.common.Device device;
|
||||||
|
|
||||||
}
|
try {
|
||||||
return responseMessage;
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
} catch (DeviceManagementServiceException deviceMgtServiceEx) {
|
||||||
@Path("/operations/{id}")
|
String errorMsg = "Device management service error";
|
||||||
public org.wso2.carbon.device.mgt.common.Device getOperations(@PathParam("id") String id) {
|
log.error(errorMsg, deviceMgtServiceEx);
|
||||||
String msg = "";
|
throw new AndroidAgentException(errorMsg, deviceMgtServiceEx);
|
||||||
DeviceManagementService dmService;
|
}
|
||||||
org.wso2.carbon.device.mgt.common.Device device =
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
||||||
new org.wso2.carbon.device.mgt.common.Device();
|
try {
|
||||||
|
device = dmService.getDevice(deviceIdentifier);
|
||||||
|
if (device == null) {
|
||||||
|
Response.status(HttpStatus.SC_NOT_FOUND);
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException deviceMgtEx) {
|
||||||
|
msg = "Error occurred while fetching the device information.";
|
||||||
|
log.error(msg, deviceMgtEx);
|
||||||
|
throw new AndroidAgentException(msg, deviceMgtEx);
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
@PUT
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
@Path("{id}")
|
||||||
} finally {
|
public Message updateDevice(@PathParam("id") String id, org.wso2.carbon.device.mgt.common.Device device) throws
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
AndroidAgentException {
|
||||||
}
|
|
||||||
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
||||||
try {
|
|
||||||
if (dmService != null) {
|
|
||||||
device = dmService.getDevice(deviceIdentifier);
|
|
||||||
if (device == null) {
|
|
||||||
Response.status(HttpStatus.SC_NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
DeviceManagementService dmService = null;
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
Message responseMessage = new Message();
|
||||||
}
|
|
||||||
|
|
||||||
} catch (DeviceManagementException e) {
|
boolean result;
|
||||||
msg = "Error occurred while fetching the device information.";
|
|
||||||
log.error(msg, e);
|
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
return device;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
||||||
|
|
||||||
|
} catch (DeviceManagementServiceException deviceManagementServiceException) {
|
||||||
|
String errorMsg = "Device management service error";
|
||||||
|
log.error(errorMsg, deviceManagementServiceException);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
||||||
|
result = dmService.updateDeviceInfo(device);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
Response.status(HttpStatus.SC_OK);
|
||||||
|
responseMessage.setResponseMessage("Device information has modified successfully.");
|
||||||
|
} else {
|
||||||
|
Response.status(HttpStatus.SC_NOT_MODIFIED);
|
||||||
|
responseMessage.setResponseMessage("Device not found for the update.");
|
||||||
|
}
|
||||||
|
return responseMessage;
|
||||||
|
|
||||||
|
} catch (DeviceManagementException deviceMgtEx) {
|
||||||
|
String msg = "Error occurred while modifying the device information.";
|
||||||
|
log.error(msg, deviceMgtEx);
|
||||||
|
throw new AndroidAgentException(msg, deviceMgtEx);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,17 +16,16 @@
|
|||||||
|
|
||||||
package cdm.api.android;
|
package cdm.api.android;
|
||||||
|
|
||||||
|
import cdm.api.android.common.AndroidAgentException;
|
||||||
import cdm.api.android.util.AndroidAPIUtils;
|
import cdm.api.android.util.AndroidAPIUtils;
|
||||||
import cdm.api.android.util.AndroidConstants;
|
|
||||||
import cdm.api.android.util.Message;
|
import cdm.api.android.util.Message;
|
||||||
import org.apache.commons.httpclient.HttpStatus;
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
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.DeviceIdentifier;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
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 org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
|
||||||
|
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
@ -39,186 +38,171 @@ import javax.ws.rs.core.Response;
|
|||||||
@Consumes({ "application/json", "application/xml" })
|
@Consumes({ "application/json", "application/xml" })
|
||||||
public class Enrollment {
|
public class Enrollment {
|
||||||
|
|
||||||
private static Log log = LogFactory.getLog(Enrollment.class);
|
private static Log log = LogFactory.getLog(Enrollment.class);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD",
|
* Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD",
|
||||||
* "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"},
|
* "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"},
|
||||||
* {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"},
|
* {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"},
|
||||||
* {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"},
|
* {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"},
|
||||||
* {"name":"osVersion","value":"5.0.0"}]}
|
* {"name":"osVersion","value":"5.0.0"}]}
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
@POST
|
@POST
|
||||||
public Message enrollDevice(Device device) {
|
public Message enrollDevice(org.wso2.carbon.device.mgt.common.Device device) throws AndroidAgentException {
|
||||||
|
|
||||||
boolean result = false;
|
DeviceManagementService dmService;
|
||||||
int status = 0;
|
Message responseMsg = new Message();
|
||||||
String msg = "";
|
|
||||||
DeviceManagementService dmService;
|
|
||||||
Message responseMsg = new Message();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
||||||
} finally {
|
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
||||||
if (dmService != null) {
|
String errorMsg = "Device management service error";
|
||||||
device.setType(
|
log.error(errorMsg, deviceServiceMgtEx);
|
||||||
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
||||||
result = dmService.enrollDevice(device);
|
}
|
||||||
Response.status(HttpStatus.SC_CREATED);
|
|
||||||
responseMsg.setResponseMessage("Device enrollment has succeeded");
|
|
||||||
return responseMsg;
|
|
||||||
|
|
||||||
} else {
|
try {
|
||||||
responseMsg.setResponseMessage(
|
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
||||||
AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
|
dmService.enrollDevice(device);
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
Response.status(HttpStatus.SC_CREATED);
|
||||||
return responseMsg;
|
responseMsg.setResponseMessage("Device enrollment succeeded");
|
||||||
}
|
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) {
|
|
||||||
|
|
||||||
boolean result = false;
|
@GET
|
||||||
String msg = "";
|
@Path("{id}")
|
||||||
DeviceManagementService dmService;
|
public Message isEnrolled(@PathParam("id") String id) throws AndroidAgentException {
|
||||||
Message responseMsg = new Message();
|
|
||||||
|
|
||||||
try {
|
boolean result;
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
DeviceManagementService dmService;
|
||||||
} finally {
|
Message responseMsg = new Message();
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
try {
|
||||||
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
||||||
|
|
||||||
/*
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
||||||
* Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD",
|
String errorMsg = "Device management service error";
|
||||||
* "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"},
|
log.error(errorMsg, deviceServiceMgtEx);
|
||||||
* {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"},
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
||||||
* {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"},
|
}
|
||||||
* {"name":"osVersion","value":"5.0.0"}]}
|
|
||||||
*
|
|
||||||
**/
|
|
||||||
@PUT
|
|
||||||
@Path("{id}")
|
|
||||||
public Message modifyEnrollment(@PathParam("id") String id, Device device) {
|
|
||||||
boolean result = false;
|
|
||||||
String msg = "";
|
|
||||||
DeviceManagementService dmService;
|
|
||||||
Message responseMsg = new Message();
|
|
||||||
|
|
||||||
try {
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
||||||
} finally {
|
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (dmService != null) {
|
result = dmService.isEnrolled(deviceIdentifier);
|
||||||
device.setType(
|
if (result) {
|
||||||
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
Response.status(HttpStatus.SC_OK);
|
||||||
result = dmService.modifyEnrollment(device);
|
responseMsg.setResponseMessage("Device has already enrolled");
|
||||||
|
} else {
|
||||||
|
Response.status(HttpStatus.SC_NOT_FOUND);
|
||||||
|
responseMsg.setResponseMessage("Device not found");
|
||||||
|
}
|
||||||
|
|
||||||
if (result) {
|
return responseMsg;
|
||||||
responseMsg.setResponseMessage("Device enrollment has updated successfully");
|
|
||||||
Response.status(HttpStatus.SC_OK);
|
|
||||||
} else {
|
|
||||||
responseMsg.setResponseMessage("Update enrollment has failed");
|
|
||||||
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);
|
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
responseMsg.setResponseMessage(msg);
|
|
||||||
return responseMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
} catch (DeviceManagementException deviceMgtEx) {
|
||||||
|
String errormsg = "Error occurred while enrollment of the device.";
|
||||||
|
log.error(errormsg, deviceMgtEx);
|
||||||
|
throw new AndroidAgentException(errormsg, deviceMgtEx);
|
||||||
|
}
|
||||||
|
|
||||||
@DELETE
|
}
|
||||||
@Path("{id}")
|
|
||||||
public Message disenrollDevice(@PathParam("id") String id) {
|
|
||||||
|
|
||||||
boolean result = false;
|
/*
|
||||||
String msg = "";
|
* Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD",
|
||||||
DeviceManagementService dmService;
|
* "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"},
|
||||||
Message responseMsg = new Message();
|
* {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"},
|
||||||
|
* {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"},
|
||||||
|
* {"name":"osVersion","value":"5.0.0"}]}
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
@PUT
|
||||||
|
@Path("{id}")
|
||||||
|
public Message modifyEnrollment(@PathParam("id") String id, org.wso2.carbon.device.mgt.common.Device device)
|
||||||
|
throws AndroidAgentException {
|
||||||
|
|
||||||
try {
|
boolean result;
|
||||||
dmService = AndroidAPIUtils.getDeviceManagementService();
|
DeviceManagementService dmService;
|
||||||
} finally {
|
Message responseMsg = new Message();
|
||||||
PrivilegedCarbonContext.endTenantFlow();
|
|
||||||
}
|
|
||||||
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
||||||
try {
|
|
||||||
if (dmService != null) {
|
|
||||||
result = dmService.disenrollDevice(deviceIdentifier);
|
|
||||||
if (result) {
|
|
||||||
responseMsg.setResponseMessage("Device has disenrolled successfully");
|
|
||||||
Response.status(HttpStatus.SC_OK);
|
|
||||||
} else {
|
|
||||||
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;
|
try {
|
||||||
} catch (DeviceManagementException e) {
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
||||||
msg = "Error occurred while disenrolling the device";
|
|
||||||
log.error(msg, e);
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
||||||
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
String errorMsg = "Device management service error";
|
||||||
responseMsg.setResponseMessage(msg);
|
log.error(errorMsg, deviceServiceMgtEx);
|
||||||
return responseMsg;
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
try {
|
||||||
|
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("device not found for enrollment");
|
||||||
|
Response.status(HttpStatus.SC_NOT_MODIFIED);
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseMsg;
|
||||||
|
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
String errorMsg = "Error occurred while modifying enrollment of the device";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
responseMsg.setResponseMessage(errorMsg);
|
||||||
|
return responseMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@DELETE
|
||||||
|
@Path("{id}")
|
||||||
|
public Message disenrollDevice(@PathParam("id") String id) throws AndroidAgentException {
|
||||||
|
|
||||||
|
DeviceManagementService dmService;
|
||||||
|
Message responseMsg = new Message();
|
||||||
|
|
||||||
|
boolean result;
|
||||||
|
|
||||||
|
try {
|
||||||
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
||||||
|
|
||||||
|
} catch (DeviceManagementServiceException deviceServiceMgtEx) {
|
||||||
|
String errorMsg = "Device management service error";
|
||||||
|
log.error(errorMsg, deviceServiceMgtEx);
|
||||||
|
throw new AndroidAgentException(errorMsg, deviceServiceMgtEx);
|
||||||
|
}
|
||||||
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = dmService.disenrollDevice(deviceIdentifier);
|
||||||
|
if (result) {
|
||||||
|
responseMsg.setResponseMessage("Device has disenrolled successfully");
|
||||||
|
Response.status(HttpStatus.SC_OK);
|
||||||
|
} else {
|
||||||
|
responseMsg.setResponseMessage("Device not found");
|
||||||
|
Response.status(HttpStatus.SC_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseMsg;
|
||||||
|
} 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(errorMsg);
|
||||||
|
return responseMsg;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,15 +18,19 @@
|
|||||||
|
|
||||||
package cdm.api.android.common;
|
package cdm.api.android.common;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.ext.ExceptionMapper;
|
import javax.ws.rs.ext.ExceptionMapper;
|
||||||
|
|
||||||
public class ErrorHandler implements ExceptionMapper {
|
|
||||||
|
|
||||||
public Response toResponse(Throwable throwable) {
|
@Produces({ "application/json", "application/xml" })
|
||||||
Response.Status status;
|
public class ErrorHandler implements ExceptionMapper<AndroidAgentException> {
|
||||||
status = Response.Status.INTERNAL_SERVER_ERROR;
|
|
||||||
// return Response.status(status).header("exception", exception.getMessage()).build();
|
public Response toResponse(AndroidAgentException exception) {
|
||||||
return null;
|
ErrorMessage errorMessage = new ErrorMessage();
|
||||||
|
errorMessage.setErrorMessage(exception.getErrorMessage());
|
||||||
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,18 +16,20 @@
|
|||||||
|
|
||||||
package cdm.api.android.util;
|
package cdm.api.android.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
import org.wso2.carbon.device.mgt.common.*;
|
import org.wso2.carbon.device.mgt.common.*;
|
||||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
|
||||||
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AndroidAPIUtil class provides utility function used by Android REST-API classes.
|
* AndroidAPIUtil class provides utility function used by Android REST-API classes.
|
||||||
*/
|
*/
|
||||||
public class AndroidAPIUtils {
|
public class AndroidAPIUtils {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(AndroidAPIUtils.class);
|
||||||
|
|
||||||
public static DeviceIdentifier convertToDeviceIdentifierObject(String deviceId) {
|
public static DeviceIdentifier convertToDeviceIdentifierObject(String deviceId) {
|
||||||
DeviceIdentifier identifier = new DeviceIdentifier();
|
DeviceIdentifier identifier = new DeviceIdentifier();
|
||||||
identifier.setId(deviceId);
|
identifier.setId(deviceId);
|
||||||
@ -36,14 +38,22 @@ public class AndroidAPIUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static DeviceManagementService getDeviceManagementService() {
|
public static DeviceManagementService getDeviceManagementService() throws DeviceManagementServiceException{
|
||||||
|
|
||||||
|
// until complete login this is use to load super tenant context
|
||||||
|
|
||||||
DeviceManagementService dmService;
|
DeviceManagementService dmService;
|
||||||
PrivilegedCarbonContext.startTenantFlow();
|
PrivilegedCarbonContext.startTenantFlow();
|
||||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||||
ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
|
ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
|
||||||
ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
|
ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
|
||||||
dmService = (DeviceManagementService) ctx
|
dmService = (DeviceManagementService) ctx.getOSGiService(DeviceManagementService.class, null);
|
||||||
.getOSGiService(DeviceManagementService.class, null);
|
|
||||||
|
if (dmService == null){
|
||||||
|
log.error("device management service not initialized");
|
||||||
|
throw new DeviceManagementServiceException("device management service not initialized");
|
||||||
|
}
|
||||||
|
PrivilegedCarbonContext.endTenantFlow();
|
||||||
return dmService;
|
return dmService;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,6 +29,7 @@
|
|||||||
</jaxrs:serviceBeans>
|
</jaxrs:serviceBeans>
|
||||||
<jaxrs:providers>
|
<jaxrs:providers>
|
||||||
<ref bean="jsonProvider"/>
|
<ref bean="jsonProvider"/>
|
||||||
|
<ref bean="errorHandler"/>
|
||||||
</jaxrs:providers>
|
</jaxrs:providers>
|
||||||
</jaxrs:server>
|
</jaxrs:server>
|
||||||
<jaxrs:server id="deviceManagementService" address="/devices">
|
<jaxrs:server id="deviceManagementService" address="/devices">
|
||||||
@ -37,6 +38,7 @@
|
|||||||
</jaxrs:serviceBeans>
|
</jaxrs:serviceBeans>
|
||||||
<jaxrs:providers>
|
<jaxrs:providers>
|
||||||
<ref bean="jsonProvider"/>
|
<ref bean="jsonProvider"/>
|
||||||
|
<ref bean="errorHandler"/>
|
||||||
</jaxrs:providers>
|
</jaxrs:providers>
|
||||||
</jaxrs:server>
|
</jaxrs:server>
|
||||||
<jaxrs:server id="enrollmentService" address="/enrollment">
|
<jaxrs:server id="enrollmentService" address="/enrollment">
|
||||||
@ -45,6 +47,17 @@
|
|||||||
</jaxrs:serviceBeans>
|
</jaxrs:serviceBeans>
|
||||||
<jaxrs:providers>
|
<jaxrs:providers>
|
||||||
<ref bean="jsonProvider"/>
|
<ref bean="jsonProvider"/>
|
||||||
|
<ref bean="errorHandler"/>
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
<jaxrs:server id="testService" address="/test">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<ref bean="testServiceBean"/>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<ref bean="jsonProvider"/>
|
||||||
|
<ref bean="errorHandler"/>
|
||||||
</jaxrs:providers>
|
</jaxrs:providers>
|
||||||
</jaxrs:server>
|
</jaxrs:server>
|
||||||
|
|
||||||
@ -52,5 +65,6 @@
|
|||||||
<bean id="deviceMgtServiceBean" class="cdm.api.android.Device"/>
|
<bean id="deviceMgtServiceBean" class="cdm.api.android.Device"/>
|
||||||
<bean id="enrollmentServiceBean" class="cdm.api.android.Enrollment"/>
|
<bean id="enrollmentServiceBean" class="cdm.api.android.Enrollment"/>
|
||||||
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
|
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
|
||||||
|
<bean id="errorHandler" class="cdm.api.android.common.ErrorHandler"/>
|
||||||
</beans>
|
</beans>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user