use of org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition in project carbon-apimgt by wso2.
the class OperationPoliciesApiServiceImpl method addCommonOperationPolicy.
/**
* Add a common operation policy that can be used by all the APIs
*
* @param policySpecFileInputStream Input stream of the common policy specification file
* @param policySpecFileDetail Common policy specification
* @param synapsePolicyDefinitionFileInputStream Input stream of the synapse policy definition file
* @param synapsePolicyDefinitionFileDetail Synapse definition of the operation policy
* @param ccPolicyDefinitionFileInputStream Input stream of the choreo connect policy definition file
* @param ccPolicyDefinitionFileDetail Choreo connect definition of the operation policy
* @param messageContext message context
* @return Added common operation policy DTO as response
*/
@Override
public Response addCommonOperationPolicy(InputStream policySpecFileInputStream, Attachment policySpecFileDetail, InputStream synapsePolicyDefinitionFileInputStream, Attachment synapsePolicyDefinitionFileDetail, InputStream ccPolicyDefinitionFileInputStream, Attachment ccPolicyDefinitionFileDetail, MessageContext messageContext) throws APIManagementException {
try {
OperationPolicyDefinition ccPolicyDefinition = null;
OperationPolicyDefinition synapseDefinition = null;
OperationPolicySpecification policySpecification;
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
if (policySpecFileInputStream != null) {
String jsonContent = "";
jsonContent = RestApiPublisherUtils.readInputStream(policySpecFileInputStream, policySpecFileDetail);
String fileName = policySpecFileDetail.getDataHandler().getName();
String fileContentType = URLConnection.guessContentTypeFromName(fileName);
if (org.apache.commons.lang3.StringUtils.isBlank(fileContentType)) {
fileContentType = policySpecFileDetail.getContentType().toString();
}
if (APIConstants.YAML_CONTENT_TYPE.equals(fileContentType)) {
jsonContent = CommonUtil.yamlToJson(jsonContent);
}
policySpecification = APIUtil.getValidatedOperationPolicySpecification(jsonContent);
OperationPolicyData operationPolicyData = new OperationPolicyData();
operationPolicyData.setOrganization(organization);
operationPolicyData.setSpecification(policySpecification);
if (synapsePolicyDefinitionFileInputStream != null) {
String synapsePolicyDefinition = RestApiPublisherUtils.readInputStream(synapsePolicyDefinitionFileInputStream, synapsePolicyDefinitionFileDetail);
synapseDefinition = new OperationPolicyDefinition();
synapseDefinition.setContent(synapsePolicyDefinition);
synapseDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.Synapse);
synapseDefinition.setMd5Hash(APIUtil.getMd5OfOperationPolicyDefinition(synapseDefinition));
operationPolicyData.setSynapsePolicyDefinition(synapseDefinition);
}
if (ccPolicyDefinitionFileInputStream != null) {
String choreoConnectPolicyDefinition = RestApiPublisherUtils.readInputStream(ccPolicyDefinitionFileInputStream, ccPolicyDefinitionFileDetail);
ccPolicyDefinition = new OperationPolicyDefinition();
ccPolicyDefinition.setContent(choreoConnectPolicyDefinition);
ccPolicyDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.ChoreoConnect);
ccPolicyDefinition.setMd5Hash(APIUtil.getMd5OfOperationPolicyDefinition(ccPolicyDefinition));
operationPolicyData.setCcPolicyDefinition(ccPolicyDefinition);
}
operationPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(operationPolicyData));
OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyName(policySpecification.getName(), organization, false);
String policyID;
if (existingPolicy == null) {
policyID = apiProvider.addCommonOperationPolicy(operationPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("A common operation policy has been added with name " + policySpecification.getName());
}
} else {
throw new APIManagementException("Existing common operation policy found for the same name.");
}
operationPolicyData.setPolicyId(policyID);
OperationPolicyDataDTO createdPolicy = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(operationPolicyData);
URI createdPolicyUri = new URI(RestApiConstants.REST_API_PUBLISHER_VERSION + "/" + RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES + "/" + policyID);
return Response.created(createdPolicyUri).entity(createdPolicy).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding a common operation policy." + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An Error has occurred while adding common operation policy", e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition 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