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 & 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;
}
Aggregations