Search in sources :

Example 26 with OperationPolicy

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

the class ApiMgtDAO method populateOperationPolicyWithRS.

/**
 * This method will read the result set and populate OperationPolicy object, which later will be set to the URI template.
 * This object has the information regarding the policy allocation
 *
 * @param rs Result set
 * @return OperationPolicy object
 * @throws APIManagementException
 * @throws SQLException
 */
private OperationPolicy populateOperationPolicyWithRS(ResultSet rs) throws SQLException, APIManagementException {
    OperationPolicy operationPolicy = new OperationPolicy();
    operationPolicy.setPolicyName(rs.getString("POLICY_NAME"));
    operationPolicy.setPolicyId(rs.getString("POLICY_UUID"));
    operationPolicy.setOrder(rs.getInt("POLICY_ORDER"));
    operationPolicy.setDirection(rs.getString("DIRECTION"));
    operationPolicy.setParameters(APIMgtDBUtil.convertJSONStringToMap(rs.getString("PARAMETERS")));
    return operationPolicy;
}
Also used : OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy)

Example 27 with OperationPolicy

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

the class ApiMgtDAO method setOperationPolicies.

/**
 * Sets operation policies to uriTemplates map
 *
 * @param uuid         UUID of API or API Revision
 * @param uriTemplates URI Templates map with URL_MAPPING_ID as the map key
 * @throws SQLException
 * @throws APIManagementException
 */
private void setOperationPolicies(String uuid, Map<Integer, URITemplate> uriTemplates) throws SQLException, APIManagementException {
    String currentApiUuid;
    String query;
    boolean isRevision = false;
    APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        currentApiUuid = apiRevision.getApiUUID();
        query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_FOR_API_REVISION_SQL;
        isRevision = true;
    } else {
        query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_OF_API_SQL;
        currentApiUuid = uuid;
    }
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(query)) {
        int apiId = getAPIID(currentApiUuid);
        ps.setInt(1, apiId);
        if (isRevision) {
            ps.setString(2, uuid);
        }
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                int uriTemplateId = rs.getInt("URL_MAPPING_ID");
                URITemplate uriTemplate = uriTemplates.get(uriTemplateId);
                if (uriTemplate != null) {
                    OperationPolicy operationPolicy = populateOperationPolicyWithRS(rs);
                    uriTemplate.addOperationPolicy(operationPolicy);
                }
            }
        }
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) PreparedStatement(java.sql.PreparedStatement)

Example 28 with OperationPolicy

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

the class ApiMgtDAO method getAPIProductResourceMappings.

/**
 * get resource mapping of the api product
 * TODO://Get resource scopes from AM_API_RESOURCE_SCOPE table and retrieve scope meta data and bindings from KM.
 *
 * @param productIdentifier api product identifier
 * @throws APIManagementException
 */
public List<APIProductResource> getAPIProductResourceMappings(APIProductIdentifier productIdentifier) throws APIManagementException {
    int productId = getAPIProductId(productIdentifier);
    List<APIProductResource> productResourceList = new ArrayList<>();
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        if (checkAPIUUIDIsARevisionUUID(productIdentifier.getUUID()) == null) {
            String sql = SQLConstants.GET_RESOURCES_OF_PRODUCT;
            try (PreparedStatement ps = connection.prepareStatement(sql)) {
                ps.setInt(1, productId);
                ps.setString(2, String.valueOf(productId));
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        APIProductResource resource = new APIProductResource();
                        APIIdentifier apiId = new APIIdentifier(rs.getString("API_PROVIDER"), rs.getString("API_NAME"), rs.getString("API_VERSION"));
                        apiId.setUuid(rs.getString("API_UUID"));
                        resource.setProductIdentifier(productIdentifier);
                        resource.setApiIdentifier(apiId);
                        resource.setApiName(rs.getString("API_NAME"));
                        URITemplate uriTemplate = new URITemplate();
                        uriTemplate.setUriTemplate(rs.getString("URL_PATTERN"));
                        uriTemplate.setResourceURI(rs.getString("URL_PATTERN"));
                        uriTemplate.setHTTPVerb(rs.getString("HTTP_METHOD"));
                        int uriTemplateId = rs.getInt("URL_MAPPING_ID");
                        uriTemplate.setId(uriTemplateId);
                        uriTemplate.setAuthType(rs.getString("AUTH_SCHEME"));
                        uriTemplate.setThrottlingTier(rs.getString("THROTTLING_TIER"));
                        try (PreparedStatement scopesStatement = connection.prepareStatement(SQLConstants.GET_SCOPE_KEYS_BY_URL_MAPPING_ID)) {
                            scopesStatement.setInt(1, uriTemplateId);
                            try (ResultSet scopesResult = scopesStatement.executeQuery()) {
                                while (scopesResult.next()) {
                                    Scope scope = new Scope();
                                    scope.setKey(scopesResult.getString("SCOPE_NAME"));
                                    uriTemplate.setScopes(scope);
                                }
                            }
                        }
                        try (PreparedStatement policiesStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_BY_URI_TEMPLATE_ID)) {
                            policiesStatement.setInt(1, uriTemplateId);
                            try (ResultSet policiesResult = policiesStatement.executeQuery()) {
                                List<OperationPolicy> operationPolicies = new ArrayList<>();
                                while (policiesResult.next()) {
                                    OperationPolicy policy = populateOperationPolicyWithRS(policiesResult);
                                    operationPolicies.add(policy);
                                }
                                uriTemplate.setOperationPolicies(operationPolicies);
                            }
                        }
                        resource.setUriTemplate(uriTemplate);
                        productResourceList.add(resource);
                    }
                }
            }
        } else {
            String sql = SQLConstants.GET_RESOURCES_OF_PRODUCT_REVISION;
            try (PreparedStatement ps = connection.prepareStatement(sql)) {
                ps.setInt(1, productId);
                ps.setString(2, productIdentifier.getUUID());
                ps.setString(3, productIdentifier.getUUID());
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        APIProductResource resource = new APIProductResource();
                        APIIdentifier apiId = new APIIdentifier(rs.getString("API_PROVIDER"), rs.getString("API_NAME"), rs.getString("API_VERSION"));
                        apiId.setUuid(rs.getString("API_UUID"));
                        resource.setProductIdentifier(productIdentifier);
                        resource.setApiIdentifier(apiId);
                        resource.setApiName(rs.getString("API_NAME"));
                        URITemplate uriTemplate = new URITemplate();
                        uriTemplate.setUriTemplate(rs.getString("URL_PATTERN"));
                        uriTemplate.setResourceURI(rs.getString("URL_PATTERN"));
                        uriTemplate.setHTTPVerb(rs.getString("HTTP_METHOD"));
                        int uriTemplateId = rs.getInt("URL_MAPPING_ID");
                        uriTemplate.setId(uriTemplateId);
                        uriTemplate.setAuthType(rs.getString("AUTH_SCHEME"));
                        uriTemplate.setThrottlingTier(rs.getString("THROTTLING_TIER"));
                        try (PreparedStatement scopesStatement = connection.prepareStatement(SQLConstants.GET_SCOPE_KEYS_BY_URL_MAPPING_ID)) {
                            scopesStatement.setInt(1, uriTemplateId);
                            try (ResultSet scopesResult = scopesStatement.executeQuery()) {
                                while (scopesResult.next()) {
                                    Scope scope = new Scope();
                                    scope.setKey(scopesResult.getString("SCOPE_NAME"));
                                    uriTemplate.setScopes(scope);
                                }
                            }
                        }
                        try (PreparedStatement policiesStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_BY_URI_TEMPLATE_ID)) {
                            policiesStatement.setInt(1, uriTemplateId);
                            try (ResultSet policiesResult = policiesStatement.executeQuery()) {
                                List<OperationPolicy> operationPolicies = new ArrayList<>();
                                while (policiesResult.next()) {
                                    OperationPolicy policy = populateOperationPolicyWithRS(policiesResult);
                                    operationPolicies.add(policy);
                                }
                                uriTemplate.setOperationPolicies(operationPolicies);
                            }
                        }
                        resource.setUriTemplate(uriTemplate);
                        productResourceList.add(resource);
                    }
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get product resources of api product : " + productIdentifier, e);
    }
    return productResourceList;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) PreparedStatement(java.sql.PreparedStatement) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) ResultSet(java.sql.ResultSet) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 29 with OperationPolicy

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

the class ApiMgtDAO method setOperationPoliciesToURITemplatesMap.

/**
 * Sets operation policies to uriTemplates map
 *
 * @param uuid         UUID of API or API Revision
 * @param uriTemplates URI Templates map with 'URL_PATTERN + HTTP_METHOD' as the map key
 * @throws SQLException
 * @throws APIManagementException
 */
private void setOperationPoliciesToURITemplatesMap(String uuid, Map<String, URITemplate> uriTemplates) throws SQLException, APIManagementException {
    String currentApiUuid;
    String query;
    boolean isRevision = false;
    APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        currentApiUuid = apiRevision.getApiUUID();
        query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_FOR_API_REVISION_SQL;
        isRevision = true;
    } else {
        query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_OF_API_SQL;
        currentApiUuid = uuid;
    }
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(query)) {
        int apiId = getAPIID(currentApiUuid);
        ps.setInt(1, apiId);
        if (isRevision) {
            ps.setString(2, uuid);
        }
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                String key = rs.getString("URL_PATTERN") + rs.getString("HTTP_METHOD");
                URITemplate uriTemplate = uriTemplates.get(key);
                if (uriTemplate != null) {
                    OperationPolicy operationPolicy = populateOperationPolicyWithRS(rs);
                    uriTemplate.addOperationPolicy(operationPolicy);
                }
            }
        }
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) PreparedStatement(java.sql.PreparedStatement)

Example 30 with OperationPolicy

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

OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)28 ArrayList (java.util.ArrayList)21 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)20 HashMap (java.util.HashMap)16 PreparedStatement (java.sql.PreparedStatement)13 ResultSet (java.sql.ResultSet)12 Connection (java.sql.Connection)11 Gson (com.google.gson.Gson)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 SQLException (java.sql.SQLException)8 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)7 Scope (org.wso2.carbon.apimgt.api.model.Scope)7 HashSet (java.util.HashSet)6 LinkedHashSet (java.util.LinkedHashSet)6 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5