From 3d416da6fe050fa266f1aa5bdd0f7b3f8253638c Mon Sep 17 00:00:00 2001 From: madhawap Date: Tue, 14 Jun 2016 12:03:14 +0530 Subject: [PATCH 1/2] add changes to 'since' QueryParam and 'if-modified-since' HeaderParam --- .../api/ActivityInfoProviderService.java | 4 +- .../impl/ActivityProviderServiceImpl.java | 40 ++++++++++++------- 2 files changed, 28 insertions(+), 16 deletions(-) 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 d7f91d9c1d6..3cdc31ed078 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 @@ -150,11 +150,11 @@ public interface ActivityInfoProviderService { @Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"}) Response getActivities( @ApiParam( - name = "timestamp", + name = "since", value = "Validates if the requested variant has not been modified since the time specified, this " + "should be provided in unix format in seconds.", required = false) - @QueryParam("timestamp") long timestamp, + @QueryParam("since") String since, @ApiParam( name = "offset", value = "Starting point within the complete list of items qualified.", 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 7b24aac133d..5e28b29a47f 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 @@ -74,42 +74,54 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService @GET @Override public Response getActivities( - @QueryParam("timestamp") long timestamp, + @QueryParam("since") String since, @QueryParam("offset") int offset, @QueryParam("limit") int limit, @HeaderParam("If-Modified-Since") String ifModifiedSince) { - long sinceTimestamp = 0; - boolean isSinceModifiedIsSet = false; + long ifModifiedSinceTimestamp = 0; + long sinceTimestamp =0; + boolean isIfModifiedSinceSet = false; if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { + Date sinceDate; + SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); try { - SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); - Date sinceDate = format.parse(ifModifiedSince); - sinceTimestamp = sinceDate.getTime(); + sinceDate = format.parse(ifModifiedSince); } catch (ParseException e) { throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) - .setMessage("Invalid date " + "string is provided in 'If-Modified-Since' header").build()); + .setMessage("Invalid date string is provided in 'If-Modified-Since' header").build()); } + ifModifiedSinceTimestamp = sinceDate.getTime(); } - if (sinceTimestamp > timestamp) { - timestamp = sinceTimestamp; - isSinceModifiedIsSet = true; + if (since != null && !since.isEmpty()){ + Date sinceDate; + SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); + try{ + sinceDate = format.parse(since); + }catch (ParseException e){ + throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) + .setMessage("Invalid date string is provided in 'since' filter").build()); + } + sinceTimestamp = sinceDate.getTime(); + } + if (ifModifiedSinceTimestamp >= sinceTimestamp) { + sinceTimestamp = ifModifiedSinceTimestamp; + isIfModifiedSinceSet = true; } List activities; DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - activities = dmService.getActivitiesUpdatedAfter(timestamp); + activities = dmService.getActivitiesUpdatedAfter(sinceTimestamp); if (activities == null || activities.size() == 0) { - if (isSinceModifiedIsSet) { + if (isIfModifiedSinceSet) { return Response.status(Response.Status.NOT_MODIFIED).entity("No activities " + - "after the timestamp provided in 'If-Modified-Since' header").build(); + "after the time provided in 'If-Modified-Since' header").build(); } throw new NotFoundException( new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No activities " + "found.").build()); } - } catch (OperationManagementException e) { String msg = "ErrorResponse occurred while fetching the activities updated after given time stamp."; log.error(msg, e); From 59119f9167519e3a4b167f524e41b661f25d112d Mon Sep 17 00:00:00 2001 From: madhawap Date: Tue, 14 Jun 2016 18:08:43 +0530 Subject: [PATCH 2/2] add changes to 'since' QueryParam and 'if-modified-since' HeaderParam --- .../jaxrs/service/impl/ActivityProviderServiceImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 5e28b29a47f..fc26db284d2 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 @@ -83,15 +83,15 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService long sinceTimestamp =0; boolean isIfModifiedSinceSet = false; if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { - Date sinceDate; + Date ifSinceDate; SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); try { - sinceDate = format.parse(ifModifiedSince); + ifSinceDate = format.parse(ifModifiedSince); } catch (ParseException e) { throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) .setMessage("Invalid date string is provided in 'If-Modified-Since' header").build()); } - ifModifiedSinceTimestamp = sinceDate.getTime(); + ifModifiedSinceTimestamp = ifSinceDate.getTime(); } if (since != null && !since.isEmpty()){ Date sinceDate; @@ -112,7 +112,7 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - activities = dmService.getActivitiesUpdatedAfter(sinceTimestamp); + activities = dmService.getActivitiesUpdatedAfter(sinceTimestamp/1000); if (activities == null || activities.size() == 0) { if (isIfModifiedSinceSet) { return Response.status(Response.Status.NOT_MODIFIED).entity("No activities " +