Search in sources :

Example 11 with OperationPolicySpecification

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

the class ApiMgtDAO method addAPISpecificOperationPolicy.

/**
 * Add a new API specific operation policy to the database
 *
 * @param apiUUID      Unique Identifier of API
 * @param revisionUUID Unique Identifier of API revision
 * @param policyData   Unique Identifier of API
 * @return UUID of the newly created shared policy
 * @throws APIManagementException
 */
public String addAPISpecificOperationPolicy(String apiUUID, String revisionUUID, OperationPolicyData policyData) throws APIManagementException {
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            String policyID = addAPISpecificOperationPolicy(connection, apiUUID, revisionUUID, policyData, null);
            connection.commit();
            return policyID;
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to add API specific operation policy " + policySpecification.getName() + " for API " + apiUUID, e);
        }
    } catch (SQLException e) {
        handleException("Failed to add API specific operation policy " + policySpecification.getName() + " for API " + apiUUID, e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 12 with OperationPolicySpecification

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

the class ApiMgtDAO method updateOperationPolicy.

/**
 * Update an existing operation policy
 *
 * @param connection DB connection
 * @param policyId   Shared policy UUID
 * @param policyData Updated policy definition
 * @throws SQLException
 */
private void updateOperationPolicy(Connection connection, String policyId, OperationPolicyData policyData) throws SQLException {
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    PreparedStatement statement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.UPDATE_OPERATION_POLICY_CONTENT);
    statement.setString(1, policySpecification.getName());
    statement.setString(2, policySpecification.getDisplayName());
    statement.setString(3, policySpecification.getDescription());
    statement.setString(4, policySpecification.getApplicableFlows().toString());
    statement.setString(5, policySpecification.getSupportedGateways().toString());
    statement.setString(6, policySpecification.getSupportedApiTypes().toString());
    statement.setBinaryStream(7, new ByteArrayInputStream(APIUtil.getPolicyAttributesAsString(policySpecification).getBytes()));
    statement.setString(8, policyData.getOrganization());
    statement.setString(9, policySpecification.getCategory().toString());
    statement.setBoolean(10, policySpecification.isMultipleAllowed());
    statement.setString(11, policyData.getMd5Hash());
    statement.setString(12, policyId);
    statement.executeUpdate();
    statement.close();
    if (policyData.getSynapsePolicyDefinition() != null) {
        updateOperationPolicyDefinition(connection, policyId, policyData.getSynapsePolicyDefinition());
    }
    if (policyData.getCcPolicyDefinition() != null) {
        updateOperationPolicyDefinition(connection, policyId, policyData.getCcPolicyDefinition());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PreparedStatement(java.sql.PreparedStatement) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 13 with OperationPolicySpecification

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

the class ApiMgtDAO method populatePolicySpecificationFromRS.

/**
 * This method will read the result set and populate OperationPolicySpecification object.
 *
 * @param rs Result set
 * @return OperationPolicySpecification object
 * @throws APIManagementException
 * @throws SQLException
 */
private OperationPolicySpecification populatePolicySpecificationFromRS(ResultSet rs) throws SQLException {
    OperationPolicySpecification policySpecification = new OperationPolicySpecification();
    policySpecification.setName(rs.getString("POLICY_NAME"));
    policySpecification.setDisplayName(rs.getString("DISPLAY_NAME"));
    policySpecification.setDescription(rs.getString("POLICY_DESCRIPTION"));
    policySpecification.setApplicableFlows(getListFromString(rs.getString("APPLICABLE_FLOWS")));
    policySpecification.setSupportedApiTypes(getListFromString(rs.getString("API_TYPES")));
    policySpecification.setSupportedGateways(getListFromString(rs.getString("GATEWAY_TYPES")));
    policySpecification.setCategory(OperationPolicySpecification.PolicyCategory.valueOf(rs.getString("POLICY_CATEGORY")));
    policySpecification.setMultipleAllowed(rs.getBoolean("MULTIPLE_ALLOWED"));
    List<OperationPolicySpecAttribute> policySpecAttributes = null;
    try (InputStream policyParametersStream = rs.getBinaryStream("POLICY_PARAMETERS")) {
        String policyParametersString = IOUtils.toString(policyParametersStream);
        policySpecAttributes = new Gson().fromJson(policyParametersString, new TypeToken<List<OperationPolicySpecAttribute>>() {
        }.getType());
    } catch (IOException e) {
        log.error("Error while converting policy specification attributes for the policy " + policySpecification.getName(), e);
    }
    policySpecification.setPolicyAttributes(policySpecAttributes);
    return policySpecification;
}
Also used : OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 14 with OperationPolicySpecification

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

Example 15 with OperationPolicySpecification

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

the class APIProviderImpl method importOperationPolicy.

/**
 * This method will be used to import Operation policy. This will check existing API specific policy first and
 * then common policy.
 * If API specific policy exists and MD5 hash matches, it will not import and will return the existing API specific policy.
 * If the existing API specific policy is different in md5, it will be updated the existing policy
 * If a common policy exists and MD5 hash match, it will return the common policy's id. This policy will be imported at the API update.
 * If the common policy is different then the imported policy, a new API specific policy will be created.
 * If there aren't any existing policies, a new API specific policy will be created.
 *
 * @param importedPolicyData Imported policy
 * @param organization       Organization name
 * @return corrosponding policy ID for imported data
 * @throws APIManagementException if failed to delete APIRevision
 */
@Override
public String importOperationPolicy(OperationPolicyData importedPolicyData, String organization) throws APIManagementException {
    OperationPolicySpecification importedSpec = importedPolicyData.getSpecification();
    OperationPolicyData existingOperationPolicy = getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedPolicyData.getApiUUID(), null, organization, false);
    String policyId = null;
    if (existingOperationPolicy != null) {
        if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
            if (log.isDebugEnabled()) {
                log.debug("Matching API specific policy found for imported policy and MD5 hashes match.");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Even though existing API specific policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ".Therefore updating the existing policy");
            }
            updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization);
        }
        policyId = existingOperationPolicy.getPolicyId();
    } else {
        existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), organization, false);
        if (existingOperationPolicy != null) {
            if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
                if (log.isDebugEnabled()) {
                    log.debug("Matching common policy found for imported policy and Md5 hashes match.");
                }
                policyId = existingOperationPolicy.getPolicyId();
            } else {
                importedSpec.setName(importedSpec.getName() + "_imported");
                importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported");
                importedPolicyData.setSpecification(importedSpec);
                importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData));
                policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
                if (log.isDebugEnabled()) {
                    log.debug("Even though existing common policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ". A new policy created with ID " + policyId);
                }
            }
        } else {
            policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
            if (log.isDebugEnabled()) {
                log.debug("There aren't any existing policies for the imported policy. A new policy created with ID " + policyId);
            }
        }
    }
    return policyId;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) 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