From b7da99a621188d14189de423b8697d2f0e8169cd Mon Sep 17 00:00:00 2001 From: geethkokila Date: Sat, 28 May 2016 08:25:27 +0530 Subject: [PATCH] Adding activity apis to get the updated once after a given time --- .../api/ActivityInfoProviderService.java | 52 ++++++++ .../service/api/DeviceManagementService.java | 112 ++++++++++++++++++ .../impl/ActivityProviderServiceImpl.java | 18 +++ .../impl/DeviceManagementServiceImpl.java | 39 +++++- .../details/mgt/DeviceInformationManager.java | 8 ++ .../impl/DeviceInformationManagerImpl.java | 21 ++++ 6 files changed, 249 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java index f7606437e1..8d12252274 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java @@ -93,4 +93,56 @@ public interface ActivityInfoProviderService { required = false) @HeaderParam("If-Modified-Since") String ifModifiedSince); + + + @GET + @Path("/") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + response = Activity.class, + responseContainer = "List", + value = "Retrieve details of a particular activity.", + notes = "This will return information of a particular activities i.e. meta information of operations, " + + "etc; including the responses from the devices which happened after given time.") + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "OK. \n Activity details is successfully fetched", + response = Activity.class, + responseContainer = "List", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching activity data.") + }) + @Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"}) + Response getActivities( + @ApiParam( + name = "timestamp", + value = "Validates if the requested variant has not been modified since the time specified, this " + + "should be provided in unix format in seconds.", + required = true) + @QueryParam("timestamp") String timestamp); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index a2e1ddde6c..d07290f1f7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -129,6 +129,75 @@ public interface DeviceManagementService { required = false) @QueryParam("limit") int limit); + @GET + @Path("{type}/{id}/info") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Retrieve devices information from the supplied device identifier.", + notes = "This will return device information such as CPU usage, memory usage etc for supplied device " + + "identifier.", + response = DeviceInfo.class, + tags = "Device Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Information of the submitted list of devices is returned", + response = DeviceInfo.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests.")}), + @ApiResponse( + code = 303, + message = "See Other. \n Source can be retrieved from the URL specified at the Location header.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The Source URL of the document.")}), + @ApiResponse( + code = 304, + message = "Not Modified. \n " + + "Empty body because the client already has the latest version of the requested resource."), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error."), + @ApiResponse( + code = 406, + message = "Not Acceptable. \n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving information of the list of the devices submitted.") + }) + @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) + Response getDeviceInfo( + @ApiParam( + name = "type", + value = "The device type, such as ios, android or windows.", + required = true) + @PathParam("type") String type, + @ApiParam( + name = "id", + value = "The device identifier of the device.", + required = true) + @PathParam("id") String id, + @ApiParam( + name = "If-Modified-Since", + value = "Validates if the requested variant has not been modified since the time specified", + required = false) + @HeaderParam("If-Modified-Since") String ifModifiedSince); + @POST @ApiOperation( consumes = MediaType.APPLICATION_JSON, @@ -300,6 +369,49 @@ public interface DeviceManagementService { required = false) @HeaderParam("If-Modified-Since") String ifModifiedSince); + + @POST + @Path("/locations") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Get the device location of a given devices and a device type.", + notes = "This will return the device locations including latitude and longitude as well the " + + "physical address of the given devices.", + response = DeviceLocation.class, + responseContainer = "List", + tags = "Device Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "Successfully fetched the device location.", + response = DeviceLocation.class, + responseContainer = "List"), + @ApiResponse( + code = 304, + message = "Not Modified. \n " + + "Empty body because the client already has the latest version of the requested resource."), + @ApiResponse( + code = 404, + message = "Location details are not available for the given devices."), + @ApiResponse( + code = 500, + message = "Error occurred while getting the device location.") + }) + @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) + Response getDeviceLocations( + @ApiParam( + name = "deviceIds", + value = "List of device identifiers", + required = true) List deviceIds, + @ApiParam( + name = "If-Modified-Since", + value = "Validates if the requested variant has not been modified since the time specified", + required = false) + @HeaderParam("If-Modified-Since") String ifModifiedSince); + @GET @Path("/{type}/{id}/features") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java index 8098adcc1e..dca5f7e1e2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java @@ -29,7 +29,9 @@ import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.HeaderParam; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; +import java.util.List; public class ActivityProviderServiceImpl implements ActivityInfoProviderService { @@ -53,4 +55,20 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService return Response.status(Response.Status.OK).entity(operation).build(); } + @Override + @Path("/") + public Response getActivities( + @QueryParam("timestamp") String timestamp) { + List activities = null; + DeviceManagementProviderService dmService; + try { + dmService = DeviceMgtAPIUtils.getDeviceManagementService(); + activities = dmService.getActivitiesUpdatedAfter(Long.parseLong(timestamp)); + } catch (OperationManagementException e) { + String msg = "Error occurred while fetching the activities updated after given time stamp."; + log.error(msg, e); + Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(activities).build(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index b5c95c099a..01d45f3eac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -82,6 +82,26 @@ public class DeviceManagementServiceImpl implements DeviceManagementService{ } } + @GET + @Path("{type}/{id}/info") + public Response getDeviceInfo(@PathParam("type") String type, @PathParam("id") String id, + @HeaderParam("If-Modified-Since") String timestamp) { + DeviceInformationManager informationManager; + DeviceInfo deviceInfo; + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(id); + deviceIdentifier.setType(type); + informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); + deviceInfo = informationManager.getDeviceInfo(deviceIdentifier); + } catch (DeviceDetailsMgtException e) { + String msg = "Error occurred while getting the device information."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(deviceInfo).build(); + } + @POST @Override public Response getDevicesInfo( @@ -152,6 +172,23 @@ public class DeviceManagementServiceImpl implements DeviceManagementService{ return Response.status(Response.Status.OK).entity(deviceLocation).build(); } + @POST + @Path("/locations") + public Response getDeviceLocations(List deviceIdentifiers, + @HeaderParam("If-Modified-Since") String ifModifiedSince) { + DeviceInformationManager informationManager; + List deviceLocations; + try { + informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); + deviceLocations = informationManager.getDeviceLocations(deviceIdentifiers); + } catch (DeviceDetailsMgtException e) { + String msg = "Error occurred while getting the device location."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(deviceLocations).build(); + } + @GET @Path("/{type}/{id}/features") @Override @@ -180,7 +217,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService{ @POST @Path("/search-devices") @Override - public Response searchDevices(@QueryParam("offset") int offset, int limit, SearchContext searchContext) { + public Response searchDevices(@QueryParam("offset") int offset, @QueryParam("limit") int limit, SearchContext searchContext) { SearchManagerService searchManagerService; List devices; try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index 5b62a0f8dd..1c947b6372 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -72,6 +72,14 @@ public interface DeviceInformationManager { */ DeviceLocation getDeviceLocation(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException; + /** + * This method will return the device location with latitude, longitude, address etc.. of supplied devices. + * @param deviceIdentifiers - List of Device identifier and device type. + * @return Device Location list. + * @throws DeviceDetailsMgtException + */ + List getDeviceLocations(List deviceIdentifiers) throws DeviceDetailsMgtException; + // /** // * This method will manage the storing of device application list. // * @param deviceApplication - Device application list. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index b2266d35cd..bb269cc975 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -183,5 +183,26 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } + @Override + public List getDeviceLocations(List deviceIdentifiers) throws DeviceDetailsMgtException { + + try { + List devices = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider().getAllDevices(deviceIdentifiers.get(0).getType()); + List deviceLocations = new ArrayList<>(); + DeviceManagementDAOFactory.openConnection(); + for (Device device : devices) { + deviceLocations.add(deviceDetailsDAO.getDeviceLocation(device.getId())); + } + return deviceLocations; + } catch (DeviceManagementException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving the devices.", e); + } catch (SQLException e) { + throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); + } catch (DeviceDetailsMgtDAOException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving device locations.", e); + } + } + }