|
|
|
@ -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.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.beans.PolicyList;
|
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil;
|
|
|
|
|
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.core.MediaType;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
@ -53,6 +54,7 @@ import java.util.List;
|
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
|
|
|
|
|
private static final String API_BASE_PATH = "/policies";
|
|
|
|
|
private static final Log log = LogFactory.getLog(PolicyManagementServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@ -74,31 +76,38 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
String username = threadLocalCarbonContext.getUsername();
|
|
|
|
|
try {
|
|
|
|
|
if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) {
|
|
|
|
|
throw new UnauthorizedAccessException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage
|
|
|
|
|
("Current logged in user is not authorized to add policies").build());
|
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage
|
|
|
|
|
("Current logged in user is not authorized to add policies").build()).build();
|
|
|
|
|
}
|
|
|
|
|
} 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);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
|
pap.addPolicy(policy);
|
|
|
|
|
return Response.status(Response.Status.CREATED).entity("Policy has been added successfully").build();
|
|
|
|
|
Policy createdPolicy = pap.addPolicy(policy);
|
|
|
|
|
|
|
|
|
|
return Response.created(new URI(API_BASE_PATH + "/" + createdPolicy.getId())).entity(createdPolicy).build();
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while adding policy";
|
|
|
|
|
String msg = "Error occurred while adding policy";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()).build();
|
|
|
|
|
} 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);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -138,21 +147,14 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
try {
|
|
|
|
|
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
|
|
|
|
|
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());
|
|
|
|
|
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);
|
|
|
|
|
} 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);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Response.status(Response.Status.OK).entity(targetPolicies).build();
|
|
|
|
@ -168,14 +170,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP();
|
|
|
|
|
policy = policyAdministratorPoint.getPolicy(id);
|
|
|
|
|
if (policy == null) {
|
|
|
|
|
throw new NotFoundException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No policy found.").build());
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(
|
|
|
|
|
"No policy found with the id '" + id + "'").build()).build();
|
|
|
|
|
}
|
|
|
|
|
} 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);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).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.setId(id);
|
|
|
|
|
PolicyAdministratorPoint pap = policyManagementService.getPAP();
|
|
|
|
|
Policy exisitingPolicy = pap.getPolicy(id);
|
|
|
|
|
if (exisitingPolicy == null) {
|
|
|
|
|
Policy existingPolicy = pap.getPolicy(id);
|
|
|
|
|
if (existingPolicy == 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";
|
|
|
|
|
String msg = "Error occurred while updating the policy";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
|
} 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);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -227,15 +230,15 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while removing policies";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
|
}
|
|
|
|
|
if (policyDeleted) {
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Policies have been successfully deleted").build();
|
|
|
|
|
} else {
|
|
|
|
|
//TODO:Check of this logic is correct
|
|
|
|
|
throw new NotFoundException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Policy doesn't exist").build());
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage("Policy doesn't exist").build()).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -256,18 +259,18 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "ErrorResponse occurred while activating policies";
|
|
|
|
|
String msg = "Error occurred while activating policies";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()).build();
|
|
|
|
|
}
|
|
|
|
|
if (isPolicyActivated) {
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated")
|
|
|
|
|
.build();
|
|
|
|
|
} else {
|
|
|
|
|
throw new NotFoundException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Selected policies have " +
|
|
|
|
|
"not been activated").build());
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage("Selected policies have " +
|
|
|
|
|
"not been activated").build()).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -290,16 +293,16 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "Exception in inactivating policies.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
|
}
|
|
|
|
|
if (isPolicyDeActivated) {
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Selected policies have been successfully " +
|
|
|
|
|
"deactivated").build();
|
|
|
|
|
} else {
|
|
|
|
|
throw new NotFoundException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("Selected policies have " +
|
|
|
|
|
"not been deactivated").build());
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage("Selected policies have " +
|
|
|
|
|
"not been deactivated").build()).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -315,8 +318,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
String msg = "Exception in applying changes.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()).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) {
|
|
|
|
|
String error = "Exception in updating policy priorities.";
|
|
|
|
|
log.error(error, e);
|
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(error).build());
|
|
|
|
|
return Response.serverError().entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(error).build()).build();
|
|
|
|
|
}
|
|
|
|
|
if (policiesUpdated) {
|
|
|
|
|
return Response.status(Response.Status.OK).entity("Policy Priorities successfully "
|
|
|
|
|
+ "updated.").build();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
throw new NotFoundException(
|
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(
|
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy priorities did "
|
|
|
|
|
+ "not update. Bad Request.").build());
|
|
|
|
|
+ "not update. Bad Request.").build()).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|