|
|
@ -18,9 +18,10 @@ package cdm.api.android;
|
|
|
|
|
|
|
|
|
|
|
|
import cdm.api.android.util.AndroidAPIUtils;
|
|
|
|
import cdm.api.android.util.AndroidAPIUtils;
|
|
|
|
import cdm.api.android.util.AndroidConstants;
|
|
|
|
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.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
|
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
@ -33,158 +34,170 @@ import javax.ws.rs.core.Response;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Android Device Enrollment REST-API implementation.
|
|
|
|
* Android Device Enrollment REST-API implementation.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Produces({ "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);
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
public Response enrollDevice(String jsonPayload) {
|
|
|
|
public Message enrollDevice(String jsonPayload) {
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
int status = 0;
|
|
|
|
boolean result = false;
|
|
|
|
String msg = "";
|
|
|
|
int status = 0;
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
String msg = "";
|
|
|
|
try {
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
if (dmService != null) {
|
|
|
|
}
|
|
|
|
result = dmService.enrollDevice(device);
|
|
|
|
Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload);
|
|
|
|
status = 1;
|
|
|
|
try {
|
|
|
|
} else {
|
|
|
|
if (dmService != null) {
|
|
|
|
status = -1;
|
|
|
|
result = dmService.enrollDevice(device);
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
Response.status(HttpStatus.SC_CREATED);
|
|
|
|
}
|
|
|
|
responseMsg.setResponseMessage("Device enrollment has succeeded");
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
return responseMsg;
|
|
|
|
msg = "Error occurred while enrolling the device";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
} else {
|
|
|
|
status = -1;
|
|
|
|
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
switch (status) {
|
|
|
|
return responseMsg;
|
|
|
|
case 1:
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
return Response.status(HttpStatus.SC_CREATED).entity("Device enrollment has succeeded").build();
|
|
|
|
log.error(msg, e);
|
|
|
|
}
|
|
|
|
responseMsg.setResponseMessage("Error occurred while enrolling the device");
|
|
|
|
break;
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
case -1:
|
|
|
|
return responseMsg;
|
|
|
|
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.status(HttpStatus.SC_BAD_REQUEST).entity("Device enrollment has Failed").build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Path("{id}")
|
|
|
|
@Path("{id}")
|
|
|
|
public Message isEnrolled(@PathParam("id") String id) {
|
|
|
|
public Response isEnrolled(@PathParam("id") String id) {
|
|
|
|
|
|
|
|
boolean result = false;
|
|
|
|
boolean result = false;
|
|
|
|
int status = 0;
|
|
|
|
String msg = "";
|
|
|
|
String msg = "";
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
try {
|
|
|
|
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
try {
|
|
|
|
} finally {
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
|
|
if (dmService != null) {
|
|
|
|
try {
|
|
|
|
result = dmService.isEnrolled(deviceIdentifier);
|
|
|
|
if (dmService != null) {
|
|
|
|
status = 1;
|
|
|
|
result = dmService.isEnrolled(deviceIdentifier);
|
|
|
|
} else {
|
|
|
|
if (result) {
|
|
|
|
status = -1;
|
|
|
|
Response.status(HttpStatus.SC_OK);
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
responseMsg.setResponseMessage("Device already enroll");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
msg = "Error occurred while checking enrollment of the device";
|
|
|
|
Response.status(HttpStatus.SC_NOT_FOUND);
|
|
|
|
log.error(msg, e);
|
|
|
|
responseMsg.setResponseMessage("Device not enroll");
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return responseMsg;
|
|
|
|
switch (status) {
|
|
|
|
} else {
|
|
|
|
case 1:
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
if (result) {
|
|
|
|
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
|
|
|
|
return Response.status(HttpStatus.SC_OK).entity(result).build();
|
|
|
|
return responseMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
case -1:
|
|
|
|
msg = "Error occurred while checking enrollment of the device";
|
|
|
|
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
log.error(msg, e);
|
|
|
|
}
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
return Response.status(HttpStatus.SC_NOT_FOUND).entity(result).build();
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
return responseMsg;
|
|
|
|
|
|
|
|
}
|
|
|
|
@PUT
|
|
|
|
|
|
|
|
@Path("{id}")
|
|
|
|
}
|
|
|
|
public Response modifyEnrollment(@PathParam("id") String id, String jsonPayload) {
|
|
|
|
|
|
|
|
boolean result = false;
|
|
|
|
@PUT
|
|
|
|
int status = 0;
|
|
|
|
@Path("{id}")
|
|
|
|
String msg = "";
|
|
|
|
public Message modifyEnrollment(@PathParam("id") String id, String jsonPayload) {
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
boolean result = false;
|
|
|
|
try {
|
|
|
|
String msg = "";
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
} finally {
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload);
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
try {
|
|
|
|
} finally {
|
|
|
|
if (dmService != null) {
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
result = dmService.modifyEnrollment(device);
|
|
|
|
}
|
|
|
|
status = 1;
|
|
|
|
Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
status = -1;
|
|
|
|
if (dmService != null) {
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
result = dmService.modifyEnrollment(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
if (result) {
|
|
|
|
msg = "Error occurred while modifying enrollment of the device";
|
|
|
|
responseMsg.setResponseMessage("update device");
|
|
|
|
log.error(msg, e);
|
|
|
|
Response.status(HttpStatus.SC_OK);
|
|
|
|
status = -1;
|
|
|
|
}else{
|
|
|
|
}
|
|
|
|
responseMsg.setResponseMessage("Update enrollment has failed");
|
|
|
|
switch (status) {
|
|
|
|
Response.status(HttpStatus.SC_NOT_MODIFIED);
|
|
|
|
case 1:
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
} else {
|
|
|
|
return Response.status(HttpStatus.SC_OK).entity("Enrollment information has modified").build();
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
break;
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
case -1:
|
|
|
|
}
|
|
|
|
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
return responseMsg;
|
|
|
|
}
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
return Response.status(HttpStatus.SC_NOT_MODIFIED).entity("Update enrollment has failed").build();
|
|
|
|
msg = "Error occurred while modifying enrollment of the device";
|
|
|
|
}
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
@DELETE
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
@Path("{id}")
|
|
|
|
return responseMsg;
|
|
|
|
public Response disenrollDevice(@PathParam("id") String id) {
|
|
|
|
}
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
int status = 0;
|
|
|
|
}
|
|
|
|
String msg = "";
|
|
|
|
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
@DELETE
|
|
|
|
try {
|
|
|
|
@Path("{id}")
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
public Message disenrollDevice(@PathParam("id") String id) {
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
boolean result = false;
|
|
|
|
}
|
|
|
|
String msg = "";
|
|
|
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
|
|
DeviceManagementService dmService;
|
|
|
|
try {
|
|
|
|
Message responseMsg = new Message();
|
|
|
|
if (dmService != null) {
|
|
|
|
|
|
|
|
result = dmService.disenrollDevice(deviceIdentifier);
|
|
|
|
|
|
|
|
status = 1;
|
|
|
|
try {
|
|
|
|
} else {
|
|
|
|
dmService = AndroidAPIUtils.getDeviceManagementService();
|
|
|
|
status = -1;
|
|
|
|
} finally {
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
|
|
|
|
msg = "Error occurred while disenrolling the device";
|
|
|
|
try {
|
|
|
|
log.error(msg, e);
|
|
|
|
if (dmService != null) {
|
|
|
|
status = -1;
|
|
|
|
result = dmService.disenrollDevice(deviceIdentifier);
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
switch (status) {
|
|
|
|
responseMsg.setResponseMessage("Dis enrolled device");
|
|
|
|
case 1:
|
|
|
|
Response.status(HttpStatus.SC_OK);
|
|
|
|
if (result) {
|
|
|
|
}else{
|
|
|
|
return Response.status(HttpStatus.SC_OK).entity(result).build();
|
|
|
|
responseMsg.setResponseMessage("Device not found");
|
|
|
|
}
|
|
|
|
Response.status(HttpStatus.SC_NOT_FOUND);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case -1:
|
|
|
|
} else {
|
|
|
|
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
|
|
|
|
return Response.status(HttpStatus.SC_NOT_FOUND).entity("Device not found").build();
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return responseMsg;
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
msg = "Error occurred while disenrolling the device";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
responseMsg.setResponseMessage(msg);
|
|
|
|
|
|
|
|
return responseMsg;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|