Search in sources :

Example 16 with OperationPolicySpecification

use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification 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);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Aggregations

OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)13 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 ArrayList (java.util.ArrayList)6 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)6 OperationPolicyDefinition (org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition)5 IOException (java.io.IOException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 PreparedStatement (java.sql.PreparedStatement)3 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)3 OperationPolicySpecAttribute (org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute)3 OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)3 Gson (com.google.gson.Gson)2 URI (java.net.URI)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 JSONObject (org.json.simple.JSONObject)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2