Search in sources :

Example 6 with OperationPolicySpecification

use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.

the class ImportUtils method validateOperationPolicies.

public static Set<URITemplate> validateOperationPolicies(API api, APIProvider provider, String extractedFolderPath, Map<String, List<OperationPolicy>> extractedPoliciesMap, String tenantDomain) {
    String policyDirectory = extractedFolderPath + File.separator + ImportExportConstants.POLICIES_DIRECTORY;
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    for (URITemplate uriTemplate : uriTemplates) {
        String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
        if (extractedPoliciesMap.containsKey(key)) {
            List<OperationPolicy> operationPolicies = extractedPoliciesMap.get(key);
            List<OperationPolicy> validatedOperationPolicies = new ArrayList<>();
            if (operationPolicies != null && !operationPolicies.isEmpty()) {
                for (OperationPolicy policy : operationPolicies) {
                    try {
                        OperationPolicySpecification policySpec = getOperationPolicySpecificationFromFile(policyDirectory, policy.getPolicyName());
                        OperationPolicyData operationPolicyData = new OperationPolicyData();
                        operationPolicyData.setSpecification(policySpec);
                        operationPolicyData.setOrganization(tenantDomain);
                        operationPolicyData.setApiUUID(api.getUuid());
                        OperationPolicyDefinition synapseDefinition = APIUtil.getOperationPolicyDefinitionFromFile(policyDirectory, policy.getPolicyName(), APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION);
                        if (synapseDefinition != null) {
                            synapseDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.Synapse);
                            operationPolicyData.setSynapsePolicyDefinition(synapseDefinition);
                        }
                        OperationPolicyDefinition ccDefinition = APIUtil.getOperationPolicyDefinitionFromFile(policyDirectory, policy.getPolicyName(), APIConstants.CC_POLICY_DEFINITION_EXTENSION);
                        if (ccDefinition != null) {
                            ccDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.ChoreoConnect);
                            operationPolicyData.setCcPolicyDefinition(ccDefinition);
                        }
                        operationPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(operationPolicyData));
                        String policyID = provider.importOperationPolicy(operationPolicyData, tenantDomain);
                        policy.setPolicyId(policyID);
                        validatedOperationPolicies.add(policy);
                    } catch (APIManagementException e) {
                        log.error(e);
                    }
                }
            }
            uriTemplate.setOperationPolicies(validatedOperationPolicies);
        }
    }
    return uriTemplates;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) OperationPolicyDefinition(org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 7 with OperationPolicySpecification

use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.

the class OperationPolicyMappingUtil method fromOperationPolicyDataToDTO.

public static OperationPolicyDataDTO fromOperationPolicyDataToDTO(OperationPolicyData policyData) {
    OperationPolicyDataDTO policyDataDTO = new OperationPolicyDataDTO();
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    policyDataDTO.setId(policyData.getPolicyId());
    policyDataDTO.setMd5(policyData.getMd5Hash());
    policyDataDTO.setIsAPISpecific(policyData.isApiSpecificPolicy());
    policyDataDTO.setName(policySpecification.getName());
    policyDataDTO.setDisplayName(policySpecification.getDisplayName());
    policyDataDTO.setDescription(policySpecification.getDescription());
    policyDataDTO.setSupportedGateways(policySpecification.getSupportedGateways());
    policyDataDTO.setSupportedApiTypes(policySpecification.getSupportedApiTypes());
    policyDataDTO.setApplicableFlows(policySpecification.getApplicableFlows());
    policyDataDTO.setMultipleAllowed(policySpecification.isMultipleAllowed());
    policyDataDTO.setCategory(policySpecification.getCategory().toString());
    if (policySpecification.getPolicyAttributes() != null) {
        List<OperationPolicySpecAttributeDTO> specAttributeDtoList = new ArrayList<>();
        for (OperationPolicySpecAttribute specAttribute : policySpecification.getPolicyAttributes()) {
            OperationPolicySpecAttributeDTO specAttributeDTO = fromOperationPolicySpecAttributesToDTO(specAttribute);
            specAttributeDtoList.add(specAttributeDTO);
        }
        policyDataDTO.setPolicyAttributes(specAttributeDtoList);
    }
    return policyDataDTO;
}
Also used : OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) ArrayList(java.util.ArrayList) OperationPolicySpecAttributeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicySpecAttributeDTO) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 8 with OperationPolicySpecification

use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method addAPISpecificOperationPolicy.

/**
 * Add an API specific operation policy
 *
 * @param apiId                                  UUID of the API
 * @param policySpecFileInputStream              Input stream of the policy specification file
 * @param policySpecFileDetail                   Operation 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 Operation operation policy DTO as response
 */
@Override
public Response addAPISpecificOperationPolicy(String apiId, InputStream policySpecFileInputStream, Attachment policySpecFileDetail, InputStream synapsePolicyDefinitionFileInputStream, Attachment synapsePolicyDefinitionFileDetail, InputStream ccPolicyDefinitionFileInputStream, Attachment ccPolicyDefinitionFileDetail, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // validate if api exists
        validateAPIExistence(apiId);
        String jsonContent = "";
        OperationPolicyDefinition synapseDefinition = null;
        OperationPolicyDefinition ccPolicyDefinition = null;
        OperationPolicySpecification policySpecification;
        if (policySpecFileInputStream != null) {
            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.setApiUUID(apiId);
            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.getAPISpecificOperationPolicyByPolicyName(policySpecification.getName(), apiId, null, organization, false);
            String policyID;
            if (existingPolicy == null) {
                policyID = apiProvider.addAPISpecificOperationPolicy(apiId, operationPolicyData, organization);
                if (log.isDebugEnabled()) {
                    log.debug("An API specific operation policy has been added for the API " + apiId + " with id " + policyID);
                }
            } else {
                throw new APIManagementException("An API specific operation policy found for the same name.");
            }
            operationPolicyData.setPolicyId(policyID);
            OperationPolicyDataDTO operationPolicyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(operationPolicyData);
            URI createdPolicyUri = new URI(RestApiConstants.REST_API_PUBLISHER_VERSION + RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + "/" + RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES + "/" + policyID);
            return Response.created(createdPolicyUri).entity(operationPolicyDataDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding an API specific operation policy." + e.getMessage();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An error has occurred while adding an API specific " + "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) JSONException(org.json.JSONException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SdkClientException(com.amazonaws.SdkClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URISyntaxException(java.net.URISyntaxException) BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException) CryptoException(org.wso2.carbon.core.util.CryptoException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException)

Example 9 with OperationPolicySpecification

use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification 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 10 with OperationPolicySpecification

use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.

the class ApiMgtDAO method addCommonOperationPolicy.

/**
 * Operation policy implementation
 */
/**
 * Add a new common operation policy to the database. This will first add the operation policy content to the
 * AM_OPERATION_POLICY table and another entry to AM_COMMON_OPERATION_POLICY table.
 *
 * @param policyData Operation policy data.
 * @return UUID of the newly created shared policy
 * @throws APIManagementException
 */
public String addCommonOperationPolicy(OperationPolicyData policyData) throws APIManagementException {
    String policyUUID = null;
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            policyUUID = addOperationPolicyContent(connection, policyData);
            String dbQuery = SQLConstants.OperationPolicyConstants.ADD_COMMON_OPERATION_POLICY;
            PreparedStatement statement = connection.prepareStatement(dbQuery);
            statement.setString(1, policyUUID);
            statement.executeUpdate();
            statement.close();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to add common operation policy " + policySpecification.getName(), e);
        }
    } catch (SQLException e) {
        handleException("Failed to add common operation policy " + policySpecification.getName(), e);
    }
    return policyUUID;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Aggregations

OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)13 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 ArrayList (java.util.ArrayList)6 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)6 OperationPolicyDefinition (org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition)5 IOException (java.io.IOException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 PreparedStatement (java.sql.PreparedStatement)3 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)3 OperationPolicySpecAttribute (org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute)3 OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)3 Gson (com.google.gson.Gson)2 URI (java.net.URI)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 JSONObject (org.json.simple.JSONObject)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2