Search in sources :

Example 16 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method populateGlobalPolicyList.

private List<GlobalPolicy> populateGlobalPolicyList(ResultSet resultSet) throws SQLException {
    List<GlobalPolicy> globalPolicies = new ArrayList<>();
    if (resultSet != null) {
        while (resultSet.next()) {
            GlobalPolicy globalPolicyDTO = new GlobalPolicy();
            globalPolicyDTO.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
            globalPolicyDTO.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
            globalPolicyDTO.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
            String tenantDomain = APIUtil.getTenantDomainFromTenantId(globalPolicyDTO.getTenantId());
            globalPolicyDTO.setTenantDomain(tenantDomain);
            globalPolicyDTO.setKeyTemplate(resultSet.getString(ThrottlePolicyConstants.COLUMN_KEY_TEMPLATE));
            InputStream siddhiQueryBlob = resultSet.getBinaryStream(ThrottlePolicyConstants.COLUMN_SIDDHI_QUERY);
            String siddhiQuery = null;
            if (siddhiQueryBlob != null) {
                siddhiQuery = APIMgtDBUtil.getStringFromInputStream(siddhiQueryBlob);
            }
            globalPolicyDTO.setSiddhiQuery(siddhiQuery);
            globalPolicies.add(globalPolicyDTO);
        }
    }
    return globalPolicies;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

Example 17 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method getAllGlobalPolicies.

public List<GlobalPolicy> getAllGlobalPolicies(String tenantDomain) {
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_TENANT_GLOBAL_POLICIES_SQL)) {
        int tenantId = 0;
        try {
            tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        } catch (UserStoreException e) {
            log.error("Error in loading Global Policies for tenantDomain : " + tenantDomain, e);
        }
        ps.setInt(1, tenantId);
        try (ResultSet resultSet = ps.executeQuery()) {
            return populateGlobalPolicyList(resultSet);
        }
    } catch (SQLException e) {
        log.error("Error in loading global policies for tenantId : " + tenantDomain, e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 18 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method getGlobalPolicyByNameForTenant.

public GlobalPolicy getGlobalPolicyByNameForTenant(String policyName, String tenantDomain) {
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_GLOBAL_POLICY_SQL)) {
        int tenantId = 0;
        try {
            tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        } catch (UserStoreException e) {
            log.error("Error in loading Global Policy for tenantDomain : " + tenantDomain, e);
        }
        ps.setString(1, policyName);
        ps.setInt(2, tenantId);
        try (ResultSet resultSet = ps.executeQuery()) {
            if (resultSet.next()) {
                GlobalPolicy globalPolicyDTO = new GlobalPolicy();
                globalPolicyDTO.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
                globalPolicyDTO.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
                globalPolicyDTO.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
                globalPolicyDTO.setTenantDomain(tenantDomain);
                globalPolicyDTO.setKeyTemplate(resultSet.getString(ThrottlePolicyConstants.COLUMN_KEY_TEMPLATE));
                InputStream siddhiQueryBlob = resultSet.getBinaryStream(ThrottlePolicyConstants.COLUMN_SIDDHI_QUERY);
                String siddhiQuery = null;
                if (siddhiQueryBlob != null) {
                    siddhiQuery = APIMgtDBUtil.getStringFromInputStream(siddhiQueryBlob);
                }
                globalPolicyDTO.setSiddhiQuery(siddhiQuery);
                return globalPolicyDTO;
            }
        }
    } catch (SQLException e) {
        log.error("Error in loading global policies by policyId : " + policyName + " of " + policyName, e);
    }
    return null;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 19 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class APIProviderImplTest method getPolicyGlobalLevel.

private GlobalPolicy getPolicyGlobalLevel() {
    GlobalPolicy policy = new GlobalPolicy("1");
    policy.setDescription("Description");
    String siddhiQuery = "FROM RequestStream\n" + "SELECT 'global_1' AS rule, messageID, true AS isEligible, (cast(map:get(propertiesMap,’ip’),’string’)" + " == 3232235778) as isLocallyThrottled," + " 'global_1_key' AS throttle_key\n" + "INSERT INTO EligibilityStream;";
    policy.setSiddhiQuery(siddhiQuery);
    return policy;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)

Example 20 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class APIProviderImplTest method testIsGlobalPolicyKeyTemplateExists.

@Test
public void testIsGlobalPolicyKeyTemplateExists() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    GlobalPolicy globalPolicy = Mockito.mock(GlobalPolicy.class);
    Mockito.when(apimgtDAO.isKeyTemplatesExist(globalPolicy)).thenReturn(true);
    assertTrue(apiProvider.isGlobalPolicyKeyTemplateExists(globalPolicy));
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)19 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)9 InputStream (java.io.InputStream)7 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)6 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)6 ResultSet (java.sql.ResultSet)5 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)4 SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)4