Search in sources :

Example 1 with OperationPolicyComparator

use of org.wso2.carbon.apimgt.impl.utils.OperationPolicyComparator in project carbon-apimgt by wso2.

the class SynapsePolicyAggregator method populatePolicyCaseList.

public static List<Object> populatePolicyCaseList(URITemplate template, String pathToAchieve, String flow, List<Object> caseList) throws APIManagementException {
    Map<String, Object> caseMap = new HashMap<>();
    String uriTemplateString = template.getUriTemplate();
    String method = template.getHTTPVerb();
    String key = method + "_" + uriTemplateString.replaceAll("[\\W]", "\\\\$0");
    // This will replace & with &amp; for query params
    key = StringEscapeUtils.escapeXml(StringEscapeUtils.unescapeXml(key));
    List<String> caseBody = new ArrayList<>();
    String policyDirectory = pathToAchieve + File.separator + ImportExportConstants.POLICIES_DIRECTORY;
    List<OperationPolicy> operationPolicies = template.getOperationPolicies();
    Collections.sort(operationPolicies, new OperationPolicyComparator());
    for (OperationPolicy policy : operationPolicies) {
        if (flow.equals(policy.getDirection())) {
            Map<String, Object> policyParameters = policy.getParameters();
            OperationPolicySpecification policySpecification = ImportUtils.getOperationPolicySpecificationFromFile(policyDirectory, policy.getPolicyName());
            if (policySpecification.getSupportedGateways().contains(APIConstants.OPERATION_POLICY_SUPPORTED_GATEWAY_SYNAPSE)) {
                OperationPolicyDefinition policyDefinition = APIUtil.getOperationPolicyDefinitionFromFile(policyDirectory, policy.getPolicyName(), APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION);
                if (policyDefinition != null) {
                    String renderedTemplate = renderPolicyTemplate(policyDefinition.getContent(), policyParameters);
                    if (renderedTemplate != null && !renderedTemplate.isEmpty()) {
                        caseBody.add(renderedTemplate);
                    }
                } else {
                    log.error("Policy definition for " + policy.getPolicyName() + " is not found in the artifact");
                }
            } else {
                log.error("Policy " + policy.getPolicyName() + " does not support Synapse gateway. " + "Hence skipped");
            }
        }
    }
    if (caseBody.size() != 0) {
        caseMap.put("case_regex", key);
        caseMap.put("policy_sequence", caseBody);
        caseList.add(caseMap);
    }
    return caseList;
}
Also used : OperationPolicyDefinition(org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition) OperationPolicyComparator(org.wso2.carbon.apimgt.impl.utils.OperationPolicyComparator) HashMap(java.util.HashMap) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) ArrayList(java.util.ArrayList) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)1 OperationPolicyDefinition (org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition)1 OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)1 OperationPolicyComparator (org.wso2.carbon.apimgt.impl.utils.OperationPolicyComparator)1