Search in sources :

Example 6 with OperationPolicyDataDTO

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;
}
Also used : OperationPolicyDefinition(org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition) OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Aggregations

OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)4 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)4 OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)3 SdkClientException (com.amazonaws.SdkClientException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 ParseException (org.json.simple.parser.ParseException)2 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException)2 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)2 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)2 OperationPolicyDefinition (org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition)2