From 24e353698f0714894ef4e37eb175091b424fb911 Mon Sep 17 00:00:00 2001 From: mharindu Date: Mon, 16 May 2016 12:12:47 +0530 Subject: [PATCH 1/3] Added permission annotations to virtual fire alarm API --- .../impl/AndroidSenseControllerService.java | 5 +++++ .../impl/AndroidSenseManagerService.java | 5 +++++ .../VirtualFireAlarmControllerService.java | 4 ++-- .../impl/VirtualFireAlarmManagerService.java | 6 ++++++ .../src/main/webapp/META-INF/permissions.xml | 20 +++---------------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java index 43792b859..bbd8358a8 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.iot.androidsense.service.impl; import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature; import javax.ws.rs.Consumes; @@ -44,6 +45,7 @@ public interface AndroidSenseControllerService { @Path("device/{deviceId}/words") @POST @Feature(code = "keywords", name = "Add Keywords", description = "Send keywords to the device", type = "operation") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"}) Response sendKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("keywords") String keywords); /** @@ -56,12 +58,14 @@ public interface AndroidSenseControllerService { @POST @Feature(code = "threshold", name = "Add a Threshold", description = "Set a threshold for word in the device", type = "operation") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"}) Response sendThreshold(@PathParam("deviceId") String deviceId, @QueryParam("threshold") String threshold); @Path("device/{deviceId}/words") @DELETE @Feature(code = "remove", name = "Remove Keywords", description = "Remove the keywords", type = "operation") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"}) Response removeKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("words") String words); /** @@ -71,6 +75,7 @@ public interface AndroidSenseControllerService { @GET @Consumes("application/json") @Produces("application/json") + @Permission(scope = "android_sense_device", permissions = {"/permission/admin/device-mgt/user/stats"}) Response getAndroidSenseDeviceStats(@PathParam("deviceId") String deviceId, @PathParam("sensorName") String sensor, @QueryParam("from") long from, @QueryParam("to") long to); diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java index 68e48074a..1e03db16e 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.iot.androidsense.service.impl; import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -36,20 +37,24 @@ public interface AndroidSenseManagerService { @Path("/devices/{device_id}") @POST + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices"}) Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName); @Path("/devices/{device_id}") @DELETE + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/remove"}) Response removeDevice(@PathParam("device_id") String deviceId); @Path("/devices/{device_id}") @PUT + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/update"}) Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); @Path("/devices/{device_id}") @GET @Consumes("application/json") @Produces("application/json") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/list"}) Response getDevice(@PathParam("device_id") String deviceId); } diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java index 3bbbe3ad4..30523c444 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java @@ -49,7 +49,7 @@ public interface VirtualFireAlarmControllerService { */ @POST @Path("device/{deviceId}/buzz") - @Permission(scope = "virtual_firealarm_user", permissions = {"device-mgt/virtual_firealarm/user"}) + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/operation"}) @Feature(code = "buzz", name = "Buzzer On / Off", type = "operation", description = "Switch on/off Virtual Fire Alarm Buzzer. (On / Off)") Response switchBuzzer(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol, @@ -61,7 +61,7 @@ public interface VirtualFireAlarmControllerService { */ @Path("device/stats/{deviceId}") @GET - @Permission(scope = "virtual_firealarm_user", permissions = {"device-mgt/virtual_firealarm/user"}) + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/stats"}) @Consumes("application/json") @Produces("application/json") Response getVirtualFirealarmStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java index 7b5bdaf09..4b0f3c13f 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl; import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import javax.ws.rs.Consumes; @@ -38,28 +39,33 @@ public interface VirtualFireAlarmManagerService { @Path("/devices/{device_id}") @DELETE + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/devices/remove"}) Response removeDevice(@PathParam("device_id") String deviceId); @Path("/devices/{device_id}") @PUT + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/devices/update"}) Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); @Path("/devices/{device_id}") @GET @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/devices/list"}) Response getDevice(@PathParam("device_id") String deviceId); @Path("/devices") @GET @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) + @Permission(scope = "virtual_firealarm_user", permissions = {"/device-mgt/user/devices/list"}) Response getFirealarmDevices(); @Path("/devices/download") @GET @Produces("application/zip") + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user"}) Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType); } \ No newline at end of file diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml index 8b8957385..072cd1e74 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml @@ -44,7 +44,7 @@ Download device - /device-mgt/virtual_firealarm/user + /device-mgt/user /enrollment/devices/download GET virtual_firealarm_user @@ -63,32 +63,18 @@ GET virtual_firealarm_user - - Register Device - /device-mgt/user/operations - /device/register/*/*/* - POST - virtual_firealarm_device - Control Buzz - /device-mgt/user/operations + /device-mgt/user/operation /device/*/buzz POST virtual_firealarm_user - - Push Temperature - /device-mgt/user/stats - /device/temperature - POST - virtual_firealarm_device - Get Stats /device-mgt/user/stats /device/stats/* GET - virtual_firealarm_device + virtual_firealarm_user \ No newline at end of file From d7b84cf9e4bd17337af509702d143329fe816cdc Mon Sep 17 00:00:00 2001 From: mharindu Date: Mon, 16 May 2016 14:14:39 +0530 Subject: [PATCH 2/3] Fixed conflicts --- .../impl/AndroidSenseManagerService.java | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100644 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java deleted file mode 100644 index ffb9a2d49..000000000 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you 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.service.impl; - -import org.wso2.carbon.apimgt.annotations.api.API; -import org.wso2.carbon.apimgt.annotations.api.Permission; - -import javax.ws.rs.*; -import javax.ws.rs.core.Response; -@Path("enrollment") -@API(name = "android_sense_mgt", version = "1.0.0", context = "/android_sense_mgt", tags = {"android_sense"}) -public interface AndroidSenseManagerService { - - @Path("/devices/{device_id}") - @POST - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices"}) - Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName); - - @Path("/devices/{device_id}") - @DELETE - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/remove"}) - Response removeDevice(@PathParam("device_id") String deviceId); - - @Path("/devices/{device_id}") - @PUT - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/update"}) - Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); - - @Path("/devices/{device_id}") - @GET - @Consumes("application/json") - @Produces("application/json") - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/list"}) - Response getDevice(@PathParam("device_id") String deviceId); - -} - From 0fee083c4b2205bf218f2e8ad21468dc00599c2b Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 18 May 2016 22:03:16 +0530 Subject: [PATCH 3/3] Added a null check to virtualfire-alarm --- .../service/impl/VirtualFireAlarmServiceImpl.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java index bc075f22c..8af28181d 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java @@ -62,6 +62,7 @@ import java.util.*; public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { private static final String XMPP_PROTOCOL = "XMPP"; + private static final String MQTT_PROTOCOL = "MQTT"; private static final String KEY_TYPE = "PRODUCTION"; private static ApiApplicationKey apiApplicationKey; private static Log log = LogFactory.getLog(VirtualFireAlarmServiceImpl.class); @@ -70,13 +71,24 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { @Path("device/{deviceId}/buzz") public Response switchBuzzer(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol, @FormParam("state") String state) { + if (state == null || state.isEmpty()) { + log.error("State is not defined for the buzzer operation"); + return Response.status(Response.Status.BAD_REQUEST).build(); + } String switchToState = state.toUpperCase(); if (!switchToState.equals(VirtualFireAlarmConstants.STATE_ON) && !switchToState.equals( VirtualFireAlarmConstants.STATE_OFF)) { log.error("The requested state change shoud be either - 'ON' or 'OFF'"); return Response.status(Response.Status.BAD_REQUEST).build(); } - String protocolString = protocol.toUpperCase(); + String protocolString; + + if (protocol == null || protocol.isEmpty()) { + protocolString = MQTT_PROTOCOL; + } else { + protocolString = protocol.toUpperCase(); + } + if (log.isDebugEnabled()) { log.debug("Sending request to switch-bulb of device [" + deviceId + "] via " + protocolString);