From 18b8f0c41d6fcd4b057984464d616f7cd16b14b0 Mon Sep 17 00:00:00 2001 From: Menaka Madushanka Date: Tue, 5 Jan 2016 20:06:07 +0530 Subject: [PATCH] Android sense separated into mgt and controller --- .../pom.xml | 206 ++++++++++++++++ .../service/impl/AndroidSenseService.java | 170 +------------ .../service/impl/util/DeviceJSON.java | 2 +- .../service/impl/util/SensorJSON.java | 2 +- .../src/main/webapp/META-INF/resources.xml | 0 .../webapp/META-INF/webapp-classloading.xml | 0 .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 +- .../src/main/webapp/WEB-INF/web.xml | 0 .../pom.xml | 11 +- .../mgt/service/impl/AndroidSenseService.java | 223 ++++++++++++++++++ .../src/main/webapp/META-INF/resources.xml | 123 ++++++++++ .../webapp/META-INF/webapp-classloading.xml | 33 +++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 41 ++++ .../src/main/webapp/WEB-INF/web.xml | 73 ++++++ .../device-mgt-iot-androidsense/pom.xml | 3 +- 15 files changed, 715 insertions(+), 174 deletions(-) create mode 100644 components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/pom.xml rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense => org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller}/service/impl/AndroidSenseService.java (72%) rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense => org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller}/service/impl/util/DeviceJSON.java (82%) rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense => org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller}/service/impl/util/SensorJSON.java (81%) rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api => org.wso2.carbon.device.mgt.iot.androidsense.controller.api}/src/main/webapp/META-INF/resources.xml (100%) rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api => org.wso2.carbon.device.mgt.iot.androidsense.controller.api}/src/main/webapp/META-INF/webapp-classloading.xml (100%) rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api => org.wso2.carbon.device.mgt.iot.androidsense.controller.api}/src/main/webapp/WEB-INF/cxf-servlet.xml (96%) rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api => org.wso2.carbon.device.mgt.iot.androidsense.controller.api}/src/main/webapp/WEB-INF/web.xml (100%) rename components/device-mgt-iot-androidsense/{org.wso2.carbon.device.mgt.iot.androidsense.api => org.wso2.carbon.device.mgt.iot.androidsense.mgt.api}/pom.xml (94%) create mode 100644 components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/mgt/service/impl/AndroidSenseService.java create mode 100644 components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/resources.xml create mode 100644 components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml create mode 100644 components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml create mode 100644 components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/web.xml diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/pom.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/pom.xml new file mode 100644 index 0000000000..ca698674a7 --- /dev/null +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/pom.xml @@ -0,0 +1,206 @@ + + + + device-mgt-iot-androidsense + org.wso2.carbon.devicemgt-plugins + 1.9.2-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.device.mgt.iot.androidsense.controller.api + 1.9.2-SNAPSHOT + war + WSO2 Carbon - Android Sense Controller API + WSO2 Carbon - Android Sense Service Controller API Implementation + http://wso2.org + + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + provided + + + org.apache.axis2.wso2 + axis2-client + + + + + + + org.apache.cxf + cxf-rt-frontend-jaxws + provided + + + org.apache.cxf + cxf-rt-frontend-jaxrs + provided + + + org.apache.cxf + cxf-rt-transports-http + provided + + + + org.eclipse.paho + mqtt-client + + + + + org.apache.httpcomponents + httpasyncclient + 4.1 + provided + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot + provided + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.androidsense + provided + + + + org.codehaus.jackson + jackson-core-asl + + + org.codehaus.jackson + jackson-jaxrs + + + javax + javaee-web-api + provided + + + javax.ws.rs + jsr311-api + provided + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.analytics + provided + + + + commons-httpclient.wso2 + commons-httpclient + provided + + + + org.wso2.carbon + org.wso2.carbon.utils + provided + + + org.bouncycastle.wso2 + bcprov-jdk15on + + + org.wso2.carbon + org.wso2.carbon.user.api + + + org.wso2.carbon + org.wso2.carbon.queuing + + + org.wso2.carbon + org.wso2.carbon.base + + + org.apache.axis2.wso2 + axis2 + + + org.igniterealtime.smack.wso2 + smack + + + org.igniterealtime.smack.wso2 + smackx + + + jaxen + jaxen + + + commons-fileupload.wso2 + commons-fileupload + + + org.apache.ant.wso2 + ant + + + org.apache.ant.wso2 + ant + + + commons-httpclient.wso2 + commons-httpclient + + + org.eclipse.equinox + javax.servlet + + + org.wso2.carbon + org.wso2.carbon.registry.api + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.annotations + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.webapp.publisher + provided + + + + + + + + maven-compiler-plugin + + UTF-8 + ${wso2.maven.compiler.source} + ${wso2.maven.compiler.target} + + + + maven-war-plugin + + android_sense + + + + + + diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseService.java b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/AndroidSenseService.java similarity index 72% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseService.java rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/AndroidSenseService.java index 92e14a1012..0bb7c4dc63 100644 --- a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseService.java +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/AndroidSenseService.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.wso2.carbon.device.mgt.iot.androidsense.service.impl; +package org.wso2.carbon.device.mgt.iot.androidsense.controller.service.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -23,18 +23,12 @@ import org.wso2.carbon.apimgt.annotations.device.DeviceType; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException; import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.iot.DeviceManagement; +import org.wso2.carbon.device.mgt.iot.androidsense.controller.service.impl.util.SensorJSON; import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants; -import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.DeviceJSON; -import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.SensorJSON; +import org.wso2.carbon.device.mgt.iot.androidsense.controller.service.impl.util.DeviceJSON; import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord; -import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.apimgt.annotations.device.feature.Feature; import javax.servlet.http.HttpServletResponse; @@ -42,8 +36,6 @@ import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.io.File; -import java.util.Date; @DeviceType( value = "android_sense" ) @API( name="android_sense", version="1.0.0", context="/android_sense") @@ -78,162 +70,6 @@ public class AndroidSenseService { @Context //injected response proxy supporting multiple thread private HttpServletResponse response; - @Path("manager/device") - @PUT - public boolean register(@FormParam("deviceId") String deviceId, - @FormParam("owner") String owner) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - try { - if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - response.setStatus(Response.Status.CONFLICT.getStatusCode()); - return true; - } - Device device = new Device(); - device.setDeviceIdentifier(deviceId); - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - - enrolmentInfo.setDateOfEnrolment(new Date().getTime()); - enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); - String name = owner + " android " + deviceId; - device.setName(name); - device.setType(AndroidSenseConstants.DEVICE_TYPE); - enrolmentInfo.setOwner(owner); - device.setEnrolmentInfo(enrolmentInfo); - boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); - - if (added) { - response.setStatus(Response.Status.OK.getStatusCode()); - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - } - - return added; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - } - - @Path("manager/device/remove/{device_id}") - @DELETE - public void removeDevice(@PathParam("device_id") String deviceId, - @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - try { - boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); - if (removed) { - response.setStatus(Response.Status.OK.getStatusCode()); - - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - - } - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("manager/device/update/{device_id}") - @POST - public boolean updateDevice( - @PathParam("device_id") String deviceId, @QueryParam("name") String name, - @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - try { - Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); - device.setDeviceIdentifier(deviceId); - - // device.setDeviceTypeId(deviceTypeId); - device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); - - device.setName(name); - device.setType(AndroidSenseConstants.DEVICE_TYPE); - - boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device); - - if (updated) { - response.setStatus(Response.Status.OK.getStatusCode()); - - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - - } - return updated; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("manager/device/{device_id}") - @GET - @Consumes("application/json") - @Produces("application/json") - public Device getDevice( - @PathParam("device_id") String deviceId) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - - try { - return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); - - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return null; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("manager/device/{sketch_type}/download") - @GET - @Produces("application/octet-stream") - public Response downloadSketch(@PathParam("sketch_type") String sketchType) { - //TODO:: null check customDeviceName at UI level - try { - - String sep = File.separator; - String sketchFolder = "repository" + sep + "resources" + sep + "sketches" + sep + "android_sense" + sep; - String archivesPath = CarbonUtils.getCarbonHome() + sep + sketchFolder; - - Response.ResponseBuilder rb = Response.ok(new File(archivesPath+sep+"androidsense.apk")); - rb.header("Content-Disposition", - "attachment; filename=\"" + "androidsense.apk" + "\""); - return rb.build(); - } catch (IllegalArgumentException ex) { - return Response.status(400).entity(ex.getMessage()).build();//bad request - } - - } - /* Service to push all the sensor data collected by the Android Called by the Android device */ @Path("controller/sensordata") diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/util/DeviceJSON.java b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/util/DeviceJSON.java similarity index 82% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/util/DeviceJSON.java rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/util/DeviceJSON.java index 0c7c13d821..2c87cd5cca 100644 --- a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/util/DeviceJSON.java +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/util/DeviceJSON.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util; +package org.wso2.carbon.device.mgt.iot.androidsense.controller.service.impl.util; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/util/SensorJSON.java b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/util/SensorJSON.java similarity index 81% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/util/SensorJSON.java rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/util/SensorJSON.java index 70c7f7cf3b..3a3482637f 100644 --- a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/util/SensorJSON.java +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/controller/service/impl/util/SensorJSON.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util; +package org.wso2.carbon.device.mgt.iot.androidsense.controller.service.impl.util; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/resources.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/META-INF/resources.xml similarity index 100% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/resources.xml rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/META-INF/resources.xml diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/webapp-classloading.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/META-INF/webapp-classloading.xml similarity index 100% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/webapp-classloading.xml rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/META-INF/webapp-classloading.xml diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/WEB-INF/cxf-servlet.xml similarity index 96% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/cxf-servlet.xml rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 4a40628a99..be42a6bbb9 100644 --- a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -30,7 +30,7 @@ + class="org.wso2.carbon.device.mgt.iot.androidsense.controller.service.impl.AndroidSenseService"/> diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/web.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/web.xml rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.controller.api/src/main/webapp/WEB-INF/web.xml diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/pom.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/pom.xml similarity index 94% rename from components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/pom.xml rename to components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/pom.xml index 1a07767662..a6ab537c66 100644 --- a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.api/pom.xml +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/pom.xml @@ -10,11 +10,11 @@ 4.0.0 - org.wso2.carbon.device.mgt.iot.androidsense.api + org.wso2.carbon.device.mgt.iot.androidsense.mgt.api 1.9.2-SNAPSHOT war - WSO2 Carbon - Android Sense API - WSO2 Carbon - Android Sense Service API Implementation + WSO2 Carbon - Android Sense Management API + WSO2 Carbon - Android Sense Service Management API Implementation http://wso2.org @@ -176,6 +176,11 @@ org.wso2.carbon.apimgt.annotations provided + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.webapp.publisher + provided + diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/mgt/service/impl/AndroidSenseService.java b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/mgt/service/impl/AndroidSenseService.java new file mode 100644 index 0000000000..71020357d6 --- /dev/null +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/mgt/service/impl/AndroidSenseService.java @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wso2.carbon.device.mgt.iot.androidsense.mgt.service.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.iot.DeviceManagement; +import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants; +import org.wso2.carbon.device.mgt.iot.apimgt.AccessTokenInfo; +import org.wso2.carbon.device.mgt.iot.apimgt.TokenClient; +import org.wso2.carbon.device.mgt.iot.exception.AccessTokenException; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import java.io.File; +import java.util.Date; + +@API( name="android_sense", version="1.0.0", context="/android_sense") +public class AndroidSenseService { + + private static Log log = LogFactory.getLog(AndroidSenseService.class); + + //TODO; replace this tenant domain + private final String SUPER_TENANT = "carbon.super"; + + @Context //injected response proxy supporting multiple thread + private HttpServletResponse response; + + @Path("manager/device") + @PUT + public boolean register(@FormParam("deviceId") String deviceId, + @FormParam("owner") String owner) { + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + try { + if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { + response.setStatus(Response.Status.CONFLICT.getStatusCode()); + return true; + } + Device device = new Device(); + device.setDeviceIdentifier(deviceId); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + +// KeyGenerationUtil.createApplicationKeys("android_sense"); +// +// TokenClient accessTokenClient = new TokenClient(AndroidSenseConstants.DEVICE_TYPE); +// AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId); +// +// //create token +// String accessToken = accessTokenInfo.getAccess_token(); +// String refreshToken = accessTokenInfo.getRefresh_token(); + + enrolmentInfo.setDateOfEnrolment(new Date().getTime()); + enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); + enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); + String name = owner + " android " + deviceId; + device.setName(name); + device.setType(AndroidSenseConstants.DEVICE_TYPE); + enrolmentInfo.setOwner(owner); + device.setEnrolmentInfo(enrolmentInfo); + boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); + + if (added) { + response.setStatus(Response.Status.OK.getStatusCode()); +// response.addHeader("access", accessToken); +// response.addHeader("refresh", refreshToken); + } else { + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + } + + return added; + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return false; + } catch (Exception e) { + e.printStackTrace(); + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return false; + } finally { + deviceManagement.endTenantFlow(); + } + } + + @Path("manager/device/remove/{device_id}") + @DELETE + public void removeDevice(@PathParam("device_id") String deviceId, + @Context HttpServletResponse response) { + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + try { + boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); + if (removed) { + response.setStatus(Response.Status.OK.getStatusCode()); + + } else { + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + + } + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } finally { + deviceManagement.endTenantFlow(); + } + + } + + @Path("manager/device/update/{device_id}") + @POST + public boolean updateDevice( + @PathParam("device_id") String deviceId, @QueryParam("name") String name, + @Context HttpServletResponse response) { + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + try { + Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); + device.setDeviceIdentifier(deviceId); + + // device.setDeviceTypeId(deviceTypeId); + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + + device.setName(name); + device.setType(AndroidSenseConstants.DEVICE_TYPE); + + boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device); + + if (updated) { + response.setStatus(Response.Status.OK.getStatusCode()); + + } else { + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + + } + return updated; + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return false; + } finally { + deviceManagement.endTenantFlow(); + } + + } + + @Path("manager/device/{device_id}") + @GET + @Consumes("application/json") + @Produces("application/json") + public Device getDevice( + @PathParam("device_id") String deviceId) { + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + + try { + return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); + + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return null; + } finally { + deviceManagement.endTenantFlow(); + } + + } + + @Path("manager/device/{sketch_type}/download") + @GET + @Produces("application/octet-stream") + public Response downloadSketch(@PathParam("sketch_type") String sketchType) { + //TODO:: null check customDeviceName at UI level + try { + + String sep = File.separator; + String sketchFolder = "repository" + sep + "resources" + sep + "sketches" + sep + "android_sense" + sep; + String archivesPath = CarbonUtils.getCarbonHome() + sep + sketchFolder; + + Response.ResponseBuilder rb = Response.ok(new File(archivesPath+sep+"androidsense.apk")); + rb.header("Content-Disposition", + "attachment; filename=\"" + "androidsense.apk" + "\""); + return rb.build(); + } catch (IllegalArgumentException ex) { + return Response.status(400).entity(ex.getMessage()).build();//bad request + } + + } + +} diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/resources.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/resources.xml new file mode 100644 index 0000000000..a2f040ee53 --- /dev/null +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/resources.xml @@ -0,0 +1,123 @@ + + + + + + + + + Any + PUT + http://localhost:9763/android_sense/manager/device/register + /manager/device/register/* + + + Any + DELETE + http://localhost:9763/android_sense/manager/device/remove + /manager/device/remove/* + + + Any + POST + http://localhost:9763/android_sense/manager/device/update + /manager/device/update/* + + + Any + GET + http://localhost:9763/android_sense/manager/device/{device_id} + /manager/device/{device_id} + + + Any + GET + http://localhost:9763/android_sense/controller/sensordata + /controller/sensordata + + + Any + GET + http://localhost:9763/android_sense/controller/readbattery + /controller/readbattery + + + Any + GET + http://localhost:9763/android_sense/controller/readlight + /controller/readlight + + + Any + GET + http://localhost:9763/android_sense/controller/readgps + /controller/readgps + + + Any + GET + http://localhost:9763/android_sense/controller/readmagnetic + /controller/readmagnetic + + + Any + GET + http://localhost:9763/android_sense/controller/readgravity + /controller/readgravity + + + Any + GET + http://localhost:9763/android_sense/controller/readpressure + /controller/readpressure + + + Any + GET + http://localhost:9763/android_sense/controller/readproximity + /controller/readproximity + + + Any + GET + http://localhost:9763/android_sense/controller/readgyroscope + /controller/readgyroscope + + + Any + GET + http://localhost:9763/android_sense/controller/readrotation + /controller/readrotation + + + Any + GET + http://localhost:9763/android_sense/controller/readaccelerometer + /controller/readaccelerometer + + diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml new file mode 100644 index 0000000000..fa44619195 --- /dev/null +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml @@ -0,0 +1,33 @@ + + + + + + + + + false + + + CXF,Carbon + diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml new file mode 100644 index 0000000000..40218f1a79 --- /dev/null +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/web.xml b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..3748c69795 --- /dev/null +++ b/components/device-mgt-iot-androidsense/org.wso2.carbon.device.mgt.iot.androidsense.mgt.api/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,73 @@ + + + Android Sense + Android Sense + + + CXFServlet + org.apache.cxf.transport.servlet.CXFServlet + 1 + + + + + CXFServlet + /* + + + + + + + + + + + + + + managed-api-enabled + false + + + managed-api-owner + admin + + + managed-api-name + android_sense + + + managed-api-endpoint + https://localhost:9443/android_sense + + + managed-api-version + 1.0.0 + + + managed-api-context + /android_sense + + + managed-api-isSecured + true + + + + + + + + + + + + + + + diff --git a/components/device-mgt-iot-androidsense/pom.xml b/components/device-mgt-iot-androidsense/pom.xml index 95e0c843d0..d3664b8a8c 100644 --- a/components/device-mgt-iot-androidsense/pom.xml +++ b/components/device-mgt-iot-androidsense/pom.xml @@ -37,7 +37,8 @@ org.wso2.carbon.device.mgt.iot.androidsense - org.wso2.carbon.device.mgt.iot.androidsense.api + org.wso2.carbon.device.mgt.iot.androidsense.controller.api + org.wso2.carbon.device.mgt.iot.androidsense.mgt.api