adding proper error messages for APIs

revert-70aa11f8
inoshperera 9 years ago
parent dfc673b08f
commit 5620e38681

@ -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.core.service.DeviceManagementProviderService;
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.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; 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.*;
@ -55,8 +55,9 @@ 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) {
return Response.status(Response.Status.NOT_FOUND).entity("No activity can be found upon the provided " + throw new NotFoundException(
"activity id '" + id + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No activity can be " +
"found upon the provided activity id '" + id + "'").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.";
@ -74,11 +75,16 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService
@HeaderParam("If-Modified-Since") String ifModifiedSince, @HeaderParam("If-Modified-Since") String ifModifiedSince,
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
List<Activity> activities = null; List<Activity> activities;
DeviceManagementProviderService dmService; DeviceManagementProviderService dmService;
try { try {
dmService = DeviceMgtAPIUtils.getDeviceManagementService(); dmService = DeviceMgtAPIUtils.getDeviceManagementService();
activities = dmService.getActivitiesUpdatedAfter(timestamp); 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) { } 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.";

@ -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.common.configuration.mgt.PlatformConfiguration;
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.ConfigurationManagementService; 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.service.impl.util.UnexpectedServerErrorException;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.util.MDMAppConstants; import org.wso2.carbon.device.mgt.jaxrs.util.MDMAppConstants;
@ -74,6 +75,7 @@ public class ConfigurationServiceImpl implements ConfigurationManagementService
@Override @Override
public Response updateConfiguration(PlatformConfiguration config) { public Response updateConfiguration(PlatformConfiguration config) {
try { try {
RequestValidationUtil.validateUpdateConfiguration(config);
DeviceMgtAPIUtils.getPlatformConfigurationManagementService().saveConfiguration(config, DeviceMgtAPIUtils.getPlatformConfigurationManagementService().saveConfiguration(config,
MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH); MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH);
//Schedule the task service //Schedule the task service

@ -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.DeviceList;
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.DeviceManagementService; 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.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; 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 org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
@ -90,8 +90,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
result = dms.getAllDevices(request); result = dms.getAllDevices(request);
} }
if (result == null) { if (result == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No device is currently enrolled " + throw new NotFoundException(
"with the server").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device is currently" +
" enrolled with the server").build());
} }
DeviceList devices = new DeviceList(); DeviceList devices = new DeviceList();
devices.setList((List<Device>) result.getData()); devices.setList((List<Device>) result.getData());
@ -120,8 +121,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceInfo = informationManager.getDeviceInfo(deviceIdentifier); deviceInfo = informationManager.getDeviceInfo(deviceIdentifier);
if (deviceInfo == null) { if (deviceInfo == null) {
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is " + throw new NotFoundException(
"found upon the give type '" + type + "' and id '" + id + "'").build(); 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) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device information."; 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()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
if (device == null) { if (device == null) {
return Response.status(Response.Status.NOT_FOUND).entity("Requested device of type '" + type + throw new NotFoundException(
"', which carries id '" + id + "' does not exist").build(); 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(); 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)); deviceLocation = informationManager.getDeviceLocation(new DeviceIdentifier(id, type));
if (deviceLocation == null || deviceLocation.getLatitude() == null || if (deviceLocation == null || deviceLocation.getLatitude() == null ||
deviceLocation.getLongitude() == null) { deviceLocation.getLongitude() == null) {
return Response.status(Response.Status.NOT_FOUND).entity("Location details are not available for the " + throw new NotFoundException(
"given device id '" + id + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Location details " +
"are not available for the given device id '" + id + "'").build());
} }
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the last updated location of the '" + type + "' device, " + 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) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
DeviceInformationManager informationManager; DeviceInformationManager informationManager;
List<DeviceLocation> deviceLocations; List<DeviceLocation> deviceLocations;
RequestValidationUtil.validateDeviceIdentifiers(deviceIdentifiers);
try { try {
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceLocations = informationManager.getDeviceLocations(deviceIdentifiers); 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) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device location."; String msg = "Error occurred while getting the device location.";
log.error(msg, e); log.error(msg, e);
@ -242,8 +252,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
dms = DeviceMgtAPIUtils.getDeviceManagementService(); dms = DeviceMgtAPIUtils.getDeviceManagementService();
FeatureManager fm = dms.getFeatureManager(type); FeatureManager fm = dms.getFeatureManager(type);
if (fm == null) { if (fm == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No feature manager is registered with " + throw new NotFoundException(
"the given type '" + type + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No feature manager is " +
"registered with the given type '" + type + "'").build());
} }
features = fm.getFeatures(); features = fm.getFeatures();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
@ -263,6 +274,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("limit") int limit, SearchContext searchContext) { @QueryParam("limit") int limit, SearchContext searchContext) {
SearchManagerService searchManagerService; SearchManagerService searchManagerService;
List<DeviceWrapper> devices; List<DeviceWrapper> devices;
if(searchContext == null) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Search context is " +
"empty.").build());
}
try { try {
searchManagerService = DeviceMgtAPIUtils.getSearchManagerService(); searchManagerService = DeviceMgtAPIUtils.getSearchManagerService();
devices = searchManagerService.search(searchContext); devices = searchManagerService.search(searchContext);
@ -273,8 +289,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} }
if (devices == null) { if (devices == null) {
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + throw new NotFoundException(
"the provided type and id").build(); 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(); return Response.status(Response.Status.OK).entity(devices).build();
} }
@ -296,8 +313,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
amc = DeviceMgtAPIUtils.getAppManagementService(); amc = DeviceMgtAPIUtils.getAppManagementService();
applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type)); applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type));
if (applications == null) { if (applications == null) {
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon" + throw new NotFoundException(
" the provided type and id").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that " +
"no applications is found upon the provided type and id").build());
} }
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while fetching the apps of the '" + type + "' device, which carries " + 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(); dms = DeviceMgtAPIUtils.getDeviceManagementService();
operations = dms.getOperations(new DeviceIdentifier(id, type)); operations = dms.getOperations(new DeviceIdentifier(id, type));
if (operations == null) { if (operations == null) {
Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + throw new NotFoundException(
"the provided type and id"); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that" +
" no operation is found upon the provided type and id").build());
} }
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg = "Error occurred while fetching the operations for the '" + type + "' device, which " + 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(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
Policy policy = policyManagementService.getAppliedPolicyToDevice(new DeviceIdentifier(id, type)); Policy policy = policyManagementService.getAppliedPolicyToDevice(new DeviceIdentifier(id, type));
if (policy == null) { if (policy == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No policy has been found for the '" + throw new NotFoundException(
type + "' device, which carries the id '" + id + "'").build(); 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(); return Response.status(Response.Status.OK).entity(policy).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {

@ -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.NotificationContext;
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.NotificationManagementService; 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.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; 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.*;
@ -59,9 +59,10 @@ public class NotificationManagementServiceImpl implements NotificationManagement
notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications(); notifications = DeviceMgtAPIUtils.getNotificationManagementService().getAllNotifications();
} }
if (notifications == null) { if (notifications == null || notifications.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No notification is available to be " + throw new NotFoundException(
"retrieved").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No notification is " +
"available to be retrieved.").build());
} }
return Response.status(Response.Status.OK).entity(notifications).build(); return Response.status(Response.Status.OK).entity(notifications).build();
} catch (NotificationManagementException e) { } catch (NotificationManagementException e) {

@ -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.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.beans.PolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService; 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.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
@ -50,6 +53,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@POST @POST
@Override @Override
public Response addPolicy(PolicyWrapper policyWrapper) { public Response addPolicy(PolicyWrapper policyWrapper) {
RequestValidationUtil.validatePolicyDetails(policyWrapper);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
Policy policy = this.getPolicyFromWrapper(policyWrapper); Policy policy = this.getPolicyFromWrapper(policyWrapper);
@ -63,13 +67,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
String username = threadLocalCarbonContext.getUsername(); String username = threadLocalCarbonContext.getUsername();
try { try {
if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) { if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) {
return Response.status(Response.Status.UNAUTHORIZED).entity("Current logged in user is " + throw new UnauthorizedAccessException(
"not authorized to add policies").build(); new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage("Current logged in user " +
"is not authorized to add policies").build());
} }
} catch (DeviceAccessAuthorizationException e) { } catch (DeviceAccessAuthorizationException e) {
String msg = "ErrorResponse occurred while checking if the current user is authorized to add a policy"; String msg = "ErrorResponse occurred while checking if the current user is authorized to add a policy";
log.error(msg, e); 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 { try {
@ -79,7 +85,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while adding policy"; String msg = "ErrorResponse occurred while adding policy";
log.error(msg, e); 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(); PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
policies = policyAdministratorPoint.getPolicies(); policies = policyAdministratorPoint.getPolicies();
if (policies == null || policies.size() == 0) { 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) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while retrieving all available policies"; String msg = "ErrorResponse occurred while retrieving all available policies";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).entity(policies).build();
} }
@ -129,12 +138,14 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
policy = policyAdministratorPoint.getPolicy(id); policy = policyAdministratorPoint.getPolicy(id);
if (policy == null) { 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) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while retrieving policy corresponding to the id '" + id + "'"; String msg = "ErrorResponse occurred while retrieving policy corresponding to the id '" + id + "'";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).entity(policy).build();
} }
@ -143,22 +154,25 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@Path("/{id}") @Path("/{id}")
@Override @Override
public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) { public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) {
RequestValidationUtil.validatePolicyDetails(policyWrapper);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
Policy policy = this.getPolicyFromWrapper(policyWrapper); Policy policy = this.getPolicyFromWrapper(policyWrapper);
try { try {
PolicyAdministratorPoint pap = policyManagementService.getPAP(); PolicyAdministratorPoint pap = policyManagementService.getPAP();
pap.updatePolicy(policy); 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) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while updating the policy"; String msg = "ErrorResponse occurred while updating the policy";
log.error(msg, e); 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 @POST
@Override @Override
public Response removePolicies(List<Integer> policyIds) { public Response removePolicies(List<Integer> policyIds) {
RequestValidationUtil.validatePolicyIds(policyIds);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
boolean policyDeleted = true; boolean policyDeleted = true;
try { try {
@ -172,13 +186,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while removing policies"; String msg = "ErrorResponse occurred while removing policies";
log.error(msg, e); 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) { if (policyDeleted) {
return Response.status(Response.Status.OK).entity("Policies have been successfully deleted").build(); return Response.status(Response.Status.OK).entity("Policies have been successfully deleted").build();
} else { } else {
//TODO:Check of this logic is correct //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") @Path("/activate-policy")
@Override @Override
public Response activatePolicies(List<Integer> policyIds) { public Response activatePolicies(List<Integer> policyIds) {
RequestValidationUtil.validatePolicyIds(policyIds);
try { try {
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
PolicyAdministratorPoint pap = policyManagementService.getPAP(); PolicyAdministratorPoint pap = policyManagementService.getPAP();
@ -195,7 +212,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while activating policies"; String msg = "ErrorResponse occurred while activating policies";
log.error(msg, e); 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(); 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") @Path("/deactivate-policy")
@Override @Override
public Response deactivatePolicies(List<Integer> policyIds) { public Response deactivatePolicies(List<Integer> policyIds) {
RequestValidationUtil.validatePolicyIds(policyIds);
try { try {
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
PolicyAdministratorPoint pap = policyManagementService.getPAP(); PolicyAdministratorPoint pap = policyManagementService.getPAP();
@ -213,7 +232,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "Exception in inactivating policies."; String msg = "Exception in inactivating policies.";
log.error(msg, e); 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 " + return Response.status(Response.Status.OK).entity("Selected policies have been successfully " +
"deactivated").build(); "deactivated").build();

@ -22,7 +22,10 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants; 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.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.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper;
import org.wso2.carbon.device.mgt.jaxrs.util.SetReferenceTransformer; import org.wso2.carbon.device.mgt.jaxrs.util.SetReferenceTransformer;
@ -57,12 +60,14 @@ public class RoleManagementServiceImpl implements RoleManagementService {
try { try {
filteredRoles = getRolesFromUserStore(); filteredRoles = getRolesFromUserStore();
if (filteredRoles == null || filteredRoles.size() == 0) { 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) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving roles from the underlying user stores"; String msg = "ErrorResponse occurred while retrieving roles from the underlying user stores";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).entity(filteredRoles).build();
} }
@ -73,6 +78,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
public Response getPermissionsOfRole( public Response getPermissionsOfRole(
@PathParam("roleName") String roleName, @PathParam("roleName") String roleName,
@HeaderParam("If-Modified-Since") String ifModifiedSince) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
RequestValidationUtil.validateRoleName(roleName);
try { try {
final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm();
org.wso2.carbon.user.core.UserRealm userRealmCore = null; org.wso2.carbon.user.core.UserRealm userRealmCore = null;
@ -83,19 +89,22 @@ public class RoleManagementServiceImpl implements RoleManagementService {
final UserRealmProxy userRealmProxy = new UserRealmProxy(userRealmCore); final UserRealmProxy userRealmProxy = new UserRealmProxy(userRealmCore);
rolePermissions = this.getUIPermissionNode(roleName, userRealmProxy); rolePermissions = this.getUIPermissionNode(roleName, userRealmProxy);
if (rolePermissions == null) { if (rolePermissions == null) {
return Response.status(Response.Status.NOT_FOUND).entity("No permissions found for the role '" + throw new NotFoundException(
roleName + "'").build(); new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No permissions found" +
" for the role '" + roleName + "'").build());
} }
return Response.status(Response.Status.OK).entity(rolePermissions).build(); return Response.status(Response.Status.OK).entity(rolePermissions).build();
} catch (UserAdminException e) { } catch (UserAdminException e) {
String msg = "ErrorResponse occurred while retrieving the permissions of role '" + roleName + "'"; String msg = "ErrorResponse occurred while retrieving the permissions of role '" + roleName + "'";
log.error(msg, e); 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) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving the underlying user realm attached to the " + String msg = "ErrorResponse occurred while retrieving the underlying user realm attached to the " +
"current logged in user"; "current logged in user";
log.error(msg, e); 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 @Override
public Response getRole(@PathParam("roleName") String roleName, public Response getRole(@PathParam("roleName") String roleName,
@HeaderParam("If-Modified-Since") String ifModifiedSince) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
RequestValidationUtil.validateRoleName(roleName);
RoleWrapper roleWrapper = new RoleWrapper(); RoleWrapper roleWrapper = new RoleWrapper();
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
@ -148,11 +158,16 @@ public class RoleManagementServiceImpl implements RoleManagementService {
roleWrapper.setPermissionList(rolePermissions); roleWrapper.setPermissionList(rolePermissions);
String[] permListAr = new String[permList.size()]; String[] permListAr = new String[permList.size()];
roleWrapper.setPermissions(permList.toArray(permListAr)); 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) { } catch (UserStoreException | UserAdminException e) {
String msg = "ErrorResponse occurred while retrieving the user role '" + roleName + "'"; String msg = "ErrorResponse occurred while retrieving the user role '" + roleName + "'";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).entity(roleWrapper).build();
} }
@ -170,6 +185,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@POST @POST
@Override @Override
public Response addRole(RoleWrapper roleWrapper) { public Response addRole(RoleWrapper roleWrapper) {
RequestValidationUtil.validateRoleName(roleWrapper.getRoleName());
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -188,7 +204,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while adding role '" + roleWrapper.getRoleName() + "'"; String msg = "ErrorResponse occurred while adding role '" + roleWrapper.getRoleName() + "'";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).build();
} }
@ -197,6 +214,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@Path("/{roleName}") @Path("/{roleName}")
@Override @Override
public Response updateRole(@PathParam("roleName") String roleName, RoleWrapper roleWrapper) { public Response updateRole(@PathParam("roleName") String roleName, RoleWrapper roleWrapper) {
RequestValidationUtil.validateRoleName(roleName);
String newRoleName = roleWrapper.getRoleName(); String newRoleName = roleWrapper.getRoleName();
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
@ -230,7 +248,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while updating role '" + roleName + "'"; String msg = "ErrorResponse occurred while updating role '" + roleName + "'";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).build();
} }
@ -239,6 +258,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@Path("/{roleName}") @Path("/{roleName}")
@Override @Override
public Response deleteRole(@PathParam("roleName") String roleName) { public Response deleteRole(@PathParam("roleName") String roleName) {
RequestValidationUtil.validateRoleName(roleName);
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
final AuthorizationManager authorizationManager = DeviceMgtAPIUtils.getAuthorizationManager(); final AuthorizationManager authorizationManager = DeviceMgtAPIUtils.getAuthorizationManager();
@ -251,7 +271,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while deleting the role '" + roleName + "'"; String msg = "ErrorResponse occurred while deleting the role '" + roleName + "'";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).build();
} }
@ -260,6 +281,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
@Path("/{roleName}/users") @Path("/{roleName}/users")
@Override @Override
public Response updateUsersOfRole(@PathParam("roleName") String roleName, List<String> users) { public Response updateUsersOfRole(@PathParam("roleName") String roleName, List<String> users) {
RequestValidationUtil.validateRoleName(roleName);
RequestValidationUtil.validateUsers(users);
try { try {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -277,7 +300,8 @@ public class RoleManagementServiceImpl implements RoleManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while updating the users of the role '" + roleName + "'"; String msg = "ErrorResponse occurred while updating the users of the role '" + roleName + "'";
log.error(msg, e); 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(); return Response.status(Response.Status.OK).build();
} }

@ -26,9 +26,12 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; 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.UserCredentialWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; 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.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.Constants;
import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; 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."); " already exists. Therefore, request made to add user was refused.");
} }
// returning response with bad request state // returning response with bad request state
return Response.status(Response.Status.CONFLICT).entity("User by username: " + userWrapper.getUsername() + throw new ConflictException(
" already exists. Therefore, request made to add user was refused.").build(); new ErrorResponse.ErrorResponseBuilder().setCode(409l).setMessage("User by username: " +
userWrapper.getUsername() + " already exists. Therefore, request made to add user " +
"was refused.").build());
} else { } else {
String initialUserPassword = this.generateInitialUserPassword(); String initialUserPassword = this.generateInitialUserPassword();
Map<String, String> defaultUserClaims = Map<String, String> defaultUserClaims =
@ -85,11 +90,13 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Exception in trying to add user '" + userWrapper.getUsername() + "' to the user store"; String msg = "Exception in trying to add user '" + userWrapper.getUsername() + "' to the user store";
log.error(msg, e); 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) { } catch (DeviceManagementException e) {
String msg = "ErrorResponse occurred while inviting user to enroll the device"; String msg = "ErrorResponse occurred while inviting user to enroll the device";
log.error(msg, e); 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) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving information of the user '" + username + "'"; String msg = "ErrorResponse occurred while retrieving information of the user '" + username + "'";
log.error(msg, e); 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) { } catch (UserStoreException | UnsupportedEncodingException e) {
String msg = "Exception in trying to update user by username: " + userWrapper.getUsername(); String msg = "Exception in trying to update user by username: " + userWrapper.getUsername();
log.error(msg, e); 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) { } catch (UserStoreException e) {
String msg = "Exception in trying to remove user by username: " + username; String msg = "Exception in trying to remove user by username: " + username;
log.error(msg, e); 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) { } catch (UserStoreException e) {
String msg = "Exception in trying to retrieve roles for user by username: " + username; String msg = "Exception in trying to retrieve roles for user by username: " + username;
log.error(msg, e); 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) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving the list of users"; String msg = "ErrorResponse occurred while retrieving the list of users";
log.error(msg, e); 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) { } catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving the list of users using the filter : " + filter; String msg = "ErrorResponse occurred while retrieving the list of users using the filter : " + filter;
log.error(msg, e); 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());
} }
} }

@ -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());
}
}

@ -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());
}
}

@ -18,9 +18,12 @@
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl.util; 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.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper; 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.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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<DeviceIdentifier> 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<Integer> 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<String> users) {
if (users == null || users.size() == 0) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("User list isn't valid.").build
());
}
}
} }

Loading…
Cancel
Save