From e60e7b6d7f358fbb08564dce8af05413235089c0 Mon Sep 17 00:00:00 2001 From: harshanL Date: Thu, 18 Dec 2014 17:07:10 +0530 Subject: [PATCH] Fixed issues in Android JAX-RS app --- .../impl/dao/impl/MobileDeviceDAOImpl.java | 27 +- .../src/main/java/cdm/api/android/Device.java | 206 +++++----- .../main/java/cdm/api/android/Enrollment.java | 353 +++++++++--------- 3 files changed, 299 insertions(+), 287 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java index 42164e2c3..890c7c8da 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java @@ -48,19 +48,20 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { MobileDevice mobileDevice = null; try { conn = this.getConnection(); - String createDBQuery = + String selectDBQuery = "SELECT * FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; - stmt = conn.prepareStatement(createDBQuery); + stmt = conn.prepareStatement(selectDBQuery); stmt.setString(1, deviceId); ResultSet resultSet = stmt.executeQuery(); while (resultSet.next()) { - mobileDevice.setMobileDeviceId(resultSet.getString(0)); - mobileDevice.setRegId(resultSet.getString(1)); - mobileDevice.setImei(resultSet.getString(2)); - mobileDevice.setImsi(resultSet.getString(3)); - mobileDevice.setOsVersion(resultSet.getString(4)); - mobileDevice.setModel(resultSet.getString(5)); - mobileDevice.setVendor(resultSet.getString(6)); + mobileDevice = new MobileDevice(); + mobileDevice.setMobileDeviceId(resultSet.getString(1)); + mobileDevice.setRegId(resultSet.getString(2)); + mobileDevice.setImei(resultSet.getString(3)); + mobileDevice.setImsi(resultSet.getString(4)); + mobileDevice.setOsVersion(resultSet.getString(5)); + mobileDevice.setModel(resultSet.getString(6)); + mobileDevice.setVendor(resultSet.getString(7)); break; } } catch (SQLException e) { @@ -117,10 +118,10 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String createDBQuery = + String updateDBQuery = "UPDATE MBL_DEVICE SET REG_ID = ?, IMEI = ?, IMSI = ?, OS_VERSION = ?," + "DEVICE_MODEL = ?, VENDOR = ? WHERE MOBILE_DEVICE_ID = ?"; - stmt = conn.prepareStatement(createDBQuery); + stmt = conn.prepareStatement(updateDBQuery); stmt.setString(1, mobileDevice.getRegId()); stmt.setString(2, mobileDevice.getImei()); stmt.setString(3, mobileDevice.getImsi()); @@ -150,9 +151,9 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String createDBQuery = + String deleteDBQuery = "DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; - stmt = conn.prepareStatement(createDBQuery); + stmt = conn.prepareStatement(deleteDBQuery); stmt.setString(1,deviceId); int rows = stmt.executeUpdate(); if(rows>0){ diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java index 0b1cdf35e..cc75b3a89 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java @@ -30,7 +30,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import javax.ws.rs.*; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; /** @@ -40,106 +39,107 @@ import java.util.List; @Consumes({ "application/json", "application/xml" }) public class Device { - private static Log log = LogFactory.getLog(Device.class); - - @GET - public List getAllDevices() { - - List devices = null; - String msg = ""; - DeviceManagementService dmService; - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - 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 - @Path("{id}") - 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 { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); - try { - if (dmService != null) { - device = dmService.getDevice(deviceIdentifier); - if (device == null) { - Response.status(HttpStatus.SC_NOT_FOUND); - } - - } else { - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - - } catch (DeviceManagementException e) { - msg = "Error occurred while fetching the device information"; - log.error(msg, e); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - return device; - } - - @PUT - @Path("{id}") - public Message updateDevice(@PathParam("id") String id, org.wso2.carbon.device.mgt.common.Device device) { - - boolean result = false; - String msg = ""; - DeviceManagementService dmService; - Message responseMessage = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - try { - if (dmService != null) { - result = dmService.updateDeviceInfo(device); - if (result) { - Response.status(HttpStatus.SC_OK); - responseMessage.setResponseMessage("Device has modified"); - } 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) { - msg = "Error occurred while modifying the device information"; - log.error(msg, e); - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); - responseMessage.setResponseMessage(msg); - - } - - return responseMessage; - } + private static Log log = LogFactory.getLog(Device.class); + + @GET + public List getAllDevices() { + List devices = null; + String msg = ""; + DeviceManagementService dmService; + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + 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 + @Path("{id}") + 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 { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); + try { + if (dmService != null) { + device = dmService.getDevice(deviceIdentifier); + if (device == null) { + Response.status(HttpStatus.SC_NOT_FOUND); + } + + } else { + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + + } catch (DeviceManagementException e) { + msg = "Error occurred while fetching the device information."; + log.error(msg, e); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + return device; + } + + @PUT + @Path("{id}") + public Message updateDevice(@PathParam("id") String id, + org.wso2.carbon.device.mgt.common.Device device) { + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMessage = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } 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) { + msg = "Error occurred while modifying the device information."; + log.error(msg, e); + Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR); + responseMessage.setResponseMessage(msg); + + } + return responseMessage; + } } diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java index fca33567e..f2eb449d7 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java @@ -39,175 +39,186 @@ import javax.ws.rs.core.Response; @Consumes({ "application/json", "application/xml" }) public class Enrollment { - private static Log log = LogFactory.getLog(Enrollment.class); - - /* - * Request Format : {"deviceIdentifier":"macid","description":"ww","ownership":"ww", - * "properties":[{"name":"username","value":"ww"},{"name":"device","value":"ww"}, - * {"name":"imei","value":"imei"},{"name":"imsi","value":"imsi"},{"name":"model","value":"mi3"}, - * {"name":"regId","value":"regid"},{"name":"vendor","value":"vendor"}, - * {"name":"osVersion","value":"Lolipop"}]} - * - **/ - @POST - public Message enrollDevice(Device device) { - - boolean result = false; - int status = 0; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - - try { - if (dmService != null) { - device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); - result = dmService.enrollDevice(device); - Response.status(HttpStatus.SC_CREATED); - responseMsg.setResponseMessage("Device enrollment has 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; - } - - } - - @GET - @Path("{id}") - public Message isEnrolled(@PathParam("id") String id) { - - boolean result = false; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - 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 already enroll"); - } else { - - Response.status(HttpStatus.SC_NOT_FOUND); - responseMsg.setResponseMessage("Device not enroll"); - } - 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 enrollment of the device"; - log.error(msg, e); - 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; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - - try { - if (dmService != null) { - result = dmService.modifyEnrollment(device); - - if (result) { - responseMsg.setResponseMessage("update device"); - 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; - } - - } - - @DELETE - @Path("{id}") - public Message disenrollDevice(@PathParam("id") String id) { - - boolean result = false; - String msg = ""; - DeviceManagementService dmService; - Message responseMsg = new Message(); - - - try { - dmService = AndroidAPIUtils.getDeviceManagementService(); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); - try { - if (dmService != null) { - result = dmService.disenrollDevice(deviceIdentifier); - if (result) { - responseMsg.setResponseMessage("Dis enrolled device"); - 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; - } 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; - } - } + private static Log log = LogFactory.getLog(Enrollment.class); + + /* + * 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"}]} + * + **/ + @POST + public Message enrollDevice(Device device) { + + boolean result = false; + int status = 0; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + + try { + if (dmService != null) { + device.setType( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + result = dmService.enrollDevice(device); + Response.status(HttpStatus.SC_CREATED); + responseMsg.setResponseMessage("Device enrollment has 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; + } + + } + + @GET + @Path("{id}") + public Message isEnrolled(@PathParam("id") String id) { + + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + 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; + } + + } + + /* + * 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 + @Path("{id}") + public Message modifyEnrollment(@PathParam("id") String id, Device device) { + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + + try { + if (dmService != null) { + 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"); + 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; + } + + } + + @DELETE + @Path("{id}") + public Message disenrollDevice(@PathParam("id") String id) { + + boolean result = false; + String msg = ""; + DeviceManagementService dmService; + Message responseMsg = new Message(); + + try { + dmService = AndroidAPIUtils.getDeviceManagementService(); + } finally { + 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; + } 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; + } + } }