Search in sources :

Example 6 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class APIProviderImpl method validateAppliedPolicyWithSpecification.

private boolean validateAppliedPolicyWithSpecification(OperationPolicySpecification policySpecification, OperationPolicy appliedPolicy, API api) throws APIManagementException {
    // Validate the policy applied direction
    if (!policySpecification.getApplicableFlows().contains(appliedPolicy.getDirection())) {
        if (log.isDebugEnabled()) {
            log.debug("The policy " + policySpecification.getName() + " is not support in the " + appliedPolicy.getDirection() + " flow. Hence skipped.");
        }
        throw new APIManagementException(policySpecification.getName() + " cannot be used in the " + appliedPolicy.getDirection() + " flow.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
    }
    // Validate the API type
    if (!policySpecification.getSupportedApiTypes().contains(api.getType())) {
        if (log.isDebugEnabled()) {
            log.debug("The policy " + policySpecification.getName() + " cannot be used for the " + api.getType() + " API type.");
        }
        throw new APIManagementException(policySpecification.getName() + " cannot be used for the " + api.getType() + " API type.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
    }
    // Validate policy Attributes
    if (policySpecification.getPolicyAttributes() != null) {
        for (OperationPolicySpecAttribute attribute : policySpecification.getPolicyAttributes()) {
            if (attribute.isRequired()) {
                Object appliedPolicyAttribute = appliedPolicy.getParameters().get(attribute.getName());
                if (appliedPolicyAttribute != null) {
                    if (attribute.getValidationRegex() != null) {
                        Pattern pattern = Pattern.compile(attribute.getValidationRegex(), Pattern.CASE_INSENSITIVE);
                        Matcher matcher = pattern.matcher((String) appliedPolicyAttribute);
                        if (!matcher.matches()) {
                            throw new APIManagementException("Policy attribute " + attribute.getName() + " regex validation error.", ExceptionCodes.INVALID_OPERATION_POLICY_PARAMETERS);
                        }
                    }
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Required policy attribute " + attribute.getName() + " is not found for the the policy " + policySpecification.getName());
                    }
                    throw new APIManagementException("Required policy attribute " + attribute.getName() + " is not found for the the policy " + policySpecification.getName() + appliedPolicy.getDirection() + " flow.", ExceptionCodes.MISSING_MANDATORY_POLICY_ATTRIBUTES);
                }
            }
        }
    }
    return true;
}
Also used : OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) Pattern(java.util.regex.Pattern) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Matcher(java.util.regex.Matcher) JSONObject(org.json.simple.JSONObject)

Example 7 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class APIProviderImpl method setOperationPoliciesToURITemplates.

@Override
public void setOperationPoliciesToURITemplates(String apiId, Set<URITemplate> uriTemplates) throws APIManagementException {
    Set<URITemplate> uriTemplatesWithPolicies = apiMgtDAO.getURITemplatesWithOperationPolicies(apiId);
    if (!uriTemplatesWithPolicies.isEmpty()) {
        // This is a temporary map to keep operation policies list of URI Templates against the URI mapping ID
        Map<String, List<OperationPolicy>> operationPoliciesMap = new HashMap<>();
        for (URITemplate uriTemplate : uriTemplatesWithPolicies) {
            String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
            List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
            if (!operationPolicies.isEmpty()) {
                operationPoliciesMap.put(key, operationPolicies);
            }
        }
        for (URITemplate uriTemplate : uriTemplates) {
            String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
            if (operationPoliciesMap.containsKey(key)) {
                uriTemplate.setOperationPolicies(operationPoliciesMap.get(key));
            }
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class APIMappingUtil method setOperationPoliciesToOperationsDTO.

/**
 * Reads the operationPolicies from the API object passed in, and sets them back to the API Operations DTO
 *
 * @param api               API object
 * @param apiOperationsDTO  List of API Operations DTO
 */
private static void setOperationPoliciesToOperationsDTO(API api, List<APIOperationsDTO> apiOperationsDTO) {
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    Map<String, URITemplate> uriTemplateMap = new HashMap<>();
    for (URITemplate uriTemplate : uriTemplates) {
        String key = uriTemplate.getUriTemplate() + ":" + uriTemplate.getHTTPVerb();
        uriTemplateMap.put(key, uriTemplate);
    }
    for (APIOperationsDTO operationsDTO : apiOperationsDTO) {
        String key = operationsDTO.getTarget() + ":" + operationsDTO.getVerb();
        List<OperationPolicy> operationPolicies = uriTemplateMap.get(key).getOperationPolicies();
        if (!operationPolicies.isEmpty()) {
            operationsDTO.setOperationPolicies(OperationPolicyMappingUtil.fromOperationPolicyListToDTO(operationPolicies));
        }
    }
}
Also used : HashMap(java.util.HashMap) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO)

Example 9 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class OperationPolicyMappingUtil method fromDTOToOperationPolicy.

public static OperationPolicy fromDTOToOperationPolicy(OperationPolicyDTO operationPolicyDTO) {
    OperationPolicy operationPolicy = new OperationPolicy();
    operationPolicy.setPolicyName(operationPolicyDTO.getPolicyName());
    operationPolicy.setPolicyId(operationPolicyDTO.getPolicyId());
    operationPolicy.setParameters(operationPolicyDTO.getParameters());
    return operationPolicy;
}
Also used : OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy)

Example 10 with OperationPolicy

use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.

the class OperationPolicyMappingUtil method fromOperationPolicyToDTO.

public static OperationPolicyDTO fromOperationPolicyToDTO(OperationPolicy operationPolicy) {
    OperationPolicyDTO dto = new OperationPolicyDTO();
    dto.setPolicyName(operationPolicy.getPolicyName());
    dto.setPolicyId(operationPolicy.getPolicyId());
    dto.setOrder(operationPolicy.getOrder());
    dto.setParameters(operationPolicy.getParameters());
    return dto;
}
Also used : OperationPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDTO)

Aggregations

OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)28 ArrayList (java.util.ArrayList)21 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)20 HashMap (java.util.HashMap)16 PreparedStatement (java.sql.PreparedStatement)13 ResultSet (java.sql.ResultSet)12 Connection (java.sql.Connection)11 Gson (com.google.gson.Gson)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 SQLException (java.sql.SQLException)8 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)7 Scope (org.wso2.carbon.apimgt.api.model.Scope)7 HashSet (java.util.HashSet)6 LinkedHashSet (java.util.LinkedHashSet)6 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5