|
|
@ -23,11 +23,13 @@ import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
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.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.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.PolicyFilteringUtil;
|
|
|
|
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;
|
|
|
@ -51,6 +53,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Response addPolicy(PolicyWrapper policyWrapper) {
|
|
|
|
public Response addPolicy(PolicyWrapper policyWrapper) {
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Policy policy = this.getPolicyFromWrapper(policyWrapper);
|
|
|
|
Policy policy = this.getPolicyFromWrapper(policyWrapper);
|
|
|
|
|
|
|
|
|
|
|
|
List<Device> devices = policy.getDevices();
|
|
|
|
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();
|
|
|
|
return javax.ws.rs.core.Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
pap.addPolicy(policy);
|
|
|
|
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) {
|
|
|
|
} 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();
|
|
|
|
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) {
|
|
|
|
private Policy getPolicyFromWrapper(PolicyWrapper policyWrapper) throws DeviceManagementException {
|
|
|
|
Policy policy = new org.wso2.carbon.policy.mgt.common.Policy();
|
|
|
|
Policy policy = new Policy();
|
|
|
|
policy.setPolicyName(policyWrapper.getPolicyName());
|
|
|
|
policy.setPolicyName(policyWrapper.getPolicyName());
|
|
|
|
policy.setProfileId(policyWrapper.getProfileId());
|
|
|
|
|
|
|
|
policy.setDescription(policyWrapper.getDescription());
|
|
|
|
policy.setDescription(policyWrapper.getDescription());
|
|
|
|
policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
|
|
|
|
policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
|
|
|
|
policy.setOwnershipType(policyWrapper.getOwnershipType());
|
|
|
|
policy.setOwnershipType(policyWrapper.getOwnershipType());
|
|
|
|
policy.setRoles(policyWrapper.getRoles());
|
|
|
|
policy.setRoles(policyWrapper.getRoles());
|
|
|
|
policy.setUsers(policyWrapper.getUsers());
|
|
|
|
policy.setUsers(policyWrapper.getUsers());
|
|
|
|
policy.setTenantId(policyWrapper.getTenantId());
|
|
|
|
|
|
|
|
policy.setCompliance(policyWrapper.getCompliance());
|
|
|
|
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;
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -116,7 +131,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
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
|
|
|
|
@GET
|
|
|
@ -144,19 +160,29 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) {
|
|
|
|
public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) {
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
Policy policy = this.getPolicyFromWrapper(policyWrapper);
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
Policy policy = this.getPolicyFromWrapper(policyWrapper);
|
|
|
|
|
|
|
|
policy.setId(id);
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
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);
|
|
|
|
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();
|
|
|
|
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
|
|
|
|
@POST
|
|
|
|
|
|
|
|
@Path("/remove-policy")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Response removePolicies(List<Integer> policyIds) {
|
|
|
|
public Response removePolicies(List<Integer> policyIds) {
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
@ -164,8 +190,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
for (int i : policyIds) {
|
|
|
|
for (int i : policyIds) {
|
|
|
|
org.wso2.carbon.policy.mgt.common.Policy policy = pap.getPolicy(i);
|
|
|
|
Policy policy = pap.getPolicy(i);
|
|
|
|
if (!pap.deletePolicy(policy)) {
|
|
|
|
if (policy == null || !pap.deletePolicy(policy)) {
|
|
|
|
policyDeleted = false;
|
|
|
|
policyDeleted = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -182,41 +208,62 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@PUT
|
|
|
|
@Path("/activate-policy")
|
|
|
|
@Path("/activate-policy")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Response activatePolicies(List<Integer> policyIds) {
|
|
|
|
public Response activatePolicies(List<Integer> policyIds) {
|
|
|
|
|
|
|
|
boolean isPolicyActivated = false;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
for (int i : policyIds) {
|
|
|
|
for (int i : policyIds) {
|
|
|
|
|
|
|
|
Policy policy = pap.getPolicy(i);
|
|
|
|
|
|
|
|
if (policy != null) {
|
|
|
|
pap.activatePolicy(i);
|
|
|
|
pap.activatePolicy(i);
|
|
|
|
|
|
|
|
isPolicyActivated = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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();
|
|
|
|
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")
|
|
|
|
@Path("/deactivate-policy")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Response deactivatePolicies(List<Integer> policyIds) {
|
|
|
|
public Response deactivatePolicies(List<Integer> policyIds) {
|
|
|
|
|
|
|
|
boolean isPolicyDeActivated = false;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
for (int i : policyIds) {
|
|
|
|
for (int i : policyIds) {
|
|
|
|
|
|
|
|
Policy policy = pap.getPolicy(i);
|
|
|
|
|
|
|
|
if (policy != null) {
|
|
|
|
pap.inactivatePolicy(i);
|
|
|
|
pap.inactivatePolicy(i);
|
|
|
|
|
|
|
|
isPolicyDeActivated = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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();
|
|
|
|
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 " +
|
|
|
|
return Response.status(Response.Status.OK).entity("Selected policies have been successfully " +
|
|
|
|
"deactivated").build();
|
|
|
|
"deactivated").build();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity("Selected policies have not been deactivated")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|