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