Fixed issues in Android JAX-RS app

4.x.x
harshanL 10 years ago
parent 0ad9d1bd16
commit e60e7b6d7f

@ -48,19 +48,20 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
MobileDevice mobileDevice = null; MobileDevice mobileDevice = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String createDBQuery = String selectDBQuery =
"SELECT * FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; "SELECT * FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(createDBQuery); stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, deviceId); stmt.setString(1, deviceId);
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
mobileDevice.setMobileDeviceId(resultSet.getString(0)); mobileDevice = new MobileDevice();
mobileDevice.setRegId(resultSet.getString(1)); mobileDevice.setMobileDeviceId(resultSet.getString(1));
mobileDevice.setImei(resultSet.getString(2)); mobileDevice.setRegId(resultSet.getString(2));
mobileDevice.setImsi(resultSet.getString(3)); mobileDevice.setImei(resultSet.getString(3));
mobileDevice.setOsVersion(resultSet.getString(4)); mobileDevice.setImsi(resultSet.getString(4));
mobileDevice.setModel(resultSet.getString(5)); mobileDevice.setOsVersion(resultSet.getString(5));
mobileDevice.setVendor(resultSet.getString(6)); mobileDevice.setModel(resultSet.getString(6));
mobileDevice.setVendor(resultSet.getString(7));
break; break;
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -117,10 +118,10 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String createDBQuery = String updateDBQuery =
"UPDATE MBL_DEVICE SET REG_ID = ?, IMEI = ?, IMSI = ?, OS_VERSION = ?," + "UPDATE MBL_DEVICE SET REG_ID = ?, IMEI = ?, IMSI = ?, OS_VERSION = ?," +
"DEVICE_MODEL = ?, VENDOR = ? WHERE MOBILE_DEVICE_ID = ?"; "DEVICE_MODEL = ?, VENDOR = ? WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(createDBQuery); stmt = conn.prepareStatement(updateDBQuery);
stmt.setString(1, mobileDevice.getRegId()); stmt.setString(1, mobileDevice.getRegId());
stmt.setString(2, mobileDevice.getImei()); stmt.setString(2, mobileDevice.getImei());
stmt.setString(3, mobileDevice.getImsi()); stmt.setString(3, mobileDevice.getImsi());
@ -150,9 +151,9 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String createDBQuery = String deleteDBQuery =
"DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; "DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(createDBQuery); stmt = conn.prepareStatement(deleteDBQuery);
stmt.setString(1,deviceId); stmt.setString(1,deviceId);
int rows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
if(rows>0){ if(rows>0){

@ -30,7 +30,6 @@ 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.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -40,106 +39,107 @@ 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() {
List<org.wso2.carbon.device.mgt.common.Device> devices = null;
List<org.wso2.carbon.device.mgt.common.Device> devices = null; String msg = "";
String msg = ""; DeviceManagementService dmService;
DeviceManagementService dmService;
try {
try { dmService = AndroidAPIUtils.getDeviceManagementService();
dmService = AndroidAPIUtils.getDeviceManagementService(); } finally {
} finally { PrivilegedCarbonContext.endTenantFlow();
PrivilegedCarbonContext.endTenantFlow(); }
} try {
try { if (dmService != null) {
if (dmService != null) { devices = dmService.getAllDevices(
devices = dmService.getAllDevices(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
Response.status(HttpStatus.SC_OK); Response.status(HttpStatus.SC_OK);
} else { } else {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
msg = "Error occurred while fetching the device list"; msg = "Error occurred while fetching the device list.";
log.error(msg, e); log.error(msg, e);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} }
return devices; return devices;
} }
@GET @GET
@Path("{id}") @Path("{id}")
public org.wso2.carbon.device.mgt.common.Device getDevice(@PathParam("id") String id) { public org.wso2.carbon.device.mgt.common.Device getDevice(@PathParam("id") String id) {
String msg = "";
String msg = ""; DeviceManagementService dmService;
DeviceManagementService dmService; org.wso2.carbon.device.mgt.common.Device device =
org.wso2.carbon.device.mgt.common.Device device = new org.wso2.carbon.device.mgt.common.Device(); new org.wso2.carbon.device.mgt.common.Device();
try { try {
dmService = AndroidAPIUtils.getDeviceManagementService(); dmService = AndroidAPIUtils.getDeviceManagementService();
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
try { try {
if (dmService != null) { if (dmService != null) {
device = dmService.getDevice(deviceIdentifier); device = dmService.getDevice(deviceIdentifier);
if (device == null) { if (device == null) {
Response.status(HttpStatus.SC_NOT_FOUND); Response.status(HttpStatus.SC_NOT_FOUND);
} }
} else { } else {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
msg = "Error occurred while fetching the device information"; msg = "Error occurred while fetching the device information.";
log.error(msg, e); log.error(msg, e);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} }
return device; return device;
} }
@PUT @PUT
@Path("{id}") @Path("{id}")
public Message updateDevice(@PathParam("id") String id, org.wso2.carbon.device.mgt.common.Device device) { public Message updateDevice(@PathParam("id") String id,
org.wso2.carbon.device.mgt.common.Device device) {
boolean result = false; boolean result = false;
String msg = ""; String msg = "";
DeviceManagementService dmService; DeviceManagementService dmService;
Message responseMessage = new Message(); Message responseMessage = new Message();
try { try {
dmService = AndroidAPIUtils.getDeviceManagementService(); dmService = AndroidAPIUtils.getDeviceManagementService();
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
try { try {
if (dmService != null) { if (dmService != null) {
result = dmService.updateDeviceInfo(device); device.setType(
if (result) { DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
Response.status(HttpStatus.SC_OK); result = dmService.updateDeviceInfo(device);
responseMessage.setResponseMessage("Device has modified"); if (result) {
} else { Response.status(HttpStatus.SC_OK);
Response.status(HttpStatus.SC_NOT_MODIFIED); responseMessage.setResponseMessage("Device information has modified successfully.");
responseMessage.setResponseMessage("Update device has failed"); } else {
} Response.status(HttpStatus.SC_NOT_MODIFIED);
} else { responseMessage.setResponseMessage("Update device has failed.");
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); }
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } else {
responseMessage.setResponseMessage(msg); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
responseMessage.setResponseMessage(msg);
} catch (DeviceManagementException e) { }
msg = "Error occurred while modifying the device information";
log.error(msg, e); } catch (DeviceManagementException e) {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); msg = "Error occurred while modifying the device information.";
responseMessage.setResponseMessage(msg); log.error(msg, e);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} responseMessage.setResponseMessage(msg);
return responseMessage; }
} return responseMessage;
}
} }

@ -39,175 +39,186 @@ 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":"ww","ownership":"ww", * Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD",
* "properties":[{"name":"username","value":"ww"},{"name":"device","value":"ww"}, * "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"},
* {"name":"imei","value":"imei"},{"name":"imsi","value":"imsi"},{"name":"model","value":"mi3"}, * {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"},
* {"name":"regId","value":"regid"},{"name":"vendor","value":"vendor"}, * {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"},
* {"name":"osVersion","value":"Lolipop"}]} * {"name":"osVersion","value":"5.0.0"}]}
* *
**/ **/
@POST @POST
public Message enrollDevice(Device device) { public Message enrollDevice(Device device) {
boolean result = false; boolean result = false;
int status = 0; int status = 0;
String msg = ""; String msg = "";
DeviceManagementService dmService; DeviceManagementService dmService;
Message responseMsg = new Message(); Message responseMsg = new Message();
try { try {
dmService = AndroidAPIUtils.getDeviceManagementService(); dmService = AndroidAPIUtils.getDeviceManagementService();
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
try { try {
if (dmService != null) { if (dmService != null) {
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); device.setType(
result = dmService.enrollDevice(device); DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
Response.status(HttpStatus.SC_CREATED); result = dmService.enrollDevice(device);
responseMsg.setResponseMessage("Device enrollment has succeeded"); Response.status(HttpStatus.SC_CREATED);
return responseMsg; responseMsg.setResponseMessage("Device enrollment has succeeded");
return responseMsg;
} else {
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); } else {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); responseMsg.setResponseMessage(
return responseMsg; AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
} Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
} catch (DeviceManagementException e) { return responseMsg;
log.error(msg, e); }
responseMsg.setResponseMessage("Error occurred while enrolling the device"); } catch (DeviceManagementException e) {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); log.error(msg, e);
return responseMsg; responseMsg.setResponseMessage("Error occurred while enrolling the device");
} Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return responseMsg;
} }
@GET }
@Path("{id}")
public Message isEnrolled(@PathParam("id") String id) { @GET
@Path("{id}")
boolean result = false; public Message isEnrolled(@PathParam("id") String id) {
String msg = "";
DeviceManagementService dmService; boolean result = false;
Message responseMsg = new Message(); String msg = "";
DeviceManagementService dmService;
try { Message responseMsg = new Message();
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally { try {
PrivilegedCarbonContext.endTenantFlow(); dmService = AndroidAPIUtils.getDeviceManagementService();
} } finally {
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); PrivilegedCarbonContext.endTenantFlow();
try { }
if (dmService != null) { DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
result = dmService.isEnrolled(deviceIdentifier); try {
if (result) { if (dmService != null) {
Response.status(HttpStatus.SC_OK); result = dmService.isEnrolled(deviceIdentifier);
responseMsg.setResponseMessage("Device already enroll"); if (result) {
} else { Response.status(HttpStatus.SC_OK);
responseMsg.setResponseMessage("Device has already enrolled");
Response.status(HttpStatus.SC_NOT_FOUND); } else {
responseMsg.setResponseMessage("Device not enroll"); Response.status(HttpStatus.SC_NOT_FOUND);
} responseMsg.setResponseMessage("Device has not enrolled");
return responseMsg; }
} else { return responseMsg;
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); } else {
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return responseMsg; responseMsg.setResponseMessage(
} AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
} catch (DeviceManagementException e) { return responseMsg;
msg = "Error occurred while checking enrollment of the device"; }
log.error(msg, e); } catch (DeviceManagementException e) {
responseMsg.setResponseMessage(msg); msg = "Error occurred while checking the enrollment of the device.";
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); log.error(msg, e);
return responseMsg; responseMsg.setResponseMessage(msg);
} Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return responseMsg;
} }
@PUT }
@Path("{id}")
public Message modifyEnrollment(@PathParam("id") String id, Device device) { /*
boolean result = false; * Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD",
String msg = ""; * "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"},
DeviceManagementService dmService; * {"name":"imei","value":"356938035643809"},{"name":"imsi","value":"404685505601234"},{"name":"model","value":"Galaxy S5"},
Message responseMsg = new Message(); * {"name":"regId","value":"02fab24b2242"},{"name":"vendor","value":"Samsung"},
* {"name":"osVersion","value":"5.0.0"}]}
try { *
dmService = AndroidAPIUtils.getDeviceManagementService(); **/
} finally { @PUT
PrivilegedCarbonContext.endTenantFlow(); @Path("{id}")
} public Message modifyEnrollment(@PathParam("id") String id, Device device) {
boolean result = false;
try { String msg = "";
if (dmService != null) { DeviceManagementService dmService;
result = dmService.modifyEnrollment(device); Message responseMsg = new Message();
if (result) { try {
responseMsg.setResponseMessage("update device"); dmService = AndroidAPIUtils.getDeviceManagementService();
Response.status(HttpStatus.SC_OK); } finally {
}else{ PrivilegedCarbonContext.endTenantFlow();
responseMsg.setResponseMessage("Update enrollment has failed"); }
Response.status(HttpStatus.SC_NOT_MODIFIED);
} try {
} else { if (dmService != null) {
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; device.setType(
responseMsg.setResponseMessage(msg); DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); result = dmService.modifyEnrollment(device);
}
return responseMsg; if (result) {
} catch (DeviceManagementException e) { responseMsg.setResponseMessage("Device enrollment has updated successfully");
msg = "Error occurred while modifying enrollment of the device"; Response.status(HttpStatus.SC_OK);
log.error(msg, e); } else {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); responseMsg.setResponseMessage("Update enrollment has failed");
responseMsg.setResponseMessage(msg); Response.status(HttpStatus.SC_NOT_MODIFIED);
return responseMsg; }
} } else {
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
} responseMsg.setResponseMessage(msg);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
@DELETE }
@Path("{id}") return responseMsg;
public Message disenrollDevice(@PathParam("id") String id) { } catch (DeviceManagementException e) {
msg = "Error occurred while modifying enrollment of the device";
boolean result = false; log.error(msg, e);
String msg = ""; Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
DeviceManagementService dmService; responseMsg.setResponseMessage(msg);
Message responseMsg = new Message(); return responseMsg;
}
try { }
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally { @DELETE
PrivilegedCarbonContext.endTenantFlow(); @Path("{id}")
} public Message disenrollDevice(@PathParam("id") String id) {
DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
try { boolean result = false;
if (dmService != null) { String msg = "";
result = dmService.disenrollDevice(deviceIdentifier); DeviceManagementService dmService;
if (result) { Message responseMsg = new Message();
responseMsg.setResponseMessage("Dis enrolled device");
Response.status(HttpStatus.SC_OK); try {
}else{ dmService = AndroidAPIUtils.getDeviceManagementService();
responseMsg.setResponseMessage("Device not found"); } finally {
Response.status(HttpStatus.SC_NOT_FOUND); PrivilegedCarbonContext.endTenantFlow();
} }
} else { DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); try {
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; if (dmService != null) {
responseMsg.setResponseMessage(msg); result = dmService.disenrollDevice(deviceIdentifier);
} if (result) {
responseMsg.setResponseMessage("Device has disenrolled successfully");
return responseMsg; Response.status(HttpStatus.SC_OK);
} catch (DeviceManagementException e) { } else {
msg = "Error occurred while disenrolling the device"; responseMsg.setResponseMessage("Device not found");
log.error(msg, e); Response.status(HttpStatus.SC_NOT_FOUND);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); }
responseMsg.setResponseMessage(msg); } else {
return responseMsg; 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);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
responseMsg.setResponseMessage(msg);
return responseMsg;
}
}
} }

Loading…
Cancel
Save