diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/androidsense.apk b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/androidsense.apk new file mode 100644 index 0000000000..89acc8aa08 Binary files /dev/null and b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/androidsense.apk differ diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/RegisterActivity.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/RegisterActivity.java index a7e069757c..e9f56a8b16 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/RegisterActivity.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/RegisterActivity.java @@ -140,6 +140,7 @@ public class RegisterActivity extends Activity { }); if (registerStatus.isRegistered()) { + LocalRegistry.setEnrolled(getApplicationContext(), true); LocalRegistry.addUsername(getApplicationContext(), username); LocalRegistry.addDeviceId(getApplicationContext(), deviceId); LocalRegistry.addMqttPort(getApplicationContext(), mqttPortNo); diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java index 0f852fd6c6..d1ae9ad038 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java @@ -143,7 +143,7 @@ public class DataPublisherService extends Service { } SenseDataHolder.resetWordDataHolder(); //publish the data - if (events.size() > 0) { + if (events.size() > 0 && LocalRegistry.isEnrolled(context)) { String user = LocalRegistry.getUsername(context); String deviceId = LocalRegistry.getDeviceId(context); JSONArray jsonArray = new JSONArray(); diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Location/LocationDataReader.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Location/LocationDataReader.java index 3504a12452..ca97f21200 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Location/LocationDataReader.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Location/LocationDataReader.java @@ -165,5 +165,4 @@ public class LocationDataReader extends DataReader implements LocationListener { } } - } diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/ActivitySelectSensor.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/ActivitySelectSensor.java index b93a3a6270..2ceeba026d 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/ActivitySelectSensor.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/ActivitySelectSensor.java @@ -176,7 +176,7 @@ public class ActivitySelectSensor extends AppCompatActivity Intent activity = new Intent(getApplicationContext(), RegisterActivity.class); startActivity(activity); } - + LocalRegistry.setEnrolled(getApplicationContext(), false); LocalRegistry.removeUsername(getApplicationContext()); LocalRegistry.removeDeviceId(getApplicationContext()); LocalRegistry.removeServerURL(getApplicationContext()); diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/LocalRegistry.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/LocalRegistry.java index e9d3d430f7..be6af804fe 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/LocalRegistry.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/LocalRegistry.java @@ -35,6 +35,7 @@ public class LocalRegistry { private static final String ACCESS_TOKEN_KEY = "accessTokenKey"; private static final String REFRESH_TOKEN_KEY = "refreshTokenKey"; private static final String MQTT_PORT_KEY = "mqttPort"; + private static final String IS_ENROLLED_KEY = "enrolled"; private static boolean exists = false; private static String username; private static String deviceId; @@ -43,6 +44,7 @@ public class LocalRegistry { private static String accessToken; private static String refreshToken; private static int mqttPort; + private static boolean enrolled; public static boolean isExist(Context context) { if (!exists) { @@ -209,6 +211,22 @@ public class LocalRegistry { return LocalRegistry.mqttPort; } + public static void setEnrolled(Context context, boolean enrolled) { + SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(IS_ENROLLED_KEY, enrolled); + editor.commit(); + LocalRegistry.enrolled = enrolled; + } + + public static boolean isEnrolled(Context context) { + if (!LocalRegistry.enrolled) { + SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); + return LocalRegistry.enrolled = sharedpreferences.getBoolean(IS_ENROLLED_KEY, false); + } + return LocalRegistry.enrolled; + } + public static String getServerHost(Context context) { URL url = null; diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_register.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_register.xml index 16940538c8..42c37ab259 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_register.xml +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_register.xml @@ -27,19 +27,19 @@ android:id="@+id/username" android:inputType="text" android:maxLines="1" android:singleLine="true" - android:text="admin"/> + /> + /> diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.analytics/src/main/resources/carbonapps/Accelerometer/Eventstream_accelerometer_1.0.0/artifact.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.analytics/src/main/resources/carbonapps/Accelerometer/Eventstream_accelerometer_1.0.0/artifact.xml deleted file mode 100644 index 4a9d840f94..0000000000 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.analytics/src/main/resources/carbonapps/Accelerometer/Eventstream_accelerometer_1.0.0/artifact.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - org.wso2.iot.devices.accelerometer_1.0.0.json - - diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.analytics/src/main/resources/carbonapps/Android_Sense/EventReceiver_AndroidSense_1.0.0/EventReceiver_AndroidSense.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.analytics/src/main/resources/carbonapps/Android_Sense/EventReceiver_AndroidSense_1.0.0/EventReceiver_AndroidSense.xml index 650254422a..aad8f354f8 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.analytics/src/main/resources/carbonapps/Android_Sense/EventReceiver_AndroidSense_1.0.0/EventReceiver_AndroidSense.xml +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.analytics/src/main/resources/carbonapps/Android_Sense/EventReceiver_AndroidSense_1.0.0/EventReceiver_AndroidSense.xml @@ -19,13 +19,13 @@ - wso2/android_sense/# + wso2/android_sense/+/data admin device_id_json_path:event.metaData.deviceId,device_id_topic_hierarchy_index:2 default https://localhost:9443/dynamic-client-web/register tcp://localhost:1883 - false + true 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 2a574f8e61..2e9c407d26 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 @@ -46,9 +46,8 @@ public interface AndroidSenseControllerService { */ @Path("device/{deviceId}/words") @POST - @Feature(code = "keywords", name = "Add Keywords", description = "Send keywords to the device", - type = "operation") - Response sendKeyWords(@PathParam("deviceId") String deviceId, @FormParam("keywords") String keywords); + @Feature(code = "keywords", name = "Add Keywords", description = "Send keywords to the device", type = "operation") + Response sendKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("keywords") String keywords); /** * End point to send the key words to the device @@ -60,7 +59,7 @@ public interface AndroidSenseControllerService { @POST @Feature(code = "threshold", name = "Add a Threshold", description = "Set a threshold for word in the device", type = "operation") - Response sendThreshold(@PathParam("deviceId") String deviceId, @FormParam("threshold") String threshold); + Response sendThreshold(@PathParam("deviceId") String deviceId, @QueryParam("threshold") String threshold); @Path("device/{deviceId}/words") @DELETE @@ -71,7 +70,6 @@ public interface AndroidSenseControllerService { /** * Retrieve Sensor data for the device type */ - @Path("stats/{deviceId}/sensors/{sensorName}") @GET @Consumes("application/json") 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/AndroidSenseControllerServiceImpl.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/AndroidSenseControllerServiceImpl.java index ce2dbc2cb2..61e0cacfff 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/AndroidSenseControllerServiceImpl.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/AndroidSenseControllerServiceImpl.java @@ -24,6 +24,8 @@ import org.wso2.carbon.analytics.dataservice.commons.SORT; import org.wso2.carbon.analytics.dataservice.commons.SortByField; import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.transport.AndroidSenseMQTTConnector; import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.APIUtil; import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.SensorRecord; @@ -55,33 +57,56 @@ public class AndroidSenseControllerServiceImpl implements AndroidSenseController @Path("device/{deviceId}/words") @POST - public Response sendKeyWords(@PathParam("deviceId") String deviceId, @FormParam("keywords") String keywords) { + public Response sendKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("keywords") String keywords) { try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, + AndroidSenseConstants.DEVICE_TYPE))) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } androidSenseMQTTConnector.publishDeviceData(deviceId, "add", keywords); return Response.ok().build(); } catch (TransportHandlerException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } } @Path("device/{deviceId}/words/threshold") @POST - public Response sendThreshold(@PathParam("deviceId") String deviceId, @FormParam("threshold") String threshold) { + public Response sendThreshold(@PathParam("deviceId") String deviceId, @QueryParam("threshold") String threshold) { try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, + AndroidSenseConstants.DEVICE_TYPE))) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } androidSenseMQTTConnector.publishDeviceData(deviceId, "threshold", threshold); return Response.ok().build(); } catch (TransportHandlerException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); + } catch (DeviceAccessAuthorizationException e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } } @Path("device/{deviceId}/words") @DELETE - public Response removeKeyWords(@PathParam("deviceId") String deviceId, @FormParam("words") String words) { + public Response removeKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("words") String words) { try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, + AndroidSenseConstants.DEVICE_TYPE))) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } androidSenseMQTTConnector.publishDeviceData(deviceId, "remove", words); return Response.ok().build(); } catch (TransportHandlerException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } } @@ -96,13 +121,18 @@ public class AndroidSenseControllerServiceImpl implements AndroidSenseController String toDate = String.valueOf(to); String user = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String query = "owner:" + user + " AND deviceId:" + deviceId + " AND deviceType:" + - AndroidSenseConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]"; + AndroidSenseConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]"; if (sensor.equals(AndroidSenseConstants.SENSOR_WORDCOUNT)) { query = "owner:" + user + " AND deviceId:" + deviceId; } String sensorTableName = getSensorEventTableName(sensor); - List sensorDatas; + try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, + AndroidSenseConstants.DEVICE_TYPE))) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } + List sensorDatas; if (sensor.equals(AndroidSenseConstants.SENSOR_WORDCOUNT)) { List sortByFields = new ArrayList<>(); SortByField sortByField = new SortByField("time", SORT.ASC, false); @@ -116,6 +146,9 @@ public class AndroidSenseControllerServiceImpl implements AndroidSenseController String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; log.error(errorMsg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } } 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 5cb401d13d..68e48074ac 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 @@ -31,7 +31,6 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("enrollment") -@DeviceType(value = "android_sense") @API(name = "android_sense_mgt", version = "1.0.0", context = "/android_sense_mgt", tags = {"android_sense"}) public interface AndroidSenseManagerService { @@ -53,10 +52,5 @@ public interface AndroidSenseManagerService { @Produces("application/json") Response getDevice(@PathParam("device_id") String deviceId); - @Path("/devices/download") - @GET - @Produces("application/octet-stream") - Response downloadSketch(); - } 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/AndroidSenseManagerServiceImpl.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/AndroidSenseManagerServiceImpl.java index 45420f31cf..51b0c19e0f 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/AndroidSenseManagerServiceImpl.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/AndroidSenseManagerServiceImpl.java @@ -24,9 +24,9 @@ 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.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.APIUtil; import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants; -import org.wso2.carbon.utils.CarbonUtils; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -38,7 +38,6 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; -import java.io.File; import java.util.Date; @Path("enrollment") @@ -56,7 +55,6 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) { return Response.status(Response.Status.CONFLICT.getStatusCode()).build(); } - Device device = new Device(); device.setDeviceIdentifier(deviceId); EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); @@ -76,6 +74,7 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).entity(false).build(); } } catch (DeviceManagementException e) { + log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(false).build(); } } @@ -87,6 +86,9 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic deviceIdentifier.setId(deviceId); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier)) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } boolean removed = APIUtil.getDeviceManagementService().disenrollDevice(deviceIdentifier); if (removed) { return Response.ok().build(); @@ -94,6 +96,10 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build(); } } catch (DeviceManagementException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } } @@ -105,6 +111,9 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic deviceIdentifier.setId(deviceId); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier)) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier); device.setDeviceIdentifier(deviceId); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); @@ -117,6 +126,10 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build(); } } catch (DeviceManagementException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } } @@ -130,26 +143,17 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic deviceIdentifier.setId(deviceId); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier)) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier); return Response.ok().entity(device).build(); } catch (DeviceManagementException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); - } - } - - @Path("/devices/download") - @GET - @Produces("application/octet-stream") - public Response downloadSketch() { - 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(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build(); } } 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/transport/AndroidSenseMQTTConnector.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/transport/AndroidSenseMQTTConnector.java index a9e62aefbc..21e35475f1 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/transport/AndroidSenseMQTTConnector.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/transport/AndroidSenseMQTTConnector.java @@ -45,7 +45,7 @@ public class AndroidSenseMQTTConnector extends MQTTTransportHandler { private static String subscribeTopic = AndroidSenseConstants.MQTT_SUBSCRIBE_WORDS_TOPIC; private static String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5); private static final String KEY_TYPE = "PRODUCTION"; - private static final String EMPTY_STRING = ""; + private static final String DEFAULT_PASSWORD = ""; private AndroidSenseMQTTConnector() { super(iotServerSubscriber, AndroidSenseConstants.DEVICE_TYPE, @@ -75,9 +75,8 @@ public class AndroidSenseMQTTConnector extends MQTTTransportHandler { apiApplicationKey.getConsumerSecret(), applicationUsername, scopes); //create token String accessToken = accessTokenInfo.getAccessToken(); - setUsernameAndPassword(accessToken, EMPTY_STRING); + setUsernameAndPassword(accessToken, DEFAULT_PASSWORD); connectToQueue(); - subscribeToQueue(); } catch (TransportHandlerException e) { log.warn("Connection/Subscription to MQTT Broker at: " + mqttBrokerEndPoint + " failed"); try { 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/util/APIUtil.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/util/APIUtil.java index fd74ca1f3a..f5983c9df7 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/util/APIUtil.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/util/APIUtil.java @@ -13,8 +13,14 @@ import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; +import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.user.core.service.RealmService; @@ -30,7 +36,6 @@ import java.util.Map; public class APIUtil { private static Log log = LogFactory.getLog(APIUtil.class); - private static Object lock = new Object(); public static String getAuthenticatedUser() { PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); @@ -66,7 +71,8 @@ public class APIUtil { return analyticsDataAPI; } - public static List getAllEventsForDevice(String tableName, String query, List sortByFields) throws AnalyticsException { + public static List getAllEventsForDevice(String tableName, String query, + List sortByFields) throws AnalyticsException { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI(); int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query); @@ -158,37 +164,54 @@ public class APIUtil { return jwtClientManagerService; } + public static DeviceAccessAuthorizationService getDeviceAccessAuthorizationService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + DeviceAccessAuthorizationService deviceAccessAuthorizationService = + (DeviceAccessAuthorizationService) ctx.getOSGiService(DeviceAccessAuthorizationService.class, null); + if (deviceAccessAuthorizationService == null) { + String msg = "Device Authorization service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceAccessAuthorizationService; + } + + public static void registerApiAccessRoles(String user) { UserStoreManager userStoreManager = null; try { userStoreManager = getUserStoreManager(); + String[] userList = new String[]{user}; if (userStoreManager != null) { - synchronized (lock) { - String[] userList = new String[]{user}; - if (!userStoreManager.isExistingRole(Constants.DEFAULT_ROLE_NAME)) { - userStoreManager.addRole(Constants.DEFAULT_ROLE_NAME, userList, Constants.DEFAULT_PERMISSION); - } + if (!userStoreManager.isExistingRole(Constants.DEFAULT_ROLE_NAME)) { + userStoreManager.addRole(Constants.DEFAULT_ROLE_NAME, userList, Constants.DEFAULT_PERMISSION); + } else { + userStoreManager.updateUserListOfRole(Constants.DEFAULT_ROLE_NAME, null, userList); } } } catch (UserStoreException e) { - log.error("error on wso2 user component"); + log.error("Error while creating a role and adding a user for android_sense.", e); } } - private static UserStoreManager getUserStoreManager() throws UserStoreException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - return getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); - } - - public static RealmService getRealmService() { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - RealmService realmService = - (RealmService) ctx.getOSGiService(RealmService.class, null); - if (realmService == null) { - String msg = "JWT Client manager service has not initialized."; - log.error(msg); + public static UserStoreManager getUserStoreManager() { + RealmService realmService; + UserStoreManager userStoreManager; + try { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); + if (realmService == null) { + String msg = "Realm service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + int tenantId = ctx.getTenantId(); + userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager(); + } catch (UserStoreException e) { + String msg = "Error occurred while retrieving current user store manager"; + log.error(msg, e); throw new IllegalStateException(msg); } - return realmService; + return userStoreManager; } } 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/util/Constants.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/util/Constants.java index c6b2c5b1a3..acc8ba6e55 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/util/Constants.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/util/Constants.java @@ -19,15 +19,14 @@ package org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util; import org.wso2.carbon.user.core.Permission; -import org.wso2.carbon.user.core.authorization.TreeNode; /** - * This hold the constants related to android sense. + * This hold the constants related to the device type. */ public class Constants { - private static final String DEFAULT_PERMISSION_RESOURCE = "/_system/governance/permission/admin/device-mgt/android_sense/user"; + public static final String DEFAULT_PERMISSION_RESOURCE = "/permission/admin/device-mgt/android_sense/user"; public static final String DEFAULT_ROLE_NAME = "android_sense_user"; public static final Permission DEFAULT_PERMISSION[] = new Permission[]{new Permission(Constants.DEFAULT_PERMISSION_RESOURCE, - TreeNode.Permission.UI_EXECUTE.toString())}; + "ui.execute")}; } diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml index e81d61e59a..b0bb72ed89 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml @@ -65,7 +65,7 @@ Add device - /login + /device-mgt/user /enrollment/devices/* POST android_sense_user @@ -77,13 +77,6 @@ DELETE android_sense_user - - Download device - /device-mgt/android_sense/user - /enrollment/devices/download - GET - android_sense_user - Update device /device-mgt/android_sense/user diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/asset/androidsense.apk b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/asset/androidsense.apk new file mode 100644 index 0000000000..b1d1f0fbb9 Binary files /dev/null and b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/asset/androidsense.apk differ diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/js/download.js b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/js/download.js index f963c0e0c5..250d07109b 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/js/download.js +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/js/download.js @@ -56,14 +56,6 @@ function showPopup() { deviceType = this.value; } }); - if (deviceType == 'virtual_firealarm') { - $('.sketchType').remove(); - $('input[name="sketchType"][value="virtual_firealarm"]').prop('checked', true); - $("label[for='virtual_firealarm']").text("Simple Agent"); - $("label[for='virtual_firealarm_advanced']").text("Advanced Agent"); - } else { - $('.sketchTypes').remove(); - } } /* @@ -90,10 +82,6 @@ function attachEvents() { * on Device Management page in WSO2 DC Console. */ $("a.download-link").click(function () { - var sketchType = $(this).data("sketchtype"); - var deviceType = $(this).data("devicetype"); - var downloadDeviceAPI = "/devicemgt/api/devices/sketch/generate_link"; - var payload = {"sketchType": sketchType, "deviceType": deviceType}; $(modalPopupContent).html($('#download-device-modal-content').html()); showPopup(); var deviceName; @@ -104,29 +92,6 @@ function attachEvents() { } }); $('label[for=deviceName]').remove(); - if (deviceName && deviceName.length >= 4) { - payload.deviceName = deviceName; - invokerUtil.post( - downloadDeviceAPI, - payload, - function (data, textStatus, jqxhr) { - doAction(data); - }, - function (data) { - doAction(data); - } - ); - } else if (deviceName) { - $('.controls').append(''); - $('.control-group').removeClass('success').addClass('error'); - } else { - $('.controls').append(''); - $('.control-group').removeClass('success').addClass('error'); - } }); $("a#download-device-cancel-link").click(function () { @@ -134,47 +99,4 @@ function attachEvents() { }); }); -} - -//Device owner removed. -function downloadAgent() { - $('#downloadForm').submit(); - hidePopup(); - $(modalPopupContent).html($('#device-agent-downloading-content').html()); - showPopup(); - setTimeout(function () { - hidePopup(); - }, 1000); -} - -function doAction(data) { - //if it is saml redirection response - if (data.status == null) { - document.write(data); - } - if (data.status == "200") { - $(modalPopupContent).html($('#download-device-modal-content-links').html()); - $("input#download-device-url").val(data.responseText); - $("input#download-device-url").focus(function () { - $(this).select(); - }); - showPopup(); - } else if (data.status == "401") { - $(modalPopupContent).html($('#device-401-content').html()); - $("#device-401-link").click(function () { - window.location = "/devicemgt/login"; - }); - showPopup(); - } else if (data == "403") { - $(modalPopupContent).html($('#device-403-content').html()); - $("#device-403-link").click(function () { - window.location = "/devicemgt/login"; - }); - showPopup(); - } else { - $(modalPopupContent).html($('#device-unexpected-error-content').html()); - $("a#device-unexpected-error-link").click(function () { - hidePopup(); - }); - } } \ No newline at end of file diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/type-view.hbs b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/type-view.hbs index 6548564faa..b045844071 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/type-view.hbs +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/type-view.hbs @@ -39,8 +39,7 @@    Proceed to [Prepare] section.
- + Enroll Device

@@ -225,7 +224,7 @@ -