|
|
|
@ -23,11 +23,13 @@ import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
|
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.PolicyFilteringUtil;
|
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
|
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil;
|
|
|
|
|
import org.wso2.carbon.policy.mgt.common.Policy;
|
|
|
|
@ -51,6 +53,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
@Override
|
|
|
|
|
public Response addPolicy(PolicyWrapper policyWrapper) {
|
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
|
try {
|
|
|
|
|
Policy policy = this.getPolicyFromWrapper(policyWrapper);
|
|
|
|
|
|
|
|
|
|
List<Device> devices = policy.getDevices();
|
|
|
|
@ -72,28 +75,40 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
return javax.ws.rs.core.Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
|
pap.addPolicy(policy);
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Policy has been added successfully").build();
|
|
|
|
|
return Response.status(Response.Status.CREATED).entity("Policy has been added successfully").build();
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while adding policy";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while retrieving device list.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Policy getPolicyFromWrapper(PolicyWrapper policyWrapper) {
|
|
|
|
|
Policy policy = new org.wso2.carbon.policy.mgt.common.Policy();
|
|
|
|
|
private Policy getPolicyFromWrapper(PolicyWrapper policyWrapper) throws DeviceManagementException {
|
|
|
|
|
Policy policy = new Policy();
|
|
|
|
|
policy.setPolicyName(policyWrapper.getPolicyName());
|
|
|
|
|
policy.setProfileId(policyWrapper.getProfileId());
|
|
|
|
|
policy.setDescription(policyWrapper.getDescription());
|
|
|
|
|
policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
|
|
|
|
|
policy.setOwnershipType(policyWrapper.getOwnershipType());
|
|
|
|
|
policy.setRoles(policyWrapper.getRoles());
|
|
|
|
|
policy.setUsers(policyWrapper.getUsers());
|
|
|
|
|
policy.setTenantId(policyWrapper.getTenantId());
|
|
|
|
|
policy.setCompliance(policyWrapper.getCompliance());
|
|
|
|
|
//TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here.
|
|
|
|
|
List<Device> devices = null;
|
|
|
|
|
List<DeviceIdentifier> deviceIdentifiers = policyWrapper.getDeviceIdentifiers();
|
|
|
|
|
if (deviceIdentifiers != null) {
|
|
|
|
|
for (DeviceIdentifier id : deviceIdentifiers) {
|
|
|
|
|
devices.add(DeviceMgtAPIUtils.getDeviceManagementService().getDevice(id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
policy.setDevices(devices);
|
|
|
|
|
policy.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
|
|
|
|
|
return policy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -116,7 +131,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
}
|
|
|
|
|
return Response.status(Response.Status.OK).entity(policies).build();
|
|
|
|
|
return Response.status(Response.Status.OK).entity(PolicyFilteringUtil.getPolicies(policies, offset, limit))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@ -144,19 +160,29 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
@Override
|
|
|
|
|
public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) {
|
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
|
Policy policy = this.getPolicyFromWrapper(policyWrapper);
|
|
|
|
|
try {
|
|
|
|
|
Policy policy = this.getPolicyFromWrapper(policyWrapper);
|
|
|
|
|
policy.setId(id);
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
|
Policy exisitingPolicy = pap.getPolicy(id);
|
|
|
|
|
if (exisitingPolicy == null) {
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build();
|
|
|
|
|
}
|
|
|
|
|
pap.updatePolicy(policy);
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Policy has successfully been updated").build();
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while updating the policy";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while retrieving the device list.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@Path("/remove-policy")
|
|
|
|
|
@Override
|
|
|
|
|
public Response removePolicies(List<Integer> policyIds) {
|
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
@ -164,8 +190,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
try {
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
|
for (int i : policyIds) {
|
|
|
|
|
org.wso2.carbon.policy.mgt.common.Policy policy = pap.getPolicy(i);
|
|
|
|
|
if (!pap.deletePolicy(policy)) {
|
|
|
|
|
Policy policy = pap.getPolicy(i);
|
|
|
|
|
if (policy == null || !pap.deletePolicy(policy)) {
|
|
|
|
|
policyDeleted = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -182,41 +208,62 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@PUT
|
|
|
|
|
@Path("/activate-policy")
|
|
|
|
|
@Override
|
|
|
|
|
public Response activatePolicies(List<Integer> policyIds) {
|
|
|
|
|
boolean isPolicyActivated = false;
|
|
|
|
|
try {
|
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
|
for (int i : policyIds) {
|
|
|
|
|
Policy policy = pap.getPolicy(i);
|
|
|
|
|
if (policy != null) {
|
|
|
|
|
pap.activatePolicy(i);
|
|
|
|
|
isPolicyActivated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while activating policies";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
}
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated").build();
|
|
|
|
|
if (isPolicyActivated) {
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated")
|
|
|
|
|
.build();
|
|
|
|
|
} else {
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity("Selected policies have not been activated")
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@PUT
|
|
|
|
|
@Path("/deactivate-policy")
|
|
|
|
|
@Override
|
|
|
|
|
public Response deactivatePolicies(List<Integer> policyIds) {
|
|
|
|
|
boolean isPolicyDeActivated = false;
|
|
|
|
|
try {
|
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
|
for (int i : policyIds) {
|
|
|
|
|
Policy policy = pap.getPolicy(i);
|
|
|
|
|
if (policy != null) {
|
|
|
|
|
pap.inactivatePolicy(i);
|
|
|
|
|
isPolicyDeActivated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "Exception in inactivating policies.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
|
}
|
|
|
|
|
if (isPolicyDeActivated) {
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Selected policies have been successfully " +
|
|
|
|
|
"deactivated").build();
|
|
|
|
|
} else {
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity("Selected policies have not been deactivated")
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|