use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class APIProviderImpl method attachOperationPoliciesToAPI.
private void attachOperationPoliciesToAPI(API newAPI, Map<String, String> clonedOperationPolicyMap, Map<String, List<OperationPolicy>> operationPoliciesMap) throws APIManagementException {
operationPoliciesMap.forEach((key, operationPolicies) -> operationPolicies.forEach(operationPolicy -> operationPolicy.setPolicyId(clonedOperationPolicyMap.get(operationPolicy.getPolicyId()))));
Set<URITemplate> uriTemplates = newAPI.getUriTemplates();
if (uriTemplates != null) {
for (URITemplate uriTemplate : uriTemplates) {
List<OperationPolicy> operationPolicies = operationPoliciesMap.get(uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate());
uriTemplate.setOperationPolicies(operationPolicies);
}
apiMgtDAO.addOperationPolicyMapping(uriTemplates);
}
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class APIProviderImpl method validateOperationPolicyParameters.
private void validateOperationPolicyParameters(API api, String tenantDomain) throws APIManagementException {
boolean isOperationPoliciesAllowedForAPIType = true;
Set<URITemplate> uriTemplates = api.getUriTemplates();
if (APIConstants.API_TYPE_WS.equals(api.getType()) || APIConstants.API_TYPE_SSE.equals(api.getType()) || APIConstants.API_TYPE_WEBSUB.equals(api.getType())) {
if (log.isDebugEnabled()) {
log.debug("Operation policies are not allowed for " + api.getType() + " APIs");
}
isOperationPoliciesAllowedForAPIType = false;
}
Map<String, Integer> policyOccurrenceMap = new HashMap<>();
for (URITemplate uriTemplate : uriTemplates) {
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
List<OperationPolicy> validatedPolicies = new ArrayList<>();
if (operationPolicies != null && !operationPolicies.isEmpty() && isOperationPoliciesAllowedForAPIType) {
for (OperationPolicy policy : operationPolicies) {
String policyId = policy.getPolicyId();
// First check the API specific operation policy list
OperationPolicyData policyData = getAPISpecificOperationPolicyByPolicyId(policyId, api.getUuid(), tenantDomain, false);
if (policyData != null) {
if (log.isDebugEnabled()) {
log.debug("A policy is found for " + policyId + " as " + policyData.getSpecification().getName() + ". Validating the policy");
}
if (policyData.isRevision()) {
throw new APIManagementException("Invalid policy selected. " + policyId + " policy is not found.", ExceptionCodes.INVALID_OPERATION_POLICY);
}
OperationPolicySpecification policySpecification = policyData.getSpecification();
if (validateAppliedPolicyWithSpecification(policySpecification, policy, api)) {
String policyOccurrenceKey = uriTemplate.getUriTemplate() + "_" + policy.getDirection() + "_" + policy.getPolicyName();
int previousOccurrenceCount = 0;
if (policyOccurrenceMap.get(policyOccurrenceKey) != null) {
previousOccurrenceCount = policyOccurrenceMap.get(policyOccurrenceKey);
if (previousOccurrenceCount > 0 && !policySpecification.isMultipleAllowed()) {
throw new APIManagementException("Policy multiple allowed property violated. " + policySpecification.getDisplayName() + " cannot be applied multiple times.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
}
policyOccurrenceMap.put(policyOccurrenceKey, previousOccurrenceCount + 1);
validatedPolicies.add(policy);
}
} else {
// TODO: get policy based on the name
OperationPolicyData commonPolicyData = getCommonOperationPolicyByPolicyId(policyId, tenantDomain, false);
if (commonPolicyData != null) {
// attributes and added to API policy list
if (log.isDebugEnabled()) {
log.debug("A common policy is found for " + policyId + ". Validating the policy");
}
OperationPolicySpecification commonPolicySpec = commonPolicyData.getSpecification();
if (validateAppliedPolicyWithSpecification(commonPolicySpec, policy, api)) {
String policyOccurrenceKey = uriTemplate.getUriTemplate() + "_" + policy.getDirection() + "_" + policy.getPolicyName();
int previousOccurrenceCount = 0;
if (policyOccurrenceMap.get(policyOccurrenceKey) != null) {
previousOccurrenceCount = policyOccurrenceMap.get(policyOccurrenceKey);
if (previousOccurrenceCount > 0 && !commonPolicySpec.isMultipleAllowed()) {
throw new APIManagementException("Policy multiple allowed property violated. " + commonPolicySpec.getDisplayName() + " cannot be applied multiple times.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
}
policyOccurrenceMap.put(policyOccurrenceKey, previousOccurrenceCount + 1);
validatedPolicies.add(policy);
}
} else {
throw new APIManagementException("Selected policy " + policyId + " is not found.", ExceptionCodes.INVALID_OPERATION_POLICY);
}
}
}
}
uriTemplate.setOperationPolicies(validatedPolicies);
}
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class APIProviderImpl method extractAndDropOperationPoliciesFromURITemplate.
private static Map<String, List<OperationPolicy>> extractAndDropOperationPoliciesFromURITemplate(Set<URITemplate> uriTemplates) {
Map<String, List<OperationPolicy>> operationPoliciesMap = new HashMap<>();
for (URITemplate uriTemplate : uriTemplates) {
String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
if (!operationPolicies.isEmpty()) {
operationPoliciesMap.put(key, operationPolicies);
}
uriTemplate.setOperationPolicies(null);
}
return operationPoliciesMap;
}
Aggregations