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 c5f2fb728c5..7ae9974bfdb 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 @@ -25,8 +25,8 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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.RequestValidationUtil; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; +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.util.DeviceMgtAPIUtils; import javax.ws.rs.*; @@ -55,8 +55,9 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService dmService = DeviceMgtAPIUtils.getDeviceManagementService(); activity = dmService.getOperationByActivityId(id); if (activity == null) { - return Response.status(Response.Status.NOT_FOUND).entity("No activity can be found upon the provided " + - "activity id '" + id + "'").build(); + 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."; @@ -74,11 +75,16 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset, @QueryParam("limit") int limit) { - List activities = null; + List activities; DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); activities = dmService.getActivitiesUpdatedAfter(timestamp); + if (activities == null || activities.size() == 0) { + 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."; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java index 4a2126f6aae..4cde53bad1a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ConfigurationServiceImpl.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManageme import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.ConfigurationManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.MDMAppConstants; @@ -74,6 +75,7 @@ public class ConfigurationServiceImpl implements ConfigurationManagementService @Override public Response updateConfiguration(PlatformConfiguration config) { try { + RequestValidationUtil.validateUpdateConfiguration(config); DeviceMgtAPIUtils.getPlatformConfigurationManagementService().saveConfiguration(config, MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH); //Schedule the task service 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 75e886cb92d..6fc28510ab1 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 @@ -38,8 +38,8 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; +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.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; @@ -90,8 +90,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { result = dms.getAllDevices(request); } if (result == null) { - return Response.status(Response.Status.NOT_FOUND).entity("No device is currently enrolled " + - "with the server").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device is currently" + + " enrolled with the server").build()); } DeviceList devices = new DeviceList(); devices.setList((List) result.getData()); @@ -120,8 +121,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); deviceInfo = informationManager.getDeviceInfo(deviceIdentifier); if (deviceInfo == null) { - return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is " + - "found upon the give type '" + type + "' and id '" + id + "'").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that no " + + "device is found upon the give type '" + type + "' and id '" + id + "'").build()); } } catch (DeviceDetailsMgtException e) { String msg = "Error occurred while getting the device information."; @@ -174,8 +176,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } if (device == null) { - return Response.status(Response.Status.NOT_FOUND).entity("Requested device of type '" + type + - "', which carries id '" + id + "' does not exist").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Requested device of type '" + + type + "', which carries id '" + id + "' does not exist").build()); } return Response.status(Response.Status.OK).entity(device).build(); } @@ -196,8 +199,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { deviceLocation = informationManager.getDeviceLocation(new DeviceIdentifier(id, type)); if (deviceLocation == null || deviceLocation.getLatitude() == null || deviceLocation.getLongitude() == null) { - return Response.status(Response.Status.NOT_FOUND).entity("Location details are not available for the " + - "given device id '" + id + "'").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Location details " + + "are not available for the given device id '" + id + "'").build()); } } catch (DeviceDetailsMgtException e) { String msg = "Error occurred while getting the last updated location of the '" + type + "' device, " + @@ -215,9 +219,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @HeaderParam("If-Modified-Since") String ifModifiedSince) { DeviceInformationManager informationManager; List deviceLocations; + RequestValidationUtil.validateDeviceIdentifiers(deviceIdentifiers); try { informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); deviceLocations = informationManager.getDeviceLocations(deviceIdentifiers); + if (deviceLocations == null || deviceLocations.size() == 0) { + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No location related " + + "details were found.").build()); + } } catch (DeviceDetailsMgtException e) { String msg = "Error occurred while getting the device location."; log.error(msg, e); @@ -242,8 +252,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { dms = DeviceMgtAPIUtils.getDeviceManagementService(); FeatureManager fm = dms.getFeatureManager(type); if (fm == null) { - return Response.status(Response.Status.NOT_FOUND).entity("No feature manager is registered with " + - "the given type '" + type + "'").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No feature manager is " + + "registered with the given type '" + type + "'").build()); } features = fm.getFeatures(); } catch (DeviceManagementException e) { @@ -263,6 +274,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("limit") int limit, SearchContext searchContext) { SearchManagerService searchManagerService; List devices; + if(searchContext == null) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Search context is " + + "empty.").build()); + } try { searchManagerService = DeviceMgtAPIUtils.getSearchManagerService(); devices = searchManagerService.search(searchContext); @@ -273,8 +289,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } if (devices == null) { - return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + - "the provided type and id").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that no device " + + "is found upon the provided type and id").build()); } return Response.status(Response.Status.OK).entity(devices).build(); } @@ -296,8 +313,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { amc = DeviceMgtAPIUtils.getAppManagementService(); applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type)); if (applications == null) { - return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon" + - " the provided type and id").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that " + + "no applications is found upon the provided type and id").build()); } } catch (ApplicationManagementException e) { String msg = "Error occurred while fetching the apps of the '" + type + "' device, which carries " + @@ -326,8 +344,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { dms = DeviceMgtAPIUtils.getDeviceManagementService(); operations = dms.getOperations(new DeviceIdentifier(id, type)); if (operations == null) { - Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + - "the provided type and id"); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that" + + " no operation is found upon the provided type and id").build()); } } catch (OperationManagementException e) { String msg = "Error occurred while fetching the operations for the '" + type + "' device, which " + @@ -351,8 +370,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); Policy policy = policyManagementService.getAppliedPolicyToDevice(new DeviceIdentifier(id, type)); if (policy == null) { - return Response.status(Response.Status.NOT_FOUND).entity("No policy has been found for the '" + - type + "' device, which carries the id '" + id + "'").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policy has " + + "been found for the '" + type + "' device, which carries the id '" + id + "'").build()); } return Response.status(Response.Status.OK).entity(policy).build(); } catch (PolicyManagementException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java index 077996f0b7b..8d7a2a72b12 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java @@ -25,8 +25,8 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagement import org.wso2.carbon.device.mgt.jaxrs.NotificationContext; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.NotificationManagementService; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; +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.util.DeviceMgtAPIUtils; import javax.ws.rs.*; @@ -59,9 +59,10 @@ public class NotificationManagementServiceImpl implements NotificationManagement notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications(); } - if (notifications == null) { - return Response.status(Response.Status.NOT_FOUND).entity("No notification is available to be " + - "retrieved").build(); + if (notifications == null || notifications.size() == 0) { + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No notification is " + + "available to be retrieved.").build()); } return Response.status(Response.Status.OK).entity(notifications).build(); } catch (NotificationManagementException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index faf2b02b1e2..1914a3a6b44 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -26,8 +26,11 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService; +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.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil; import org.wso2.carbon.policy.mgt.common.Policy; @@ -50,6 +53,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @POST @Override public Response addPolicy(PolicyWrapper policyWrapper) { + RequestValidationUtil.validatePolicyDetails(policyWrapper); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); Policy policy = this.getPolicyFromWrapper(policyWrapper); @@ -63,13 +67,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { String username = threadLocalCarbonContext.getUsername(); try { if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) { - return Response.status(Response.Status.UNAUTHORIZED).entity("Current logged in user is " + - "not authorized to add policies").build(); + throw new UnauthorizedAccessException( + new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage("Current logged in user " + + "is not authorized to add policies").build()); } } catch (DeviceAccessAuthorizationException e) { String msg = "ErrorResponse occurred while checking if the current user is authorized to add a policy"; log.error(msg, e); - return javax.ws.rs.core.Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } try { @@ -79,7 +85,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while adding policy"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -109,12 +116,14 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); policies = policyAdministratorPoint.getPolicies(); if (policies == null || policies.size() == 0) { - return Response.status(Response.Status.NOT_FOUND).entity("No policies found.").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policies found.").build()); } } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while retrieving all available policies"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(policies).build(); } @@ -129,12 +138,14 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); policy = policyAdministratorPoint.getPolicy(id); if (policy == null) { - return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policy found.").build()); } } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while retrieving policy corresponding to the id '" + id + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(policy).build(); } @@ -143,22 +154,25 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @Path("/{id}") @Override public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) { + RequestValidationUtil.validatePolicyDetails(policyWrapper); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); Policy policy = this.getPolicyFromWrapper(policyWrapper); try { PolicyAdministratorPoint pap = policyManagementService.getPAP(); pap.updatePolicy(policy); - return Response.status(Response.Status.OK).entity("Policy has successfully been updated").build(); + return Response.status(Response.Status.OK).entity("Policy has successfully been updated.").build(); } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while updating the policy"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @POST @Override public Response removePolicies(List policyIds) { + RequestValidationUtil.validatePolicyIds(policyIds); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); boolean policyDeleted = true; try { @@ -172,13 +186,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while removing policies"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } if (policyDeleted) { return Response.status(Response.Status.OK).entity("Policies have been successfully deleted").build(); } else { //TODO:Check of this logic is correct - return Response.status(Response.Status.NOT_FOUND).entity("Policy doesn't exist").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Policy doesn't exist").build()); } } @@ -186,6 +202,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @Path("/activate-policy") @Override public Response activatePolicies(List policyIds) { + RequestValidationUtil.validatePolicyIds(policyIds); try { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyAdministratorPoint pap = policyManagementService.getPAP(); @@ -195,7 +212,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while activating policies"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated").build(); } @@ -204,6 +222,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @Path("/deactivate-policy") @Override public Response deactivatePolicies(List policyIds) { + RequestValidationUtil.validatePolicyIds(policyIds); try { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyAdministratorPoint pap = policyManagementService.getPAP(); @@ -213,7 +232,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } catch (PolicyManagementException e) { String msg = "Exception in inactivating policies."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity("Selected policies have been successfully " + "deactivated").build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java index 2007821939a..e301c0fa68c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -22,7 +22,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.RoleManagementService; +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.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper; import org.wso2.carbon.device.mgt.jaxrs.util.SetReferenceTransformer; @@ -57,12 +60,14 @@ public class RoleManagementServiceImpl implements RoleManagementService { try { filteredRoles = getRolesFromUserStore(); if (filteredRoles == null || filteredRoles.size() == 0) { - return Response.status(Response.Status.NOT_FOUND).entity("No roles found.").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No roles found.").build()); } } catch (UserStoreException e) { String msg = "ErrorResponse occurred while retrieving roles from the underlying user stores"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(filteredRoles).build(); } @@ -73,6 +78,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { public Response getPermissionsOfRole( @PathParam("roleName") String roleName, @HeaderParam("If-Modified-Since") String ifModifiedSince) { + RequestValidationUtil.validateRoleName(roleName); try { final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); org.wso2.carbon.user.core.UserRealm userRealmCore = null; @@ -83,19 +89,22 @@ public class RoleManagementServiceImpl implements RoleManagementService { final UserRealmProxy userRealmProxy = new UserRealmProxy(userRealmCore); rolePermissions = this.getUIPermissionNode(roleName, userRealmProxy); if (rolePermissions == null) { - return Response.status(Response.Status.NOT_FOUND).entity("No permissions found for the role '" + - roleName + "'").build(); + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No permissions found" + + " for the role '" + roleName + "'").build()); } return Response.status(Response.Status.OK).entity(rolePermissions).build(); } catch (UserAdminException e) { String msg = "ErrorResponse occurred while retrieving the permissions of role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } catch (UserStoreException e) { String msg = "ErrorResponse occurred while retrieving the underlying user realm attached to the " + "current logged in user"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -125,6 +134,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { @Override public Response getRole(@PathParam("roleName") String roleName, @HeaderParam("If-Modified-Since") String ifModifiedSince) { + RequestValidationUtil.validateRoleName(roleName); RoleWrapper roleWrapper = new RoleWrapper(); try { final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); @@ -148,11 +158,16 @@ public class RoleManagementServiceImpl implements RoleManagementService { roleWrapper.setPermissionList(rolePermissions); String[] permListAr = new String[permList.size()]; roleWrapper.setPermissions(permList.toArray(permListAr)); + } else { + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Role name doesn't exist.") + .build()); } } catch (UserStoreException | UserAdminException e) { String msg = "ErrorResponse occurred while retrieving the user role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(roleWrapper).build(); } @@ -170,6 +185,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { @POST @Override public Response addRole(RoleWrapper roleWrapper) { + RequestValidationUtil.validateRoleName(roleWrapper.getRoleName()); try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (log.isDebugEnabled()) { @@ -188,7 +204,8 @@ public class RoleManagementServiceImpl implements RoleManagementService { } catch (UserStoreException e) { String msg = "ErrorResponse occurred while adding role '" + roleWrapper.getRoleName() + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).build(); } @@ -197,6 +214,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { @Path("/{roleName}") @Override public Response updateRole(@PathParam("roleName") String roleName, RoleWrapper roleWrapper) { + RequestValidationUtil.validateRoleName(roleName); String newRoleName = roleWrapper.getRoleName(); try { final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); @@ -230,7 +248,8 @@ public class RoleManagementServiceImpl implements RoleManagementService { } catch (UserStoreException e) { String msg = "ErrorResponse occurred while updating role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).build(); } @@ -239,6 +258,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { @Path("/{roleName}") @Override public Response deleteRole(@PathParam("roleName") String roleName) { + RequestValidationUtil.validateRoleName(roleName); try { final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final AuthorizationManager authorizationManager = DeviceMgtAPIUtils.getAuthorizationManager(); @@ -251,7 +271,8 @@ public class RoleManagementServiceImpl implements RoleManagementService { } catch (UserStoreException e) { String msg = "ErrorResponse occurred while deleting the role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).build(); } @@ -260,6 +281,8 @@ public class RoleManagementServiceImpl implements RoleManagementService { @Path("/{roleName}/users") @Override public Response updateUsersOfRole(@PathParam("roleName") String roleName, List users) { + RequestValidationUtil.validateRoleName(roleName); + RequestValidationUtil.validateUsers(users); try { final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (log.isDebugEnabled()) { @@ -277,7 +300,8 @@ public class RoleManagementServiceImpl implements RoleManagementService { } catch (UserStoreException e) { String msg = "ErrorResponse occurred while updating the users of the role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 260b4df2911..a395c131bfb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -26,9 +26,12 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.UserCredentialWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.ConflictException; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; @@ -62,8 +65,10 @@ public class UserManagementServiceImpl implements UserManagementService { " already exists. Therefore, request made to add user was refused."); } // returning response with bad request state - return Response.status(Response.Status.CONFLICT).entity("User by username: " + userWrapper.getUsername() + - " already exists. Therefore, request made to add user was refused.").build(); + throw new ConflictException( + new ErrorResponse.ErrorResponseBuilder().setCode(409l).setMessage("User by username: " + + userWrapper.getUsername() + " already exists. Therefore, request made to add user " + + "was refused.").build()); } else { String initialUserPassword = this.generateInitialUserPassword(); Map defaultUserClaims = @@ -85,11 +90,13 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "Exception in trying to add user '" + userWrapper.getUsername() + "' to the user store"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } catch (DeviceManagementException e) { String msg = "ErrorResponse occurred while inviting user to enroll the device"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -187,7 +194,8 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "ErrorResponse occurred while retrieving information of the user '" + username + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -245,7 +253,8 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException | UnsupportedEncodingException e) { String msg = "Exception in trying to update user by username: " + userWrapper.getUsername(); log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -293,7 +302,8 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "Exception in trying to remove user by username: " + username; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -317,7 +327,8 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "Exception in trying to retrieve roles for user by username: " + username; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -350,7 +361,8 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "ErrorResponse occurred while retrieving the list of users"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -383,7 +395,8 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "ErrorResponse occurred while retrieving the list of users using the filter : " + filter; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/ConflictException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/ConflictException.java new file mode 100644 index 00000000000..6afbf6f50aa --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/ConflictException.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.service.impl.util; + +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +public class ConflictException extends WebApplicationException { + + private static final long serialVersionUID = 14234272342342340L; + + public ConflictException(ErrorResponse error) { + super(Response.status(Response.Status.CONFLICT).entity(error).build()); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/NotFoundException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/NotFoundException.java new file mode 100644 index 00000000000..a427e942b9a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/NotFoundException.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.service.impl.util; + +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +public class NotFoundException extends WebApplicationException { + + private static final long serialVersionUID = 147943572342342340L; + + public NotFoundException(ErrorResponse error) { + super(Response.status(Response.Status.NOT_FOUND).entity(error).build()); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index 2d437f411c6..6fb6120dfd5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -18,9 +18,12 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl.util; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import java.util.ArrayList; import java.util.List; @@ -245,4 +248,55 @@ public class RequestValidationUtil { } } + public static void validateUpdateConfiguration(PlatformConfiguration config) { + if (config == null) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Configurations are not defined.") + .build()); + } else if (config.getConfiguration() == null || config.getConfiguration().size() == 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Does not contain any " + + "configuration entries.").build()); + } + } + + public static void validateDeviceIdentifiers(List deviceIdentifiers) { + if (deviceIdentifiers == null || deviceIdentifiers.size() == 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Device identifier list is " + + "empty.").build()); + } + } + + public static void validatePolicyDetails(PolicyWrapper policyWrapper) { + if (policyWrapper == null) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy is empty.").build()); + } + } + + public static void validatePolicyIds(List policyIds) { + if (policyIds == null || policyIds.size() == 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy Id list is empty.").build + ()); + } + } + + public static void validateRoleName(String roleName) { + if (roleName == null || roleName.isEmpty()) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Role name isn't valid.").build + ()); + } + } + + public static void validateUsers(List users) { + if (users == null || users.size() == 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("User list isn't valid.").build + ()); + } + } + }