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;
/** /**
@ -44,7 +43,6 @@ public class Device {
@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;
@ -56,13 +54,14 @@ public class Device {
} }
try { try {
if (dmService != null) { if (dmService != null) {
devices = dmService.getAllDevices(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); devices = dmService.getAllDevices(
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);
} }
@ -72,10 +71,10 @@ public class Device {
@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 = new org.wso2.carbon.device.mgt.common.Device(); org.wso2.carbon.device.mgt.common.Device device =
new org.wso2.carbon.device.mgt.common.Device();
try { try {
dmService = AndroidAPIUtils.getDeviceManagementService(); dmService = AndroidAPIUtils.getDeviceManagementService();
@ -95,7 +94,7 @@ public class Device {
} }
} 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);
} }
@ -104,8 +103,8 @@ public class 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;
@ -118,13 +117,15 @@ public class Device {
} }
try { try {
if (dmService != null) { if (dmService != null) {
device.setType(
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
result = dmService.updateDeviceInfo(device); result = dmService.updateDeviceInfo(device);
if (result) { if (result) {
Response.status(HttpStatus.SC_OK); Response.status(HttpStatus.SC_OK);
responseMessage.setResponseMessage("Device has modified"); responseMessage.setResponseMessage("Device information has modified successfully.");
} else { } else {
Response.status(HttpStatus.SC_NOT_MODIFIED); Response.status(HttpStatus.SC_NOT_MODIFIED);
responseMessage.setResponseMessage("Update device has failed"); responseMessage.setResponseMessage("Update device has failed.");
} }
} else { } else {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
@ -133,13 +134,12 @@ public class Device {
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
msg = "Error occurred while modifying the device information"; msg = "Error occurred while modifying 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);
responseMessage.setResponseMessage(msg); responseMessage.setResponseMessage(msg);
} }
return responseMessage; return responseMessage;
} }
} }

@ -42,11 +42,11 @@ 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
@ -66,14 +66,16 @@ public class Enrollment {
try { try {
if (dmService != null) { if (dmService != null) {
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); device.setType(
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
result = dmService.enrollDevice(device); result = dmService.enrollDevice(device);
Response.status(HttpStatus.SC_CREATED); Response.status(HttpStatus.SC_CREATED);
responseMsg.setResponseMessage("Device enrollment has succeeded"); responseMsg.setResponseMessage("Device enrollment has succeeded");
return responseMsg; return responseMsg;
} else { } else {
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); responseMsg.setResponseMessage(
AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return responseMsg; return responseMsg;
} }
@ -106,20 +108,20 @@ public class Enrollment {
result = dmService.isEnrolled(deviceIdentifier); result = dmService.isEnrolled(deviceIdentifier);
if (result) { if (result) {
Response.status(HttpStatus.SC_OK); Response.status(HttpStatus.SC_OK);
responseMsg.setResponseMessage("Device already enroll"); responseMsg.setResponseMessage("Device has already enrolled");
} else { } else {
Response.status(HttpStatus.SC_NOT_FOUND); Response.status(HttpStatus.SC_NOT_FOUND);
responseMsg.setResponseMessage("Device not enroll"); responseMsg.setResponseMessage("Device has not enrolled");
} }
return responseMsg; return responseMsg;
} else { } else {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE); responseMsg.setResponseMessage(
AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
return responseMsg; return responseMsg;
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
msg = "Error occurred while checking enrollment of the device"; msg = "Error occurred while checking the enrollment of the device.";
log.error(msg, e); log.error(msg, e);
responseMsg.setResponseMessage(msg); responseMsg.setResponseMessage(msg);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
@ -128,6 +130,14 @@ public class Enrollment {
} }
/*
* Request Format : {"deviceIdentifier":"macid","description":"description","ownership":"BYOD",
* "properties":[{"name":"username","value":"harshan"},{"name":"device","value":"Harshan S5"},
* {"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 @PUT
@Path("{id}") @Path("{id}")
public Message modifyEnrollment(@PathParam("id") String id, Device device) { public Message modifyEnrollment(@PathParam("id") String id, Device device) {
@ -144,10 +154,12 @@ public class Enrollment {
try { try {
if (dmService != null) { if (dmService != null) {
device.setType(
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
result = dmService.modifyEnrollment(device); result = dmService.modifyEnrollment(device);
if (result) { if (result) {
responseMsg.setResponseMessage("update device"); responseMsg.setResponseMessage("Device enrollment has updated successfully");
Response.status(HttpStatus.SC_OK); Response.status(HttpStatus.SC_OK);
} else { } else {
responseMsg.setResponseMessage("Update enrollment has failed"); responseMsg.setResponseMessage("Update enrollment has failed");
@ -178,7 +190,6 @@ public class Enrollment {
DeviceManagementService dmService; DeviceManagementService dmService;
Message responseMsg = new Message(); Message responseMsg = new Message();
try { try {
dmService = AndroidAPIUtils.getDeviceManagementService(); dmService = AndroidAPIUtils.getDeviceManagementService();
} finally { } finally {
@ -189,7 +200,7 @@ public class Enrollment {
if (dmService != null) { if (dmService != null) {
result = dmService.disenrollDevice(deviceIdentifier); result = dmService.disenrollDevice(deviceIdentifier);
if (result) { if (result) {
responseMsg.setResponseMessage("Dis enrolled device"); responseMsg.setResponseMessage("Device has disenrolled successfully");
Response.status(HttpStatus.SC_OK); Response.status(HttpStatus.SC_OK);
} else { } else {
responseMsg.setResponseMessage("Device not found"); responseMsg.setResponseMessage("Device not found");

Loading…
Cancel
Save