Search in sources :

Example 6 with OperationPolicyDefinition

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;
}
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)

Example 7 with OperationPolicyDefinition

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 &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

OperationPolicyDefinition (org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)5 IOException (java.io.IOException)4 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 URI (java.net.URI)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)2 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)2 OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)2 SdkClientException (com.amazonaws.SdkClientException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1