From a23f44e68291de7f7be1678cbb830fc279c20e70 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 27 Jun 2016 15:04:15 +0530 Subject: [PATCH] Improving AcitivityManagement API implementation with proper error handling, etc --- .../api/ActivityInfoProviderService.java | 9 ++-- .../impl/ActivityProviderServiceImpl.java | 49 +++++++++---------- 2 files changed, 29 insertions(+), 29 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 3cdc31ed07..275a31cd07 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 @@ -78,7 +78,7 @@ public interface ActivityInfoProviderService { response = ErrorResponse.class), @ApiResponse( code = 401, - message = ". \n Invalid request or validation error."), + message = "Unauthorized. \n Unauthorized request."), @ApiResponse( code = 404, message = "Not Found. \n No activity is found under the provided id.", @@ -88,7 +88,7 @@ public interface ActivityInfoProviderService { message = "Not Acceptable.\n The requested media type is not supported"), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n Server error occurred while fetching activity data.", + message = "Internal Server Error. \n Server error occurred while fetching activity data.", response = ErrorResponse.class) }) @Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"}) @@ -135,6 +135,9 @@ public interface ActivityInfoProviderService { code = 304, message = "Not Modified. \n Empty body because the client has already the latest version of " + "the requested resource."), + @ApiResponse( + code = 401, + message = "Unauthorized. \n Unauthorized request."), @ApiResponse( code = 404, message = "Not Found. \n No activities found.", @@ -144,7 +147,7 @@ public interface ActivityInfoProviderService { message = "Not Acceptable.\n The requested media type is not supported"), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n Server error occurred while fetching activity data.", + message = "Internal Server Error. \n Server error occurred while fetching activity data.", response = ErrorResponse.class) }) @Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"}) 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 848b0749a7..444854e1e4 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 @@ -26,8 +26,7 @@ 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.*; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.*; @@ -58,17 +57,17 @@ 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 " + - "found upon the provided activity id '" + id + "'").build()); + return Response.status(404).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("No activity can be " + + "found upon the provided activity id '" + id + "'").build()).build(); } + return Response.status(Response.Status.OK).entity(activity).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()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } - return Response.status(Response.Status.OK).entity(activity).build(); } @GET @@ -77,19 +76,21 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService @QueryParam("limit") int limit, @HeaderParam("If-Modified-Since") String ifModifiedSince) { - long ifModifiedSinceTimestamp = 0; - long sinceTimestamp = 0; + long ifModifiedSinceTimestamp; + long sinceTimestamp; 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"); try { 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()); + return Response.status(400).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage( + "Invalid date string is provided in 'If-Modified-Since' header").build()).build(); } ifModifiedSinceTimestamp = ifSinceDate.getTime(); isIfModifiedSinceSet = true; @@ -100,13 +101,15 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService 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()); + return Response.status(400).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage( + "Invalid date string is provided in 'since' filter").build()).build(); } sinceTimestamp = sinceDate.getTime(); isSinceSet = true; timestamp = sinceTimestamp / 1000; } + List activities; ActivityList activityList = new ActivityList(); DeviceManagementProviderService dmService; @@ -117,24 +120,18 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService int count = dmService.getActivityCountUpdatedAfter(timestamp); activityList.setCount(count); 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(); + if (isIfModifiedSinceSet || isSinceSet) { + return Response.notModified().build(); } - throw new NotFoundException(new ErrorResponse.ErrorResponseBuilder().setCode(404l) - .setMessage("No activities " + "found.").build()); } + return Response.ok().entity(activityList).build(); } catch (OperationManagementException e) { 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()); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } - return Response.status(Response.Status.OK).entity(activityList).build(); } + }