Improving AcitivityManagement API implementation with proper error handling, etc

revert-70aa11f8
prabathabey 9 years ago
parent ad6b95ff62
commit a23f44e682

@ -78,7 +78,7 @@ public interface ActivityInfoProviderService {
response = ErrorResponse.class), response = ErrorResponse.class),
@ApiResponse( @ApiResponse(
code = 401, code = 401,
message = ". \n Invalid request or validation error."), message = "Unauthorized. \n Unauthorized request."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n No activity is found under the provided id.", 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"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, 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) response = ErrorResponse.class)
}) })
@Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"}) @Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"})
@ -135,6 +135,9 @@ public interface ActivityInfoProviderService {
code = 304, code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " + message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."), "the requested resource."),
@ApiResponse(
code = 401,
message = "Unauthorized. \n Unauthorized request."),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Not Found. \n No activities found.", 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"), message = "Not Acceptable.\n The requested media type is not supported"),
@ApiResponse( @ApiResponse(
code = 500, 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) response = ErrorResponse.class)
}) })
@Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"}) @Permission(scope = "activity-view", permissions = {"/permission/admin/device-mgt/admin/activities/view"})

@ -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.ActivityList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; 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.api.ActivityInfoProviderService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.*; import javax.ws.rs.*;
@ -58,17 +57,17 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
dmService = DeviceMgtAPIUtils.getDeviceManagementService(); dmService = DeviceMgtAPIUtils.getDeviceManagementService();
activity = dmService.getOperationByActivityId(id); activity = dmService.getOperationByActivityId(id);
if (activity == null) { if (activity == null) {
throw new NotFoundException(new ErrorResponse.ErrorResponseBuilder().setCode(404l) return Response.status(404).entity(
.setMessage("No activity can be " + new ErrorResponse.ErrorResponseBuilder().setMessage("No activity can be " +
"found upon the provided activity id '" + id + "'").build()); "found upon the provided activity id '" + id + "'").build()).build();
} }
return Response.status(Response.Status.OK).entity(activity).build();
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg = "ErrorResponse occurred while fetching the activity for the supplied id."; String msg = "ErrorResponse occurred while fetching the activity for the supplied id.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException(new ErrorResponse.ErrorResponseBuilder().setCode(500l) return Response.serverError().entity(
.setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
return Response.status(Response.Status.OK).entity(activity).build();
} }
@GET @GET
@ -77,19 +76,21 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
@QueryParam("limit") int limit, @QueryParam("limit") int limit,
@HeaderParam("If-Modified-Since") String ifModifiedSince) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
long ifModifiedSinceTimestamp = 0; long ifModifiedSinceTimestamp;
long sinceTimestamp = 0; long sinceTimestamp;
long timestamp = 0; long timestamp = 0;
boolean isIfModifiedSinceSet = false; boolean isIfModifiedSinceSet = false;
boolean isSinceSet = false; boolean isSinceSet = false;
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
Date ifSinceDate; Date ifSinceDate;
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
try { try {
ifSinceDate = format.parse(ifModifiedSince); ifSinceDate = format.parse(ifModifiedSince);
} catch (ParseException e) { } catch (ParseException e) {
throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) return Response.status(400).entity(
.setMessage("Invalid date string is provided in 'If-Modified-Since' header").build()); new ErrorResponse.ErrorResponseBuilder().setMessage(
"Invalid date string is provided in 'If-Modified-Since' header").build()).build();
} }
ifModifiedSinceTimestamp = ifSinceDate.getTime(); ifModifiedSinceTimestamp = ifSinceDate.getTime();
isIfModifiedSinceSet = true; isIfModifiedSinceSet = true;
@ -100,13 +101,15 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
try { try {
sinceDate = format.parse(since); sinceDate = format.parse(since);
} catch (ParseException e) { } catch (ParseException e) {
throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) return Response.status(400).entity(
.setMessage("Invalid date string is provided in 'since' filter").build()); new ErrorResponse.ErrorResponseBuilder().setMessage(
"Invalid date string is provided in 'since' filter").build()).build();
} }
sinceTimestamp = sinceDate.getTime(); sinceTimestamp = sinceDate.getTime();
isSinceSet = true; isSinceSet = true;
timestamp = sinceTimestamp / 1000; timestamp = sinceTimestamp / 1000;
} }
List<Activity> activities; List<Activity> activities;
ActivityList activityList = new ActivityList(); ActivityList activityList = new ActivityList();
DeviceManagementProviderService dmService; DeviceManagementProviderService dmService;
@ -117,24 +120,18 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
int count = dmService.getActivityCountUpdatedAfter(timestamp); int count = dmService.getActivityCountUpdatedAfter(timestamp);
activityList.setCount(count); activityList.setCount(count);
if (activities == null || activities.size() == 0) { if (activities == null || activities.size() == 0) {
if (isIfModifiedSinceSet) { if (isIfModifiedSinceSet || isSinceSet) {
return Response.status(Response.Status.NOT_MODIFIED).entity( return Response.notModified().build();
"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());
} }
return Response.ok().entity(activityList).build();
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg String msg
= "ErrorResponse occurred while fetching the activities updated after given time stamp."; = "ErrorResponse occurred while fetching the activities updated after given time stamp.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException(new ErrorResponse.ErrorResponseBuilder().setCode(500l) return Response.serverError().entity(
.setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
return Response.status(Response.Status.OK).entity(activityList).build();
} }
} }

Loading…
Cancel
Save