Improving Device and Policy Management APIs and cleaning up code of Role and User Management APIs

revert-70aa11f8
prabathabey 8 years ago
parent 624e7df688
commit 37852051ff

@ -27,7 +27,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.common.search.SearchContext;
import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService;
import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -35,10 +34,7 @@ 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.beans.OperationList; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList;
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.InputValidationException;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.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.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;
@ -100,9 +96,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
try { try {
sinceDate = format.parse(ifModifiedSince); sinceDate = format.parse(ifModifiedSince);
} catch (ParseException e) { } catch (ParseException e) {
throw new InputValidationException( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Invalid date " + new ErrorResponse.ErrorResponseBuilder().setMessage("Invalid date " +
"string is provided in 'If-Modified-Since' header").build()); "string is provided in 'If-Modified-Since' header").build()).build();
} }
request.setSince(sinceDate); request.setSince(sinceDate);
result = dms.getAllDevices(request); result = dms.getAllDevices(request);
@ -116,9 +112,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
try { try {
sinceDate = format.parse(since); sinceDate = format.parse(since);
} catch (ParseException e) { } catch (ParseException e) {
throw new InputValidationException( return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Invalid date " + new ErrorResponse.ErrorResponseBuilder().setMessage("Invalid date " +
"string is provided in 'since' filter").build()); "string is provided in 'since' filter").build()).build();
} }
request.setSince(sinceDate); request.setSince(sinceDate);
result = dms.getAllDevices(request); result = dms.getAllDevices(request);
@ -140,8 +136,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while fetching all enrolled devices"; String msg = "Error occurred while fetching all enrolled devices";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
@ -161,13 +157,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while fetching the device information."; String msg = "Error occurred while fetching the device information.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
if (device == null) { if (device == null) {
throw new NotFoundException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Requested device of type '" + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Requested device of type '" +
type + "', which carries id '" + id + "' does not exist").build()); type + "', which carries id '" + id + "' does not exist").build()).build();
} }
return Response.status(Response.Status.OK).entity(device).build(); return Response.status(Response.Status.OK).entity(device).build();
} }
@ -187,17 +183,17 @@ 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) {
throw new NotFoundException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No feature manager is " + new ErrorResponse.ErrorResponseBuilder().setMessage("No feature manager is " +
"registered with the given type '" + type + "'").build()); "registered with the given type '" + type + "'").build()).build();
} }
features = fm.getFeatures(); features = fm.getFeatures();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving the list of features of '" + type + "' device, which " + String msg = "Error occurred while retrieving the list of features of '" + type + "' device, which " +
"carries the id '" + id + "'"; "carries the id '" + id + "'";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
return Response.status(Response.Status.OK).entity(features).build(); return Response.status(Response.Status.OK).entity(features).build();
} }
@ -216,13 +212,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} catch (SearchMgtException e) { } catch (SearchMgtException e) {
String msg = "Error occurred while searching for devices that matches the provided selection criteria"; String msg = "Error occurred while searching for devices that matches the provided selection criteria";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
if (devices == null || devices.size() == 0) {
Response.status(Response.Status.OK).entity(deviceList);
}
deviceList.setList(devices); deviceList.setList(devices);
return Response.status(Response.Status.OK).entity(deviceList).build(); return Response.status(Response.Status.OK).entity(deviceList).build();
} }
@ -237,23 +229,21 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
List<Application> applications; List<Application> applications;
//ApplicationList appList;
ApplicationManagementProviderService amc; ApplicationManagementProviderService amc;
try { try {
RequestValidationUtil.validateDeviceIdentifier(type, id); RequestValidationUtil.validateDeviceIdentifier(type, id);
amc = DeviceMgtAPIUtils.getAppManagementService(); amc = DeviceMgtAPIUtils.getAppManagementService();
applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type)); applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type));
if (applications == null) {
throw new NotFoundException( //TODO: return app list
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 " +
"the id '" + id + "'"; "the id '" + id + "'";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
return Response.status(Response.Status.OK).entity(applications).build(); return Response.status(Response.Status.OK).entity(applications).build();
} }
@ -276,23 +266,17 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
dms = DeviceMgtAPIUtils.getDeviceManagementService(); dms = DeviceMgtAPIUtils.getDeviceManagementService();
result = dms.getOperations(new DeviceIdentifier(id, type),request); result = dms.getOperations(new DeviceIdentifier(id, type),request);
int resultCount = result.getRecordsTotal();
if (resultCount == 0) { operationsList.setList((List<? extends Operation>) result.getData());
throw new NotFoundException( operationsList.setCount(result.getRecordsTotal());
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that" + return Response.status(Response.Status.OK).entity(operationsList).build();
" 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 " +
"carries the id '" + id + "'"; "carries the id '" + id + "'";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
operationsList.setList((List<? extends Operation>) result.getData());
operationsList.setCount(result.getRecordsTotal());
return Response.status(Response.Status.OK).entity(operationsList).build();
} }
@GET @GET
@ -306,18 +290,14 @@ 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) {
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(); return Response.status(Response.Status.OK).entity(policy).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving the current policy associated with the '" + type + String msg = "Error occurred while retrieving the current policy associated with the '" + type +
"' device, which carries the id '" + id + "'"; "' device, which carries the id '" + id + "'";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

@ -31,7 +31,6 @@ 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.*;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyList; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyList;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
@ -45,6 +44,8 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -53,6 +54,7 @@ import java.util.List;
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public class PolicyManagementServiceImpl implements PolicyManagementService { public class PolicyManagementServiceImpl implements PolicyManagementService {
private static final String API_BASE_PATH = "/policies";
private static final Log log = LogFactory.getLog(PolicyManagementServiceImpl.class); private static final Log log = LogFactory.getLog(PolicyManagementServiceImpl.class);
@POST @POST
@ -74,31 +76,38 @@ 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)) {
throw new UnauthorizedAccessException( return Response.status(Response.Status.UNAUTHORIZED).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage new ErrorResponse.ErrorResponseBuilder().setMessage
("Current logged in user is not authorized to add policies").build()); ("Current logged in user is not authorized to add policies").build()).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 = "Error occurred while checking if the current user is authorized to add a policy";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
PolicyAdministratorPoint pap = policyManagementService.getPAP(); PolicyAdministratorPoint pap = policyManagementService.getPAP();
pap.addPolicy(policy); Policy createdPolicy = pap.addPolicy(policy);
return Response.status(Response.Status.CREATED).entity("Policy has been added successfully").build();
return Response.created(new URI(API_BASE_PATH + "/" + createdPolicy.getId())).entity(createdPolicy).build();
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while adding policy"; String msg = "Error occurred while adding policy";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "ErrorResponse occurred while retrieving device list."; String msg = "Error occurred while retrieving device list.";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()).build();
} catch (URISyntaxException e) {
String msg = "Error occurred while composing the location URI, which represents information of the " +
"newly created policy";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
@ -138,21 +147,14 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
try { try {
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
policies = policyAdministratorPoint.getPolicies(); policies = policyAdministratorPoint.getPolicies();
if (policies == null || policies.size() == 0) {
throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policies found.").build());
}
targetPolicies.setCount(policies.size()); targetPolicies.setCount(policies.size());
filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit); filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit);
if (filteredPolicies.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No policies found.").build();
}
targetPolicies.setList(filteredPolicies); targetPolicies.setList(filteredPolicies);
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while retrieving all available policies"; String msg = "Error occurred while retrieving all available policies";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
return Response.status(Response.Status.OK).entity(targetPolicies).build(); return Response.status(Response.Status.OK).entity(targetPolicies).build();
@ -168,14 +170,15 @@ 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) {
throw new NotFoundException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policy found.").build()); new ErrorResponse.ErrorResponseBuilder().setMessage(
"No policy found with the id '" + id + "'").build()).build();
} }
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while retrieving policy corresponding to the id '" + id + "'"; String msg = "Error occurred while retrieving policy corresponding to the id '" + id + "'";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
return Response.status(Response.Status.OK).entity(policy).build(); return Response.status(Response.Status.OK).entity(policy).build();
} }
@ -190,22 +193,22 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
Policy policy = this.getPolicyFromWrapper(policyWrapper); Policy policy = this.getPolicyFromWrapper(policyWrapper);
policy.setId(id); policy.setId(id);
PolicyAdministratorPoint pap = policyManagementService.getPAP(); PolicyAdministratorPoint pap = policyManagementService.getPAP();
Policy exisitingPolicy = pap.getPolicy(id); Policy existingPolicy = pap.getPolicy(id);
if (exisitingPolicy == null) { if (existingPolicy == null) {
return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build(); return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build();
} }
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 = "Error occurred while updating the policy";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "ErrorResponse occurred while retrieving the device list."; String msg = "Error occurred while retrieving the device list.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
@ -227,15 +230,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);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).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
throw new NotFoundException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Policy doesn't exist").build()); new ErrorResponse.ErrorResponseBuilder().setMessage("Policy doesn't exist").build()).build();
} }
} }
@ -256,18 +259,18 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} }
} }
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "ErrorResponse occurred while activating policies"; String msg = "Error occurred while activating policies";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()).build();
} }
if (isPolicyActivated) { if (isPolicyActivated) {
return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated") return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated")
.build(); .build();
} else { } else {
throw new NotFoundException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Selected policies have " + new ErrorResponse.ErrorResponseBuilder().setMessage("Selected policies have " +
"not been activated").build()); "not been activated").build()).build();
} }
} }
@ -290,16 +293,16 @@ 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);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
if (isPolicyDeActivated) { if (isPolicyDeActivated) {
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();
} else { } else {
throw new NotFoundException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Selected policies have " + new ErrorResponse.ErrorResponseBuilder().setMessage("Selected policies have " +
"not been deactivated").build()); "not been deactivated").build()).build();
} }
} }
@ -315,8 +318,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "Exception in applying changes."; String msg = "Exception in applying changes.";
log.error(msg, e); log.error(msg, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()).build();
} }
return Response.status(Response.Status.OK).entity("Changes have been successfully updated.").build(); return Response.status(Response.Status.OK).entity("Changes have been successfully updated.").build();
} }
@ -340,17 +343,17 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String error = "Exception in updating policy priorities."; String error = "Exception in updating policy priorities.";
log.error(error, e); log.error(error, e);
throw new UnexpectedServerErrorException( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(error).build()); new ErrorResponse.ErrorResponseBuilder().setMessage(error).build()).build();
} }
if (policiesUpdated) { if (policiesUpdated) {
return Response.status(Response.Status.OK).entity("Policy Priorities successfully " return Response.status(Response.Status.OK).entity("Policy Priorities successfully "
+ "updated.").build(); + "updated.").build();
} else { } else {
throw new NotFoundException( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy priorities did " new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy priorities did "
+ "not update. Bad Request.").build()); + "not update. Bad Request.").build()).build();
} }
} }

@ -88,7 +88,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
try { try {
final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm();
if (!userRealm.getUserStoreManager().isExistingRole(roleName)) { if (!userRealm.getUserStoreManager().isExistingRole(roleName)) {
return Response.status(404).entity(new ErrorResponse.ErrorResponseBuilder().setMessage( return Response.status(Response.Status.NOT_FOUND).entity(new ErrorResponse.ErrorResponseBuilder().setMessage(
"No role exists with the name '" + roleName + "'").build()).build(); "No role exists with the name '" + roleName + "'").build()).build();
} }
@ -153,7 +153,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm();
if (!userStoreManager.isExistingRole(roleName)) { if (!userStoreManager.isExistingRole(roleName)) {
return Response.status(404).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("No role exists with the name '" + new ErrorResponse.ErrorResponseBuilder().setMessage("No role exists with the name '" +
roleName + "'").build()).build(); roleName + "'").build()).build();
} }
@ -234,7 +234,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm();
final UserStoreManager userStoreManager = userRealm.getUserStoreManager(); final UserStoreManager userStoreManager = userRealm.getUserStoreManager();
if (!userStoreManager.isExistingRole(roleName)) { if (!userStoreManager.isExistingRole(roleName)) {
return Response.status(404).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("No role exists with the name '" + new ErrorResponse.ErrorResponseBuilder().setMessage("No role exists with the name '" +
roleName + "'").build()).build(); roleName + "'").build()).build();
} }
@ -290,7 +290,7 @@ public class RoleManagementServiceImpl implements RoleManagementService {
final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm();
final UserStoreManager userStoreManager = userRealm.getUserStoreManager(); final UserStoreManager userStoreManager = userRealm.getUserStoreManager();
if (!userStoreManager.isExistingRole(roleName)) { if (!userStoreManager.isExistingRole(roleName)) {
return Response.status(404).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("No role exists with the name '" + new ErrorResponse.ErrorResponseBuilder().setMessage("No role exists with the name '" +
roleName + "'").build()).build(); roleName + "'").build()).build();
} }

@ -57,7 +57,7 @@ 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(409).entity( return Response.status(Response.Status.CONFLICT).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " + new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " +
userInfo.getUsername() + " already exists. Therefore, request made to add user " + userInfo.getUsername() + " already exists. Therefore, request made to add user " +
"was refused.").build()).build(); "was refused.").build()).build();
@ -108,7 +108,8 @@ public class UserManagementServiceImpl implements UserManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("User by username: " + username + " does not exist."); log.debug("User by username: " + username + " does not exist.");
} }
return Response.status(404).entity(new ErrorResponse.ErrorResponseBuilder().setMessage( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(
"User doesn't exist.").build()).build(); "User doesn't exist.").build()).build();
} }
@ -133,7 +134,7 @@ public class UserManagementServiceImpl implements UserManagementService {
log.debug("User by username: " + userInfo.getUsername() + log.debug("User by username: " + userInfo.getUsername() +
" doesn't exists. Therefore, request made to update user was refused."); " doesn't exists. Therefore, request made to update user was refused.");
} }
return Response.status(404).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " + new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " +
userInfo.getUsername() + " doesn't exist.").build()).build(); userInfo.getUsername() + " doesn't exist.").build()).build();
} }
@ -206,7 +207,7 @@ public class UserManagementServiceImpl implements UserManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("User by username: " + username + " does not exist for removal."); log.debug("User by username: " + username + " does not exist for removal.");
} }
return Response.status(404).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("User '" + new ErrorResponse.ErrorResponseBuilder().setMessage("User '" +
username + "' does not exist for removal.").build()).build(); username + "' does not exist for removal.").build()).build();
} }
@ -234,7 +235,7 @@ public class UserManagementServiceImpl implements UserManagementService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("User by username: " + username + " does not exist for role retrieval."); log.debug("User by username: " + username + " does not exist for role retrieval.");
} }
return Response.status(404).entity( return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " + username + new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " + username +
" does not exist for role retrieval.").build()).build(); " does not exist for role retrieval.").build()).build();
} }

Loading…
Cancel
Save