From b3f95d19c1398c49c29591b320d2c2c84ae82e06 Mon Sep 17 00:00:00 2001 From: madhawap Date: Wed, 15 Jun 2016 12:05:44 +0530 Subject: [PATCH 1/2] add changes to 'since' QueryParam and 'if-modified-since' HeaderParam --- .../impl/ActivityProviderServiceImpl.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 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 fc26db284d..d6b650c9aa 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 @@ -81,7 +81,9 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService long ifModifiedSinceTimestamp = 0; long sinceTimestamp =0; + long timestamp =0; boolean isIfModifiedSinceSet = false; + boolean isSinceSet = false; if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { Date ifSinceDate; SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); @@ -92,8 +94,9 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService .setMessage("Invalid date string is provided in 'If-Modified-Since' header").build()); } ifModifiedSinceTimestamp = ifSinceDate.getTime(); - } - if (since != null && !since.isEmpty()){ + isIfModifiedSinceSet = true; + timestamp = ifModifiedSinceTimestamp/1000; + } else if (since != null && !since.isEmpty()){ Date sinceDate; SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); try{ @@ -103,20 +106,21 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService .setMessage("Invalid date string is provided in 'since' filter").build()); } sinceTimestamp = sinceDate.getTime(); - } - if (ifModifiedSinceTimestamp >= sinceTimestamp) { - sinceTimestamp = ifModifiedSinceTimestamp; - isIfModifiedSinceSet = true; + isSinceSet = true; + timestamp = sinceTimestamp/1000; } List activities; DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - activities = dmService.getActivitiesUpdatedAfter(sinceTimestamp/1000); + activities = dmService.getActivitiesUpdatedAfter(timestamp); if (activities == null || activities.size() == 0) { if (isIfModifiedSinceSet) { return Response.status(Response.Status.NOT_MODIFIED).entity("No activities " + "after the time provided in 'If-Modified-Since' header").build(); + } else if(isSinceSet){ + return Response.status(Response.Status.NOT_MODIFIED).entity("No activities " + + "after the time provided in 'since' filter").build(); } throw new NotFoundException( new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No activities " + From 015ca0575e11e7ff0636b7741bd36dc9ccf2716e Mon Sep 17 00:00:00 2001 From: madhawap Date: Fri, 17 Jun 2016 17:36:50 +0530 Subject: [PATCH 2/2] added 'updatedTimeStamp' to the Activity get response --- .../impl/ActivityProviderServiceImpl.java | 60 +++++++++---------- .../mgt/dao/impl/GenericOperationDAOImpl.java | 8 +++ 2 files changed, 38 insertions(+), 30 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 d6b650c9aa..90ba5f259a 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 @@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.beans.ActivityList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.ActivityInfoProviderService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*; @@ -47,9 +48,8 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService @GET @Override @Path("/{id}") - public Response getActivity( - @PathParam("id") String id, - @HeaderParam("If-Modified-Since") String ifModifiedSince) { + public Response getActivity(@PathParam("id") String id, + @HeaderParam("If-Modified-Since") String ifModifiedSince) { Activity activity; DeviceManagementProviderService dmService; try { @@ -58,30 +58,28 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService dmService = DeviceMgtAPIUtils.getDeviceManagementService(); activity = dmService.getOperationByActivityId(id); if (activity == null) { - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No activity can be " + + throw new NotFoundException(new ErrorResponse.ErrorResponseBuilder().setCode(404l) + .setMessage("No activity can be " + "found upon the provided activity id '" + id + "'").build()); } } catch (OperationManagementException e) { String msg = "ErrorResponse occurred while fetching the activity for the supplied id."; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + throw new UnexpectedServerErrorException(new ErrorResponse.ErrorResponseBuilder().setCode(500l) + .setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(activity).build(); } @GET @Override - public Response getActivities( - @QueryParam("since") String since, - @QueryParam("offset") int offset, - @QueryParam("limit") int limit, - @HeaderParam("If-Modified-Since") String ifModifiedSince) { + public Response getActivities(@QueryParam("since") String since, @QueryParam("offset") int offset, + @QueryParam("limit") int limit, + @HeaderParam("If-Modified-Since") String ifModifiedSince) { long ifModifiedSinceTimestamp = 0; - long sinceTimestamp =0; - long timestamp =0; + long sinceTimestamp = 0; + long timestamp = 0; boolean isIfModifiedSinceSet = false; boolean isSinceSet = false; if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { @@ -95,42 +93,44 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService } ifModifiedSinceTimestamp = ifSinceDate.getTime(); isIfModifiedSinceSet = true; - timestamp = ifModifiedSinceTimestamp/1000; - } else if (since != null && !since.isEmpty()){ + timestamp = ifModifiedSinceTimestamp / 1000; + } else if (since != null && !since.isEmpty()) { Date sinceDate; SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); - try{ + try { sinceDate = format.parse(since); - }catch (ParseException e){ + } catch (ParseException e) { throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) .setMessage("Invalid date string is provided in 'since' filter").build()); } sinceTimestamp = sinceDate.getTime(); isSinceSet = true; - timestamp = sinceTimestamp/1000; + timestamp = sinceTimestamp / 1000; } List activities; + ActivityList activityList = new ActivityList(); DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); activities = dmService.getActivitiesUpdatedAfter(timestamp); if (activities == null || activities.size() == 0) { if (isIfModifiedSinceSet) { - return Response.status(Response.Status.NOT_MODIFIED).entity("No activities " + - "after the time provided in 'If-Modified-Since' header").build(); - } else if(isSinceSet){ - return Response.status(Response.Status.NOT_MODIFIED).entity("No activities " + - "after the time provided in 'since' filter").build(); + return Response.status(Response.Status.NOT_MODIFIED).entity( + "No activities " + "after the time provided in 'If-Modified-Since' header") + .build(); + } else if (isSinceSet) { + return Response.status(Response.Status.NOT_MODIFIED).entity( + "No activities " + "after the time provided in 'since' filter").build(); } - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No activities " + - "found.").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."; + String msg + = "ErrorResponse occurred while fetching the activities updated after given time stamp."; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + throw new UnexpectedServerErrorException(new ErrorResponse.ErrorResponseBuilder().setCode(500l) + .setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(activities).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index fc7751cda2..811c6314e3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; +import org.apache.axis2.databinding.types.soapencoding.Integer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -305,6 +306,7 @@ public class GenericOperationDAOImpl implements OperationDAO { List operationResponses = new ArrayList<>(); if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp(String.valueOf(rs.getInt("UPDATED_TIMESTAMP"))); operationResponses.add(this.getOperationResponse(rs)); } activityStatus.setResponses(operationResponses); @@ -385,6 +387,9 @@ public class GenericOperationDAOImpl implements OperationDAO { activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp(String.valueOf(rs.getInt("UPDATED_TIMESTAMP"))); + } if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { operationResponses.add(this.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); @@ -411,6 +416,9 @@ public class GenericOperationDAOImpl implements OperationDAO { activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp(String.valueOf(rs.getInt("UPDATED_TIMESTAMP"))); + } if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { operationResponses.add(this.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID");