use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO 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;
}
Aggregations